From b39ecacde29cbccb1df416d2a39d7bb961b622e8 Mon Sep 17 00:00:00 2001 From: A Holt Date: Sun, 12 May 2019 06:55:50 -0400 Subject: [PATCH] Properly emulate ./runrole openvpn + comment code --- roles/openvpn/templates/iiab-support | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/roles/openvpn/templates/iiab-support b/roles/openvpn/templates/iiab-support index 868f30ac6..037d08afb 100644 --- a/roles/openvpn/templates/iiab-support +++ b/roles/openvpn/templates/iiab-support @@ -1,5 +1,8 @@ #!/bin/bash +# openvpn_handle is stored in 2 files on disk, one slightly stripped down (from +# the other) due to Ansible. So we emulate Ansible's behavior, when reading from +# (and later writing to) disk, removing outer cruft as explained on Lines 28-30 handle1=$(grep "^openvpn_handle:" /etc/iiab/local_vars.yml | sed "s/^openvpn_handle:\s*\(['\"]\)\(.*\)\1\s*$/\2/") echo -e "\n/etc/iiab/local_vars.yml source/master copy: $handle1" if [ -f /etc/iiab/openvpn_handle ]; then @@ -11,23 +14,28 @@ fi echo -en "\e[1mPlease type a descriptive OpenVPN machine name (openvpn_handle) such as:\n\n cape-town-school-36-rpi-2019-05-31\n\nOr hit [Enter] to keep the existing name:\e[0m " read ans < /dev/tty -ans=$(echo $ans | sed "s/^\s*\(['\"]\)\(.*\)\1\s*$/\2/") #if [ "$ans" != "" ] || ( [ "$handle1" = "" ] && [ ! -f /etc/iiab/openvpn_handle ] ); then -if [ "$ans" != "" ] || ( [ "$handle1" = "" ] && [ ! -v handle2 ] ); then # equivalent to above +# -v (below) checks if var's defined: equivalent to file existence test above +if [ "$ans" != "" ] || ( [ "$handle1" = "" ] && [ ! -v handle2 ] ); then if grep -q '^openvpn_handle:' /etc/iiab/local_vars.yml; then sed -i "s/^openvpn_handle:.*/openvpn_handle: $ans/" /etc/iiab/local_vars.yml else echo "openvpn_handle: $ans" >> /etc/iiab/local_vars.yml fi + + # BEHAVIOR JUST LIKE ANSIBLE'S: create /etc/iiab/openvpn_handle from the + # "^openvpn_handle:" line in /etc/iiab/local_vars.yml by (1) removing outer + # spacing IF NEC, then (2) removing 1 pair of matching outer quotes IF NEC. + ans=$(echo $ans | sed "s/^\s*\(['\"]\)\(.*\)\1\s*$/\2/") echo $ans > /etc/iiab/openvpn_handle echo -e "\n\e[1mSAVED: openvpn_handle recorded into both above files.\e[0m\n" +elif [ "$handle1" != "$handle2" ]; then # Sloppily, but conveniently here, + # bash treats "$handle2" as "" when var undefined, catching all conflicts! + echo -e "\n\e[41mYou MUST specify an OpenVPN machine name (openvpn_handle) to resolve the above\e[0m" + echo -e "\e[41mnaming conflict. Please rerun to proceed.\e[0m\n" + exit 1 else - if [ "$handle1" != "$handle2" ]; then # sloppily (but conveniently here) bash treats "$handle2" as "" when var's not defined! - echo -e "\n\e[41mYou MUST specify an OpenVPN machine name (openvpn_handle) to resolve the above\e[0m" - echo -e "\e[41mnaming conflict. Please rerun to proceed.\e[0m\n" - exit 1 - fi echo -e "\n\e[1mWARNING: openvpn_handle remains unchanged in both above files.\e[0m\n" fi