(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
This commit is contained in:
Scott Hussey 2019-01-24 17:16:08 -06:00
parent 7f50e96ff3
commit 8d56122554
3 changed files with 48 additions and 5 deletions

View File

@ -6,11 +6,48 @@ set -x
# to source the maas system_id # to source the maas system_id
HOST_MOUNT_PATH=${HOST_MOUNT_PATH:-"/host_cloud-init/"} 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() { unregister_maas_rack() {
sys_id="$1" sys_id="$1"
echo "Deregister this pod as MAAS rack controller ${sys_id}." echo "Deregister this pod as MAAS rack controller ${sys_id}."
maas login local "$MAAS_ENDPOINT" "$MAAS_API_KEY" maas login local "$MAAS_ENDPOINT" "$MAAS_API_KEY"
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" 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/maas_id
rm -f ~maas/secret rm -f ~maas/secret
} }
@ -28,7 +65,7 @@ register_maas_rack() {
# register forever # register forever
while [ 1 ]; while [ 1 ];
do do
if maas-rack register --url=${MAAS_ENDPOINT} --secret="${MAAS_REGION_SECRET}"; if maas-rack register --url="${MAAS_ENDPOINT}" --secret="${MAAS_REGION_SECRET}";
then then
echo "Successfully registered with MaaS Region Controller" echo "Successfully registered with MaaS Region Controller"
break break
@ -70,7 +107,12 @@ then
if [[ "$HOST_SYSTEM_ID" != "$POD_SYSTEM_ID" ]] if [[ "$HOST_SYSTEM_ID" != "$POD_SYSTEM_ID" ]]
then then
unregister_maas_rack "$POD_SYSTEM_ID" 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 else
echo "Found existing maas_id, assuming already registered." echo "Found existing maas_id, assuming already registered."
fi fi

View File

@ -19,7 +19,8 @@ RUN apt-get -qq update && \
software-properties-common \ software-properties-common \
libvirt-bin \ libvirt-bin \
systemd \ systemd \
patch patch \
jq
# Don't start any optional services except for the few we need. # Don't start any optional services except for the few we need.
RUN find /etc/systemd/system \ RUN find /etc/systemd/system \

View File

@ -5,7 +5,7 @@ After=network-online.target
[Service] [Service]
Type=oneshot 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 ExecStart=/usr/local/bin/register-rack-controller.sh
[Install] [Install]