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