#!/bin/bash
# Copyright (C) 2017
#
# This file is part of cloud. It is used to assist with the following features:
# 1. config_set_passwords
# 2. config_ssh_pub_key
# 3. config_network

RETVAL=0

cdrom_label="config-2"
cloud_init="/usr/bin/cloud-init"
last_action_dir="/usr/local/qcloud/action/"

log_output() {
    echo "[`date "+%Y-%m-%d %H:%M:%S"`]:${1}"
}

_exit() {
    umount $mount_dir
    rm -rf $mount_dir
    rm -rf /run/cloud-init/.instance-id
    exit $RETVAL
}

config_set_passwords() {
    log_output "config_set_passwords, begin"
    rm -rf /var/lib/cloud/instance/sem/config_set_passwords
    $cloud_init single --n set-passwords
    RETVAL=$?
    log_output "config_set_passwords return value ${RETVAL}, end"
    return $RETVAL
}

config_set_hostname() {
    log_output "config_set_hostname, begin"
    rm -rf /var/lib/cloud/instance/sem/config_set_hostname
    $cloud_init single --n set_hostname
    hostnamectl --transient set-hostname $hostname &
    hostnamectl --static set-hostname $hostname &
    hostnamectl --pretty set-hostname $hostname &
    RETVAL=$?
    log_output "config_set_hostname return value ${RETVAL}, end"
    return $RETVAL
}

config_set_hosts() {
    log_output "config_set_hosts, begin"
    hosts="127.0.0.1 localhost localhost.localdomain $hostname"
    sed -i "/^127.0.0.1/d" /etc/hosts
    RETVAL=$?
    echo $hosts >> /etc/hosts
    log_output "config_set_hosts return value ${RETVAL}, end"
    return $RETVAL
}

config_network_dhcp() {
    log_output "config_network(dhcp), begin"
    if [ "$reserve_hosts" == "false" ]; then
        config_set_hosts
        config_set_hostname
    fi
    ifdown $network_device
    ifup $network_device
}

config_network_static() {
    log_output "config_network(static), begin"
    ifconfig $network_device $ip_addr netmask $netmask
    ifdown $network_device
    if [ "$reserve_hosts" == "false" ]; then
        config_set_hosts
        config_set_hostname
    fi

    rm -f /var/lib/cloud/instance/sem/config_resolv_conf
    $cloud_init single -n resolv_conf
    RETVAL=$?
    log_output "config_network_static resolv_conf return value ${RETVAL}, end"

    rm -f /var/lib/cloud/instance/sem/config_write_files
    $cloud_init single -n write-files
    RETVAL=$?
    ifup $network_device
    log_output "config_network_static write-files return value ${RETVAL}, end"
}

install_agent() {
    log_output "install_agent, begin"
    cp -r "$mount_dir/qcloud_init/" /
    agent_file="/qcloud_init/install_${agent_name}.sh"
    if [ ! -f "$agent_file" ]
    then
        log_output "$agent_file not exits."
    else
        bash "$agent_file"
    fi

    RETVAL=$?
    rm -rf /qcloud_init/
    log_output "install_agent return value ${RETVAL}, end"
}

config_network() {
    rm -f /var/lib/cloud/data/instance-id
    $cloud_init init --local
    RETVAL=$?

    if [ "$net_boot_type" == "static" ]; then
        config_network_static
    else
        config_network_dhcp
    fi

    log_output "config_network return value ${RETVAL}, end"
}

config_users_groups() {
    log_output "config_users_groups, begin"
    rm -rf /var/lib/cloud/instance/sem/config_users_groups
    $cloud_init single --n users-groups
    RETVAL=$?
    log_output "config_users_groups return value ${RETVAL}, end"
    return $RETVAL
}

config_ssh_pub_key() {
    log_output "config_ssh_pub_key, begin"
    if [ "$username" == "root" ]; then
        ssh_key_file='/root/.ssh/authorized_keys'
    else
        ssh_key_file=/home/${username}/.ssh/authorized_keys
    fi
    rm -f $ssh_key_file
    config_users_groups
    config_set_passwords
    RETVAL=$?
    log_output "config_ssh_pub_key return value ${RETVAL}, end"
    return $RETVAL
}

load_action_conf() {
    dev=$(blkid -tLABEL=$cdrom_label -odevice)
    mount_dir=$(mktemp -d /tmp/tmp.XXXXXX)
    action_file="${mount_dir}""${qcloud_action_file}"
    mount $dev $mount_dir
    if [ -f $action_file ]; then
        . $action_file
        RETVAL=0
    else
        RETVAL=1
    fi
    umount $mount_dir
    rm -rf $mount_dir
    return $RETVAL
}


rm -rf /run/cloud-init/.instance-id
dev=$(blkid -tLABEL=$cdrom_label -odevice)
if [ -z '$dev' ]; then
    dev='/dev/sr0'
fi
mount_dir=$(mktemp -d /tmp/tmp.XXXXXX)
mount $dev $mount_dir

qcloud_action_dir=${mount_dir}/qcloud_action
for action_conf in ${qcloud_action_dir}/*.conf
do
    if [ `basename $action_conf` == "os.conf" ]; then
        continue
    fi
    . $action_conf
    if [ $? -ne 0 ] ; then
        log_output "load ${action_conf} failed."
        continue
    fi
    last_action_file=${last_action_dir}/$action
    last_action_timestamp=`cat $last_action_file`
    current_action_timestamp=$timestamp
    if [ "$last_action_timestamp" == "$current_action_timestamp" ];then
        continue
    fi

    case "$action" in
        config_set_passwords)
            config_set_passwords
            RETVAL=$?
            ;;
        config_ssh_pub_key)
            config_ssh_pub_key
            RETVAL=$?
            ;;
        config_network)
            config_network
            RETVAL=$?
            ;;
        install_agent)
            install_agent
            RETVAL=$?
            ;;
        config_set_hostname)
            config_set_hostname
            RETVAL=$?
            ;;
        *)
            log_output "${action} not found."
            RETVAL=3
            ;;
    esac

    if [ $RETVAL -eq 0 ] ; then
        mkdir -p $last_action_dir
        echo $current_action_timestamp > $last_action_file
    fi
done
_exit
