~viswesn/juju-ci-tools/aws_boto3

« back to all changes in this revision

Viewing changes to maas/reset-fabrics.bash

  • Committer: Curtis Hovey
  • Date: 2017-01-25 02:32:29 UTC
  • mfrom: (1855 trunk)
  • mto: This revision was merged to the branch mainline in revision 1865.
  • Revision ID: curtis@canonical.com-20170125023229-g7c6bzt0cqe1j8g3
Merged trunk and resolved conflicts.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/bin/bash
 
2
# Reset maas fabrics and vlans to 0, the defaults.
 
3
set -eux
 
4
 
 
5
 
 
6
ENV=$1
 
7
 
 
8
# Learn all the system_id and hostnames to lookup later.
 
9
ALL_SYSTEM_HOSTS=$(
 
10
    maas $ENV machines read |
 
11
    grep -P 'system_id|hostname' |
 
12
    sed -r ' N;s/\n/ /; s/( +"system_id": .[^,]+,)(.*)/\2 \1/; s, +"(system_id|hostname)": ,,g; s/"//g; s/,/@/; s/,//')
 
13
# Find the unwanted fabric, which is most likely the one with a number
 
14
# greater than 9.
 
15
FABRIC=$(
 
16
    maas $ENV fabrics read |
 
17
    sed -r '/\/fabrics\/[0-9][0-9]+/!d; s,.*/fabrics/([0-9][0-9]+)/.*,\1,')
 
18
if [[ $FABRIC == "" ]]; then
 
19
    exit 0
 
20
fi
 
21
# Find the vlan id of the default fabric.
 
22
VLAN=$(
 
23
    maas $ENV fabric read 0 |
 
24
    sed -r '/vlans/,/}/!d; /"id"/!d; s,[^0-9],,g' |
 
25
    head -1)
 
26
# Learn the misconfigured interfaces by attempting to delete
 
27
# the unwanted fabric.
 
28
INTERFACES=$(
 
29
    maas $ENV fabric delete $FABRIC 2>&1 |
 
30
    sed -r 's,(^.*:|\([^\)]*\)) ,,g; s, on ,@,g; s/,//g' ||
 
31
    true)
 
32
# Reset the fabric and vlan of each machine on the unwanted fabric.
 
33
for iface_machine in $INTERFACES; do
 
34
    iface=$(echo $iface_machine | cut -d @ -f1)
 
35
    machine=$(echo $iface_machine | cut -d @ -f2)
 
36
    system_id=$(
 
37
        echo "$ALL_SYSTEM_HOSTS" |
 
38
        grep $machine@ | cut -d @ -f2)
 
39
    maas $ENV machine release $system_id
 
40
    sleep 5
 
41
    maas $ENV interface update $system_id $iface fabric=0 vlan=$VLAN
 
42
done
 
43
# Delete the unwanted fabric.
 
44
maas $ENV fabric delete $FABRIC