#!/bin/bash
set -e -x

gateway_address="http://ros.rancher.io/gateway"

sigProcess() {
    echo "SIGINT/SIGTERM signal..."
    kill -9 $$
}

trap sigProcess SIGINT SIGKILL SIGTERM

read_policy() {
    policy=`ros c get rancher.upgrade.policy`
    if [[ -z ${policy} ]]; then
        echo "can not read upgrade.policy"
        return 1
    fi
    return $(($policy))
}

report_activity() {
    arch=`uname -m`
    uuid=`cat /sys/class/dmi/id/product_uuid || true`
    release=`ros -v | awk '{print $2}'`
    response=`wget --server-response ${gateway_address}/report --header 'Accept: application/json' --header 'Content-type: application/json' --header "arch: $arch" --header "uuid: $uuid" --header "release: $release" 2>&1 | awk '/^  HTTP/{print $2}'`
    if [[ -n ${response} && ${response} -ge 200 && ${response} -le 300 ]]; then
        echo "report activity success"
    else
        echo "report activity failed"
        return 1
    fi
}

upgrade_operate() {
    recommend_version=`wget ${gateway_address}/version -q -O -`
    case ${policy} in
        download)
            echo "upgrade.policy is 'download'"
            report_activity
            if [[ ${recommend_version} ]];
            then
                system-docker pull ${recommend_version}
            else
                echo "pull recommend image failed"
                return 1
            fi
        ;;
        auto)
            echo "upgrade.policy is 'auto'"
            report_activity
            if [[ ${recommend_version} ]];
            then
                ros os upgrade -i ${recommend_version} -f
            else
                echo "execute ros os upgrade failed"
                return 1
            fi
        ;;
        none)
            echo "upgrade.policy is 'none'"
        ;;
        *)
            echo "upgrade.policy is 'illegal'"
        ;;
    esac
}

read_policy

n=0
until [[ ${n} -ge 5 ]]
do
    upgrade_operate && break
    n=$[$n+1]
    sleep 5
done