Skip to content

Instantly share code, notes, and snippets.

@pinkeen
Last active July 16, 2021 13:13
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save pinkeen/28b06f001bd578bb19187472940e486e to your computer and use it in GitHub Desktop.
Save pinkeen/28b06f001bd578bb19187472940e486e to your computer and use it in GitHub Desktop.
Helper scripts for Magento Commerce Cloud prod/stage envs

Magento Cloud helper scripts

I drop them into the root project's dir and push them to the repo. Mostly they should use whatever env you've got checked out with magento-cloud command.

Tested only on Mac with Pro/Stage envs, use at your own risk YMMV.

Tip: You can adjust .gitignore like this for convenience:

!/mgs-*
!/mgs-*/**
# Just exit if not on MGC node...
[[ ! -z "$MAGENTO_CLOUD_ENVIRONMENT" ]] || return
source "$MAGENTO_CLOUD_DIR/mgs-bash-config.sh"
#!/usr/bin/env bash
[[ $_ != $0 ]] || (echo "This script must be sourced, not executed!" && exit 1)
[[ -z "$MGS_BASH_CFG_LOADED" ]] || return 0
echo "Loading MageSuite interactive bash config..."
export MGS_BASH_CFG_LOADED="yes"
# -- Set up bash history ---
# Actually store history on this read-only filesystem.
# Also share it across nodes using this mounted dir.
export HISTFILE="$MAGENTO_CLOUD_DIR/var/run/bash_history"
export HIST_STAMPS="yyyy-mm-dd"
export PROMPT_COMMAND='history -a'
export HISTSIZE=500000
export HISTFILESIZE=100000
export HISTCONTROL="erasedups:ignoreboth"
export HISTIGNORE="&:[ ]*:exit:ls:bg:fg:history:clear"
export HISTTIMEFORMAT='%F %T '
shopt -s histappend
shopt -s cmdhist
shopt -s histreedit
shopt -s histverify
shopt -s lithist
set -o histexpand
bind '"\e[A": history-search-backward'
bind '"\e[B": history-search-forward'
bind '"\e[C": forward-char'
bind '"\e[D": backward-char'
# -- Set up global options --
export TIME_STYLE="+%Y-%m-%d %H:%M"
export EDITOR='nano'
shopt -s complete_fullquote
shopt -s globstar
shopt -s direxpand
shopt -u progcomp
shopt -u cdspell
shopt -u dirspell
shopt -u autocd
# -- Set up prompt --
MGS_MGC_NODE_NR=`(echo "test">&2 | grep "$(hostname -i)" /etc/hosts | grep -v "$(hostname)" | cut -d' ' -f2 | sed -e 's/host\([1-9]\)/\1/g' || true ) 2>/dev/null`
[[ -z "${MGS_MGC_NODE_NR}" ]] || MGS_MGC_NODE_NR_SUFFIX="-node-${MGS_MGC_NODE_NR}"
export MGS_MGC_ENV_NAME="${MAGENTO_CLOUD_APPLICATION_NAME}-${MAGENTO_CLOUD_BRANCH}"
export MGS_MGC_NODE_NAME="${MGS_MGC_ENV_NAME}${MGS_MGC_NODE_NR_SUFFIX}"
export MGS_MGC_NODE_NR
export PS1="\[\e[0;31m\][mgs-shell]\[\e[0m\] \[\e[0;33m\]\u@\[\e[0;32m\]$MGS_MGC_NODE_NAME\[\e[0m\] \[\e[0;34m\]\w\[\e[0m\] $ "
# -- Set up useful aliases --
alias ls='ls --color=auto -N'
alias ll='ls -lh --group-directories-first'
alias l='ls -lAh --group-directories-first'
alias lst='ls -lht'
alias lss='ls -lhS'
alias cp='cp -v'
alias rm='rm -v'
alias mv='mv -v'
alias pgrep='pgrep -f'
alias pkill='pkill -f'
alias magento="$MAGENTO_CLOUD_DIR/bin/magento"
#!/usr/bin/env bash
set -euo pipefail
trap "jobs -p | xargs kill 2>/dev/null || true; echo ' => Terminated background jobs, exiting gracefully...'; exit 0" EXIT INT HUP QUIT
_SSH_HOST="$(magento-cloud ssh --pipe --yes)"
# _TMPDIR="$(pwd)/.tmp"
# mkdir -p "$_TMPDIR"
_ES_HTTP_PORT_REMOTE="$(magento-cloud environment:relationships --property=elasticsearch.0.port)"
_ES_HTTP_PORT_LOCAL="$(perl -e 'print int(rand(65000-2000)) + 2000')"
# _ES_HTTP_SOCK_LOCAL="$_TMPDIR/mgs-es-hq-$(perl -e 'print int(rand(65000-2000)) + 2000').sock"
_ES_HQ_PORT_DOCKER="5000"
_ES_HQ_PORT_LOCAL="$(perl -e 'print int(rand(65000-2000)) + 2000')"
_ES_HTTP_URL_DOCKER="http://docker.for.mac.localhost:${_ES_HTTP_PORT_LOCAL}"
# _ES_HTTP_SOCK_DOCKER="/var/run/mgs-es-hq-$(perl -e 'print int(rand(65000-2000)) + 2000').sock"
_ES_TUNNEL_CMD="ssh -n -N -L $_ES_HTTP_PORT_LOCAL:localhost:$_ES_HTTP_PORT_REMOTE $_SSH_HOST"
echo " => Launching tunnel: $_ES_TUNNEL_CMD"
$_ES_TUNNEL_CMD &
sleep 0.5s
_ES_HQ_PORT_DOCKER_CMD="docker run -a STDIN -a STDERR --rm -p ${_ES_HQ_PORT_LOCAL}:${_ES_HQ_PORT_DOCKER} -e HQ_DEFAULT_URL=${_ES_HTTP_URL_DOCKER} elastichq/elasticsearch-hq"
echo " => Launching docker container: $_ES_HQ_PORT_DOCKER_CMD"
$_ES_HQ_PORT_DOCKER_CMD &
sleep 5s
_ES_HQ_LOCAL_URL="http://localhost:${_ES_HQ_PORT_LOCAL}/"
echo ""
echo " => Local URL: $_ES_HQ_LOCAL_URL"
echo " => Elasticsearch API in Docker URL: $_ES_HTTP_URL_DOCKER"
echo ""
echo " => Opening in browser "
open "$_ES_HQ_LOCAL_URL"
echo ""
echo " !! Press CTRL+C to close tunnel and exit ..."
wait
#!/usr/bin/env bash
set -euo pipefail
trap "jobs -p | xargs kill 2>/dev/null || true; echo ' => Terminated background jobs, exiting gracefully...'; exit 0" EXIT INT HUP QUIT
_SSH_HOST="$(magento-cloud ssh --pipe --yes)"
_RMQ_USER="$(magento-cloud environment:relationships --property=rabbitmq.0.username)"
_RMQ_PASS="$(magento-cloud environment:relationships --property=rabbitmq.0.password)"
_RMQ_MGMT_PORT_REMOTE="15672"
_RMQ_MGMT_PORT_LOCAL="$(perl -e 'print int(rand(65000-2000)) + 2000')"
_RMQ_TUNNEL_CMD="ssh -n -N -L $_RMQ_MGMT_PORT_LOCAL:localhost:$_RMQ_MGMT_PORT_REMOTE $_SSH_HOST"
echo " => Launching tunnel: $_RMQ_TUNNEL_CMD"
$_RMQ_TUNNEL_CMD &
sleep 0.5s
_RMQ_MGMT_LOCAL_URL="http://${_RMQ_USER}:${_RMQ_PASS}@localhost:${_RMQ_MGMT_PORT_LOCAL}/"
_RMQ_MGMT_LOCAL_URL="http://localhost:${_RMQ_MGMT_PORT_LOCAL}/"
echo ""
echo " => Local URL: $_RMQ_MGMT_LOCAL_URL"
echo " => User: $_RMQ_USER"
echo " => Password: $_RMQ_PASS"
echo ""
echo " => Opening in browser "
open "$_RMQ_MGMT_LOCAL_URL"
echo ""
echo " !! Press CTRL+C to close tunnel and exit ..."
wait
#!/usr/bin/env bash
set -euo pipefail
if [[ $# -lt 3 ]] ; then
echo ""
echo " This script syncs directories directly between two Magento Cloud envs."
echo ""
echo " Usage: "
echo " $0 [directory to sync] [source env] [dest env] <extra rsync opts>"
echo ""
echo " Note: Passing '--dry-run' as extra rsync opts is suggested before proceeding."
echo ""
echo " Example: $0 pub/media production staging --exclude=/catalog/product/catalog/"
echo ""
exit 9
fi
trap "jobs -p | xargs kill 2>/dev/null || true; echo ' => Terminated background jobs, exiting gracefully...'" EXIT INT HUP QUIT
_SYNC_PATH="$(echo $1 | sed 's~/*$~/~g')"
_ENV_SRC="$2"
_ENV_DST="$3"
shift 3
_SSH_HOST_SRC="$(magento-cloud ssh --pipe --yes --environment $_ENV_SRC)"
_SSH_HOST_DST="$(magento-cloud ssh --pipe --yes --environment $_ENV_DST)"
_SSH_FWD_PORT="51954"
_RSYNC_EXCLUDES='--exclude=*.csv --exclude=*.tmp --exclude=tmp/ --exclude=cache/ --exclude=thumbnail/ --exclude=.*'
_RSYNC_OPTS="-pavW --progress --itemize-changes --human-readable --delete '--rsh=ssh -o StrictHostKeyChecking=no'"
_RSYNC_OPTS_EXTRA="$@"
_RSYNC_CMD="set -x; rsync $_RSYNC_OPTS $_RSYNC_OPTS_EXTRA $_RSYNC_EXCLUDES $_SSH_HOST_SRC:$_SYNC_PATH $_SYNC_PATH"
_SYNC_CMD="ssh -t -A $_SSH_HOST_DST -- $_RSYNC_CMD"
echo -e ""
echo -e " => Preparing to sync: $_ENV_SRC:$_SYNC_PATH -> $_ENV_DST:$_SYNC_PATH"
echo -e " => Using rsync cmd: $_RSYNC_CMD"
echo -e " => By executing: $_SYNC_CMD"
echo -e ""
echo -e " => Press enter to proceed..."
read X
# sleep 10s
#echo -e " => Setting up ssh-agent..."
#eval `ssh-agent` && ssh-add
echo -e " => Ssh keys in agent: \n $(ssh-add -l)"
echo -e ""
echo -e " => Starting sync!"
$_SYNC_CMD
echo -e " => Sync success!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment