iec: view: Stop filtering out the sonar job
[ci-management.git] / jjb / shell / ta-install.sh
1 #!/bin/bash -e
2
3 # Copyright 2019 AT&T
4 # Copyright 2020 ENEA
5
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 #     http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 #
18
19 _yaml2json() {
20     python -c 'import json,sys,yaml; json.dump(yaml.safe_load(sys.stdin.read()), sys.stdout)'
21 }
22
23 #Work-flow:
24
25 #0. Get values for the environment variables
26
27 # The following must be provided.
28     REC_ISO_IMAGE_NAME="${REC_ISO_IMAGE_NAME:?'Must be defined!'}"
29     REC_PROVISIONING_ISO_NAME="${REC_PROVISIONING_ISO_NAME:?'Must be defined!'}"
30     REC_USER_CONFIG="${REC_USER_CONFIG:?'Must be defined!'}"
31
32 # The next set may be modified if necessary but are best left as-is
33     ADMIN_PASSWD="admin"
34     POD_NAME="REC-$BUILD_ID"
35     HTTPS_PORT=8443
36     API_PORT=15101
37     # Max time (in minutes) to wait for the remote-installer to return completed
38     # Currently 4 hours
39     MAX_TIME=240
40
41     # These should probably not be changed
42     WORKDIR="$(pwd)"
43     BASEDIR="$WORKDIR/rec_basedir"
44
45
46 cat <<EOF
47     --------------------------------------------
48     WORKDIR is $WORKDIR
49     BASEDIR is $BASEDIR
50     POD_NAME is $POD_NAME
51     REC_ISO_IMAGE_NAME is $REC_ISO_IMAGE_NAME
52     REC_PROVISIONING_ISO_NAME is $REC_PROVISIONING_ISO_NAME
53     REC_USER_CONFIG is $REC_USER_CONFIG
54     --------------------------------------------
55 EOF
56
57 # Create cleanup hook
58 _cleanup() {
59     exit_status=$?
60     set +e
61     sudo chown -R jenkins:jenkins "$WORKDIR"
62     docker cp 'remote-installer':/var/log/remote-installer.log "$BASEDIR/"
63     docker rm -f 'remote-installer'
64     trap - EXIT ERR HUP INT QUIT TERM
65     exit $exit_status
66 }
67 trap _cleanup EXIT ERR HUP INT QUIT TERM
68
69 # Enable debugging
70     set -x
71
72 #1. Create a new directory to be used for holding the installation artifacts.
73
74     # Clean workspace from previous run
75     sudo chown -R jenkins:jenkins "$WORKDIR"
76     rm -rf "$BASEDIR"
77
78     # Create the base directory structure
79     mkdir -p "$BASEDIR/images" \
80              "$BASEDIR/certificates" \
81              "$BASEDIR/installations" \
82              "$BASEDIR/user-configs/$POD_NAME"
83
84 #2. Get REC golden and bootcd images from REC Nexus artifacts
85
86     cd "$BASEDIR/images/"
87     curl -sL "$REC_ISO_IMAGE_NAME"        > "$(basename "$REC_ISO_IMAGE_NAME")"
88     curl -sL "$REC_PROVISIONING_ISO_NAME" > "$(basename "$REC_PROVISIONING_ISO_NAME")"
89
90 #3. Get the user-config.yaml file and admin_password file for the CD environment from the
91 #   cd-environments repo and copy it to the user-configs sub-directory under the directory
92 #   created in (1). Copy the files to a cloud-specific directory identified by the cloudname.
93
94     cd "$BASEDIR/user-configs/$POD_NAME"
95     curl -sL "$REC_USER_CONFIG" > user_config.yaml
96     echo "$ADMIN_PASSWD" > admin_passwd
97
98     FIRST_CONTROLLER_IP="$(_yaml2json < user_config.yaml | \
99         jq -r '.hosts[]|select(.service_profiles[]|contains("caas_master", "controller"))|.hwmgmt.address' | \
100         sort | head -1 )"
101     HOST_IP="$(ip route get "$FIRST_CONTROLLER_IP" | grep -Pzo 'src\s+\K([^\s]*)' )"
102
103 #4. Copy the sever certificates, the client certificates in addition to CA certificate to
104 #  the certificates sub-directory under the directory created in (1).
105 #   The following certificates are expected to be available in the directory:
106 #
107 #   cacert.pem: The CA certificate
108 #   servercert.pem: The server certificate signed by the CA
109 #   serverkey.pem: The server key
110 #   clientcert.pem: The client certificate signed by the CA
111 #   clientkey.pem: The client key
112 #
113
114     cd "$WORKDIR/git/remote-installer/test/certificates"
115     ./create.sh
116     cp -a {ca,client,server}{cert,key}.pem "${BASEDIR}/certificates/"
117
118 #5. Build the remote installer docker-image.
119     cd "$WORKDIR/git/remote-installer/scripts/"
120     ./build.sh "$HTTPS_PORT" "$API_PORT"
121
122 #6. Start the remote installer
123
124     cd "$WORKDIR/git/remote-installer/scripts/"
125     if ! ./start.sh -b "$BASEDIR" -e "$HOST_IP" -s "$HTTPS_PORT" -a "$API_PORT" -p "$ADMIN_PASSWD"
126     then
127         echo 'Failed to run workflow.'
128         exit 1
129     fi
130
131 #7. Wait for the remote installer to become running.
132 #   check every 30 seconds to see if it has it has a status of "running"
133
134     DOCKER_STATUS=""
135
136     while [ ${#DOCKER_STATUS} -eq 0 ]; do
137         sleep 30
138         DOCKER_ID=$(docker ps | grep 'remote-installer' | awk '{print $1}')
139         DOCKER_STATUS=$(docker ps -f status=running | grep "$DOCKER_ID")
140     done
141
142 #8. Start the installation by sending the following http request to the installer API
143
144 #    POST url: https://localhost:$API_PORT/v1/installations
145 #    REQ body json- encoded
146 #    {
147 #        'cloudname': $POD_NAME,
148 #        'iso': $REC_ISO_IMAGE_NAME,
149 #        'provisioning-iso': $REC_PROVISIONING_ISO_NAME
150 #    }
151 #    REP body json-encoded
152 #    {
153 #        'uuid': $INSTALLATION_UUID
154 #    }
155
156 INSTALL_ISO="$(basename "$REC_ISO_IMAGE_NAME")"
157 BOOT_ISO="$(basename "$REC_PROVISIONING_ISO_NAME")"
158 cat >rec_request.json <<EOF
159 {
160     "cloud-name": "$POD_NAME",
161     "iso": "$INSTALL_ISO",
162     "provisioning-iso": "$BOOT_ISO"
163 }
164 EOF
165
166     # Get the IP address of the remote installer container
167     RI_IP=$HOST_IP
168
169     RESPONSE=$(curl -k --silent \
170                     --header "Content-Type: application/json" \
171                     -d "@rec_request.json" \
172                     --cert "$BASEDIR/certificates/clientcert.pem" \
173                     --key  "$BASEDIR/certificates/clientkey.pem" \
174                     "https://${RI_IP}:${API_PORT}/v1/installations")
175     echo "$0: RESPONSE IS $RESPONSE"
176
177     INSTALLATION_UUID="$(echo "$RESPONSE" | jq -r ".uuid")"
178
179 #9. Follow the progress of the installation by sending the following http request to the installer API
180
181 #    GET url: https://localhost:$API_PORT/v1/installations/$INSTALLATION_UUID
182 #
183 #    REP body json-encoded
184 #    {
185 #        'status': <ongoing|completed|failed>,
186 #        'description': <description>,
187 #        'percentage': <the progess precentage>
188 #    }
189 #
190 #
191
192 # check the status every minute until it has become "completed"
193 # (for a maximum of MAX_TIME minutes)
194
195     STATUS="ongoing"
196     NTIMES=$MAX_TIME
197     while [ "$STATUS" == "ongoing" -a $NTIMES -gt 0 ]; do
198         sleep 60
199         NTIMES=$((NTIMES - 1))
200         RESPONSE=$(curl -k --silent \
201                         --cert "$BASEDIR/certificates/clientcert.pem" \
202                         --key  "$BASEDIR/certificates/clientkey.pem" \
203                         "https://${RI_IP}:${API_PORT}/v1/installations/$INSTALLATION_UUID/state")
204         STATUS="$(echo "$RESPONSE" | jq -r ".status")"
205         PCT="$(   echo "$RESPONSE" | jq -r ".percentage")"
206         DESCR="$( echo "$RESPONSE" | jq -r ".description")"
207         echo "$(date): Status is $STATUS ($PCT) $DESCR"
208     done
209
210     if [ "$STATUS" == "ongoing" -a $NTIMES -le 0 ]; then
211         echo "Installation failed after $MAX_TIME minutes."
212         echo "RESPONSE: $RESPONSE"
213         exit 1
214     elif [ "$STATUS" != "completed" ]; then
215         echo "Installation failed."
216         echo "RESPONSE: $RESPONSE"
217         exit 1
218     fi
219
220     echo "Installation complete!"
221
222 #10. Done
223     exit 0