Thursday, August 6, 2020

Javascript learning on the way

Curry: 


Apply and Call : Both take first argument to change the its current context, 


The call() method takes arguments separately.

The apply() method takes arguments as an array.

Wednesday, July 22, 2020

Updating Google AMP cache

Recently I have a chance to work with Google AMP for some Wordpress Sites, Here is what i have done to be able to update AMP cache for any URL of the sites.

Here i created a repository for my implementation: https://github.com/vanduc1102/php-amp-update-cache

Please refer to the official document here: https://developers.google.com/amp/cache/overview

Using Site Ground Git to deploy your Wordpress Site

Site Ground Git gives us an option to have some sites such as staging or test  to make our developer life easier.


In the screenshot above, we have 2 sites for testing before roll-out to production.

In order to push code to testing sites, you need to register your SSH key first,


After created a SSH key, simply upload the public key via Manage SSH Keys,  Remember to authorized the key.



In case you already has a SSH key and want to use a new SSH key, you can specify the new SSH key per folder which contains your source code


When everything is ready, add the Site Ground repository as a remote track, and push the change to master.



That is it, happy coding.

Here is the git setup guide from SiteGround: https://www.siteground.com/tutorials/sg-git/

Tuesday, July 14, 2020

AWS workshop in HCMC - Kubernetes on AWS with Amazon EKS

I attended the workshop on Jul 14 2020

Summary the of the workshop


I have learned a new term of CDK - Cloud Development Kit,

They use Cloud9 - An online VS Code version to code the CDK, we practiced creating an infrastructure using TypeScript

https://cdkworkshop.com/20-typescript.html


Sunday, July 28, 2019

Google Cloud Build connect to VM on premise

GCP Cloud Build is so cool with 120 minutes free per day.

I want to use Cloud Build to execute a script to deploy a NodeJS project on my private VM.

Here is what i have done in my cloudbuild.yaml


steps:
# copy configuration bucket from GCS to cloudbuild
- name: gcr.io/cloud-builders/gsutil
  args: ['cp',
    '-r',
    'gs://${_GCS_CONFIGRATION_BUCKET}',
    '.']
# Set 400 to private key.
- name: 'kroniak/ssh-client'
  args: ['chmod',
    '400',
    '${_GCS_CONFIGRATION_BUCKET}/ssh/cloudbuild_id_rsa']
# ssh into remote instance and run a script.
- name: 'kroniak/ssh-client'
  args: ['ssh',
    '-i',
    '${_GCS_CONFIGRATION_BUCKET}/ssh/cloudbuild_id_rsa',
    '-o',
    'UserKnownHostsFile=/dev/null',
    '-o',
    'StrictHostKeyChecking=no',
    '-p',
    '${_SSH_REMOTE_PORT}',
    '${_SSH_REMOTE_USER_HOST}',
    '${_SSH_REMOTE_COMMAND}']
Because CloudBuild is stateless, you need to create your RSA keypairs and store the keys on a private GCS.


You need to add your RSA public key into ~/.ssh/authorized_keys on your server, tutorial here

And the script to pulling code and restart server.


#!/bin/sh
 
# It is good practice to print the required versions on server. 
# cause the code will execute in SSH non interactively mode.
# https://stackoverflow.com/questions/17089808/how-to-do-remote-ssh-non-interactively
 
echo "NodeJS: "$(node -v)
echo "NPM: "$(npm -v)
 
 
WORKSPACE=/working_directory/public_html
 
echo "Working directory: " $WORKSPACE
 
cd $WORKSPACE
 
git checkout develop
git pull -Xtheirs
 
echo "===================Install dependencies ==========="
npm install
echo "===================Finished install dependencies ========"
 
echo "RELOAD ENV"
pm2 reload $WORKSPACE/ecosystem.config.js production  --update-env

Here are example values of the variable.
_GCS_CONFIGRATION_BUCKET : my-private-bucket-configuration
_SSH_REMOTE_PORT :  2202 // hacker will sad.
_SSH_REMOTE_USER_HOST : aduckdev@8.8.8.8
_SSH_REMOTE_COMMAND :  /home/aduckdev/deployment_script.sh

Friday, June 28, 2019

Stupid errors that took me more than 1 hour to solve

Here is my follows up on the original post here: https://blog.juliobiason.net/thoughts/things-i-learnt-the-hard-way/#code-reviews-are-not-for-style

The list will keep increasing day by day but hopefully i wont repeat any of the items here.

Soft-Hyphen Characters


https://en.wikipedia.org/wiki/Soft_hyphen

There was a request from my client to remove Soft-Hyphen character from a JSON.
A JSON use to render a page on Android and iOS.
Client did not specify the devices or OS, And I did not get the issue at first on Android.
But I could see some red-dot in JSON, I did a replace


replace({ '­':'', '­':'', '­': '' }) 

Sadly, It was not enough,
Took me some hours to find a solution here: https://stackoverflow.com/questions/10148184/php-check-if-string-contains-soft-hypen-and-replace-it/56885146#56885146

Even my friend told me that i should convert the U+00AD into a character and use that character to replace

And yes!, if you're checking the solution on StackOverflow, the answer is


$str = str_replace('­', '', $str);


And here was the diff on Stash



Throw Error in Promise.

What did i do?
I was trying to throw some error inside a Promise, but I forgot to put catch in the end of the end. My web server was hanging for several minutes before it responded.

function doAsync (){
  return new Promise( (resolve, reason ) =>{
      // do something to make error happens.
      if(error){
       throw new Error("ABC")
      }
     resolve("ok");
  });
}

For the code, we have two solutions. 1.1 Use resolve with the error.

function doAsync (){
  return new Promise( (resolve, reason ) =>{
      // do something to make error happens.
      if(error){
       return reason( new Error("ABC") );
      }
     resolve("ok");
  });
}

1.2 Use catch to return the error value.

function doAsync (){
  return new Promise( (resolve, reason ) =>{
      // do something to make error happens.
      if(error){
       throw new Error("ABC");
      }
     resolve("ok");
  }).catch(err => errorHandling(err))
}

Friday, March 1, 2019

Setup SSH with RSA keypairs

Firstly, you can easily remote to server with 
ssh your_username@server_address

But you can remove the annoying by being asked typing password many times, using RSA keypairs, it is also more secured than using password.


1. Creating a key pairs on Workstation



ssh-keygen -t rsa - b 4096

Add passphrase is recommended

To read your public key

cat ~/.ssh/id_rsa.pub

2. Uploading public keys to the Server



SSH to the Server

ssh your_username@server_address 

Create a file in .ssh/authorized_keys  and paste the public key into

you can do it from Workstation with one command using 

cat ~/.ssh/id_rsa.pub | ssh your_username@server_address 'cat >> .ssh/authorized_keys'

And change the permission

ssh your_username@server_address ‘chmod 700 .ssh; chmod 640 .ssh/authorized_keys’

From now we can ssh with being asked for password

ssh your_username@server_address

3. Turn off password authentication


On Server Machine

sudo vim /etc/ssh/sshd_config

Uncomment Passwordauthentication , change the value to No

And  restart ssh 

sudo systemctl restart ssh