Created
May 28, 2024 14:46
-
-
Save lcatlett/569ff919b08db3007bcaea78fa551e94 to your computer and use it in GitHub Desktop.
Bulk Pantheon Log Collection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# This script is used to streamline the process of collecting logs accross multiple environments and containers for a site. | |
# It sets up the necessary SSH configuration and keys to disable interactive prompts, then uses Terminus to collect logs from the desired site's environments. | |
# USAGE:./collect-logz <site> <env> where <env> is a single pantheon environment such as live or dev, or "all" to collect logs from all environments. | |
# EXAMPLE:./collect-logz ufc-com all will collect logs from all environments for the ufc-com site. | |
# EXAMPLE:./collect-logz ufc-com live will collect logs from the live environment for the ufc-com site. | |
# Put this script in /usr/local/bin/collect-logz and make it executable with chmod +x /usr/local/bin/collect-logz | |
SITE=$1 | |
ENV=$2 | |
# unset SSH_ASKPASS | |
# export GIT_ASKPASS= | |
function setup_variables { | |
# !! IMPORTANT !! Change this value to the path of the private key you want to use for syncing logs from Pantheon. This key should not be password protected. | |
# Example: SSH_PRIVATE_KEY_PATH="$HOME/.ssh/id_rsa" | |
SSH_PRIVATE_KEY_PATH="$HOME/.ssh/id_rsa" | |
# if the SSH_PRIVATE_KEY_PATH is not set, ask the user which key to use. Exit if the user doesn't provide a key. | |
if [[ -z "$SSH_PRIVATE_KEY_PATH" ]]; then | |
echo "SSH_PRIVATE_KEY_PATH is not set. Please enter the path to the private key you want to use for deployment to Pantheon." | |
read -p "Enter the path to the private key: " SSH_PRIVATE_KEY_PATH | |
if [[ -z "$SSH_PRIVATE_KEY_PATH" ]]; then | |
echo "SSH_PRIVATE_KEY_PATH is not set. Exiting." | |
exit 1 | |
fi | |
fi | |
} | |
# Setup ssh config for drush.in hosts. | |
# This is to help avoid password prompts when using git and ssh. | |
# TODO: If drush.in host exists in ~/.ssh/config, add the following lines to the existing config. | |
function ssh_config_setup() { | |
if grep -q "Host \*.drush.in" ~/.ssh/config; then | |
echo "Host *.drush.in already exists in ~/.ssh/config. Updating config." | |
awk ' | |
BEGIN { | |
printHost=0 | |
updated=0 | |
} | |
/^Host \*.drush.in/ { | |
printHost=1 | |
} | |
printHost && /^ StrictHostKeyChecking/ { | |
print " StrictHostKeyChecking no" | |
updated=1 | |
next | |
} | |
printHost && /^ PasswordAuthentication/ { | |
print " PasswordAuthentication no" | |
updated=1 | |
next | |
} | |
printHost && /^ HostkeyAlgorithms/ { | |
print " HostkeyAlgorithms +ssh-rsa" | |
updated=1 | |
next | |
} | |
printHost && /^ / { | |
printHost=0 | |
} | |
{ print } | |
END { | |
if (printHost && !updated) { | |
print " StrictHostKeyChecking no" | |
print " PasswordAuthentication no" | |
print " HostkeyAlgorithms +ssh-rsa" | |
} | |
}' ~/.ssh/config > temp && mv temp ~/.ssh/config | |
# sed -i '' 's/Host \*.drush.in/Host \*.drush.in\n StrictHostKeyChecking no\n PasswordAuthentication no\n HostkeyAlgorithms +ssh-rsa\n PubkeyAcceptedAlgorithms +ssh-rsa/' ~/.ssh/config | |
else | |
echo "Host *.drush.in does not exist in ~/.ssh/config. Adding config." | |
echo -e "Host *.drush.in\n StrictHostKeyChecking no\n PasswordAuthentication no\n HostkeyAlgorithms +ssh-rsa\n PubkeyAcceptedAlgorithms +ssh-rsa" >>~/.ssh/config | |
fi | |
# if "Host *.drush.in" entry exists in ssh config, update the config in place | |
} | |
#function ssh_config_setup() { | |
# echo -e "Host *.drush.in\n StrictHostKeyChecking no\n PasswordAuthentication no\n HostkeyAlgorithms +ssh-rsa\n PubkeyAcceptedAlgorithms +ssh-rsa" >>~/.ssh/config | |
# | |
# Ensure that the SSH agent is running and has the private key loaded for this site. | |
# This is critical for avoiding password prompts when using git and ssh. | |
function ssh_agent_setup() { | |
# set SSH_PRIVATE_KEY to the contents of the private key | |
SSH_PRIVATE_KEY=$(cat $SSH_PRIVATE_KEY_PATH) | |
#echo "$SSH_PRIVATE_KEY" > private.key | |
#chmod 600 private.key | |
eval $(ssh-agent -s) | |
ssh-add $SSH_PRIVATE_KEY_PATH | |
ssh-add -L | ssh-keygen -l -E md5 -f - | awk '{print substr($2,5)}' | |
} | |
# Scan for and add the Pantheon SSH host keys to the known_hosts file for this site's code and app servers. | |
# This is to avoid prompts to confirm the authenticity of the host when connecting via SSH. | |
function ssh_host_key_scan() { | |
echo "Setting up ssh host keys for $SITE" | |
SITE_ID=$(terminus site:lookup "$SITE") | |
ssh-keyscan -t rsa -p 2222 "appserver.dev.${SITE_ID}.drush.in" >>~/.ssh/known_hosts | |
ssh-keyscan -t rsa -p 2222 "dbserver.dev.${SITE_ID}.drush.in" >>~/.ssh/known_hosts | |
ssh-keyscan -t rsa -p 2222 "codeserver.dev.${SITE_ID}.drush.in" >>~/.ssh/known_hosts | |
} | |
# cleanup function - delete private.key in cwd etc | |
function cleanup() { | |
rm -f private.key | |
unset SSH_PRIVATE_KEY | |
unset SSH_PRIVATE_KEY_PATH | |
echo "cleanup complete" | |
} | |
setup_variables | |
ssh_config_setup | |
ssh_agent_setup | |
ssh_host_key_scan | |
echo "Setting required env configuration values for $SITE" | |
if [ "$ENV" == "all" ]; then | |
ENVLIST="$(terminus env:list $SITE --format=list --fields=id)" | |
IFS=$'\n' paths=($ENVLIST) | |
else | |
ENVLIST=$ENV | |
IFS=, paths=($ENVLIST) | |
fi | |
for ((i = 0; i < ${#paths[@]}; i++)); do | |
ENVI=${paths[$i]} | |
echo "Collecting logs for $ENVI" | |
for ENV_SITE in $ENVI; do | |
terminus logs:get $SITE.$ENV_SITE --all | |
done | |
done | |
cleanup | |
echo "Log collection complete." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment