285f838db510a2c628ea9944f41195e502236d5a
[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> [-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: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         v) DEPLOY_TYPE="virt" ;;
64         *) echo "Non-option argument: '-${OPTARG}'" >&2
65            usage
66            exit 2
67            ;;
68     esac
69 done
70
71 # set vars from env if not provided by user as options
72 dest_path=${dest_path:-$HOME/opnfv-openrc.sh}
73 installer_type=${installer_type:-$INSTALLER_TYPE}
74 installer_ip=${installer_ip:-$INSTALLER_IP}
75 if [ "${installer_type}" == "fuel" ] && [ "${BRANCH}" == "master" ]; then
76     installer_ip=${SALT_MASTER_IP}
77 fi
78
79 if [ -z $dest_path ] || [ -z $installer_type ] || [ -z $installer_ip ]; then
80     usage
81     exit 2
82 fi
83
84 # Checking if destination path is valid
85 if [ -d $dest_path ]; then
86     error "Please provide the full destination path for the credentials file including the filename"
87 else
88     # Check if we can create the file (e.g. path is correct)
89     touch $dest_path || error "Cannot create the file specified. Check that the path is correct and run the script again."
90 fi
91
92
93 ssh_options="-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no"
94
95 # Start fetching the files
96 if [ "$installer_type" == "fuel" ]; then
97     verify_connectivity $installer_ip
98     if [ "${BRANCH}" == "master" ]; then
99         ssh_key=${ssh_key:-$SSH_KEY}
100         if [ -z $ssh_key ] || [ ! -f $ssh_key ]; then
101             error "Please provide path to existing ssh key for mcp deployment."
102             exit 2
103         fi
104         ssh_options+=" -i ${ssh_key}"
105
106         # retrieving controller vip
107         controller_ip=$(ssh 2>/dev/null ${ssh_options} ubuntu@${installer_ip} \
108             "sudo salt --out txt 'ctl01*' pillar.get _param:openstack_control_address | awk '{print \$2}'" | \
109             sed 's/ //g') &> /dev/null
110
111         info "Fetching rc file from controller $controller_ip..."
112         ssh ${ssh_options} ubuntu@${controller_ip} "sudo cat /root/keystonercv3" > $dest_path
113     else
114         #ip_fuel="10.20.0.2"
115         env=$(sshpass -p r00tme ssh 2>/dev/null ${ssh_options} root@${installer_ip} \
116             'fuel env'|grep operational|head -1|awk '{print $1}') &> /dev/null
117         if [ -z $env ]; then
118             error "No operational environment detected in Fuel"
119         fi
120         env_id="${FUEL_ENV:-$env}"
121
122         # Check if controller is alive (online='True')
123         controller_ip=$(sshpass -p r00tme ssh 2>/dev/null ${ssh_options} root@${installer_ip} \
124             "fuel node --env ${env_id} | grep controller | grep 'True\|  1' | awk -F\| '{print \$5}' | head -1" | \
125             sed 's/ //g') &> /dev/null
126
127         if [ -z $controller_ip ]; then
128             error "The controller $controller_ip is not up. Please check that the POD is correctly deployed."
129         fi
130
131         info "Fetching rc file from controller $controller_ip..."
132         sshpass -p r00tme ssh 2>/dev/null ${ssh_options} root@${installer_ip} \
133             "scp ${ssh_options} ${controller_ip}:/root/openrc ." &> /dev/null
134         sshpass -p r00tme scp 2>/dev/null ${ssh_options} root@${installer_ip}:~/openrc $dest_path &> /dev/null
135     fi
136     #convert to v3 URL
137     auth_url=$(cat $dest_path|grep AUTH_URL)
138     if [[ -z `echo $auth_url |grep v3` ]]; then
139         auth_url=$(echo $auth_url |sed "s|'$|v3&|")
140     fi
141     sed -i '/AUTH_URL/d' $dest_path
142     echo $auth_url >> $dest_path
143
144 elif [ "$installer_type" == "apex" ]; then
145     verify_connectivity $installer_ip
146
147     # The credentials file is located in the Instack VM (192.0.2.1)
148     # NOTE: This might change for bare metal deployments
149     info "Fetching rc file from Instack VM $installer_ip..."
150     if [ -f /root/.ssh/id_rsa ]; then
151         chmod 600 /root/.ssh/id_rsa
152     fi
153     sudo scp $ssh_options root@$installer_ip:/home/stack/overcloudrc.v3 $dest_path
154
155 elif [ "$installer_type" == "compass" ]; then
156     if [ "${BRANCH}" == "master" ]; then
157         sudo docker cp compass-tasks:/opt/openrc $dest_path &> /dev/null
158         sudo chown $(whoami):$(whoami) $dest_path
159     else
160         verify_connectivity $installer_ip
161         controller_ip=$(sshpass -p'root' ssh 2>/dev/null $ssh_options root@${installer_ip} \
162             'mysql -ucompass -pcompass -Dcompass -e"select *  from cluster;"' \
163             | awk -F"," '{for(i=1;i<NF;i++)if($i~/\"127.0.0.1\"/) {print $(i+2);break;}}'  \
164             | grep -oP "\d+.\d+.\d+.\d+")
165
166         if [ -z $controller_ip ]; then
167             error "The controller $controller_ip is not up. Please check that the POD is correctly deployed."
168         fi
169
170         info "Fetching rc file from controller $controller_ip..."
171         sshpass -p root ssh 2>/dev/null $ssh_options root@${installer_ip} \
172             "scp $ssh_options ${controller_ip}:/opt/admin-openrc.sh ." &> /dev/null
173         sshpass -p root scp 2>/dev/null $ssh_options root@${installer_ip}:~/admin-openrc.sh $dest_path &> /dev/null
174
175         info "This file contains the mgmt keystone API, we need the public one for our rc file"
176
177         if grep "OS_AUTH_URL.*v2" $dest_path > /dev/null 2>&1 ; then
178             public_ip=$(sshpass -p root ssh $ssh_options root@${installer_ip} \
179                 "ssh ${controller_ip} 'source /opt/admin-openrc.sh; openstack endpoint show identity '" \
180                 | grep publicurl | awk '{print $4}')
181         else
182             public_ip=$(sshpass -p root ssh $ssh_options root@${installer_ip} \
183                 "ssh ${controller_ip} 'source /opt/admin-openrc.sh; \
184                      openstack endpoint list --interface public --service identity '" \
185                 | grep identity | awk '{print $14}')
186         fi
187         info "public_ip: $public_ip"
188         swap_to_public $public_ip
189     fi
190
191 elif [ "$installer_type" == "joid" ]; then
192     # do nothing...for the moment
193     # we can either do a scp from the jumphost or use the -v option to transmit the param to the docker file
194     echo "Do nothing, creds will be provided through volume option at docker creation for joid"
195
196 elif [ "$installer_type" == "foreman" ]; then
197     #ip_foreman="172.30.10.73"
198     controller="oscontroller1.opnfv.com"
199     verify_connectivity $installer_ip
200
201     # Check if controller is alive (here is more difficult to get the ip from a command like "fuel node")
202     sshpass -p vagrant ssh $ssh_options root@${installer_ip} \
203         "sshpass -p Op3nStack ssh $ssh_options root@${controller} 'ls'" &> /dev/null
204     if [ $? -ne 0 ]; then
205         error "The controller ${controller} is not up. Please check that the POD is correctly deployed."
206     fi
207
208     info "Fetching openrc from a Foreman Controller '${controller}'..."
209     sshpass -p vagrant ssh $ssh_options root@${installer_ip} \
210         "sshpass -p Op3nStack scp $ssh_options root@${controller}:~/keystonerc_admin ." &> /dev/null
211     sshpass -p vagrant scp $ssh_options root@${installer_ip}:~/keystonerc_admin $dest_path &> /dev/null
212
213     #This file contains the mgmt keystone API, we need the public one for our rc file
214     admin_ip=$(cat $dest_path | grep "OS_AUTH_URL" | sed 's/^.*\=//' | sed "s/^\([\"']\)\(.*\)\1\$/\2/g" | sed s'/\/$//')
215     public_ip=$(sshpass -p vagrant ssh $ssh_options root@${installer_ip} \
216         "sshpass -p Op3nStack ssh $ssh_options root@${controller} \
217         'source keystonerc_admin;keystone endpoint-list'" \
218         | grep $admin_ip | sed 's/ /\n/g' | grep ^http | head -1) &> /dev/null
219
220 elif [ "$installer_type" == "daisy" ]; then
221     verify_connectivity $installer_ip
222     cluster=$(sshpass -p r00tme ssh 2>/dev/null $ssh_options root@${installer_ip} \
223             "source ~/daisyrc_admin; daisy cluster-list"|grep active|head -1|awk -F "|" '{print $3}') &> /dev/null
224     if [ -z $cluster ]; then
225         echo "No active cluster detected in daisy"
226         exit 1
227     fi
228
229     sshpass -p r00tme scp 2>/dev/null $ssh_options root@${installer_ip}:/etc/kolla/admin-openrc.sh $dest_path &> /dev/null
230
231 else
232     error "Installer $installer is not supported by this script"
233 fi
234
235
236 if [ ! -f $dest_path ]; then
237     error "There has been an error retrieving the credentials"
238 fi
239
240 echo "-------- Credentials: --------"
241 cat $dest_path