#!/bin/bash
# SPDX-License-Identifier: MulanPSL-2.0+
# Copyright (c) 2020 Huawei Technologies Co., Ltd. All rights reserved.

[[ $CCI_SRC ]] || $CCI_SRC=/c/cbs
[[ $LKP_SRC ]] || $LKP_SRC=/c/lkp-tests

hosts_file=$1

if [[ -z $hosts_file ]]
then
       echo "Usage: "
       echo "     ./remote-install-worker { hosts_file }"
       echo "Example: "
       echo "     ./remote-install-worker /etc/compass-ci/setup.yaml"

       exit 1
fi

worker=($(cat $hosts_file | grep "^worker: " | awk -F'worker: ' '{print $2}'))
worker_count=${#worker[@]}

source ${CCI_SRC}/lib/log.sh

index=1
for hostname in ${worker[@]}
do
	index=$((index+1))
	(
		ssh root@$hostname -C "umask 002; mkdir -p /c/"
		rsync -ra --delete /c/lkp-tests /c/cbs root@$hostname:/c/
		rsync -ra /etc/compass-ci root@$hostname:/etc/
		ssh root@$hostname -C "cd /c/cbs/sparrow/k8s; sh install-worker ems${index}"
	) &
done

wait

log_info "finish install worker"
