Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: polish scripts (error handling, paths) #22

Merged
merged 8 commits into from
Mar 28, 2024
13 changes: 13 additions & 0 deletions scripts/activate_python.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash
# This is meant to be sourced.
# Helper script for activating the correct python environment.
# Sets a default env or you can provide your own activate path for testing

DEFAULT_ENV=/cds/group/pcds/pyps/conda/venvs/ansible/bin/activate

if [ -f "${ANSIBLE_PYTHON_ACTIVATE:=${DEFAULT_ENV}}" ]; then
source "${ANSIBLE_PYTHON_ACTIVATE}"
else
echo "No Python activation script found at ${ANSIBLE_PYTHON_ACTIVATE}"
return 1

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: Shouldn't we also return 0 if ANSIBLE_PYTHON_ACTIVATE was found?

Copy link
Member Author

@ZLLentz ZLLentz Mar 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return 0 is implied, when the script rolls off the end without erroring or returning early

fi
18 changes: 7 additions & 11 deletions scripts/bootstrap_plc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,14 @@ fi
HOSTNAME="${1}"
shift

# Activate python env if we don't have ansible on the path
if [ ! -x ansible-playbook ]; then
source /cds/group/pcds/pyps/conda/venvs/ansible/bin/activate
fi

USERNAME="${PLC_USERNAME:=Administrator}"

THIS_SCRIPT="$(realpath "${0}")"
THIS_DIR="$(dirname "${THIS_SCRIPT}")"
ANSIBLE_ROOT="$(realpath "${THIS_DIR}/..")"
INVENTORY_PATH="${ANSIBLE_ROOT}/inventory/plcs.yaml"
SSH_CONFIG="${ANSIBLE_ROOT}/ssh_config"
source "${THIS_DIR}"/paths.sh

# Use the correct python env
source "${THIS_DIR}"/activate_python.sh

# Check the inventory for your plc
if grep -q "${HOSTNAME}:" "${INVENTORY_PATH}"; then
Expand All @@ -52,6 +49,8 @@ fi

# Register the ssh key with the ssh agent if needed
source "${THIS_DIR}/ssh_agent_helper.sh"
# Stop the ssh agent at exit if we started it here
trap ssh_agent_helper_cleanup EXIT

# Send the public key to the plc, if it has not already been done
ssh-copy-id -i "${SSH_KEY_FILENAME}" -o PreferredAuthentications=keyboard-interactive "${USERNAME}@${HOSTNAME}"
Expand Down Expand Up @@ -93,6 +92,3 @@ scp -F "${SSH_CONFIG}" -i "${SSH_KEY_FILENAME}" -r "${SOURCE_DIR}" "${USERNAME}@

# Run the local install version of the bootstrap playbook
ansible-playbook "${ANSIBLE_ROOT}/tcbsd-bootstrap-from-local-playbook.yaml" --extra-vars "target=${HOSTNAME} ansible_ssh_private_key_file=${SSH_KEY_FILENAME}" --ask-become-pass "$@"

# Stop the ssh agent if we started it here
ssh_agent_helper_cleanup
2 changes: 2 additions & 0 deletions scripts/dry_run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
# $ ./dry_run.sh tst_all
#
# Groups are defined in the inventory file.
set -e

if [ -z "${1}" ]; then
echo "Ansible target required"
exit 1
Expand Down
18 changes: 18 additions & 0 deletions scripts/paths.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/bash
# Sourceable script to set common vars for the various scripts.
# This sets a bunch of environment variables related to known paths
# and puts us into the ansible directory for the duration of the
# encapsulating script.
set -e

THIS_SCRIPT="$(realpath "${BASH_SOURCE[0]}")"
THIS_DIR="$(dirname "${THIS_SCRIPT}")"

ANSIBLE_ROOT="$(realpath "${THIS_DIR}/..")"
export ANSIBLE_ROOT
INVENTORY_PATH="${ANSIBLE_ROOT}/inventory/plcs.yaml"
export INVENTORY_PATH
SSH_CONFIG="${ANSIBLE_ROOT}/ssh_config"
export SSH_CONFIG

