[fuel] Disable iso download for master branch
[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>" >&2
16     echo "[-v] Virtualized deployment" >&2
17 }
18
19 info ()  {
20     logger -s -t "fetch_os_creds.info" "$*"
21 }
22
23
24 error () {
25     logger -s -t "fetch_os_creds.error" "$*"
26     exit 1
27 }
28
29
30 verify_connectivity() {
31     local ip=$1
32     info "Verifying connectivity to $ip..."
33     for i in $(seq 0 10); do
34         if ping -c 1 -W 1 $ip > /dev/null; then
35             info "$ip is reachable!"
36             return 0
37         fi
38         sleep 1
39     done
40     error "Can not talk to $ip."
41 }
42
43
44 swap_to_public() {
45     if [ "$1" != "" ]; then
46         info "Exchanging keystone public IP in rc file to $public_ip"
47         sed -i  "/OS_AUTH_URL/c\export OS_AUTH_URL=\'$public_ip'" $dest_path
48         sed -i 's/internalURL/publicURL/g' $dest_path
49     fi
50 }
51
52
53 : ${DEPLOY_TYPE:=''}
54
55 #Get options
56 while getopts ":d:i:a:h:v" optchar; do
57     case "${optchar}" in
58         d) dest_path=${OPTARG} ;;
59         i) installer_type=${OPTARG} ;;
60         a) installer_ip=${OPTARG} ;;
61         v) DEPLOY_TYPE="virt" ;;
62         *) echo "Non-option argument: '-${OPTARG}'" >&2
63            usage
64            exit 2
65            ;;
66     esac
67 done
68
69 # set vars from env if not provided by user as options
70 dest_path=${dest_path:-$HOME/opnfv-openrc.sh}
71 installer_type=${installer_type:-$INSTALLER_TYPE}
72 installer_ip=${installer_ip:-$INSTALLER_IP}
73
74 if [ -z $dest_path ] || [ -z $installer_type ] || [ -z $installer_ip ]; then
75     usage
76     exit 2
77 fi
78
79 # Checking if destination path is valid
80 if [ -d $dest_path ]; then
81     error "Please provide the full destination path for the credentials file including the filename"
82 else
83     # Check if we can create the file (e.g. path is correct)
84     touch $dest_path || error "Cannot create the file specified. Check that the path is correct and run the script again."
85 fi
86
87
88 ssh_options="-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no"
89
90 # Start fetching the files
91 if [ "$installer_type" == "fuel" ]; then
92     #ip_fuel="10.20.0.2"
93     verify_connectivity $installer_ip
94
95     env=$(sshpass -p r00tme ssh 2>/dev/null $ssh_options root@${installer_ip} \
96         'fuel env'|grep operational|head -1|awk '{print $1}') &> /dev/null
97     if [ -z $env ]; then
98         error "No operational environment detected in Fuel"
99     fi
100     env_id="${FUEL_ENV:-$env}"
101
102     # Check if controller is alive (online='True')
103     controller_ip=$(sshpass -p r00tme ssh 2>/dev/null $ssh_options root@${installer_ip} \
104         "fuel node --env ${env_id} | grep controller | grep 'True\|  1' | awk -F\| '{print \$5}' | head -1" | \
105         sed 's/ //g') &> /dev/null
106
107     if [ -z $controller_ip ]; then
108         error "The controller $controller_ip is not up. Please check that the POD is correctly deployed."
109     fi
110
111     info "Fetching rc file from controller $controller_ip..."
112     sshpass -p r00tme ssh 2>/dev/null $ssh_options root@${installer_ip} \
113         "scp $ssh_options ${controller_ip}:/root/openrc ." &> /dev/null
114     sshpass -p r00tme scp 2>/dev/null $ssh_options root@${installer_ip}:~/openrc $dest_path &> /dev/null
115
116     #This file contains the mgmt keystone API, we need the public one for our rc file
117     admin_ip=$(cat $dest_path | grep "OS_AUTH_URL" | sed 's/^.*\=//' | sed "s/^\([\"']\)\(.*\)\1\$/\2/g" | sed s'/\/$//')
118     public_ip=$(sshpass -p r00tme ssh $ssh_options root@${installer_ip} \
119         "ssh ${controller_ip} 'source openrc; openstack endpoint list'" \
120         | grep keystone | grep public | sed 's/ /\n/g' | grep ^http | head -1) &> /dev/null
121         #| grep http | head -1 | cut -d '|' -f 4 | sed 's/v1\/.*/v1\//' | sed 's/ //g') &> /dev/null
122     #NOTE: this is super ugly sed 's/v1\/.*/v1\//'OS_AUTH_URL
123     # but sometimes the output of endpoint-list is like this: http://172.30.9.70:8004/v1/%(tenant_id)s
124     # Fuel virtual need a fix
125
126     #convert to v3 URL
127     auth_url=$(cat $dest_path|grep AUTH_URL)
128     if [[ -z `echo $auth_url |grep v3` ]]; then
129         auth_url=$(echo $auth_url |sed "s|'$|v3&|")
130     fi
131     sed -i '/AUTH_URL/d' $dest_path
132     echo $auth_url >> $dest_path
133
134 elif [ "$installer_type" == "apex" ]; then
135     verify_connectivity $installer_ip
136
137     # The credentials file is located in the Instack VM (192.0.2.1)
138     # NOTE: This might change for bare metal deployments
139     info "Fetching rc file from Instack VM $installer_ip..."
140     if [ -f /root/.ssh/id_rsa ]; then
141         chmod 600 /root/.ssh/id_rsa
142     fi
143     sudo scp $ssh_options root@$installer_ip:/home/stack/overcloudrc.v3 $dest_path
144
145 elif [ "$installer_type" == "compass" ]; then
146     verify_connectivity $installer_ip
147     controller_ip=$(sshpass -p'root' ssh 2>/dev/null $ssh_options root@${installer_ip} \
148         'mysql -ucompass -pcompass -Dcompass -e"select *  from cluster;"' \
149         | awk -F"," '{for(i=1;i<NF;i++)if($i~/\"127.0.0.1\"/) {print $(i+2);break;}}'  \
150         | grep -oP "\d+.\d+.\d+.\d+")
151
152     if [ -z $controller_ip ]; then
153         error "The controller $controller_ip is not up. Please check that the POD is correctly deployed."
154     fi
155
156     info "Fetching rc file from controller $controller_ip..."
157     sshpass -p root ssh 2>/dev/null $ssh_options root@${installer_ip} \
158         "scp $ssh_options ${controller_ip}:/opt/admin-openrc.sh ." &> /dev/null
159     sshpass -p root scp 2>/dev/null $ssh_options root@${installer_ip}:~/admin-openrc.sh $dest_path &> /dev/null
160
161     info "This file contains the mgmt keystone API, we need the public one for our rc file"
162
163     if grep "OS_AUTH_URL.*v2" $dest_path > /dev/null 2>&1 ; then
164         public_ip=$(sshpass -p root ssh $ssh_options root@${installer_ip} \
165             "ssh ${controller_ip} 'source /opt/admin-openrc.sh; openstack endpoint show identity '" \
166             | grep publicurl | awk '{print $4}')
167     else
168         public_ip=$(sshpass -p root ssh $ssh_options root@${installer_ip} \
169             "ssh ${controller_ip} 'source /opt/admin-openrc.sh; \
170                  openstack endpoint list --interface public --service identity '" \
171             | grep identity | awk '{print $14}')
172     fi
173     info "public_ip: $public_ip"
174     swap_to_public $public_ip
175
176
177 elif [ "$installer_type" == "joid" ]; then
178     # do nothing...for the moment
179     # we can either do a scp from the jumphost or use the -v option to transmit the param to the docker file
180     echo "Do nothing, creds will be provided through volume option at docker creation for joid"
181
182 elif [ "$installer_type" == "foreman" ]; then
183     #ip_foreman="172.30.10.73"
184     controller="oscontroller1.opnfv.com"
185     verify_connectivity $installer_ip
186
187     # Check if controller is alive (here is more difficult to get the ip from a command like "fuel node")
188     sshpass -p vagrant ssh $ssh_options root@${installer_ip} \
189         "sshpass -p Op3nStack ssh $ssh_options root@${controller} 'ls'" &> /dev/null
190     if [ $? -ne 0 ]; then
191         error "The controller ${controller} is not up. Please check that the POD is correctly deployed."
192     fi
193
194     info "Fetching openrc from a Foreman Controller '${controller}'..."
195     sshpass -p vagrant ssh $ssh_options root@${installer_ip} \
196         "sshpass -p Op3nStack scp $ssh_options root@${controller}:~/keystonerc_admin ." &> /dev/null
197     sshpass -p vagrant scp $ssh_options root@${installer_ip}:~/keystonerc_admin $dest_path &> /dev/null
198
199     #This file contains the mgmt keystone API, we need the public one for our rc file
200     admin_ip=$(cat $dest_path | grep "OS_AUTH_URL" | sed 's/^.*\=//' | sed "s/^\([\"']\)\(.*\)\1\$/\2/g" | sed s'/\/$//')
201     public_ip=$(sshpass -p vagrant ssh $ssh_options root@${installer_ip} \
202         "sshpass -p Op3nStack ssh $ssh_options root@${controller} \
203         'source keystonerc_admin;keystone endpoint-list'" \
204         | grep $admin_ip | sed 's/ /\n/g' | grep ^http | head -1) &> /dev/null
205
206 elif [ "$installer_type" == "daisy" ]; then
207     verify_connectivity $installer_ip
208     cluster=$(sshpass -p r00tme ssh 2>/dev/null $ssh_options root@${installer_ip} \
209             "source ~/daisyrc_admin; daisy cluster-list"|grep active|head -1|awk -F "|" '{print $3}') &> /dev/null
210     if [ -z $cluster ]; then
211         echo "No active cluster detected in daisy"
212         exit 1
213     fi
214
215     sshpass -p r00tme scp 2>/dev/null $ssh_options root@${installer_ip}:/etc/kolla/admin-openrc.sh $dest_path &> /dev/null
216
217 else
218     error "Installer $installer is not supported by this script"
219 fi
220
221
222 if [ ! -f $dest_path ]; then
223     error "There has been an error retrieving the credentials"
224 fi
225
226 echo "-------- Credentials: --------"
227 cat $dest_path