Docksal: Run BLT Commands on a Remote Server

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.

This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

Docksal: Run BLT Commands on a Remote Server

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.