cdb43dd5918cd47b7601c2d696d8ddd28deddee6
[multicloud/k8s.git] / kud / hosting_providers / containerized / installer.sh
1 #!/bin/bash
2 #SPDX-license-identifier: Apache-2.0
3 ##############################################################################
4 # Copyright (c) 2018
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9 ##############################################################################
10
11 set -o errexit
12 set -o nounset
13 set -o pipefail
14 set -ex
15
16 INSTALLER_DIR="$(readlink -f "$(dirname "${BASH_SOURCE[0]}")")"
17
18 function install_prerequisites {
19     apt-get update
20     apt-get install -y software-properties-common
21     add-apt-repository -y ppa:longsleep/golang-backports
22     apt-get update
23     apt-get install -y \
24             curl \
25             gettext-base \
26             git \
27             golang-go \
28             make \
29             python3-pip \
30             rsync \
31             sshpass \
32             sudo \
33             unzip \
34             vim \
35             wget
36     update-alternatives --install /usr/bin/pip pip /usr/bin/pip3 1
37 }
38
39 # _install_ansible() - Install and Configure Ansible program
40 function _install_ansible {
41     local version=$(grep "ansible_version" ${kud_playbooks}/kud-vars.yml |
42         awk -F ': ' '{print $2}')
43     mkdir -p /etc/ansible/
44     pip install --no-cache-dir ansible==$version
45 }
46
47 function install_kubespray {
48     echo "Deploying kubernetes"
49     version=$(grep "kubespray_version" ${kud_playbooks}/kud-vars.yml | \
50         awk -F ': ' '{print $2}')
51     local_release_dir=$(grep "local_release_dir" \
52         $kud_inventory_folder/group_vars/k8s-cluster.yml | \
53         awk -F "\"" '{print $2}')
54     local tarball=v$version.tar.gz
55     _install_ansible
56     wget https://github.com/kubernetes-incubator/kubespray/archive/$tarball
57     tar -C $dest_folder -xzf $tarball
58     chown -R root:root $dest_folder/kubespray-$version
59     mkdir -p ${local_release_dir}/containers
60     rm $tarball
61
62     pushd $dest_folder/kubespray-$version/
63     pip install --no-cache-dir -r ./requirements.txt
64     make mitogen
65     popd
66     rm -f $kud_inventory_folder/group_vars/all.yml 2> /dev/null
67     if [[ -n "${verbose:-}" ]]; then
68         echo "kube_log_level: 5" | tee \
69             $kud_inventory_folder/group_vars/all.yml
70     else
71         echo "kube_log_level: 2" | tee \
72             $kud_inventory_folder/group_vars/all.yml
73     fi
74     echo "kubeadm_enabled: true" | \
75         tee --append $kud_inventory_folder/group_vars/all.yml
76     if [[ -n "${http_proxy:-}" ]]; then
77         echo "http_proxy: \"$http_proxy\"" | tee --append \
78             $kud_inventory_folder/group_vars/all.yml
79     fi
80     if [[ -n "${https_proxy:-}" ]]; then
81         echo "https_proxy: \"$https_proxy\"" | tee --append \
82             $kud_inventory_folder/group_vars/all.yml
83     fi
84 }
85
86 # install_k8s() - Install Kubernetes using kubespray tool including Kata
87 function install_k8s {
88     local cluster_name=$1
89     ansible-playbook $verbose -i \
90         $kud_inventory $kud_playbooks/preconfigure-kubespray.yml \
91         --become --become-user=root | \
92         tee $cluster_log/setup-kubernetes.log
93     if [ "$container_runtime" == "docker" ]; then
94         echo "Docker will be used as the container runtime interface"
95         ansible-playbook $verbose -i \
96             $kud_inventory $dest_folder/kubespray-$version/cluster.yml \
97             -e cluster_name=$cluster_name --become --become-user=root | \
98             tee $cluster_log/setup-kubernetes.log
99     elif [ "$container_runtime" == "containerd" ]; then
100         echo "Containerd will be used as the container runtime interface"
101         ansible-playbook $verbose -i \
102             $kud_inventory $dest_folder/kubespray-$version/cluster.yml \
103             -e $kud_kata_override_variables -e cluster_name=$cluster_name \
104             --become --become-user=root | \
105             tee $cluster_log/setup-kubernetes.log
106         #Install Kata Containers in containerd scenario
107         ansible-playbook $verbose -i \
108             $kud_inventory -e "base_dest=$HOME" \
109             $kud_playbooks/configure-kata.yml | \
110             tee $cluster_log/setup-kata.log
111     else
112         echo "Only Docker or Containerd are supported container runtimes"
113         exit 1
114     fi
115
116     # Configure environment
117     # Requires kubeconfig_localhost and kubectl_localhost to be true
118     # in inventory/group_vars/k8s-cluster.yml
119     mkdir -p $HOME/.kube
120     cp $kud_inventory_folder/artifacts/admin.conf $HOME/.kube/config
121     if !(which kubectl); then
122         cp $kud_inventory_folder/artifacts/kubectl /usr/local/bin/
123     fi
124 }
125
126 # install_addons() - Install Kubenertes AddOns
127 function install_addons {
128     if [ ${1:+1} ]; then
129         local plugins_name="$1"
130         echo "additional addons plugins $1"
131     else
132         local plugins_name=""
133         echo "no additional addons pluigns"
134     fi
135
136     source /etc/environment
137     echo "Installing Kubernetes AddOns"
138     ansible-galaxy install $verbose -r \
139         $kud_infra_folder/galaxy-requirements.yml --ignore-errors
140
141     ansible-playbook $verbose -i \
142         $kud_inventory -e "base_dest=$HOME" $kud_playbooks/configure-kud.yml \
143         | tee $cluster_log/setup-kud.log
144
145     kud_addons="${KUD_ADDONS:-} ${plugins_name}"
146
147     for addon in ${kud_addons}; do
148         echo "Deploying $addon using configure-$addon.yml playbook.."
149         ansible-playbook $verbose -i \
150             $kud_inventory -e "base_dest=$HOME" \
151             $kud_playbooks/configure-${addon}.yml | \
152             tee $cluster_log/setup-${addon}.log
153     done
154
155     echo "Run the test cases if testing_enabled is set to true."
156     if [[ "${testing_enabled}" == "true" ]]; then
157         failed_kud_tests=""
158         # Run Kata test first if Kata was installed
159         if [ "$container_runtime" == "containerd" ]; then
160             #Install Kata webhook for test pods
161             ansible-playbook $verbose -i $kud_inventory -e "base_dest=$HOME" \
162                 -e "kata_webhook_runtimeclass=$kata_webhook_runtimeclass" \
163                 $kud_playbooks/configure-kata-webhook.yml \
164                 --become --become-user=root | \
165                 sudo tee $cluster_log/setup-kata-webhook.log
166             kata_webhook_deployed=true
167             pushd $kud_tests
168             bash kata.sh || failed_kud_tests="${failed_kud_tests} kata"
169             popd
170         fi
171         #Run other plugin tests
172         for addon in ${kud_addons}; do
173             pushd $kud_tests
174             bash ${addon}.sh || failed_kud_tests="${failed_kud_tests} ${addon}"
175             case $addon in
176                 "onap4k8s" )
177                     echo "Test the onap4k8s plugin installation"
178                     for functional_test in plugin_edgex plugin_fw plugin_eaa; do
179                         bash ${functional_test}.sh --external || failed_kud_tests="${failed_kud_tests} ${functional_test}"
180                     done
181                     ;;
182                 "emco" )
183                     echo "Test the emco plugin installation"
184                     # TODO plugin_fw_v2 requires virtlet and a patched multus to succeed
185                     # for functional_test in plugin_fw_v2; do
186                     #     bash ${functional_test}.sh --external || failed_kud_tests="${failed_kud_tests} ${functional_test}"
187                     # done
188                     ;;
189             esac
190             popd
191         done
192         # Remove Kata webhook if user didn't want it permanently installed
193         if ! [ "$enable_kata_webhook" == "true" ] && [ "$kata_webhook_deployed" == "true" ]; then
194             ansible-playbook $verbose -i $kud_inventory -e "base_dest=$HOME" \
195                 -e "kata_webhook_runtimeclass=$kata_webhook_runtimeclass" \
196                 $kud_playbooks/configure-kata-webhook-reset.yml \
197                 --become --become-user=root | \
198                 sudo tee $cluster_log/kata-webhook-reset.log
199             kata_webhook_deployed=false
200         fi
201         if [[ ! -z "$failed_kud_tests" ]]; then
202             echo "Test cases failed:${failed_kud_tests}"
203             return 1
204         fi
205     fi
206
207     # Check if Kata webhook should be installed and isn't already installed
208     if [ "$enable_kata_webhook" == "true" ] && ! [ "$kata_webhook_deployed" == "true" ]; then
209         ansible-playbook $verbose -i $kud_inventory -e "base_dest=$HOME" \
210             -e "kata_webhook_runtimeclass=$kata_webhook_runtimeclass" \
211             $kud_playbooks/configure-kata-webhook.yml \
212             --become --become-user=root | \
213             sudo tee $cluster_log/setup-kata-webhook.log
214     fi
215
216     echo "Add-ons deployment complete..."
217 }
218
219 function master_ip {
220     kubectl config view --minify -o jsonpath='{.clusters[0].cluster.server}' | awk -F '[:/]' '{print $4}'
221 }
222
223 # Copy installation artifacts to be usable in host running Ansible
224 function install_host_artifacts {
225     local -r cluster_name=$1
226     local -r host_dir="/opt/kud/multi-cluster"
227     local -r host_addons_dir="${host_dir}/addons"
228     local -r host_artifacts_dir="${host_dir}/${cluster_name}/artifacts"
229
230     for addon in cdi cdi-operator cpu-manager kubevirt kubevirt-operator multus-cni node-feature-discovery ovn4nfv ovn4nfv-network qat-device-plugin sriov-network sriov-network-operator; do
231         mkdir -p ${host_addons_dir}/${addon}/{helm,profile}
232         cp -r ${kud_infra_folder}/helm/${addon} ${host_addons_dir}/${addon}/helm
233         cp -r ${kud_infra_folder}/profiles/${addon}/* ${host_addons_dir}/${addon}/profile
234         tar -czf ${host_addons_dir}/${addon}.tar.gz -C ${host_addons_dir}/${addon}/helm .
235         tar -czf ${host_addons_dir}/${addon}_profile.tar.gz -C ${host_addons_dir}/${addon}/profile .
236     done
237
238     mkdir -p ${host_addons_dir}/tests
239     for test in _common _common_test _functions topology-manager-sriov kubevirt multus ovn4nfv nfd sriov-network qat cmk; do
240         cp ${kud_tests}/${test}.sh ${host_addons_dir}/tests
241     done
242
243     mkdir -p ${host_artifacts_dir}
244     cp -rf ${kud_inventory_folder}/artifacts/* ${host_artifacts_dir}
245
246     mkdir -p ${host_artifacts_dir}/addons
247     for yaml in ${kud_infra_folder}/emco/examples/*.yaml; do
248         cp ${yaml} ${host_artifacts_dir}/addons
249     done
250     for template in addons/*.tmpl; do
251         CLUSTER_NAME="${cluster_name}" \
252         HOST_IP="$(master_ip)" \
253         KUBE_PATH="${host_artifacts_dir}/admin.conf" \
254         PACKAGES_PATH="${host_addons_dir}" \
255         envsubst <${template} >${host_artifacts_dir}/${template%.tmpl}
256     done
257 }
258
259 # _print_kubernetes_info() - Prints the login Kubernetes information
260 function _print_kubernetes_info {
261     if ! $(kubectl version &>/dev/null); then
262         return
263     fi
264
265     # Expose Dashboard using NodePort
266     node_port=30080
267     KUBE_EDITOR="sed -i \"s|type\: ClusterIP|type\: NodePort|g\"" \
268         kubectl -n kube-system edit service kubernetes-dashboard
269     KUBE_EDITOR="sed -i \"s|nodePort\: .*|nodePort\: $node_port|g\"" \
270         kubectl -n kube-system edit service kubernetes-dashboard
271
272     printf "Kubernetes Info\n===============\n" > $k8s_info_file
273     echo "Dashboard URL: https://$(master_ip):$node_port" >> $k8s_info_file
274     echo "Admin user: kube" >> $k8s_info_file
275     echo "Admin password: secret" >> $k8s_info_file
276 }
277
278 verbose=""
279 if [[ -n "${KUD_DEBUG:-}" ]]; then
280     set -o xtrace
281     verbose="-vvv"
282 fi
283
284 # Configuration values
285 dest_folder=/opt
286 kud_folder=${INSTALLER_DIR}
287 kud_infra_folder=$kud_folder/../../deployment_infra
288 kud_playbooks=$kud_infra_folder/playbooks
289 kud_tests=$kud_folder/../../tests
290 k8s_info_file=$kud_folder/k8s_info.log
291 testing_enabled=${KUD_ENABLE_TESTS:-false}
292 container_runtime=${CONTAINER_RUNTIME:-docker}
293 enable_kata_webhook=${ENABLE_KATA_WEBHOOK:-false}
294 kata_webhook_runtimeclass=${KATA_WEBHOOK_RUNTIMECLASS:-kata-qemu}
295 kata_webhook_deployed=false
296 # For containerd the etcd_deployment_type: docker is the default and doesn't work.
297 # You have to use either etcd_kubeadm_enabled: true or etcd_deployment_type: host
298 # See https://github.com/kubernetes-sigs/kubespray/issues/5713
299 kud_kata_override_variables="container_manager=containerd \
300     -e etcd_deployment_type=host -e kubelet_cgroup_driver=cgroupfs"
301
302 mkdir -p /opt/csar
303 export CSAR_DIR=/opt/csar
304
305 function install_pkg {
306     install_prerequisites
307     install_kubespray
308 }
309
310 function install_cluster {
311     version=$(grep "kubespray_version" ${kud_playbooks}/kud-vars.yml | \
312         awk -F ': ' '{print $2}')
313     export ANSIBLE_CONFIG=$dest_folder/kubespray-$version/ansible.cfg
314     install_k8s $1
315     if [ ${2:+1} ]; then
316         echo "install default addons and $2"
317         install_addons "$2"
318     else
319         install_addons
320     fi
321     echo "installed the addons"
322
323     install_host_artifacts $1
324
325     _print_kubernetes_info
326 }
327
328 function usage {
329     echo "installer usage:"
330     echo "./installer.sh --install_pkg - Install the required softwarepackage"
331     echo "./installer.sh --cluster <cluster name> \
332 - Install k8s cluster with default plugins"
333     echo "./installer.sh --cluster <cluster name> \
334 --plugins <plugin_1 plugin_2> - Install k8s cluster with default plugins \
335 and additional plugins such as onap4k8s."
336 }
337
338 if [ $# -eq 0 ]; then
339     echo "Error: No arguments supplied"
340     usage
341     exit 1
342 fi
343
344 if [ -z "$1" ]; then
345     echo "Error: Null argument passed"
346     usage
347     exit 1
348 fi
349
350 if [ "$1" == "--install_pkg" ]; then
351     export kud_inventory_folder=$kud_folder/inventory
352     kud_inventory=$kud_inventory_folder/hosts.ini
353     install_pkg
354     echo "install pkg"
355     exit 0
356 fi
357
358 if [ "$1" == "--cluster" ]; then
359     if [ -z "${2-}"  ]; then
360         echo "Error: Cluster name is null"
361         usage
362         exit 1
363     fi
364
365     cluster_name=$2
366     kud_multi_cluster_path=/opt/kud/multi-cluster
367     cluster_path=$kud_multi_cluster_path/$cluster_name
368     echo $cluster_path
369     if [ ! -d "${cluster_path}" ]; then
370         echo "Error: cluster_path ${cluster_path} doesn't exit"
371         usage
372         exit 1
373     fi
374
375     cluster_log=$kud_multi_cluster_path/$cluster_name/log
376     export kud_inventory_folder=$kud_folder/inventory/$cluster_name
377     kud_inventory=$kud_inventory_folder/hosts.ini
378
379     mkdir -p $kud_inventory_folder
380     mkdir -p $cluster_log
381     cp $kud_multi_cluster_path/$cluster_name/hosts.ini $kud_inventory_folder/
382     cp -rf $kud_folder/inventory/group_vars $kud_inventory_folder/
383
384     if [ ${3:+1} ]; then
385         if [ "$3" == "--plugins" ]; then
386             if [ -z "${4-}"  ]; then
387                 echo "Error: plugins arguments is null; Refer the usage"
388                 usage
389                 exit 1
390             fi
391             plugins_name=${@:4:$#}
392             install_cluster $cluster_name "$plugins_name"
393             exit 0
394         else
395             echo "Error: cluster argument should have plugins; \
396                 Refer the usage"
397             usage
398             exit 1
399         fi
400     fi
401     install_cluster $cluster_name
402     exit 0
403 fi
404
405
406 echo "Error: Refer the installer usage"
407 usage
408 exit 1