summaryrefslogtreecommitdiffstats
path: root/server/ipxe/bash_scripts/addServer.sh
blob: 9ef8aa92a7213ddd1d3915a192067dfdbaffaacb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/bin/bash



# We mostly do not care about the return value in function local variable
# assignments. So we can write the declaration and assignment in one line.



die() {
    echo -e "$*" 1>&2
    exit 1
}



usage() {
    local file="$(basename "$0")"

    die "$file {-r|--rack}=<rack>" \
              "{-s|--slot}=<slot>" \
             "[{-b|--bay}=<bay>]" \
             "[{-i|--interface=<vlan_interface>}]" \
             "[--rack-height=<height_in_u>]"
}



parse_mgmt() {
    local host="$1"; shift

    local dev="$(ipmitool lan print 1)"
    cat <<END
            {
                "ip":  "$(awk '/IP Address\s+:/ {print $4}' <<<"$dev")",
                "mac": "$(awk '/MAC Address\s+:/ {print $4}' <<<"$dev")",
                "hostname": "$host.rz.privat"
            }
END
}



parse_ips() {
    local host="$1"; shift
    local if="$1"; shift

    # Requires `moreutils` to be installed, but easier to parse and less error
    # prone.
    local ip_frag="$(ifdata -pa "$if" | cut -d. -f3-)"
    local mac="$(ifdata -ph "$if")"
    cat <<END
            {
                "ip": "10.19.$ip_frag",
                "mac": "$mac",
                "hostname": "$host.bwcloud.privat"
            },
            {
                "ip": "10.16.$ip_frag",
                "mac": "$mac",
                "hostname": "$host.nemo.privat"
            }
END
}



json_data() {
    local pos=$(( rack_height - slot + 1 ))
    local hostname="$(hostname -s)"

    cat << EOF
{
    "client": {
        "parents": [$rack],
        "type": "SERVER",
        "name": "$hostname",
        "uuid": "$(dmidecode -q -s system-uuid | grep -v '^#' | head -n 1 | tr 'a-z' 'A-Z')",
        "networks": [
$(parse_mgmt "$hostname"),
$(parse_ips "$hostname" "$interface")
        ],
        "location": {
            "option": "Horizontal",
            "insertion": "Front and backside",
            "slot": $pos,
            "bay": 1
        },
        "formfactor": {
            "formfactor": "19\"",
            "rackunits": 1
        }
    },
    "ipxe": false
}
EOF
}



args=$(
    getopt -o b:i:r:s:h \
           -l bay:,interface:,rack:,rack-height:,slot:,help \
           -n "$0" \
           -- \
           "$@"
) || usage
eval set -- "$args"



bay=0
interface=boot0.259
slot=
rack=
rack_height=47
while true; do
    case "$1" in
        -b|--bay)
            bay="$2"
            echo "Note: This is currently a no-op."
            echo "Bays need to be implemented first!"
            shift 2
            ;;
        -d|--interface)
            interface="$2"
            shift 2
            ;;
        -r|--rack)
            rack="$2"
            shift 2
            ;;
        --rack-height)
            rack_height="$2";
            shift 2
            ;;
        -s|--slot)
            slot="$2"
            shift 2
            ;;
        --)
            shift
            break
            ;;
        -h|--help|*)
            usage
            ;;
    esac
done
[[ -z $slot || -z $rack ]] && usage



case "$rack" in
    36|n36|r3s6)    rack=19034                  ;;
    47|n47|r4s7)    rack=19045                  ;;
    *)              die "Error: unknown rack!"  ;;
esac

(( slot > 47 || slot < 1 )) && die "Error: Slot not in valid range!"



# json_data
curl -d "$(json_data)" -H "Content-Type: application/json" -X POST --insecure https://bas.intra.uni-freiburg.de/api/registration/clients