From 8d56122554cda0ab57a39907509c60de260e1079 Mon Sep 17 00:00:00 2001 From: Scott Hussey Date: Thu, 24 Jan 2019 17:16:08 -0600 Subject: [PATCH] (fix) Give systemd service access to env - The service to register the rack controller pod does not have access to the MAAS_API_KEY env var so it fails to deregister when needed. Change-Id: I16bc63ef14a2dab463dfdca11b7e3ca13d508a9e --- .../bin/_register-rack-controller.sh.tpl | 48 +++++++++++++++++-- images/maas-rack-controller/Dockerfile | 3 +- .../scripts/register-rack-controller.service | 2 +- 3 files changed, 48 insertions(+), 5 deletions(-) diff --git a/charts/maas/templates/bin/_register-rack-controller.sh.tpl b/charts/maas/templates/bin/_register-rack-controller.sh.tpl index 3280643..37e4bb0 100644 --- a/charts/maas/templates/bin/_register-rack-controller.sh.tpl +++ b/charts/maas/templates/bin/_register-rack-controller.sh.tpl @@ -6,11 +6,48 @@ set -x # to source the maas system_id HOST_MOUNT_PATH=${HOST_MOUNT_PATH:-"/host_cloud-init/"} +get_impacted_nets() { + system_id="$1" + maas local fabrics read | jq -cr 'map(.vlans) | map(.[]) | map(select(.primary_rack == "'"$system_id"'" or .secondary_rack == "'"$system_id"'")) | .[] | {vid, fabric_id}' +} + +detach_rack_controller() { + system_id="$1" + for net in $(get_impacted_nets "$system_id"); + do + vid=$(echo "$net" | jq -r .vid) + fid=$(echo "$net" | jq -r .fabric_id) + maas local vlan update "$fid" "$vid" primary_rack='' secondary_rack='' + done +} + unregister_maas_rack() { sys_id="$1" echo "Deregister this pod as MAAS rack controller ${sys_id}." + maas login local "$MAAS_ENDPOINT" "$MAAS_API_KEY" - maas local rack-controller delete "$sys_id" + + if [[ $? -ne 0 ]]; + then + echo "Could not login to MAAS API." + exit $? + fi + + detach_rack_controller "$sys_id" + + while [ 1 ]; + do + maas local rack-controller delete "$sys_id" + + if [[ $? -ne 0 ]]; + then + echo "Could not delete rack controller." + sleep 10 + else + break + fi + done + rm -f ~maas/maas_id rm -f ~maas/secret } @@ -28,7 +65,7 @@ register_maas_rack() { # register forever while [ 1 ]; do - if maas-rack register --url=${MAAS_ENDPOINT} --secret="${MAAS_REGION_SECRET}"; + if maas-rack register --url="${MAAS_ENDPOINT}" --secret="${MAAS_REGION_SECRET}"; then echo "Successfully registered with MaaS Region Controller" break @@ -70,7 +107,12 @@ then if [[ "$HOST_SYSTEM_ID" != "$POD_SYSTEM_ID" ]] then unregister_maas_rack "$POD_SYSTEM_ID" - register_maas_rack "$HOST_SYTEM_ID" + if $?; + then + echo "Unregister of $POD_SYSTEM_ID failed, exitting." + exit 1 + fi + register_maas_rack "$HOST_SYSTEM_ID" else echo "Found existing maas_id, assuming already registered." fi diff --git a/images/maas-rack-controller/Dockerfile b/images/maas-rack-controller/Dockerfile index 9420ea3..e9b1d8f 100644 --- a/images/maas-rack-controller/Dockerfile +++ b/images/maas-rack-controller/Dockerfile @@ -19,7 +19,8 @@ RUN apt-get -qq update && \ software-properties-common \ libvirt-bin \ systemd \ - patch + patch \ + jq # Don't start any optional services except for the few we need. RUN find /etc/systemd/system \ diff --git a/images/maas-rack-controller/scripts/register-rack-controller.service b/images/maas-rack-controller/scripts/register-rack-controller.service index 0bf64b4..fb439d3 100644 --- a/images/maas-rack-controller/scripts/register-rack-controller.service +++ b/images/maas-rack-controller/scripts/register-rack-controller.service @@ -5,7 +5,7 @@ After=network-online.target [Service] Type=oneshot -PassEnvironment=MAAS_ENDPOINT MAAS_REGION_SECRET +PassEnvironment=MAAS_ENDPOINT MAAS_REGION_SECRET MAAS_API_KEY HOST_MOUNT_PATH ExecStart=/usr/local/bin/register-rack-controller.sh [Install]