From 4210a3b9e99b3259f2d60c471bae9e688069bfbf Mon Sep 17 00:00:00 2001 From: Arif Ali Date: Mon, 14 Mar 2022 11:32:10 +0000 Subject: [PATCH] Update scripts using shellcheck + fix fix_cloud_focal.sh --- check_juju.sh | 2 +- check_queues.sh | 4 +-- check_vip_issue_db.sh | 28 +++++++++--------- fix_cloud_focal.sh | 69 ++++++++++++++++++++++--------------------- get_charm_versions.sh | 6 ++-- grab_vips.sh | 4 +-- vault.sh | 2 +- vault_init.sh | 2 +- 8 files changed, 60 insertions(+), 57 deletions(-) diff --git a/check_juju.sh b/check_juju.sh index 5fdcdad..8ffb783 100755 --- a/check_juju.sh +++ b/check_juju.sh @@ -10,7 +10,7 @@ check_controller() } if [[ -z "$1" ]] ; then - check_controller $(juju controllers --format json | jq .\"current-controller\" | sed s/\"//g) + check_controller $(juju controllers --format json | jq -r .\"current-controller\") else check_controller $1 fi diff --git a/check_queues.sh b/check_queues.sh index 0698729..451db31 100755 --- a/check_queues.sh +++ b/check_queues.sh @@ -21,8 +21,8 @@ if [[ -z "$1" ]] ; then for controller_json in ${controllers} do - controller=$(echo $controller_json | jq .controller | sed s/\"//g) - model=$(echo $controller_json | jq .model | sed s/\"//g) + controller=$(echo $controller_json | jq -r .controller) + model=$(echo $controller_json | jq -r .model) check_controller ${controller} ${model} done diff --git a/check_vip_issue_db.sh b/check_vip_issue_db.sh index 734f62d..a1469d4 100755 --- a/check_vip_issue_db.sh +++ b/check_vip_issue_db.sh @@ -9,18 +9,18 @@ JUJU_DEV_FEATURE_FLAGS=developer-mode juju dump-db --format json > ${juju_db} #juju_status=/tmp/tmp.IG9jLOKBET #juju_db=/tmp/tmp.1kg5IV5I3k -hacluster_apps=$(cat ${juju_status} | jq ".applications | to_entries[] | select(.value[\"charm-name\"] == \"hacluster\") | .key" | sed s/\"//g) +hacluster_apps=$(jq -r ".applications | to_entries[] | select(.value[\"charm-name\"] == \"hacluster\") | .key" ${juju_status}) for app in ${hacluster_apps} do - super_app=$(cat ${juju_status} | jq .applications.\"${app}\".relations.ha[] | sed s/\"//g) - units=$(cat ${juju_status} | jq ".applications.\"${super_app}\".units | keys[]" | sed s/\"//g) + super_app=$(jq -r .applications.\"${app}\".relations.ha[] ${juju_status}) + units=$(jq -r ".applications.\"${super_app}\".units | keys[]" ${juju_status}) #vips=$(juju config ${super_app} vip) - vips=$(cat ${juju_db} | jq ".settings[] | select(._id | contains(\"a#${super_app}#cs\")) | .settings.vip") + vips=$(jq -r ".settings[] | select(._id | contains(\"a#${super_app}#cs\")) | .settings.vip" ${juju_db}) - bindings=$(cat ${juju_status} | jq ".applications.\"${super_app}\".\"endpoint-bindings\" | keys[]" | sed s/\"//g) + bindings=$(jq -r ".applications.\"${super_app}\".\"endpoint-bindings\" | keys[]" ${juju_status}) for r in ${bindings} do @@ -28,17 +28,17 @@ do for unit in ${units} do #relation_ids=$(juju run --unit ${unit} "relation-ids ${r}" | awk -F\: '{print $2}' | sort) - relation_ids=$(cat ${juju_db} | jq ".relations[] | select(.endpoints[].relation.name == \"${r}\") | select(.endpoints[].applicationname == \"${super_app}\")| .id") + relation_ids=$(jq ".relations[] | select(.endpoints[].relation.name == \"${r}\") | select(.endpoints[].applicationname == \"${super_app}\")| .id" ${juju_db}) for i in ${relation_ids} do - #relation_output=$(juju run --unit ${unit} "relation-get -r ${r}:${i} - ${unit}" | grep $vip) - if [[ ${r} == "cluster" ]] ; then - relation_output=$(cat ${juju_db} | jq ".settings[] | select(._id | contains(\"r#${i}#peer#${unit}\"))") - else - relation_output=$(cat ${juju_db} | jq ".settings[] | select(._id | contains(\"r#${i}#${unit}#req\"))") - fi + #relation_output=$(juju run --unit ${unit} "relation-get -r ${r}:${i} - ${unit}" | grep $vip) + if [[ ${r} == "cluster" ]] ; then + relation_output=$(jq ".settings[] | select(._id | contains(\"r#${i}#peer#${unit}\"))" ${juju_db}) + else + relation_output=$(jq ".settings[] | select(._id | contains(\"r#${i}#${unit}#req\"))" ${juju_db}) + fi - for vip in ${vips} ; do + for vip in ${vips} ; do #include_vip=$(echo $relation_output | grep ${vip}) include_vip=$(echo $relation_output | jq -rc ".settings | [.\"egress-subnets\", .\"private-address\", .\"ingress-address\"]" | grep ${vip}) @@ -52,7 +52,7 @@ do echo fi - done + done done done done diff --git a/fix_cloud_focal.sh b/fix_cloud_focal.sh index 6ecf799..3e2ad05 100755 --- a/fix_cloud_focal.sh +++ b/fix_cloud_focal.sh @@ -10,7 +10,7 @@ get_lead() { app_name=$1 - cat ${juju_status} | jq -rc ".applications.\"${app_name}\".units | to_entries[] | select(.value.leader == "true") | .key" + jq -rc ".applications.\"${app_name}\".units | to_entries[] | select(.value.leader == "true") | .key" "${juju_status}" } do_vault() @@ -21,14 +21,14 @@ do_vault() echo " " - IPS=$(cat ${juju_status} | jq '.applications.vault.units | to_entries[] | .value."public-address"' | sed s/\"//g) + IPS=$(jq -r '.applications.vault.units | to_entries[].value."public-address"' "${juju_status}") for ip in $IPS;do - echo export VAULT_ADDR=http://${ip}:8200; - export VAULT_ADDR=http://${ip}:8200; + echo export VAULT_ADDR=http://"${ip}":8200; + export VAULT_ADDR=http://"${ip}":8200; for vault_key in $(head -n3 vault-secrets.txt | awk '{print $4}');do - echo vault operator unseal -tls-skip-verify $vault_key - vault operator unseal -tls-skip-verify $vault_key + echo vault operator unseal -tls-skip-verify "$vault_key" + vault operator unseal -tls-skip-verify "$vault_key" done; done; @@ -39,36 +39,39 @@ juju-wait -v juju_status=$(mktemp) -juju status --format json > ${juju_status} +juju status --format json > "${juju_status}" -mysql_status=$(cat ${juju_status} | jq -rc ".applications.\"mysql-innodb-cluster\".units | to_entries[] | {sub:.key,status:.value[\"workload-status\"].message}") - -mysql_units=$(echo ${mysql_status} | jq .sub | sed s/\"//g) bootstrap_unit="" -mysql_lead=$(get_lead mysql-innodb-cluster) +mysql_status=$(jq -rc ".applications.\"mysql-innodb-cluster\".units | to_entries[] | {sub:.key,status:.value[\"workload-status\"].message}" "${juju_status}") -reboot_status=$(juju run-action mysql-innodb-cluster/leader reboot-cluster-from-complete-outage --wait --format json) +is_ready=$(echo "$mysql_status" | jq -rc . | grep "Mode: R/W, Cluster is ONLINE" | jq -r .sub) + +if [[ -z "${is_ready}" ]] ; then + reboot_status=$(juju run-action mysql-innodb-cluster/leader reboot-cluster-from-complete-outage --wait --format json) -outcome=$(echo $reboot_status| jq .[].results.outcome) + outcome=$(echo "$reboot_status" | jq .[].results.outcome) -if [[ ${outcome} == null ]] ; then + if [[ ${outcome} == null ]] ; then - output=$(echo $reboot_status| jq .[].results.output) + output=$(echo "$reboot_status" | jq .[].results.output) - mysql_ip=$(echo $output | sed -e 's/\\n/\n/g' 2>&1| grep Please | sed -e "s|.*Please use the most up to date instance: '\(.*\):.*|\1|") + mysql_ip=$(echo "$output" | sed -e 's/\\n/\n/g' 2>&1| grep Please | sed -e "s|.*Please use the most up to date instance: '\(.*\):.*|\1|") - bootstrap_unit=$(cat ${juju_status} | jq ".applications.\"mysql-innodb-cluster\".units | to_entries[] | select(.value.\"public-address\" == \"${mysql_ip}\") | .key" | sed s/\"//g) + bootstrap_unit=$(jq -r ".applications.\"mysql-innodb-cluster\".units | to_entries[] | select(.value.\"public-address\" == \"${mysql_ip}\") | .key" "${juju_status}") - juju run-action ${bootstrap_unit} reboot-cluster-from-complete-outage --wait + juju run-action "${bootstrap_unit}" reboot-cluster-from-complete-outage --wait + + fi + + juju run --application mysql-innodb-cluster "hooks/update-status" + + # Run update-status on all mysql-router units, to ensure that theyave connected to the mysql-innodb-cluster + jq -r ".applications | to_entries[] | select(.value[\"charm-name\"] == \"mysql-router\") | .key" "${juju_status}" \ + | xargs -I{} juju run -a "{}" -- 'hooks/update-status' fi -juju run --application mysql-innodb-cluster "hooks/update-status" - -# Run update-status on all mysql-router units, to ensure that theyave connected to the mysql-innodb-cluster -cat ${juju_status} | jq ".applications | to_entries[] | select(.value[\"charm-name\"] == \"mysql-router\") | .key" | sed s/\"//g | xargs -i juju run -a "{}" -- 'hooks/update-status' - juju run -a heat -- sudo systemctl restart heat-engine & juju run -a vault -- sudo systemctl restart vault & @@ -81,7 +84,8 @@ done wait # cleanup all crm resources -cat ${juju_status} | jq ".applications | to_entries[] | select(.value[\"charm-name\"] == \"hacluster\") | .key" | sed s/\"//g | xargs -i juju run --unit "{}"/leader -- 'sudo crm_resource -l | sed s/:.*//g | uniq | xargs -i sudo crm resource cleanup \"\{\}\"' +jq -r ".applications | to_entries[] | select(.value[\"charm-name\"] == \"hacluster\") | .key" "${juju_status}" \ + | xargs -I{} juju run --unit "{}"/leader -- 'sudo crm_resource -l | sed s/:.*//g | uniq | xargs -i sudo crm resource cleanup \"\{\}\"' do_vault @@ -89,18 +93,19 @@ do_vault echo "Sleeping 10 seconds to wait for vault to finalise unseal" sleep 10 -ceph_osd_apps=$(cat ${juju_status} | jq -rc ".applications | to_entries[] | select(.value[\"charm-name\"] == \"ceph-osd\") | .key") +ceph_osd_apps=$(jq -rc ".applications | to_entries[] | select(.value[\"charm-name\"] == \"ceph-osd\") | .key" "${juju_status}") ceph_osds="" for apps in ${ceph_osd_apps} do - ceph_osds="${ceph_osds} $(cat ${juju_status} | jq -rc ". | .applications[\"${apps}\"].units | to_entries[] | .key")" + ceph_osds="${ceph_osds} $(jq -rc ". | .applications[\"${apps}\"].units | to_entries[] | .key" "${juju_status}")" done +juju run --all -- sudo systemctl restart systemd-resolved for ceph_osd in ${ceph_osds} do - juju ssh ${ceph_osd} -- 'sudo systemctl kill --all --type=service vaultlocker-decrypt@* ; + juju ssh "${ceph_osd}" -- 'sudo systemctl kill --all --type=service vaultlocker-decrypt@* ; sudo systemctl start --all --type=service vaultlocker-decrypt@* ; sudo systemctl start --all --type=service ceph-volume@*' & done @@ -109,7 +114,7 @@ wait juju run -a ceph-osd "hooks/update-status" -lds_servers=$(cat ${juju_status} | jq -rc ". | .applications[\"landscape-server\"].units | to_entries[] | .key") +lds_servers=$(jq -rc ". | .applications[\"landscape-server\"].units | to_entries[] | .key" "${juju_status}") cat > /tmp/restart-landscape.sh << EOF #!/bin/bash @@ -119,11 +124,9 @@ EOF for lds_server in ${lds_servers} do - juju scp /tmp/restart-landscape.sh ${lds_server}:. - juju ssh ${lds_server} chmod +x restart-landscape.sh - juju ssh ${lds_server} sudo ./restart-landscape.sh & + juju scp /tmp/restart-landscape.sh "${lds_server}":. + juju ssh "${lds_server}" chmod +x restart-landscape.sh + juju ssh "${lds_server}" sudo ./restart-landscape.sh & done wait - -juju run --all -- sudo systemctl restart systemd-resolved diff --git a/get_charm_versions.sh b/get_charm_versions.sh index cc1285e..1ad2105 100755 --- a/get_charm_versions.sh +++ b/get_charm_versions.sh @@ -18,9 +18,9 @@ do # # {"charm_name":"openstack-service-checks","charm":"cs:~canonical-bootstack/openstack-service-checks-30","version":30} - app_name=$(echo $app | jq .charm_name | sed s/\"//g) - charm=$(echo $app | jq .charm | sed s/\"//g) - charm_version=$(echo $app | jq .version) + app_name=$(echo $app | jq -r .charm_name) + charm=$(echo $app | jq -r .charm) + charm_version=$(echo $app | jq -r .version) echo -n "${app_name}," if [[ $charm =~ ^cs:~ ]] ; then diff --git a/grab_vips.sh b/grab_vips.sh index 8debb70..5e63f07 100755 --- a/grab_vips.sh +++ b/grab_vips.sh @@ -1,10 +1,10 @@ #!/bin/bash -applications=$(juju status --format json | jq ".applications | to_entries[] | .key" | sed s/\"//g) +applications=$(juju status --format json | jq -r ".applications | to_entries[].key") for app in ${applications} do - vip=$(juju config $app --format json | jq ".settings.vip.value" | sed s/\"//g) + vip=$(juju config $app --format json | jq -r ".settings.vip.value") if [[ $vip != "null" ]] ; then echo "${app}: ${vip}" fi diff --git a/vault.sh b/vault.sh index 85c87c4..ee6896a 100755 --- a/vault.sh +++ b/vault.sh @@ -2,7 +2,7 @@ vault_file="vault-secrets.txt" -IPS=$(juju status vault --format json | jq '.applications.vault.units | to_entries[] | .value."public-address"' | sed s/\"//g) +IPS=$(juju status vault --format json | jq -r '.applications.vault.units | to_entries[].value."public-address"') for ip in $IPS do diff --git a/vault_init.sh b/vault_init.sh index 6f55539..4c355eb 100755 --- a/vault_init.sh +++ b/vault_init.sh @@ -9,7 +9,7 @@ export VAULT_ADDR="http://${vault_vip}:8200" vault operator init -key-shares=5 -key-threshold=3 > ${vault_file} -IPS=$(juju status vault --format json | jq '.applications.vault.units | to_entries[] | .value."public-address"' | sed s/\"//g) +IPS=$(juju status vault --format json | jq -r '.applications.vault.units | to_entries[].value."public-address"') for ip in $IPS;do echo export VAULT_ADDR=http://${ip}:8200;