Skip to content

Commit

Permalink
ENH/MAINT: rework the error handling to avoid bad states
Browse files Browse the repository at this point in the history
  • Loading branch information
ZLLentz committed Mar 27, 2024
1 parent abb38a5 commit b03718c
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 12 deletions.
5 changes: 2 additions & 3 deletions scripts/bootstrap_plc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,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 @@ -91,6 +93,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
7 changes: 4 additions & 3 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 @@ -27,12 +29,11 @@ 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
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
5 changes: 2 additions & 3 deletions scripts/update_admin_pass.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,10 @@ SSH_CONFIG="${ANSIBLE_ROOT}/ssh_config"

# 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

0 comments on commit b03718c

Please sign in to comment.