summaryrefslogtreecommitdiffstats
path: root/server/ipxe/bash_scripts/addServer.sh
diff options
context:
space:
mode:
Diffstat (limited to 'server/ipxe/bash_scripts/addServer.sh')
-rwxr-xr-xserver/ipxe/bash_scripts/addServer.sh158
1 files changed, 146 insertions, 12 deletions
diff --git a/server/ipxe/bash_scripts/addServer.sh b/server/ipxe/bash_scripts/addServer.sh
index ad6e3aa..69088bf 100755
--- a/server/ipxe/bash_scripts/addServer.sh
+++ b/server/ipxe/bash_scripts/addServer.sh
@@ -1,21 +1,90 @@
#!/bin/bash
-json_data()
-{
- cat << EOF
+
+
+# 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": [709403],
+ "parents": [$rack],
"type": "SERVER",
+ "name": "$hostname",
"uuid": "$(dmidecode -q -s system-uuid | grep -v '^#' | head -n 1 | tr '[a-z]' '[A-Z]')",
- "network": {
- "mac": "$(ip addr show | grep -Eo -m 1 'ether\s.*\sbrd' | awk '{print $2}')",
- "ip": "$(hostname -I | awk '{print $1}')"
- },
+ "networks": [
+$(parse_mgmt "$hostname"),
+$(parse_ips "$hostname" "$interface")
+ ],
"location": {
"option": "Horizontal",
- "insertion": "Back",
- "pos": 46
+ "insertion": "Front and backside",
+ "slot": $pos,
+ "bay": 1
},
"formfactor": {
"formfactor": "19\"",
@@ -27,5 +96,70 @@ json_data()
EOF
}
-curl -d "$(json_data)" -H "Content-Type: application/json" -X POST --insecure https://bas.intra.uni-freiburg.de/api/registration
-# curl --data "$(json_data)" -H "Content-Type: application/json" -X POST --insecure https://bas.stfu-kthx.net:8888/api/registration \ No newline at end of file
+
+
+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