#!/bin/bash
# Refer to: https://hub.docker.com/r/openresty/openresty
# Copyright (c) 2020 Huawei Technologies Co., Ltd. All rights reserved.
# SPDX-License-Identifier: MulanPSL-2.0+

. $CCI_SRC/container/defconfig.sh

# the redis pods defaults deployed in namespace: ems1
redis_ns=ems1

# for every redis restart, the redis pods's IP will change.
# the nginx.conf need config the new IPs, otherwise, the upload executiong will fail.
# extract redis host IPs and save them with fixed format to a tmp files
kubectl get pod -n $redis_ns -o wide | grep redis | awk '{print "                                        { ip = \""$6"\", port = 6379 },"}' > tmp_redis_lines || {
	echo "Extract redis host info failed."

	exit 1
}

# before config new redis hosts, delete old lines
sed -i '/{ ip = ".*", port = 6379}/d' nginx.conf

# add the saved redis hosts info lines to nginx config file
sed -i "/serv_list = /r tmp_redis_lines" nginx.conf

# rm the tmp file after config the nginx config file
rm tmp_redis_lines

start_pod

: ${REDIS_HOST:=172.17.0.1}
: ${REDIS_PORT:=6379}

docker_rm result-webdav

cmd=(
	docker run -d
	--name result-webdav
	--restart=always
	-p 3080:3080
	-e REDIS_HOST=$REDIS_HOST
	-e REDIS_PORT=$REDIS_PORT
	-v $PWD/nginx.conf:/usr/local/openresty/nginx/conf/nginx.conf
	-v /etc/localtime:/etc/localtime:ro
	-v /srv/result/:/srv/result/
	-v /srv/initrd/:/srv/initrd/
	-v /srv/kernel/:/srv/kernel/
	-v /srv/rpm/:/srv/rpm/
	-v /srv/tmp/rpm/:/srv/tmp/rpm/
	-v /srv/cci/build-config/:/srv/cci/build-config/
	-v /srv/cci/profile/data:/srv/cci/profile/data
	-v /srv/kunpeng:/srv/kunpeng
	-v /srv/repositories:/srv/repositories
	result-webdav
)

"${cmd[@]}"
