#!/bin/sh
#
# Copyright (c) [2013-2014] Novell, Inc.  All Rights Reserved.
#
# THIS WORK IS SUBJECT TO U.S. AND INTERNATIONAL COPYRIGHT LAWS AND TREATIES.  IT
# MAY NOT BE USED, COPIED, DISTRIBUTED, DISCLOSED, ADAPTED, PERFORMED, DISPLAYED,
# COLLECTED, COMPILED, OR LINKED WITHOUT NOVELL'S PRIOR WRITTEN CONSENT.  USE OR
# EXPLOITATION OF THIS WORK WITHOUT AUTHORIZATION COULD SUBJECT THE PERPETRATOR
# TO CRIMINAL AND CIVIL LIABILITY.
#
# NOVELL PROVIDES THE WORK "AS IS," WITHOUT ANY EXPRESS OR IMPLIED WARRANTY,
# INCLUDING WITHOUT THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
# PARTICULAR PURPOSE, AND NON-INFRINGEMENT. NOVELL, THE AUTHORS OF THE WORK, AND
# THE OWNERS OF COPYRIGHT IN THE WORK ARE NOT LIABLE FOR ANY CLAIM, DAMAGES, OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT, OR OTHERWISE, ARISING
# FROM, OUT OF, OR IN CONNECTION WITH THE WORK OR THE USE OR OTHER DEALINGS IN
# THE WORK.
# This script is executed at the end of appliance creation.  Here you can do
# one-time actions to modify your appliance before it is ever used, like
# removing files and directories to make it smaller, creating symlinks,
# generating indexes, etc.
#
# The 'kiwi_type' variable will contain the format of the appliance (oem =
# disk image, vmx = VMware, iso = CD/DVD, xen = Xen).
#

readonly GREEN='\033[0;32m'
readonly RED='\033[0;31m'
readonly NOCOLOR='\033[0m'
readonly CSP_BIN='/opt/rocketsoftware/csp/bin'
# These values gets set by the Maven assemby step
readonly CSP_CHANNEL_VERSION="2025"
readonly PRODUCT_CHANNEL_NAME="sha"
readonly PRODUCT_CHANNEL_VERSION="25.1"

printOKMessage() {
    printf "[  ${GREEN}OK${NOCOLOR}  ] CSP: $1\n" > /dev/console
}

printErrorMessage() {
    printf "[${RED}ERROR${NOCOLOR} ] CSP: $1\n" > /dev/console
}

displayStatus() {
    #Display info to help us
    cspctl status | tee /dev/console
    echo "Pods Status:" | tee /dev/console
    kubectl get pods -A | tee /dev/console
}

firewall() {
    ## Firewall Configurations
    ${CSP_BIN}/cspctl verify --fixall &> /dev/null
    /opt/novell/base_config/firewall/firewall_config.sh reload
}

symlinks() {
     ## Sym Links
    ln -s ${CSP_BIN}/kubectl /usr/bin/kubectl
}

bashprofile() {
     ## Bash Profile
    ln -s ${CSP_BIN}/env.sh /etc/profile.d/csp-env.sh
}

removeRepos() {
    #Remove all zypper repos
    zypper rr 1
    zypper rr 1
    zypper rr 1
    zypper rr 1

    zypper lr
}

addRepos() {
    if [ "$CSP_CHANNEL_VERSION" = "master" ]; then
        zypper ar http://csp-updates.dev.rocketsoftware.com/staging/csp/master/x86_64/ csp-updates
        zypper ar http://csp-updates.dev.rocketsoftware.com/staging/${PRODUCT_CHANNEL_NAME}/master/x86_64/ ${PRODUCT_CHANNEL_NAME}-updates
    else
        LEAP_VERSION=$(. /etc/os-release; echo $VERSION_ID)
        zypper ar https://csp-updates.rocketsoftware.com/csp-appliance-os/opensuse-leap-${LEAP_VERSION}-x86_64/ os-updates
        zypper ar https://csp-updates.rocketsoftware.com/csp/${CSP_CHANNEL_VERSION}/x86_64/ csp-updates
        zypper ar https://csp-updates.rocketsoftware.com/${PRODUCT_CHANNEL_NAME}/${PRODUCT_CHANNEL_VERSION}/x86_64/ ${PRODUCT_CHANNEL_NAME}-updates
    fi
}

initcsp() {
    echo "" > /dev/console
    cspctl enable > /dev/console
    cspctl verify > /dev/console
    echo "" > /dev/console
    cspctl start > /dev/console

    #in minutes
    sleepcounter=0
    isready=1
    while [ "$isready" -eq 1 ] && [ "$sleepcounter" -lt 8 ] ; do
        kubectl wait --for=condition=Ready nodes --all --timeout=1s &> /dev/null
        isready=$?
        sleepcounter=$(($sleepcounter+1))
        sleep 60
        printOKMessage "Waiting for node to be ready..."
    done

    if [ $isready -eq 1 ]; then
        printErrorMessage "Timed out waiting for node to be ready - $(date +%H:%M:%S)"
        displayStatus
        exit 1
    else
        printOKMessage "Node is ready - $(date +%H:%M:%S)"
    fi
}

clearConsole() {
    printf "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" > /dev/console
    printOKMessage "Initialization may take about 5 minutes - $(date +%H:%M:%S)"
}

registerGPGKeys() {
    rpmkeys --import /opt/novell/va_install/Rocket.repo-sign.pub
    rpmkeys --import /opt/novell/va_install/Rocket.package-sign.pub
    rpmkeys --import /opt/novell/va_install/rancher.pub
}

main() {
    clearConsole
    firewall
    bashprofile
    symlinks
    registerGPGKeys
    removeRepos
    addRepos

    initcsp
}

main "$@"