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 ##############################################################################
13 echo "usage: $0 [-v] -d <destination> -i <installer_type> -a <installer_ip>" >&2
14 echo "[-v] Virtualized deployment" >&2
18 logger -s -t "fetch_os_creds.info" "$*"
23 logger -s -t "fetch_os_creds.error" "$*"
28 verify_connectivity() {
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!"
38 error "Can not talk to $ip."
44 while getopts ":d:i:a:h:v" optchar; do
46 d) dest_path=${OPTARG} ;;
47 i) installer_type=${OPTARG} ;;
48 a) installer_ip=${OPTARG} ;;
49 v) DEPLOY_TYPE="virt" ;;
50 *) echo "Non-option argument: '-${OPTARG}'" >&2
57 # set vars from env if not provided by user as options
58 dest_path=${dest_path:-$HOME/opnfv-openrc.sh}
59 installer_type=${installer_type:-$INSTALLER_TYPE}
60 installer_ip=${installer_ip:-$INSTALLER_IP}
62 if [ -z $dest_path ] || [ -z $installer_type ] || [ -z $installer_ip ]; then
67 # Checking if destination path is valid
68 if [ -d $dest_path ]; then
69 error "Please provide the full destination path for the credentials file including the filename"
71 # Check if we can create the file (e.g. path is correct)
72 touch $dest_path || error "Cannot create the file specified. Check that the path is correct and run the script again."
76 ssh_options="-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no"
78 # Start fetching the files
79 if [ "$installer_type" == "fuel" ]; then
81 verify_connectivity $installer_ip
83 # Check if controller is alive (online='True')
84 controller_ip=$(sshpass -p r00tme ssh 2>/dev/null $ssh_options root@${installer_ip} \
85 'fuel node | grep controller | grep "True\| 1" | awk "{print \$10}" | tail -1') &> /dev/null
87 if [ -z $controller_ip ]; then
88 error "The controller $controller_ip is not up. Please check that the POD is correctly deployed."
91 info "Fetching rc file from controller $controller_ip..."
92 sshpass -p r00tme ssh 2>/dev/null $ssh_options root@${installer_ip} \
93 "scp $ssh_options ${controller_ip}:/root/openrc ." &> /dev/null
94 sshpass -p r00tme scp 2>/dev/null $ssh_options root@${installer_ip}:~/openrc $dest_path &> /dev/null
96 #This file contains the mgmt keystone API, we need the public one for our rc file
97 admin_ip=$(cat $dest_path | grep "OS_AUTH_URL" | sed 's/^.*\=//' | sed "s/^\([\"']\)\(.*\)\1\$/\2/g" | sed s'/\/$//')
98 public_ip=$(sshpass -p r00tme ssh $ssh_options root@${installer_ip} \
99 "ssh ${controller_ip} 'source openrc; openstack endpoint list --long'" \
100 | grep $admin_ip | sed 's/ /\n/g' | grep ^http | head -1) &> /dev/null
101 #| grep http | head -1 | cut -d '|' -f 4 | sed 's/v1\/.*/v1\//' | sed 's/ //g') &> /dev/null
102 #NOTE: this is super ugly sed 's/v1\/.*/v1\//'OS_AUTH_URL
103 # but sometimes the output of endpoint-list is like this: http://172.30.9.70:8004/v1/%(tenant_id)s
104 # Fuel virtual need a fix
106 if [ "$DEPLOY_TYPE" == "virt" ]; then
107 echo "INFO: Changing: internalURL -> publicURL in openrc"
108 sed -i 's/internalURL/publicURL/' $dest_path
111 elif [ "$installer_type" == "apex" ]; then
112 verify_connectivity $installer_ip
114 # The credentials file is located in the Instack VM (192.0.2.1)
115 # NOTE: This might change for bare metal deployments
116 info "Fetching rc file from Instack VM $installer_ip..."
117 if [ -f /root/.ssh/id_rsa ]; then
118 chmod 600 /root/.ssh/id_rsa
120 sudo scp $ssh_options root@$installer_ip:/home/stack/overcloudrc $dest_path
122 elif [ "$installer_type" == "compass" ]; then
123 verify_connectivity $installer_ip
124 controller_ip=$(sshpass -p'root' ssh 2>/dev/null $ssh_options root@${installer_ip} \
125 'mysql -ucompass -pcompass -Dcompass -e"select * from cluster;"' \
126 | awk -F"," '{for(i=1;i<NF;i++)if($i~/\"host[1-5]\"/) {print $(i+1);break;}}' \
127 | grep -oP "\d+.\d+.\d+.\d+")
129 if [ -z $controller_ip ]; then
130 error "The controller $controller_ip is not up. Please check that the POD is correctly deployed."
133 info "Fetching rc file from controller $controller_ip..."
134 sshpass -p root ssh 2>/dev/null $ssh_options root@${installer_ip} \
135 "scp $ssh_options ${controller_ip}:/opt/admin-openrc.sh ." &> /dev/null
136 sshpass -p root scp 2>/dev/null $ssh_options root@${installer_ip}:~/admin-openrc.sh $dest_path &> /dev/null
137 echo 'export OS_REGION_NAME=regionOne' >> $dest_path
139 info "This file contains the mgmt keystone API, we need the public one for our rc file"
140 admin_ip=$(cat $dest_path | grep "OS_AUTH_URL" | sed 's/^.*\=//' | sed "s/^\([\"']\)\(.*\)\1\$/\2/g" | sed s'/\/$//')
141 info "admin_ip: $admin_ip"
142 public_ip=$(sshpass -p root ssh $ssh_options root@${installer_ip} \
143 "ssh ${controller_ip} 'source /opt/admin-openrc.sh; keystone endpoint-list'" \
144 | grep $admin_ip | sed 's/ /\n/g' | grep ^http | head -1)
145 info "public_ip: $public_ip"
148 elif [ "$installer_type" == "joid" ]; then
149 # do nothing...for the moment
150 # we can either do a scp from the jumphost or use the -v option to transmit the param to the docker file
151 echo "Do nothing, creds will be provided through volume option at docker creation for joid"
153 elif [ "$installer_type" == "foreman" ]; then
154 #ip_foreman="172.30.10.73"
155 controller="oscontroller1.opnfv.com"
156 verify_connectivity $installer_ip
158 # Check if controller is alive (here is more difficult to get the ip from a command like "fuel node")
159 sshpass -p vagrant ssh $ssh_options root@${installer_ip} \
160 "sshpass -p Op3nStack ssh $ssh_options root@${controller} 'ls'" &> /dev/null
161 if [ $? -ne 0 ]; then
162 error "The controller ${controller} is not up. Please check that the POD is correctly deployed."
165 info "Fetching openrc from a Foreman Controller '${controller}'..."
166 sshpass -p vagrant ssh $ssh_options root@${installer_ip} \
167 "sshpass -p Op3nStack scp $ssh_options root@${controller}:~/keystonerc_admin ." &> /dev/null
168 sshpass -p vagrant scp $ssh_options root@${installer_ip}:~/keystonerc_admin $dest_path &> /dev/null
170 #This file contains the mgmt keystone API, we need the public one for our rc file
171 admin_ip=$(cat $dest_path | grep "OS_AUTH_URL" | sed 's/^.*\=//' | sed "s/^\([\"']\)\(.*\)\1\$/\2/g" | sed s'/\/$//')
172 public_ip=$(sshpass -p vagrant ssh $ssh_options root@${installer_ip} \
173 "sshpass -p Op3nStack ssh $ssh_options root@${controller} \
174 'source keystonerc_admin;keystone endpoint-list'" \
175 | grep $admin_ip | sed 's/ /\n/g' | grep ^http | head -1) &> /dev/null
178 error "Installer $installer is not supported by this script"
182 if [ ! -f $dest_path ]; then
183 error "There has been an error retrieving the credentials"
186 if [ "$public_ip" != "" ]; then
187 info "Exchanging keystone public IP in rc file to $public_ip"
188 sed -i "/OS_AUTH_URL/c\export OS_AUTH_URL=\'$public_ip'" $dest_path
193 echo "-------- Credentials: --------"