#!/bin/bash
# SPDX-License-Identifier: MulanPSL-2.0+
# Copyright (c) 2020 Huawei Technologies Co., Ltd. All rights reserved.
# For your reference only.
# It's better to run the below scripts step by step.

value="$1"
export ENABLE_OFFLINE_DEPLOY=$value
export deploy_k8s=true

[[ $CCI_SRC ]] || export CCI_SRC=/c/cbs
cd $CCI_SRC/sparrow || exit

source $CCI_SRC/container/defconfig.sh
source $CCI_SRC/lib/pub-shell-function.sh

kubectl_create()
{
	for i in {1..30}
	do
		nodename=$(kubectl get no | grep master|awk '{print $1}')
		[ -n "$nodename" ] && break
		sleep 60
	done

	[ -z "$nodename" ] && {
		echo "can't get nodename."
		exit 1
	}

	sed -i "s#NODENAME#${nodename}#g" $CCI_SRC/container/storageclass.yaml
	sed -i "s#NODENAME#${nodename}#g" $CCI_SRC/container/*/k8s/*deployment.yaml

	kubectl create ns ems1
	kubectl create -f $CCI_SRC/container/ --validate=false -n ems1
}

kubectl_label()
{
	for node in $(cat /etc/compass-ci/remote-hosts|grep -v "^#"|awk '{print $1}') 
	do
		kubectl label no $node type=testbox
	done
}

install_master()
{
	generate_secret
	0-package/install
	1-storage/tiny
	1-storage/permission
	2-network/iptables
	3-code/git
	3-code/dev-env "ems1"
	. /etc/profile.d/compass.sh
	kubectl_create
	kubectl_label
	4-docker/buildall

	check_es_ready || {
		echo "es is not ready, cbs deploy will exit."
		exit
	}
	sh $CCI_SRC/sbin/create-es-account.sh
	find $CCI_SRC/sbin/ -name "es-*-mapping.sh" -exec sh {} \;
	find $CCI_SRC/sbin/ -name "es-*-mapping.sh" -exec sh {} \;
}

install_master
k8s/remote-install-worker "/etc/compass-ci/setup.yaml"
k8s/remote-install-multi-docker "/etc/compass-ci/remote-hosts" "$HOSTNAME"
k8s/pack-lkp
