summaryrefslogblamecommitdiffstats
path: root/server/ipxe/bash_scripts/addServer.sh
blob: 9ef8aa92a7213ddd1d3915a192067dfdbaffaacb (plain) (tree)
1
2

           





































































                                                                              

               
                           
                         
                            
                                                                                             



                                     

                                   


                                              










                                 


































































                                                                                                                                        
#!/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