Browse Source

(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
Scott Hussey 2 months ago
parent
commit
8d56122554

+ 45
- 3
charts/maas/templates/bin/_register-rack-controller.sh.tpl View File

@@ -6,11 +6,48 @@ set -x
6 6
 # to source the maas system_id
7 7
 HOST_MOUNT_PATH=${HOST_MOUNT_PATH:-"/host_cloud-init/"}
8 8
 
9
+get_impacted_nets() {
10
+  system_id="$1"
11
+  maas local fabrics read | jq -cr 'map(.vlans) | map(.[]) | map(select(.primary_rack == "'"$system_id"'" or .secondary_rack == "'"$system_id"'")) | .[] | {vid, fabric_id}'
12
+}
13
+
14
+detach_rack_controller() {
15
+  system_id="$1"
16
+  for net in $(get_impacted_nets "$system_id");
17
+  do
18
+    vid=$(echo "$net" | jq -r .vid)
19
+    fid=$(echo "$net" | jq -r .fabric_id)
20
+    maas local vlan update "$fid" "$vid" primary_rack='' secondary_rack=''
21
+  done
22
+}
23
+
9 24
 unregister_maas_rack() {
10 25
   sys_id="$1"
11 26
   echo "Deregister this pod as MAAS rack controller ${sys_id}."
27
+
12 28
   maas login local "$MAAS_ENDPOINT" "$MAAS_API_KEY"
13
-  maas local rack-controller delete "$sys_id"
29
+
30
+  if [[ $? -ne 0 ]];
31
+  then
32
+    echo "Could not login to MAAS API."
33
+    exit $?
34
+  fi
35
+
36
+  detach_rack_controller "$sys_id"
37
+
38
+  while [ 1 ];
39
+  do
40
+    maas local rack-controller delete "$sys_id"
41
+
42
+    if [[ $? -ne 0 ]];
43
+    then
44
+      echo "Could not delete rack controller."
45
+      sleep 10
46
+    else
47
+      break
48
+    fi
49
+  done
50
+
14 51
   rm -f ~maas/maas_id
15 52
   rm -f ~maas/secret
16 53
 }
@@ -28,7 +65,7 @@ register_maas_rack() {
28 65
   # register forever
29 66
   while [ 1 ];
30 67
   do
31
-    if maas-rack register --url=${MAAS_ENDPOINT} --secret="${MAAS_REGION_SECRET}";
68
+    if maas-rack register --url="${MAAS_ENDPOINT}" --secret="${MAAS_REGION_SECRET}";
32 69
     then
33 70
         echo "Successfully registered with MaaS Region Controller"
34 71
         break
@@ -70,7 +107,12 @@ then
70 107
   if [[ "$HOST_SYSTEM_ID" != "$POD_SYSTEM_ID" ]]
71 108
   then
72 109
     unregister_maas_rack "$POD_SYSTEM_ID"
73
-    register_maas_rack "$HOST_SYTEM_ID"
110
+    if $?;
111
+    then
112
+      echo "Unregister of $POD_SYSTEM_ID failed, exitting."
113
+      exit 1
114
+    fi
115
+    register_maas_rack "$HOST_SYSTEM_ID"
74 116
   else
75 117
     echo "Found existing maas_id, assuming already registered."
76 118
   fi

+ 2
- 1
images/maas-rack-controller/Dockerfile View File

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

+ 1
- 1
images/maas-rack-controller/scripts/register-rack-controller.service View File

@@ -5,7 +5,7 @@ After=network-online.target
5 5
 
6 6
 [Service]
7 7
 Type=oneshot
8
-PassEnvironment=MAAS_ENDPOINT MAAS_REGION_SECRET
8
+PassEnvironment=MAAS_ENDPOINT MAAS_REGION_SECRET MAAS_API_KEY HOST_MOUNT_PATH
9 9
 ExecStart=/usr/local/bin/register-rack-controller.sh
10 10
 
11 11
 [Install]

Loading…
Cancel
Save