6722b7e80127259f359799a383ef70bf5c9a94c2
[it/dep.git] / tools / k8s / bin / gen-cloud-init.sh
1 #!/bin/bash
2 #
3 ################################################################################
4 #   Copyright (c) 2019 AT&T Intellectual Property.                             #
5 #   Copyright (c) 2019 Nokia.                                                  #
6 #                                                                              #
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                                    #
10 #                                                                              #
11 #       http://www.apache.org/licenses/LICENSE-2.0                             #
12 #                                                                              #
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 ################################################################################
19
20
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.
25
26 usage() {
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;
30     exit 1;
31 }
32
33
34 DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )"
35 set -a
36 RCS="$(find $DIR/../etc -type f -maxdepth 1)"
37 for RC in $RCS; do
38   echo "reading in values in $RC"
39   source $RC
40 done
41 set +a
42
43
44 if [ -z "$WORKSPACE" ]; then
45     export WORKSPACE=`git rev-parse --show-toplevel`
46 fi
47 HEAT_DIR="$WORKSPACE/ric-infra/00-Kubernetes/heat"
48 BIN_DIR="$WORKSPACE/ric-infra/00-Kubernetes/bin"
49 ETC_DIR="$WORKSPACE/ric-infra/00-Kubernetes/etc"
50 ENV_DIR="$WORKSPACE/ric-infra/00-Kubernetes/heat/env"
51
52 if [ -z "$1" ]; then
53   TMPL="${HEAT_DIR}/scripts/k8s_vm_install.sh"
54 else
55   TMPL="$1"
56 fi
57
58
59 if [ -z "$__RUNRICENV_GERRIT_HOST__" ]; then
60    export __RUNRICENV_GERRIT_HOST__=$gerrithost
61 fi
62 if [ -z "$__RUNRICENV_GERRIT_IP__" ]; then
63    export __RUNRICENV_GERRIT_IP__=$gerritip
64 fi
65 if [ -z "$__RUNRICENV_DOCKER_HOST__" ]; then
66    export __RUNRICENV_DOCKER_HOST__=$dockerregistry
67 fi
68 if [ -z "$__RUNRICENV_DOCKER_IP__" ]; then
69    export __RUNRICENV_DOCKER_IP__=$dockerip
70 fi
71 if [ -z "$__RUNRICENV_DOCKER_PORT__" ]; then
72    export __RUNRICENV_DOCKER_PORT__=$dockerport
73 fi
74 if [ -z "$__RUNRICENV_DOCKER_USER__" ]; then
75    export __RUNRICENV_DOCKER_USER__=$dockeruser
76 fi
77 if [ -z "$__RUNRICENV_DOCKER_PASS__" ]; then
78    export __RUNRICENV_DOCKER_PASS__=$dockerpassword
79 fi
80 if [ -z "$__RUNRICENV_DOCKER_CERT__" ]; then
81    export __RUNRICENV_DOCKER_CERT__=$dockercert
82 fi
83 if [ -z "$__RUNRICENV_DOCKER_CERT_LEN__" ]; then
84    export __RUNRICENV_DOCKER_CERT_LEN__=$(echo $dockercert | wc -c)
85 fi
86 if [ -z "$__RUNRICENV_HELMREPO_HOST__" ]; then
87    export __RUNRICENV_HELMREPO_HOST__=$helmrepo
88 fi
89 if [ -z "$__RUNRICENV_HELMREPO_PORT__" ]; then
90    export __RUNRICENV_HELMREPO_PORT__=$helmport
91 fi
92 if [ -z "$__RUNRICENV_HELMREPO_IP__" ]; then
93    export __RUNRICENV_HELMREPO_IP__=$helmip
94 fi
95 if [ -z "$__RUNRICENV_HELMREPO_USER__" ]; then
96    export __RUNRICENV_HELMREPO_USER__=$helmuser
97 fi
98 if [ -z "$__RUNRICENV_HELMREPO_PASS__" ]; then
99    export __RUNRICENV_HELMREPO_PASS__=$helmpassword
100 fi
101 if [ -z "$__RUNRICENV_HELMREPO_CERT__" ]; then
102    export __RUNRICENV_HELMREPO_CERT__=$helmcert
103 fi
104 if [ -z "$__RUNRICENV_HELMREPO_CERT_LEN__" ]; then
105    export __RUNRICENV_HELMREPO_CERT_LEN__=$(echo $helmcert | wc -c)
106 fi
107
108
109 filename=$(basename -- "$TMPL")
110 extension="${filename##*.}"
111 filename="${filename%.*}"
112
113 envsubst '${__RUNRICENV_GERRIT_HOST__}
114           ${__RUNRICENV_GERRIT_IP__}
115           ${__RUNRICENV_DOCKER_HOST__}
116           ${__RUNRICENV_DOCKER_IP__}
117           ${__RUNRICENV_DOCKER_PORT__}
118           ${__RUNRICENV_DOCKER_USER__}
119           ${__RUNRICENV_DOCKER_PASS__}
120           ${__RUNRICENV_DOCKER_CERT__}
121           ${__RUNRICENV_DOCKER_CERT__}
122           ${__RUNRICENV_DOCKER_CERT_LEN__}
123           ${__RUNRICENV_HELMREPO_HOST__}
124           ${__RUNRICENV_HELMREPO_PORT__}
125           ${__RUNRICENV_HELMREPO_IP__}
126           ${__RUNRICENV_HELMREPO_CERT__}
127           ${__RUNRICENV_HELMREPO_CERT_LEN__}
128           ${__RUNRICENV_HELMREPO_USER__}
129           ${__RUNRICENV_HELMREPO_PASS__}' < "$TMPL" > "$filename"
130
131 # fill values that are supplied by Heat stack deployment process as much as we can
132 sed -i -e "s/__docker_version__/${INFRA_DOCKER_VERSION}/g" "$filename"
133 sed -i -e "s/__k8s_version__/${INFRA_K8S_VERSION}/g" "$filename"
134 sed -i -e "s/__k8s_cni_version__/${INFRA_CNI_VERSION}/g" "$filename"
135 sed -i -e "s/__helm_version__/${INFRA_HELM_VERSION}/g" "$filename"
136 sed -i -e "s/__k8s_mst_private_ip_addr__/\$(hostname -I)/g" "$filename"
137 sed -i -e "s/__host_private_ip_addr__/\$(hostname -I)/g" "$filename"
138 #sed -i -e "s/__k8s_mst_floating_ip_addr__/\$(ec2metadata --public-ipv4)/g" "$filename"
139 sed -i -e "s/__k8s_mst_floating_ip_addr__/\$(curl ifconfig.co)/g" "$filename"
140 sed -i -e "s/__stack_name__/\$(hostname)/g" "$filename"
141 #echo "__mtu__" > /opt/config/mtu.txt
142 #echo "__cinder_volume_id__" > /opt/config/cinder_volume_id.txt
143
144 # because cloud init user data has a 16kB limit, remove all comment lines to save space.
145 # except for the #! line
146 sed -i -e '/^[ \t]*#[^!]/d' "$filename" 
147
148 chmod +x "$filename"
149
150 mv "$filename" ./k8s-1node-cloud-init.sh