From dca44b77a278e1ece034afe29d649b86290491ba Mon Sep 17 00:00:00 2001 From: Dmitry Tantsur Date: Thu, 12 Feb 2015 18:03:09 +0100 Subject: [PATCH] Simplify ironic-discoverd-ramdisk As we started using jq it makes sento to use it everywhere when building a resulting JSON. Also removes some unneeded code. Change-Id: Ib1391dc9f4e1463a9a3d0c13909ff60e3c993e82 --- .../init.d/80-ironic-discoverd-ramdisk | 77 ++++++++++--------- 1 file changed, 42 insertions(+), 35 deletions(-) diff --git a/elements/ironic-discoverd-ramdisk/init.d/80-ironic-discoverd-ramdisk b/elements/ironic-discoverd-ramdisk/init.d/80-ironic-discoverd-ramdisk index 692e49cd..30e607fa 100644 --- a/elements/ironic-discoverd-ramdisk/init.d/80-ironic-discoverd-ramdisk +++ b/elements/ironic-discoverd-ramdisk/init.d/80-ironic-discoverd-ramdisk @@ -1,5 +1,4 @@ DISCOVERD_URL=$(get_kernel_parameter discoverd_callback_url) -BOOTIF=$(get_kernel_parameter BOOTIF) if [ -z "$DISCOVERD_URL" ]; then # Some old ramdisks are around @@ -13,60 +12,68 @@ if [ -z "$DISCOVERD_URL" ]; then fi fi -function request_curl(){ - HTTP_METHOD=$1 - URL=$2 - DATA=$3 +echo '{"interfaces":{}}' > data.json - if [ ! -z "$DATA" ]; then - DATA="\"-d \"$DATA\"\"" - fi - - RESULT=$(eval curl -i -X "$HTTP_METHOD" \ - "-H 'Accept: application/json'" \ - "-H 'Content-Type: application/json'" \ - "$DATA" \ - "$URL") || troubleshoot - # CURL can't return error code on 4xx error - if echo $RESULT | grep "HTTP/1.0 4"; then - echo "Ironic API returned error: $RESULT" - troubleshoot - fi - echo $RESULT +function update() { + jq "$1" data.json > temp.json || troubleshoot + mv temp.json data.json } -IFACES= for iface in $(ls /sys/class/net/ | grep -v lo) do MAC=$(ip link show $iface | awk '/ether/ {print $2}') IP=$(ip addr show $iface | awk '/inet / { sub(/\/.*/, "", $2); print $2 }') if [ ! -z "$MAC" ]; then - IFACES="$IFACES,\"$iface\":{\"mac\":\"$MAC\",\"ip\":\"$IP\"}" + update ".interfaces[\"$iface\"] = {mac: \"$MAC\", ip: \"$IP\"}" fi done -IFACES="{$(echo $IFACES | sed s/,//)}" # NOTE(dtantsur): workaround for IPMI device not present on some systems -modprobe ipmi_msghandler || echo "WARNING: modprobe failed, ipmitool call may fail" -modprobe ipmi_devintf || echo "WARNING: modprobe failed, ipmitool call may fail" -modprobe ipmi_si || echo "WARNING: modprobe failed, ipmitool call may fail" +function modprobe_ipmi() { + modprobe ipmi_$1 || echo "WARNING: modprobe ipmi_$1 failed, ipmitool call may fail later" +} + +modprobe_ipmi msghandler +modprobe_ipmi devintf +modprobe_ipmi si BMC_ADDRESS=$(ipmitool lan print | grep -e "IP Address [^S]" | awk '{ print $4 }') +update ".ipmi_address = \"$BMC_ADDRESS\"" CPU_ARCH=$(lscpu | grep Architecture | awk '{ print $2 }') +update ".cpu_arch = \"$CPU_ARCH\"" + # NOTE(lucasagomes): dmidecode because we want to know the total # memory in the system, even the reserved part to the BIOS RAM=$(dmidecode -t 16 | grep 'Maximum Capacity' | awk '{if ($4 == "GB") s+=$3*1024; else s+=$3;} END {print s}') -CPUS=$(cat /proc/cpuinfo | grep processor | wc -l) -disk_bytes=$(fdisk -l | grep Disk | awk '{print $5}' | head -n 1) -# NOTE(dtantsur): -1 is required to give Ironic some spacing for partitioning and may be removed later -DISK_SIZE=$(($disk_bytes/1024/1024/1024 - 1)) +update ".memory_mb = $RAM" -NODE_DATA="'{\"ipmi_address\":\"$BMC_ADDRESS\",\"local_gb\":$DISK_SIZE,\"memory_mb\":$RAM,\"cpus\":$CPUS,\"cpu_arch\":\"$CPU_ARCH\"" -NODE_DATA="$NODE_DATA,\"interfaces\":$IFACES,\"boot_interface\":\"$BOOTIF\"}'" -echo Collected $NODE_DATA -NODE_RESP=$(request_curl POST $DISCOVERD_URL $NODE_DATA | tail -n1) -JSON_RESP=$(echo "$NODE_RESP" | tr '\r' '\n' | tail -n1) # drop HTTP headers +CPUS=$(cat /proc/cpuinfo | grep processor | wc -l) +update ".cpus = $CPUS" + +DISK_BYTES=$(fdisk -l | grep Disk | awk '{print $5}' | head -n 1) +# NOTE(dtantsur): -1 is required to give Ironic some spacing for partitioning and may be removed later +DISK_SIZE=$(($DISK_BYTES/1024/1024/1024 - 1)) +update ".local_gb = $DISK_SIZE" + +BOOTIF=$(get_kernel_parameter BOOTIF) +update ".boot_interface = \"$BOOTIF\"" + +echo Collected: +cat data.json + +RESULT=$(eval curl -i -X POST \ + "-H 'Accept: application/json'" \ + "-H 'Content-Type: application/json'" \ + "-d @data.json" \ + "$DISCOVERD_URL") || troubleshoot +# CURL can't return error code on 4xx error +if echo $RESULT | grep "HTTP/1.0 4"; then + echo "Ironic API returned error: $RESULT" + troubleshoot +fi + +JSON_RESP=$(echo $RESULT | tr '\r' '\n' | tail -n1) # drop HTTP headers if echo "$JSON_RESP" | jq '.ipmi_setup_credentials' | grep -q true; then USERNAME=$(echo "$JSON_RESP" | jq -r '.ipmi_username')