3 ################################################################################
4 # Copyright (c) 2019 AT&T Intellectual Property. #
5 # Copyright (c) 2019 Nokia. #
7 # Licensed under the Apache License, Version 2.0 (the "License"); #
8 # you may not use this file except in compliance with the License. #
9 # You may obtain a copy of the License at #
11 # http://www.apache.org/licenses/LICENSE-2.0 #
13 # Unless required by applicable law or agreed to in writing, software #
14 # distributed under the License is distributed on an "AS IS" BASIS, #
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
16 # See the License for the specific language governing permissions and #
17 # limitations under the License. #
18 ################################################################################
21 # the following script replaces templates in a script with env variables defined in etc folder
22 # when running without specifying a script, the default is to use the heat/scripts/k8s-vm-install.sh,
23 # the result which is a script that can be used as cloud-init script and the initial installation
24 # script that turns a newly launched VM into a single node k8s cluster with Helm.
27 echo "Usage: $0 <template file>" 1>&2;
28 echo " If the template file is supplied, the template file is processed;" 1>&2;
29 echo " Otherwise the k8s_vm_install.sh file under heat/script is used as template." 1>&2;
34 DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )"
36 RCS="$(find $DIR/../etc -type f -maxdepth 1)"
38 echo "reading in values in $RC"
44 if [ -z "$WORKSPACE" ]; then
45 export WORKSPACE=`git rev-parse --show-toplevel`
48 HEAT_DIR="$WORKSPACE/tools/k8s/heat"
49 BIN_DIR="$WORKSPACE/tools/k8s/bin"
50 ETC_DIR="$WORKSPACE/tools/k8s/etc"
51 ENV_DIR="$WORKSPACE/tools/k8s/heat/env"
54 TMPL="${HEAT_DIR}/scripts/k8s_vm_install.sh"
60 if [ -z "$__RUNRICENV_GERRIT_HOST__" ]; then
61 export __RUNRICENV_GERRIT_HOST__=$gerrithost
63 if [ -z "$__RUNRICENV_GERRIT_IP__" ]; then
64 export __RUNRICENV_GERRIT_IP__=$gerritip
66 if [ -z "$__RUNRICENV_DOCKER_HOST__" ]; then
67 export __RUNRICENV_DOCKER_HOST__=$dockerregistry
69 if [ -z "$__RUNRICENV_DOCKER_IP__" ]; then
70 export __RUNRICENV_DOCKER_IP__=$dockerip
72 if [ -z "$__RUNRICENV_DOCKER_PORT__" ]; then
73 export __RUNRICENV_DOCKER_PORT__=$dockerport
75 if [ -z "$__RUNRICENV_DOCKER_USER__" ]; then
76 export __RUNRICENV_DOCKER_USER__=$dockeruser
78 if [ -z "$__RUNRICENV_DOCKER_PASS__" ]; then
79 export __RUNRICENV_DOCKER_PASS__=$dockerpassword
81 if [ -z "$__RUNRICENV_DOCKER_CERT__" ]; then
82 export __RUNRICENV_DOCKER_CERT__=$dockercert
84 if [ -z "$__RUNRICENV_DOCKER_CERT_LEN__" ]; then
85 export __RUNRICENV_DOCKER_CERT_LEN__=$(echo $dockercert | wc -c)
87 if [ -z "$__RUNRICENV_HELMREPO_HOST__" ]; then
88 export __RUNRICENV_HELMREPO_HOST__=$helmrepo
90 if [ -z "$__RUNRICENV_HELMREPO_PORT__" ]; then
91 export __RUNRICENV_HELMREPO_PORT__=$helmport
93 if [ -z "$__RUNRICENV_HELMREPO_IP__" ]; then
94 export __RUNRICENV_HELMREPO_IP__=$helmip
96 if [ -z "$__RUNRICENV_HELMREPO_USER__" ]; then
97 export __RUNRICENV_HELMREPO_USER__=$helmuser
99 if [ -z "$__RUNRICENV_HELMREPO_PASS__" ]; then
100 export __RUNRICENV_HELMREPO_PASS__=$helmpassword
102 if [ -z "$__RUNRICENV_HELMREPO_CERT__" ]; then
103 export __RUNRICENV_HELMREPO_CERT__=$helmcert
105 if [ -z "$__RUNRICENV_HELMREPO_CERT_LEN__" ]; then
106 export __RUNRICENV_HELMREPO_CERT_LEN__=$(echo $helmcert | wc -c)
110 filename=$(basename -- "$TMPL")
111 extension="${filename##*.}"
112 filename="${filename%.*}"
114 envsubst '${__RUNRICENV_GERRIT_HOST__}
115 ${__RUNRICENV_GERRIT_IP__}
116 ${__RUNRICENV_DOCKER_HOST__}
117 ${__RUNRICENV_DOCKER_IP__}
118 ${__RUNRICENV_DOCKER_PORT__}
119 ${__RUNRICENV_DOCKER_USER__}
120 ${__RUNRICENV_DOCKER_PASS__}
121 ${__RUNRICENV_DOCKER_CERT__}
122 ${__RUNRICENV_DOCKER_CERT__}
123 ${__RUNRICENV_DOCKER_CERT_LEN__}
124 ${__RUNRICENV_HELMREPO_HOST__}
125 ${__RUNRICENV_HELMREPO_PORT__}
126 ${__RUNRICENV_HELMREPO_IP__}
127 ${__RUNRICENV_HELMREPO_CERT__}
128 ${__RUNRICENV_HELMREPO_CERT_LEN__}
129 ${__RUNRICENV_HELMREPO_USER__}
130 ${__RUNRICENV_HELMREPO_PASS__}' < "$TMPL" > "$filename"
132 # fill values that are supplied by Heat stack deployment process as much as we can
133 sed -i -e "s/__docker_version__/${INFRA_DOCKER_VERSION}/g" "$filename"
134 sed -i -e "s/__k8s_version__/${INFRA_K8S_VERSION}/g" "$filename"
135 sed -i -e "s/__k8s_cni_version__/${INFRA_CNI_VERSION}/g" "$filename"
136 sed -i -e "s/__helm_version__/${INFRA_HELM_VERSION}/g" "$filename"
137 sed -i -e "s/__k8s_mst_private_ip_addr__/\$(hostname -I)/g" "$filename"
138 sed -i -e "s/__host_private_ip_addr__/\$(hostname -I)/g" "$filename"
139 #sed -i -e "s/__k8s_mst_floating_ip_addr__/\$(ec2metadata --public-ipv4)/g" "$filename"
140 sed -i -e "s/__k8s_mst_floating_ip_addr__/\$(curl ifconfig.co)/g" "$filename"
141 sed -i -e "s/__stack_name__/\$(hostname)/g" "$filename"
142 #echo "__mtu__" > /opt/config/mtu.txt
143 #echo "__cinder_volume_id__" > /opt/config/cinder_volume_id.txt
145 # because cloud init user data has a 16kB limit, remove all comment lines to save space.
146 # except for the #! line
147 sed -i -e '/^[ \t]*#[^!]/d' "$filename"
151 K8SV=$(echo ${INFRA_K8S_VERSION} | cut -f 1-2 -d '.' | sed -e 's/\./_/g')
152 HV=$(echo ${INFRA_HELM_VERSION} | cut -f 1-2 -d '.' | sed -e 's/\./_/g')
153 DV=$(echo ${INFRA_DOCKER_VERSION} | cut -f 1-2 -d '.' | sed -e 's/\./_/g')
154 mv "$filename" ./k8s-1node-cloud-init-k_${K8SV:-cur}-h_${HV:-cur}-d_${DV:-cur}.sh