Added Ampere_Openedge hardware type
[rec.git] / workflows / pod_create.sh
1 #!/bin/bash
2 # Copyright 2019 AT&T
3
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 #     http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 #
16
17 #Work-flow:
18 #
19 #0.  Get values for the environment variables
20
21 # The following must be provided.
22    HOST_IP=
23    CLOUDNAME=
24    ADMIN_PASSWD=
25
26 # The next set may be modified if necessary but are best left as-is
27    HTTPS_PORT=8443
28    API_PORT=15101
29         # Max time (in minutes) to wait for the remote-installer to return completed
30         # Currently 2.5 hours
31         MAX_TIME=150
32
33
34    # The rest should probably not be changed
35    WORKDIR=$(dirname $0)
36    BASEDIR=$WORKDIR
37    EXTERNALROOT=/data
38    NETWORK=host
39
40    # these will come from the Blueprint file and are available in "INPUT.yaml"
41    tr , '\012' < $WORKDIR/INPUT.yaml |tr -d '{}'|sed -e 's/^  *//' -e 's/: /=/' >/tmp/env
42    . /tmp/env
43    REC_ISO_IMAGE_NAME=$iso_primary
44    REC_PROVISIONING_ISO_NAME=$iso_secondary
45    INPUT_YAML_URL=$input_yaml
46    cat <<EOF
47    --------------------------------------------
48    WORKDIR is $WORKDIR
49    HOST_IP is $HOST_IP
50    EXTERNALROOT is $EXTERNALROOT
51    REC_ISO_IMAGE_NAME is $REC_ISO_IMAGE_NAME
52    REC_PROVISIONING_ISO_NAME is $REC_PROVISIONING_ISO_NAME
53    INPUT_YAML_URL is $INPUT_YAML_URL
54    --------------------------------------------
55 EOF
56
57 #1. Create a new directory to be used for holding the installation artifacts.
58
59    #create the base directory
60    mkdir -p $BASEDIR
61
62    #images sub-directory
63    mkdir -p $BASEDIR/images
64
65    #certificates sub-directory
66    mkdir -p $BASEDIR/certificates
67
68    #user configuration and cloud admin information
69    mkdir -p $BASEDIR/user-configs
70
71    #installation logs directory
72    mkdir -p $BASEDIR/installations
73
74 #2. Get REC golden image from REC Nexus artifacts and copy it to the images sub-directory under the directory created in (1).
75
76    cd $BASEDIR/images/
77    FILENAME=$(echo "${REC_ISO_IMAGE_NAME##*/}")
78    curl $REC_ISO_IMAGE_NAME > $FILENAME
79
80 #3. Get REC booting image from REC Nexus artifacts and copy it to the images sub-directory under the directory created in (1).
81
82    cd $BASEDIR/images/
83    FILENAME=$(echo "${REC_PROVISIONING_ISO_NAME##*/}")
84    curl $REC_PROVISIONING_ISO_NAME > $FILENAME
85
86 #4. Get the user-config.yaml file and admin_password file for the CD environment from the
87 #   cd-environments repo and copy it to the user-configs sub-directory under the directory
88 #   created in (1). Copy the files to a cloud-specific directory identified by the cloudname.
89
90    cd $BASEDIR/user-configs/
91    mkdir $CLOUDNAME
92    cd $CLOUDNAME
93    curl $INPUT_YAML_URL > user_config.yaml
94    ln user_config.yaml user_config.yml
95    echo $ADMIN_PASSWD > admin_passwd
96
97 #5. Checkout the remote-installer repo from LF
98
99    mkdir $BASEDIR/git
100    cd $BASEDIR/git
101    git clone https://gerrit.akraino.org/r/ta/remote-installer
102
103 #6. 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 $BASEDIR/git/remote-installer/test/certificates
115         ./create.sh
116         cp *.pem $BASEDIR/certificates
117
118 #7. Build the remote installer docker-image.
119     cd $BASEDIR/git/remote-installer/scripts/
120     echo $0: ./build.sh "$HTTPS_PORT" "$API_PORT"
121     ./build.sh "$HTTPS_PORT" "$API_PORT"
122
123 #8. Start the remote installer
124
125    cd $BASEDIR/git/remote-installer/scripts/
126    echo $0: ./start.sh -b "$EXTERNALROOT$BASEDIR" -e "$HOST_IP" -s "$HTTPS_PORT" -a "$API_PORT" -p "$ADMIN_PASSWD"
127    if ! ./start.sh -b "$EXTERNALROOT$BASEDIR" -e "$HOST_IP" -s "$HTTPS_PORT" -a "$API_PORT" -p "$ADMIN_PASSWD"
128    then
129         echo Failed to run workflow
130         exit 1
131    fi
132
133 #9. Wait for the remote installer to become running.
134 #   check every 30 seconds to see if it has it has a status of "running"
135
136     DOCKER_STATUS=""
137
138     while [ ${#DOCKER_STATUS} -eq 0 ]; do
139         sleep 30
140
141         DOCKER_ID=$(docker ps | grep remote-installer | awk ' {print $1}')
142         DOCKER_STATUS=$(docker ps -f status=running | grep $DOCKER_ID)
143     done
144
145 #10. Start the installation by sending the following http request to the installer API
146
147 #    POST url: https://localhost:$API_PORT/v1/installations
148 #    REQ body json- encoded
149 #    {
150 #        'cloudname': $CLOUDNAME,
151 #        'iso': $REC_ISO_IMAGE_NAME,
152 #        'provisioning-iso': $REC_PROVISIONING_ISO_NAME
153 #    }
154 #    REP body json-encoded
155 #    {
156 #        'uuid': $INSTALLATION_UUID
157 #    }
158
159 rec=$(basename $REC_ISO_IMAGE_NAME)
160 boot=$(basename $REC_PROVISIONING_ISO_NAME)
161 cat >/tmp/data <<EOF
162 {
163         "cloud-name": "$CLOUDNAME",
164         "iso": "$rec",
165         "provisioning-iso": "$boot"
166 }
167 EOF
168
169         # Get the IP address of the remote installer container
170         # RI_IP=$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' remote-installer)
171         RI_IP=$HOST_IP
172
173         echo "$0: Posting to https://$RI_IP:$API_PORT/v1/installations"
174     RESPONSE=$(
175         curl -k \
176                 --header "Content-Type: application/json" \
177                 -d @/tmp/data \
178                 --cert $BASEDIR/certificates/clientcert.pem \
179                         --key  $BASEDIR/certificates/clientkey.pem \
180                 https://$RI_IP:$API_PORT/v1/installations)
181         echo "$0: RESPONSE IS $RESPONSE"
182
183     INSTALLATION_UUID=$(echo $RESPONSE | jq -r ".uuid")
184
185 #11. Follow the progress of the installation by sending the following http request to the installer API
186
187 #    GET url: https://localhost:$API_PORT/v1/installations/$INSTALLATION_UUID
188 #
189 #    REP body json-encoded
190 #    {
191 #        'status': <ongoing|completed|failed>,
192 #        'description': <description>,
193 #        'percentage': <the progess precentage>
194 #    }
195 #
196 #
197
198 # check the status every minute until it has become "completed"
199 # (for a maximum of MAX_TIME minutes)
200
201     STATUS="ongoing"
202         NTIMES=$MAX_TIME
203     while [ "$STATUS" == "ongoing" -a $NTIMES -gt 0 ]; do
204         sleep 60
205         NTIMES=$((NTIMES - 1))
206         RESPONSE=$(curl -k --silent \
207                 --cert $BASEDIR/certificates/clientcert.pem \
208                         --key  $BASEDIR/certificates/clientkey.pem \
209                 https://$RI_IP:$API_PORT/v1/installations/$INSTALLATION_UUID/state)
210         STATUS=$(echo $RESPONSE | jq -r ".status")
211         PCT=$(   echo $RESPONSE | jq -r ".percentage")
212         DESCR=$( echo $RESPONSE | jq -r ".description")
213         echo "$(date): Status is $STATUS ($PCT) $DESCR"
214     done
215         if [ "$STATUS" == "ongoing" -a $NTIMES -eq 0 ]
216         then
217                 echo "Installation failed after $MAX_TIME minutes."
218                 exit 1
219         fi
220         echo "Installation complete!"
221
222 #12. When installation is completed stop the remote installer.
223
224     cd $BASEDIR/git/remote-installer/scripts/
225     ./stop.sh
226
227         exit 0