cd "${ANSIBLE_ROOT}"
15 changes: 7 additions & 8 deletions scripts/provision_plc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
# $ ./provision_plc.sh tst_all
#
# Groups are defined in the inventory file.
set -e

if [ -z "${1}" ]; then
echo "Ansible target required"
exit 1
Expand All @@ -20,21 +22,18 @@ shift

THIS_SCRIPT="$(realpath "${0}")"
THIS_DIR="$(dirname "${THIS_SCRIPT}")"
ANSIBLE_ROOT="$(realpath "${THIS_DIR}/..")"
source "${THIS_DIR}"/paths.sh

# Activate python env if we don't have ansible on the path
if [ ! -x ansible-playbook ]; then
source /cds/group/pcds/pyps/conda/venvs/ansible/bin/activate
fi
# Use the correct python env
source "${THIS_DIR}"/activate_python.sh

# Register the ssh key with the ssh agent if needed
source "${THIS_DIR}/ssh_agent_helper.sh"
# Stop the ssh agent at exit if we started it here
trap ssh_agent_helper_cleanup EXIT

# Run the provision playbook
ansible-playbook "${ANSIBLE_ROOT}/tcbsd-provision-playbook.yaml" --extra-vars "target=${TARGET} ansible_ssh_private_key_file=${SSH_KEY_FILENAME}" --ask-become-pass "$@"

# Stop the ssh agent if we started it here
ssh_agent_helper_cleanup

# Prompt to update deployment docs
"${THIS_DIR}"/docs_prompt.sh
5 changes: 2 additions & 3 deletions scripts/setup_new_plc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@ THIS_DIR="$(dirname "${THIS_SCRIPT}")"

# Register the ssh key with the ssh agent if needed
source "${THIS_DIR}/ssh_agent_helper.sh"
# Stop the ssh agent at exit if we started it here
trap ssh_agent_helper_cleanup EXIT

# Run both playbooks and one-time pre-playbook setup
"${THIS_DIR}"/bootstrap_plc.sh "${1}"
"${THIS_DIR}"/provision_plc.sh "${1}"

# Stop the ssh agent if we started it here
ssh_agent_helper_cleanup
3 changes: 3 additions & 0 deletions scripts/ssh_agent_helper.sh
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
# Expected usage:
#
# source ssh_agent_helper.sh
set -e

SSH_KEY_FILENAME="${HOME}/.ssh/tcbsd_key_rsa"
export SSH_KEY_FILENAME
Expand All @@ -28,6 +29,8 @@ ssh_agent_helper_cleanup() {
fi
}
export ssh_agent_helper_cleanup
# Clean up immediately if something in this script fails
trap ssh_agent_helper_cleanup ERR

# Create an ssh key, if it does not already exist
if [ ! -f "${SSH_KEY_FILENAME}" ]; then
Expand Down
8 changes: 3 additions & 5 deletions scripts/update_admin_pass.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,14 @@ fi
USERNAME="${PLC_USERNAME:=Administrator}"
THIS_SCRIPT="$(realpath "${0}")"
THIS_DIR="$(dirname "${THIS_SCRIPT}")"
ANSIBLE_ROOT="$(realpath "${THIS_DIR}/..")"
SSH_CONFIG="${ANSIBLE_ROOT}/ssh_config"
source "${THIS_DIR}"/paths.sh

# Register the ssh key with the ssh agent if needed
source "${THIS_DIR}/ssh_agent_helper.sh"
# Stop the ssh agent at exit if we started it here
trap ssh_agent_helper_cleanup EXIT

for HOSTNAME in "$@"; do
echo "Logging into ${HOSTNAME}"
ssh -F "${SSH_CONFIG}" -i "${SSH_KEY_FILENAME}" -t "${USERNAME}@${HOSTNAME}" passwd
done

# Stop the ssh agent if we started it here
ssh_agent_helper_cleanup