Apex: Fix OS credentials fetch
[releng.git] / utils / fetch_os_creds.sh
1 #!/bin/bash
2 ##############################################################################
3 # Copyright (c) 2015 Ericsson AB and others.
4 # jose.lausuch@ericsson.com
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 set -o errexit
11 set -o nounset
12 set -o pipefail
13
14 usage() {
15     echo "usage: $0 [-v] -d <destination> -i <installer_type> -a <installer_ip> [-o <os_cacert>] [-s <ssh_key>]" >&2
16     echo "[-v] Virtualized deployment" >&2
17     echo "[-s <ssh_key>] Path to ssh key. For MCP deployments only" >&2
18 }
19
20 info ()  {
21     logger -s -t "fetch_os_creds.info" "$*"
22 }
23
24
25 error () {
26     logger -s -t "fetch_os_creds.error" "$*"
27     exit 1
28 }
29
30
31 verify_connectivity() {
32     local ip=$1
33     info "Verifying connectivity to $ip..."
34     for i in $(seq 0 10); do
35         if ping -c 1 -W 1 $ip > /dev/null; then
36             info "$ip is reachable!"
37             return 0
38         fi
39         sleep 1
40     done
41     error "Can not talk to $ip."
42 }
43
44
45 swap_to_public() {
46     if [ "$1" != "" ]; then
47         info "Exchanging keystone public IP in rc file to $public_ip"
48         sed -i  "/OS_AUTH_URL/c\export OS_AUTH_URL=\'$public_ip'" $dest_path
49         sed -i 's/internalURL/publicURL/g' $dest_path
50     fi
51 }
52
53
54 : ${DEPLOY_TYPE:=''}
55
56 #Get options
57 while getopts ":d:i:a:h:s:o:v" optchar; do
58     case "${optchar}" in
59         d) dest_path=${OPTARG} ;;
60         i) installer_type=${OPTARG} ;;
61         a) installer_ip=${OPTARG} ;;
62         s) ssh_key=${OPTARG} ;;
63         o) os_cacert=${OPTARG} ;;
64         v) DEPLOY_TYPE="virt" ;;
65         *) echo "Non-option argument: '-${OPTARG}'" >&2
66            usage
67            exit 2
68            ;;
69     esac
70 done
71
72 # set vars from env if not provided by user as options
73 dest_path=${dest_path:-$HOME/opnfv-openrc.sh}
74 os_cacert=${os_cacert:-$HOME/os_cacert}
75 installer_type=${installer_type:-$INSTALLER_TYPE}
76 installer_ip=${installer_ip:-$INSTALLER_IP}
77
78 if [ -z $dest_path ] || [ -z $installer_type ] || [ -z $installer_ip ]; then
79     usage
80     exit 2
81 fi
82
83 # Checking if destination path is valid
84 if [ -d $dest_path ]; then
85     error "Please provide the full destination path for the credentials file including the filename"
86 else
87     # Check if we can create the file (e.g. path is correct)
88     touch $dest_path || error "Cannot create the file specified. Check that the path is correct and run the script again."
89 fi
90
91
92 ssh_options="-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no"
93
94 # Start fetching the files
95 info "Fetching rc file..."
96 if [ "$installer_type" == "fuel" ]; then
97     verify_connectivity "${installer_ip}"
98     # stable/danube uses old Fuel, requires user/pass; new Fuel uses keypair
99     if [[ ! "${BRANCH}" =~ "danube" ]]; then
100         ssh_user="ubuntu"
101         ssh_key="${ssh_key:-$SSH_KEY}"
102         if [ -z "$ssh_key" ] || [ ! -f "$ssh_key" ]; then
103             error "Please provide path to existing ssh key for mcp deployment."
104             exit 2
105         fi
106         ssh_options+=" -i ${ssh_key}"
107
108         # retrieving controller vip
109         controller_ip=$(ssh 2>/dev/null ${ssh_options} "${ssh_user}@${installer_ip}" \
110             "sudo salt --out yaml 'ctl*' pillar.get _param:openstack_control_address | \
111                 awk '{print \$2; exit}'") &> /dev/null
112
113         info "... from controller ${controller_ip} ..."
114         ssh ${ssh_options} "${ssh_user}@${controller_ip}" \
115             "sudo cat /root/keystonercv3" > "${dest_path}"
116
117         if [[ ! "${BUILD_TAG}" =~ 'virtual' ]]; then
118             ssh ${ssh_options} "${ssh_user}@${installer_ip}" \
119                 "cat /etc/ssl/certs/os_cacert" > "${os_cacert}"
120         fi
121     else
122         env=$(sshpass -p r00tme ssh 2>/dev/null ${ssh_options} root@${installer_ip} \
123             'fuel env'|grep operational|head -1|awk '{print $1}') &> /dev/null
124         if [ -z $env ]; then
125             error "No operational environment detected in Fuel"
126         fi
127         env_id="${FUEL_ENV:-$env}"
128
129         # Check if controller is alive (online='True')
130         controller_ip=$(sshpass -p r00tme ssh 2>/dev/null ${ssh_options} root@${installer_ip} \
131             "fuel node --env ${env_id} | grep controller | grep 'True\|  1' | awk -F\| '{print \$5}' | head -1" | \
132             sed 's/ //g') &> /dev/null
133
134         if [ -z $controller_ip ]; then
135             error "The controller $controller_ip is not up. Please check that the POD is correctly deployed."
136         fi
137
138         info "... from controller $controller_ip..."
139         sshpass -p r00tme ssh 2>/dev/null ${ssh_options} root@${installer_ip} \
140             "scp ${ssh_options} ${controller_ip}:/root/openrc ." &> /dev/null
141         sshpass -p r00tme scp 2>/dev/null ${ssh_options} root@${installer_ip}:~/openrc $dest_path &> /dev/null
142     fi
143     #convert to v3 URL
144     auth_url=$(cat $dest_path|grep AUTH_URL)
145     if [[ -z `echo $auth_url |grep v3` ]]; then
146         auth_url=$(echo $auth_url |sed "s|'$|v3&|")
147     fi
148     sed -i '/AUTH_URL/d' $dest_path
149     echo $auth_url >> $dest_path
150
151 elif [ "$installer_type" == "apex" ]; then
152     if ! ipcalc -c $installer_ip; then
153       installer_ip=$(sudo virsh domifaddr undercloud | grep -Eo '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}')
154       if [ -z "$installer_ip" ] || ! $(ipcalc -c $installer_ip); then
155         echo "Unable to find valid IP for Apex undercloud: ${installer_ip}"
156         exit 1
157       fi
158     fi
159     verify_connectivity $installer_ip
160
161     # The credentials file is located in the Instack VM (192.0.2.1)
162     # NOTE: This might change for bare metal deployments
163     info "... from Instack VM $installer_ip..."
164     if [ -f /root/.ssh/id_rsa ]; then
165         chmod 600 /root/.ssh/id_rsa
166     fi
167
168     if [ "${BRANCH}" == "stable/fraser" ]; then
169       rc_file=overcloudrc.v3
170     else
171       rc_file=overcloudrc
172     fi
173     sudo scp $ssh_options root@$installer_ip:/home/stack/${rc_file} $dest_path
174
175 elif [ "$installer_type" == "compass" ]; then
176     if [ "${BRANCH}" == "stable/danube" ]; then
177         verify_connectivity $installer_ip
178         controller_ip=$(sshpass -p'root' ssh 2>/dev/null $ssh_options root@${installer_ip} \
179             'mysql -ucompass -pcompass -Dcompass -e"select *  from cluster;"' \
180             | awk -F"," '{for(i=1;i<NF;i++)if($i~/\"127.0.0.1\"/) {print $(i+2);break;}}'  \
181             | grep -oP "\d+.\d+.\d+.\d+")
182
183         if [ -z $controller_ip ]; then
184             error "The controller $controller_ip is not up. Please check that the POD is correctly deployed."
185         fi
186
187         info "... from controller $controller_ip..."
188         sshpass -p root ssh 2>/dev/null $ssh_options root@${installer_ip} \
189             "scp $ssh_options ${controller_ip}:/opt/admin-openrc.sh ." &> /dev/null
190         sshpass -p root scp 2>/dev/null $ssh_options root@${installer_ip}:~/admin-openrc.sh $dest_path &> /dev/null
191
192         info "This file contains the mgmt keystone API, we need the public one for our rc file"
193
194         if grep "OS_AUTH_URL.*v2" $dest_path > /dev/null 2>&1 ; then
195             public_ip=$(sshpass -p root ssh $ssh_options root@${installer_ip} \
196                 "ssh ${controller_ip} 'source /opt/admin-openrc.sh; openstack endpoint show identity '" \
197                 | grep publicurl | awk '{print $4}')
198         else
199             public_ip=$(sshpass -p root ssh $ssh_options root@${installer_ip} \
200                 "ssh ${controller_ip} 'source /opt/admin-openrc.sh; \
201                      openstack endpoint list --interface public --service identity '" \
202                 | grep identity | awk '{print $14}')
203         fi
204         info "public_ip: $public_ip"
205         swap_to_public $public_ip
206     else
207         sudo docker cp compass-tasks:/opt/openrc $dest_path &> /dev/null
208         sudo chown $(whoami):$(whoami) $dest_path
209         sudo docker cp compass-tasks:/opt/os_cacert $os_cacert
210     fi
211
212 elif [ "$installer_type" == "joid" ]; then
213     # do nothing...for the moment
214     # we can either do a scp from the jumphost or use the -v option to transmit the param to the docker file
215     info "Do nothing, creds will be provided through volume option at docker creation for joid"
216
217 elif [ "$installer_type" == "foreman" ]; then
218     #ip_foreman="172.30.10.73"
219     controller="oscontroller1.opnfv.com"
220     verify_connectivity $installer_ip
221
222     # Check if controller is alive (here is more difficult to get the ip from a command like "fuel node")
223     sshpass -p vagrant ssh $ssh_options root@${installer_ip} \
224         "sshpass -p Op3nStack ssh $ssh_options root@${controller} 'ls'" &> /dev/null
225     if [ $? -ne 0 ]; then
226         error "The controller ${controller} is not up. Please check that the POD is correctly deployed."
227     fi
228
229     info "Fetching openrc from a Foreman Controller '${controller}'..."
230     sshpass -p vagrant ssh $ssh_options root@${installer_ip} \
231         "sshpass -p Op3nStack scp $ssh_options root@${controller}:~/keystonerc_admin ." &> /dev/null
232     sshpass -p vagrant scp $ssh_options root@${installer_ip}:~/keystonerc_admin $dest_path &> /dev/null
233
234     #This file contains the mgmt keystone API, we need the public one for our rc file
235     admin_ip=$(cat $dest_path | grep "OS_AUTH_URL" | sed 's/^.*\=//' | sed "s/^\([\"']\)\(.*\)\1\$/\2/g" | sed s'/\/$//')
236     public_ip=$(sshpass -p vagrant ssh $ssh_options root@${installer_ip} \
237         "sshpass -p Op3nStack ssh $ssh_options root@${controller} \
238         'source keystonerc_admin;keystone endpoint-list'" \
239         | grep $admin_ip | sed 's/ /\n/g' | grep ^http | head -1) &> /dev/null
240
241 elif [ "$installer_type" == "daisy" ]; then
242     verify_connectivity $installer_ip
243     cluster=$(sshpass -p r00tme ssh 2>/dev/null $ssh_options root@${installer_ip} \
244             "source ~/daisyrc_admin; daisy cluster-list"|grep active|head -1|awk -F "|" '{print $3}') &> /dev/null
245     if [ -z $cluster ]; then
246         echo "No active cluster detected in daisy"
247         exit 1
248     fi
249
250     sshpass -p r00tme scp 2>/dev/null $ssh_options root@${installer_ip}:/etc/kolla/admin-openrc.sh $dest_path &> /dev/null
251
252 elif ["$installer_type" == "osa"]; then
253     # Get RC file from control server
254     filename=$(ssh -o StrictHostKeyChecking=no root@${controller_ip} find /var/lib/lxc/controller00_nova_api_placement_container-* -name openrc)
255     scp root@${controller_ip}:${filename} ${destpath}
256 else
257     error "Installer $installer is not supported by this script"
258 fi
259
260
261 if [ ! -f $dest_path ]; then
262     error "There has been an error retrieving the credentials"
263 fi
264
265 echo "-------- Credentials: --------"
266 cat $dest_path