Merge "Enable merge jobs to trigger downstream job"
[ci-management.git] / jjb / shell / kni_deploy_libvirt.sh
1 #!/bin/bash
2 #
3 # Copyright (c) 2019 Red Hat
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 #        http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 set -e -u -x -o pipefail
18
19 export PATH=$PATH:/usr/local/go/bin:/usr/local/bin
20 KNI_PATH='src/gerrit.akraino.org/kni/'
21 SITE_NAME='testing.virt.edge-sites.net'
22
23 echo '---> Starting kni installer generation'
24 export GOPATH=${WORKSPACE}/
25
26 function approve_certs() {
27     # sleep for the first 10 min
28     sleep 600
29
30     # temporary fix, autoapprove certificates on background
31     while /bin/true; do
32         export KUBECONFIG=$HOME/.kni/${SITE_NAME}/final_manifests/auth/kubeconfig
33         oc get csr | grep worker | grep Pending | awk '{print $1}' | xargs -n 1 oc adm certificate approve || true
34         sleep 60
35     done
36 }
37
38 # move the blueprint to an inner directory
39 mkdir ${WORKSPACE}/blueprint-pae
40 mv base profiles sites tools ${WORKSPACE}/blueprint-pae/
41
42 # clone installer in the right directory
43 sudo rm -rf ${WORKSPACE}/${KNI_PATH}
44 mkdir -p ${WORKSPACE}/${KNI_PATH}
45 pushd ${WORKSPACE}/${KNI_PATH}/
46 git clone https://gerrit.akraino.org/r/kni/installer
47 pushd installer
48
49 # first build kni installer
50 make build 2>&1 | tee ${WORKSPACE}/build.log
51
52 # do a host preparation and cleanup
53 bash utils/prep_host.sh virt.edge-sites.net
54 wget https://raw.githubusercontent.com/openshift/installer/master/scripts/maintenance/virsh-cleanup.sh
55 chmod a+x ./virsh-cleanup.sh
56 sudo -E bash -c "yes Y | ./virsh-cleanup.sh"
57
58 # add the right credentials to kni
59 mkdir $HOME/.kni || true
60 cp $WORKSPACE/akraino-secrets/coreos-pull-secret $HOME/.kni/pull-secret.json || true
61 cp $HOME/.ssh/id_rsa.pub $HOME/.kni/id_rsa.pub || true
62
63 # start the workflow
64 sudo rm -rf /$HOME/.kni/${SITE_NAME}/final_manifests || true
65 ./knictl fetch_requirements file://${WORKSPACE}/blueprint-pae//sites/${SITE_NAME} 2>&1 | tee ${WORKSPACE}/libvirt_requirements.log
66 ./knictl prepare_manifests ${SITE_NAME} 2>&1 | tee ${WORKSPACE}/libvirt_manifests.log
67
68 # now run the cluster
69 source $HOME/.kni/${SITE_NAME}/profile.env
70 approve_certs &
71 FUNCTION_PID=$!
72 sudo -E $HOME/.kni/${SITE_NAME}/requirements/openshift-install create cluster --dir=/$HOME/.kni/${SITE_NAME}/final_manifests 2>&1 | tee ${WORKSPACE}/libvirt_deploy.log
73 STATUS=$?
74 kill $FUNCTION_PID || true
75
76 # output tfstate
77 echo "metadata.json for removing cluster"
78 cat $HOME/.kni/${SITE_NAME}/final_manifests/metadata.json
79
80 if [ $STATUS -ne 0 ]; then
81     echo "Error deploying in libvirt"
82     exit 1
83 fi
84
85 echo "Cluster successfully deployed! Start applying workloads"
86 ./knictl apply_workloads ${SITE_NAME} 2>&1 | tee ${WORKSPACE}/libvirt_workloads.log
87 STATUS=$?
88
89 if [ $STATUS -ne 0 ]; then
90     echo "Error applying workloads to libvirt"
91     exit 1
92 fi
93
94 popd
95 popd
96
97 exit $STATUS