Session Prices

Feel free to negotiate depending on your situation.

Scheduling

Hit me up to work out a scheduled time and date.
[email protected]
213-995-7078 text or voice
Introducing Some Simple Automation

From our previous example of a Node.js application, we'd like to deploy that to a production server. We have a myriad of tools at our disposal to do so, but there are two that do this job beautifully.

Introducing rsync and ssh. You may already know about some or all of these tools, but we'll introduce them here pertaining to their abilities to deploy software.

 

Rsync

Our first tool is rsync. This tool is great for transferring files from one place to another. It does efficient file copying, only copying files when things have changed. This tool is great for transferring files to servers and doing backups as well.

Let's introduce a basic rsync command to see how it works. This next command will copy from one location to another on your local computer.

rsync -avz ~/app/* ~/backup/app/

This will copy your Node.js code in ~/app/ to ~/backup/app/.

You should see a whole lot of files in the list being copied.

Now, if you change the index.js file and rerun the above command, you should only see one file being copied. This makes it very efficient and fast to copy files over the network.

Rsync also has built in abilities to copy files over ssh. Let's look at that now:

rsync -avz ~/app/* dev@staging:/pub/app/www/

You should notice that we added dev@staging: to the command. This specifies a user name dev and a server staging, separated by an @ symbol. This works exactly like email. So, user@server.

The directory on the remote server is specified after the colon, /pub/app/www/.

This command uses ssh to transfer files, so if you have ssh setup correctly, it should automatically transfer the files to your server without using a password.

What if my server isn't using the default ssh port? Just add: -e "ssh -p [port]".

rsync -avz -e "ssh -p 8888" ~/app/* dev@staging:/pub/app/www/

What if there are certain directories or files I'd like to exclude? You can easily exclude items by including one or many --exclude options to rsync.

rsync -avz -e "ssh -p 8888" --exclude "package.lock" --exclude "package.json" ~/app/* dev@staging:/pub/app/www/

What does -avz mean at the beginning of each rsync command? Those are also command line options.

  • a = archive - basically, keep all permissions the exact same from the source to the destination.
  • v = verbose - list all the files that are being copied and list any stats at the end.
  • z = compress files while transferring.

There are a metric fuck ton of rsync options. To read up on them, at your terminal type: man rsync. But, what we've introduced here will cover 90% of everything you'll need to do with it.

 

SSH

You might be wondering why we're introducing ssh here? You've probably already used it to login to remote servers and do adminstrative tasks on them. What you might not know about ssh is you can also use it to run commands in an automated fashion. This can be helpful to fix things up, restart services, and do other administrative tasks.

Let's look at an example. This gets the date from a remote server:

ssh dev@staging 'date'
Wed Jan 27 16:16:37 PST 2021

What happened here? We basically ran an ssh command and it returned the output from the remote server to our terminal and then returned to our terminal. We never actually logged in.

This is great when we need to say restart a service for changes to be recognized:

ssh root@staging 'systemctl reload nginx'

The previous command just reloaded the nginx web server to pick up any changes that might have been made.

What if my server isn't using the default port for ssh? We just add -p [port]. For example:

ssh -p 8888 dev@staging 'ls -la'
drwx------ 4 dev  dev  4096 Nov 25 17:29 .
drwxr-xr-x 4 root root 4096 Nov 25 18:03 ..
-rw-r--r-- 1 dev  dev  3771 Feb 25  2020 .bashrc
drwx------ 2 dev  dev  4096 Nov 25 17:29 .cache
-rw-r--r-- 1 dev  dev   807 Feb 25  2020 .profile
drwxr-xr-x 2 dev  dev  4096 Nov 25 17:26 .ssh

You can run any remote command this way and get the results piped back through ssh as if the command was being run locally.

For example, let's list our directories and grep for .ssh:

ssh dev@staging 'ls -la' | grep ".ssh"
drwxr-xr-x 2 dev  dev  4096 Nov 25 17:26 .ssh

This can be extremely powerful as you'll see. You can also pipe through ssh to the remote server. For example, let's create a file on the remote server:

echo "This is a new file" | ssh dev@staging 'cat > new_file'

Then, let's list the directory to see the new file created:

ssh dev@staging 'ls -la'
drwx------ 4 dev  dev  4096 Jan 27 16:28 .
drwxr-xr-x 4 root root 4096 Nov 25 18:03 ..
-rw-r--r-- 1 dev  dev  3771 Feb 25  2020 .bashrc
drwx------ 2 dev  dev  4096 Nov 25 17:29 .cache
-rw-r--r-- 1 dev  dev   807 Feb 25  2020 .profile
drwxr-xr-x 2 dev  dev  4096 Nov 25 17:26 .ssh
-rw-rw-r-- 1 dev  dev    19 Jan 27 16:28 new_file

And finally, let's dump the file to the terminal to verify that our text was added to the file:

ssh dev@staging 'cat new_file'
This is a new file