├── pull-database-remote.sh ├── ssh.sh ├── pull-files.sh ├── includes └── remote.sh ├── push-libraries.sh ├── local-config.sh ├── common ├── functions.sh └── init.sh ├── pull-database.sh ├── script.sh ├── README.md └── .lando-scripts.options.example /pull-database-remote.sh: -------------------------------------------------------------------------------- 1 | ## Remote database pull file 2 | ## This file is executed on the remote server during database pull via SSH. 3 | ## ------------------------------------------------------------ 4 | 5 | trap exit ERR 6 | 7 | WORKING_DIR=$1 8 | DB_BACKUP_CMD=$2 9 | 10 | # Backup db 11 | cd $WORKING_DIR 12 | # Execute backup command 13 | echo $(tput setab 6); echo; echo; echo " Backing up database..."; echo; echo $(tput sgr 0); $DB_BACKUP_CMD 14 | 15 | exit 0 16 | -------------------------------------------------------------------------------- /ssh.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | ## SSH connection handler file 4 | ## This file is used to establish remote SSH connections. 5 | ## ------------------------------------------------------------ 6 | 7 | # Load common functions and variables 8 | SCRIPTPATH=$(dirname "${BASH_SOURCE[0]}") && source $SCRIPTPATH/common/init.sh 9 | 10 | include remote 11 | 12 | # We need a remote username to continue 13 | REMOTE_USER="$(remote_user)" 14 | [ ! -z "$REMOTE_USER" ] || die "A username is required!" 15 | 16 | tell "Connecting to ${REMOTE_USER}@$SSH_SERVER..." 17 | 18 | # Start SSH connection 19 | ssh -t -p $SSH_PORT $REMOTE_USER@$SSH_SERVER "cd $SSH_REM_DIR; exec \$SHELL -l" 20 | -------------------------------------------------------------------------------- /pull-files.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | ## Lando Script launcher 4 | ## This file is used launch lando-scripts scripts. 5 | ## ------------------------------------------------------------ 6 | 7 | REMOTE_TASKS=true # This script connects to remote servers. 8 | 9 | # Load common functions and variables 10 | SCRIPTPATH=$(dirname "${BASH_SOURCE[0]}") && source $SCRIPTPATH/common/init.sh 11 | 12 | read -p "Overwrite local files directory? (y/n) " -n 1 -r 13 | echo 14 | 15 | if [[ $REPLY =~ ^[Yy]$ ]] 16 | then 17 | trap exit ERR 18 | 19 | rsync -avz --del --progress -e "ssh -p $SSH_PORT -i $SSH_KEY_FILE" $SSH_USER@$SSH_SERVER:$SSH_REM_DIR/$FILES_DIR /app/$FILES_DIR/.. 20 | 21 | kill $SSH_AGENT_PID 22 | fi 23 | -------------------------------------------------------------------------------- /includes/remote.sh: -------------------------------------------------------------------------------- 1 | ## Remote connection handler. 2 | ## 3 | ## This file contains functions for preparing remote server connections. 4 | ## ------------------------------------------------------------ 5 | 6 | # This script connects to remote servers. 7 | REMOTE_TASKS=true 8 | 9 | # Returns the remote user as defined in the options file. If not defined, it asks for the username. 10 | function remote_user() { 11 | if [[ -n "${SSH_USER}" ]]; then 12 | echo $SSH_USER 13 | else 14 | read -p "$($TXT_CYAN)$(echo -e 'Enter your remote username: \n\b > ')$($TXT_RESET)" -re 15 | if [[ ! -z "$REPLY" ]]; then 16 | SSH_USER=${REPLY} 17 | echo $SSH_USER 18 | else 19 | exit 1 20 | fi 21 | fi 22 | } 23 | -------------------------------------------------------------------------------- /push-libraries.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | ## Push libraries script 4 | ## 5 | ## This script pushes code libraries, such as those installed 6 | ## by Composer, to the remote server. 7 | ## ------------------------------------------------------------ 8 | 9 | REMOTE_TASKS=true # This script connects to remote servers. 10 | 11 | # Load common functions and variables 12 | SCRIPTPATH=$(dirname "${BASH_SOURCE[0]}") && source $SCRIPTPATH/common/init.sh 13 | 14 | # Check that required vars are defined. 15 | if [ -z "$LIBRARY_DIRS" ] 16 | then 17 | echo "Please set LIBRARY_DIRS in .lando-scripts.options.sh before using this command." 18 | exit 1 19 | fi 20 | 21 | read -p "Overwrite remote library directories? (y/n) " -n 1 -r 22 | echo 23 | 24 | if [[ $REPLY =~ ^[Yy]$ ]] 25 | then 26 | trap exit ERR 27 | 28 | cd /app; rsync -avzR -e "ssh -p $SSH_PORT" $LIBRARY_DIRS $SSH_USER@$SSH_SERVER:$SSH_REM_DIR 29 | 30 | kill $SSH_AGENT_PID 31 | fi 32 | -------------------------------------------------------------------------------- /local-config.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | echo "Applying local configuration for $LANDO_APP_NAME app..." 4 | 5 | # Load common functions and variables 6 | SCRIPTPATH=$(dirname "${BASH_SOURCE[0]}") && source $SCRIPTPATH/common/init.sh 7 | 8 | ### 9 | ## Execute pre-config commands 10 | ### 11 | 12 | for C in "${!PRE_CONFIG_COMMANDS[@]}"; do 13 | ${PRE_CONFIG_COMMANDS[$C]} 14 | done 15 | 16 | ### 17 | ## Drush commands 18 | ### 19 | 20 | if $DRUSH_ENABLE; then 21 | # Run drush commands in webroot 22 | cd $LANDO_WEBROOT 23 | 24 | # Enable modules 25 | drush pm-enable -y $DRUSH_ENABLE_MODULES 26 | 27 | # Set Variables 28 | for V in "${!DRUSH_VARIABLE_SET[@]}"; do 29 | drush variable-set $V "${DRUSH_VARIABLE_SET[$V]}" 30 | done 31 | 32 | # Return to previous working directory 33 | cd $CWD 34 | fi 35 | 36 | ### 37 | ## Execute post-config commands 38 | ### 39 | 40 | for C in "${!POST_CONFIG_COMMANDS[@]}"; do 41 | ${POST_CONFIG_COMMANDS[$C]} 42 | done 43 | -------------------------------------------------------------------------------- /common/functions.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | ## Common Functions 4 | ## 5 | ## This script contains common functions for use within other scripts. There's 6 | ## no need to source this file as it is sourced by init.sh. 7 | ## ------------------------------------------------------------ 8 | 9 | THIS_DIR=$(dirname "${BASH_SOURCE[0]}") # The directory where this script resides. 10 | 11 | # Some useful functions 12 | tell() { echo "$($TXT_YEL)$*$($TXT_RESET)" >&2; } 13 | yell() { echo "$($TXT_RED)$(tput bold)$*$($TXT_RESET)" >&2; } 14 | die() { echo; yell "[Error] $*"; echo; exit 111; } 15 | try() { "$@" || yell "Cannot $*"; } 16 | 17 | # Sources include files 18 | include() { 19 | try source "${THIS_DIR}/../includes/$1.sh" 20 | } 21 | 22 | # Sources a file if it exists. 23 | # Usage: `source_if_exists /path/to/file` 24 | source_if_exists() { 25 | [[ -f "$1" ]] && source "$1" 26 | } 27 | 28 | # Scripts that require root can call this function to stop execution if user is not root or sudo. 29 | require_root() { 30 | if [[ $UID != 0 ]]; then 31 | die "This script requires elevated privileges." 32 | fi 33 | } 34 | -------------------------------------------------------------------------------- /common/init.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | ## Script Initializer 4 | ## 5 | ## This script contains common variables and functions to initialize other 6 | ## scripts within this project. Source this script at the beginning of other 7 | ## scripts by using: 8 | ## 9 | ## SCRIPTPATH=$(dirname "${BASH_SOURCE[0]}) && source $SCRIPTPATH/common/init.sh 10 | ## 11 | ## ------------------------------------------------------------ 12 | 13 | # Init variables 14 | THIS_DIR=$(dirname "${BASH_SOURCE[0]}") # The directory where this script resides. 15 | CALLER_DIR=$(dirname "${BASH_SOURCE[1]}") # The directory of the script that called init.sh 16 | CALLER_NAME=$(basename "${BASH_SOURCE[1]}") # The name of the script that called init.sh 17 | CWD=$(pwd) # Current working directory 18 | 19 | # Some text colors 20 | TXT_RESET="tput sgr 0" 21 | TXT_BOLD="tput bold" 22 | TXT_CYAN="tput setaf 6" 23 | TXT_YEL="tput setaf 3" 24 | TXT_RED="tput setaf 1" 25 | 26 | # Source helper functions 27 | source $THIS_DIR/functions.sh 28 | 29 | # Load options 30 | source $THIS_DIR/../../.lando-scripts.options.sh 31 | 32 | # Capture ssh key once 33 | if [[ $SSH_KEY_PW == true && $REMOTE_TASKS == true ]] ; then 34 | eval `ssh-agent` > /dev/null 35 | ssh-add $SSH_KEY_FILE 36 | fi 37 | -------------------------------------------------------------------------------- /pull-database.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | read -p "Overwrite your local database? (y/n) " -n 1 -r 4 | echo 5 | 6 | if [[ $REPLY =~ ^[Yy]$ ]] 7 | then 8 | trap exit ERR 9 | 10 | # Load common functions and variables 11 | SCRIPTPATH=$(dirname "${BASH_SOURCE[0]}") && source $SCRIPTPATH/common/init.sh 12 | 13 | include remote 14 | 15 | # We need a remote username to continue 16 | REMOTE_USER="$(remote_user)" 17 | [ ! -z "$REMOTE_USER" ] || die "A username is required!" 18 | 19 | function download_db() { 20 | # Download db dump from remote server 21 | scp -P $SSH_PORT $REMOTE_USER@$SSH_SERVER:$DB_REM_DUMP_FILE . 22 | } 23 | 24 | 25 | if $DB_REM_SCRIPT ; then 26 | # Execute tasks on remote server to dump db. 27 | ssh -tt -p $SSH_PORT $REMOTE_USER@$SSH_SERVER "/bin/bash -sl -- \"$SSH_REM_DIR\" \"$DB_REM_BACKUP_CMD\"" < $SCRIPTPATH/pull-database-remote.sh 28 | 29 | download_db 30 | 31 | # Delete newly created db dump on remote. 32 | ssh -t -p $SSH_PORT $REMOTE_USER@$SSH_SERVER "rm -fv $DB_REM_DUMP_FILE" 33 | 34 | else 35 | # Download db dump that already exists. 36 | download_db 37 | fi 38 | 39 | # Import db dump 40 | /helpers/mysql-import.sh $DB_REM_DUMP_NAME.gz 41 | 42 | [ -z "$SSH_AGENT_PID" ] || kill $SSH_AGENT_PID 43 | fi 44 | -------------------------------------------------------------------------------- /script.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | ## Lando Script launcher 4 | ## This file is used launch lando-scripts scripts. 5 | ## ------------------------------------------------------------ 6 | 7 | # Define available scripts 8 | declare -A LANDO_SCRIPTS=( 9 | [ssh]='ssh.sh' 10 | [pull-db]='pull-database.sh' 11 | [pull-files]='pull-files.sh' 12 | [local-config]='local-config.sh' 13 | [push-lib]='push-libraries.sh' 14 | ) 15 | 16 | SCRIPTPATH=$(dirname "${BASH_SOURCE[0]}"); 17 | 18 | # Display help message 19 | function lando-scripts-help { 20 | # List avaialable scripts 21 | echo; echo "Usage: lando script