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 -d <destination> -i <installer_type> -a <installer_ip>" >&2
17 logger -s -t "fetch_os_creds.info" "$*"
22 logger -s -t "fetch_os_creds.error" "$*"
27 verify_connectivity() {
29 info "Verifying connectivity to $ip..."
30 for i in $(seq 0 10); do
31 if ping -c 1 -W 1 $ip > /dev/null; then
32 info "$ip is reachable!"
37 error "Can not talk to $ip."
43 while getopts ":d:i:a:h:" optchar; do
45 d) dest_path=${OPTARG} ;;
46 i) installer_type=${OPTARG} ;;
47 a) installer_ip=${OPTARG} ;;
48 *) echo "Non-option argument: '-${OPTARG}'" >&2
55 # set vars from env if not provided by user as options
56 dest_path=${dest_path:-$HOME/opnfv-openrc.sh}
57 installer_type=${installer_type:-$INSTALLER_TYPE}
58 installer_ip=${installer_ip:-$INSTALLER_IP}
60 if [ -z $dest_path ] || [ -z $installer_type ] || [ -z $installer_ip ]; then
65 # Checking if destination path is valid
66 if [ -d $dest_path ]; then
67 error "Please provide the full destination path for the credentials file including the filename"
69 # Check if we can create the file (e.g. path is correct)
70 touch $dest_path || error "Cannot create the file specified. Check that the path is correct and run the script again."
74 ssh_options="-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no"
76 # Start fetching the files
77 if [ "$installer_type" == "fuel" ]; then
79 verify_connectivity $installer_ip
81 # Check if controller is alive (online='True')
82 controller_ip=$(sshpass -p r00tme ssh 2>/dev/null $ssh_options root@${installer_ip} \
83 'fuel node | grep controller | grep True | awk "{print \$10}" | tail -1') &> /dev/null
85 if [ -z $controller_ip ]; then
86 error "The controller $controller_ip is not up. Please check that the POD is correctly deployed."
89 info "Fetching rc file from controller $controller_ip..."
90 sshpass -p r00tme ssh 2>/dev/null $ssh_options root@${installer_ip} \
91 "scp $ssh_options ${controller_ip}:/root/openrc ." &> /dev/null
92 sshpass -p r00tme scp 2>/dev/null $ssh_options root@${installer_ip}:~/openrc $dest_path &> /dev/null
94 #This file contains the mgmt keystone API, we need the public one for our rc file
95 admin_ip=$(cat $dest_path | grep "OS_AUTH_URL" | sed 's/^.*\=//' | sed "s/^\([\"']\)\(.*\)\1\$/\2/g" | sed s'/\/$//')
96 public_ip=$(sshpass -p r00tme ssh $ssh_options root@${installer_ip} \
97 "ssh ${controller_ip} 'source openrc; keystone endpoint-list'" \
98 | grep $admin_ip | sed 's/ /\n/g' | grep ^http | head -1)
99 #| grep http | head -1 | cut -d '|' -f 4 | sed 's/v1\/.*/v1\//' | sed 's/ //g') &> /dev/null
100 #NOTE: this is super ugly sed 's/v1\/.*/v1\//'OS_AUTH_URL
101 # but sometimes the output of endpoint-list is like this: http://172.30.9.70:8004/v1/%(tenant_id)s
104 elif [ "$installer_type" == "foreman" ]; then
105 #ip_foreman="172.30.10.73"
106 controller="oscontroller1.opnfv.com"
107 verify_connectivity $installer_ip
109 # Check if controller is alive (here is more difficult to get the ip from a command like "fuel node")
110 sshpass -p vagrant ssh $ssh_options root@${installer_ip} \
111 "sshpass -p Op3nStack ssh $ssh_options root@${controller} 'ls'" &> /dev/null
112 if [ $? -ne 0 ]; then
113 error "The controller ${controller} is not up. Please check that the POD is correctly deployed."
116 info "Fetching openrc from a Foreman Controller '${controller}'..."
117 sshpass -p vagrant ssh $ssh_options root@${installer_ip} \
118 "sshpass -p Op3nStack scp $ssh_options root@${controller}:~/keystonerc_admin ." &> /dev/null
119 sshpass -p vagrant scp $ssh_options root@${installer_ip}:~/keystonerc_admin $dest_path &> /dev/null
121 #This file contains the mgmt keystone API, we need the public one for our rc file
122 public_ip=$(sshpass -p vagrant ssh $ssh_options root@${installer_ip} \
123 "sshpass -p Op3nStack ssh $ssh_options root@${controller} \
124 'source keystonerc_admin;keystone endpoint-list'" \
125 | grep http | head -1 | cut -d '|' -f 4 | sed 's/ //g') &> /dev/null
128 error "Installer $installer is not supported by this script"
133 if [ "$public_ip" == "" ]; then
134 error "Cannot retrieve the public IP from keystone"
137 info "Keystone public IP is $public_ip"
138 sed -i "/OS_AUTH_URL/c\export OS_AUTH_URL=\'$public_ip'" $dest_path
140 echo "-------- Credentials: --------"