#!/bin/bash
# Origin: https://hub.docker.com/r/openresty/openresty
# Copyright (C) 2016-2020  Eric D. Evan Wies
# SPDX-License-Identifier: MulanPSL-2.0+
# Copyright (c) 2020 Huawei Technologies Co., Ltd. All rights reserved.

BASE_IMAGE=$1
CCI_SRC=${CCI_SRC:-/c/cbs}

. ${CCI_SRC}/container/defconfig.sh

[[ -z ${BASE_IMAGE} ]] && {
	echo "Please specify the base image."

	exit 1
}

# copy nginx.conf from orignal result-web-dav dir
# used to refresh the redis node IPs for the conf file
cp ${CCI_SRC}/container/result-webdav/nginx.conf ./

# 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

docker_skip_rebuild "${BASE_IMAGE}"

docker build \
       --no-cache \
       -t \
       $BASE_IMAGE \
       --build-arg BASE_IMAGE=${BASE_IMAGE} \
       .
