From 69265fcebbf145f7bf85d85b0d2eca3805add1d8 Mon Sep 17 00:00:00 2001 From: Oliver Walsh Date: Fri, 22 Sep 2017 11:56:17 +0100 Subject: [PATCH] Fix running complex command lines via bootstrap_host_exec This currently uses eval $* which doesn't correctly handle complex args e.g with complex quoting, subshells etc.. exec "$@" is the correct command to run. "$@" is special in bash and expands to "$1" "$2" "$3" etc... to pass through command line args verbatim. Also exec accepts a list of args while eval accepts a string. However fixing it may break command strings that happened to work previously. So this change deprecates bootstrap_host_exec, replacing it with bootstrap_host_only_eval and bootstrap_host_only_exec. Change-Id: I993c3354a3d9fd392fa4fa2e3b5b8ed421487a88 Closes-bug: 1718914 --- scripts/bootstrap_host_only_eval | 19 +++++++++++++++++++ scripts/bootstrap_host_only_exec | 19 +++++++++++++++++++ setup.cfg | 2 ++ 3 files changed, 40 insertions(+) create mode 100755 scripts/bootstrap_host_only_eval create mode 100755 scripts/bootstrap_host_only_exec diff --git a/scripts/bootstrap_host_only_eval b/scripts/bootstrap_host_only_eval new file mode 100755 index 000000000..b4c2772fe --- /dev/null +++ b/scripts/bootstrap_host_only_eval @@ -0,0 +1,19 @@ +#!/bin/bash +set -e +SERVICE_NAME=$1 +if [ -z "$SERVICE_NAME" ]; then + echo "Please supply a valid service name." + exit 1 +fi +shift +if [ -z "$*" ]; then + echo "Please supply a valid 'command' to run as an argument." + exit 1 +fi +HOSTNAME=$(/bin/hostname -s) +SERVICE_NODEID=$(/bin/hiera -c /etc/puppet/hiera.yaml "${SERVICE_NAME}_short_bootstrap_node_name") +if [[ "$HOSTNAME" == "$SERVICE_NODEID" ]]; then + eval $* +else + echo "Skipping execution since this is not the bootstrap node for this service." +fi diff --git a/scripts/bootstrap_host_only_exec b/scripts/bootstrap_host_only_exec new file mode 100755 index 000000000..30255e3cb --- /dev/null +++ b/scripts/bootstrap_host_only_exec @@ -0,0 +1,19 @@ +#!/bin/bash +set -e +SERVICE_NAME=$1 +if [ -z "$SERVICE_NAME" ]; then + echo "Please supply a valid service name." + exit 1 +fi +shift +if [ -z "$*" ]; then + echo "Please supply a valid 'command' to run as an argument." + exit 1 +fi +HOSTNAME=$(/bin/hostname -s) +SERVICE_NODEID=$(/bin/hiera -c /etc/puppet/hiera.yaml "${SERVICE_NAME}_short_bootstrap_node_name") +if [[ "$HOSTNAME" == "$SERVICE_NODEID" ]]; then + exec "$@" +else + echo "Skipping execution since this is not the bootstrap node for this service." +fi diff --git a/setup.cfg b/setup.cfg index f9c698625..67d3fdf15 100644 --- a/setup.cfg +++ b/setup.cfg @@ -24,6 +24,8 @@ packages = scripts = scripts/bootstrap_host_exec + scripts/bootstrap_host_only_eval + scripts/bootstrap_host_only_exec scripts/create_freeipa_enroll_envfile.py scripts/pull-puppet-modules scripts/run-validation