How Docksal Custom commands and CLI are the bridge

Running Drush commands on a remote server is simple with the use of Drush aliases. You define where the server is, how to connect to the server, the user that connects to the server, and the path of the application. Drush takes care of that connection and performs the command you have called on the server. With the Acquia BLT tool, you don’t have the same option. So, you are forced to ssh to the server, navigate to the proper directory, and run your command on the server, or are you?
With Docksal, you can use ssh config to create host aliases and write a custom command to run that command on the specified server. Here is a look at the steps necessary to achieve that goal:
Create an ssh config file
If you are not familiar with ssh config files, may I suggest a little side reading about them. Having an ssh config file in your home directory can be extremely useful when you have to connect to various servers with different usernames or special parameters. Instead of having to type a long command every time you want to connect to a server, you simply have to remember your host alias.
Similarly in a Drupal project, you have multiple hosts connections. You have your prod and dev environments, but you may also have a QA environment or staging environment. Depending on your project, you may have a list of hosts to connect to. In my case, the username is the same regardless of which team member is accessing the host. We use ssh keys to manage access for that single user on the host. So, the ssh config file will look the same for all users. In the Docksal CLI image, there is a global ssh config file. We will need to create a config file that will be for all users of the project and update the global ssh config file in the CLI when the cli image starts up.
The ssh file can be placed anywhere to start. I created mine in .docksal/services/cli/ssh_config.txt
Host stage
HostName staging-server.example.com
Port 22
User drupal
RequestTTY force
RemoteCommand cd /opt/www/stage/current && bash -l
Host ode
HostName ode-server.example.com
Port 22
User drupal
RequestTTY force
RemoteCommand cd /opt/www/ode/current && bash -l
Host prod
HostName prod-server.example.com
Port 22
User accountuser
RequestTTY force
RemoteCommand cd /var/www/html/application && bash -l
Host dev
HostName dev-server.example.com
Port 22
User accountuser
RequestTTY force
RemoteCommand cd /var/www/html/applicationdev && bash -l
Host test
HostName test-server.example.com
Port 22
User accountuser
RequestTTY force
RemoteCommand cd /var/www/html/applicationstg && bash -l
Host *
StrictHostKeyChecking no
We will then need a startup.sh script for the cli.
#!/usr/bin/env bash
cat /var/www/.docksal/services/cli/ssh_config.txt | sudo tee -a /etc/ssh/ssh_config > /dev/null
This script copies the contents of your project ssh_config.txt file and appends it to the global ssh config in the CLI. Run fin project reset cli and the startup script will run.
Create a custom command blt-remote
#!/usr/bin/env bash
#: exec_target = cli
## Run blt command on remote server
##
## Usage: fin blt-remote <host> <command>
# Abort if anything fails
set -e
# Validate script param input
validate_input ()
{
if [[ -z $1 ]]
then
echo "Usage: fin blt-remote <env> <command>"
exit 1
fi
env=$1
shift
command="$@"
}
# -- Execution --
validate_input "$@"
echo -e "Starting Running command..."
echo "./vendor/bin/blt $command; exit" | ssh $env
The command will take the host and command you want to execute with its parameters and send them to your host.
Now you will be able to run your blt commands like this:
fin blt-remote dev artifact:update:drupal --environment=dev
Run BLT Commands on a Remote Server was originally published in Docksal Maintainers Blog on Medium, where people are continuing the conversation by highlighting and responding to this story.