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