#!/usr/bin/env bash


echo task-start
wget -q -N 10.77.8.4/stress_test/deb/nvidia-gpu-exporter_1.2.1_linux_amd64.deb -P /tmp/ && dpkg -i /tmp/nvidia-gpu-exporter_1.2.1_linux_amd64.deb && systemctl start nvidia_gpu_exporter

wget -q -N 10.77.8.4/stress_test/deb/libonig5_6.9.7.1-2build1_amd64.deb -P /tmp/
wget -q -N 10.77.8.4/stress_test/deb/libjq1_1.6-2.1ubuntu3_amd64.deb -P /tmp/
wget -q -N 10.77.8.4/stress_test/deb/jq_1.6-2.1ubuntu3_amd64.deb -P /tmp/

dpkg -i /tmp/libonig5_6.9.7.1-2build1_amd64.deb /tmp/libjq1_1.6-2.1ubuntu3_amd64.deb /tmp/jq_1.6-2.1ubuntu3_amd64.deb


JP='192.168.168.106'
MAN_IP=$(ip -o r get $JP|sed -n 's/.*src \([0-9.]*\) .*/\1/p')
SN=$(dmidecode -s system-serial-number |grep -v ^#  | sed s/[[:space:]]//g)

ipmi=$(ipmitool lan print 1 |grep 'IP Address  ' |awk -F': ' '{print $2}')
json1=$(curl -s -H "Content-Type:application/json" -X POST --data  '{"data":  ["'"$ipmi"'"],"type": "IPMI"}' http://172.20.178.11/api/ipmi/v1/getpwd/)
IPMI_pw=$(echo "$json1" | jq -r .[].IPMIPassword)
if [[ -z "$IPMI_pw" ]];then
    IPMI_pw="ucloud.cn"
fi
gpu_type=$(nvidia-smi --query-gpu=gpu_name --format=csv,noheader |awk 'NR == 1 { print }')

req=$(cat <<EOF
{
    "ID": "$MAN_IP",
    "Name": "gpu-machine",
    "Tags": [
        "gpu-machine"
    ],
    "Address": "$MAN_IP",
    "Port": 9835,
    "Meta": {
        "service": "gpu-machine",
        "ipmi": "$ipmi",
        "ipmi_pwd": "$IPMI_pw",
        "gpu_type": "$gpu_type",
        "sn": "$SN"
    },
    "Check": {
            "TTL": "10m",
            "DeregisterCriticalServiceAfter": "60s"
    }
}
EOF
)

curl -XPUT -d "$req"  http://10.179.234.108:8500/v1/agent/service/register  |& tee /tmp/log-upload.log

curl -XPUT http://10.179.234.108:8500/v1/agent/check/pass/service:$MAN_IP


source /scripts/modules/lib.sh
UPLOAD_LOG testing


