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 if [ -z $dest_path ] || [ -z $installer_type ] || [ -z $installer_ip ]; then
61 # Checking if destination path is valid
62 if [ -d $dest_path ]; then
63 error "Please provide the full destination path for the credentials file including the filename"
65 # Check if we can create the file (e.g. path is correct)
66 touch $dest_path || error "Cannot create the file specified. Check that the path is correct and run the script again."
70 ssh_options="-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no"
72 # Start fetching the files
73 if [ "$installer_type" == "fuel" ]; then
75 verify_connectivity $installer_ip
77 # Check if controller is alive (online='True')
78 controller_ip=$(sshpass -p r00tme ssh 2>/dev/null $ssh_options root@${installer_ip} \
79 'fuel node | grep controller | grep True | awk "{print \$10}" | tail -1') &> /dev/null
81 if [ -z $controller_ip ]; then
82 error "The controller $controller_ip is not up. Please check that the POD is correctly deployed."
85 info "Fetching rc file from controller $controller_ip..."
86 sshpass -p r00tme ssh 2>/dev/null $ssh_options root@${installer_ip} \
87 "scp $ssh_options ${controller_ip}:/root/openrc ." &> /dev/null
88 sshpass -p r00tme scp 2>/dev/null $ssh_options root@${installer_ip}:~/openrc $dest_path &> /dev/null
90 #This file contains the mgmt keystone API, we need the public one for our rc file
91 admin_ip=$(cat $dest_path | grep "OS_AUTH_URL" | sed 's/^.*\=//' | sed "s/^\([\"']\)\(.*\)\1\$/\2/g" | sed s'/.$//')
92 public_ip=$(sshpass -p r00tme ssh $ssh_options root@${installer_ip} \
93 "ssh ${controller_ip} 'source openrc; keystone endpoint-list'" \
94 | grep $admin_ip | cut -d ' ' -f 12)
95 #| grep http | head -1 | cut -d '|' -f 4 | sed 's/v1\/.*/v1\//' | sed 's/ //g') &> /dev/null
96 #NOTE: this is super ugly sed 's/v1\/.*/v1\//'OS_AUTH_URL
97 # but sometimes the output of endpoint-list is like this: http://172.30.9.70:8004/v1/%(tenant_id)s
100 elif [ "$installer_type" == "foreman" ]; then
101 #ip_foreman="172.30.10.73"
102 controller="oscontroller1.opnfv.com"
103 verify_connectivity $installer_ip
105 # Check if controller is alive (here is more difficult to get the ip from a command like "fuel node")
106 sshpass -p vagrant ssh $ssh_options root@${installer_ip} \
107 "sshpass -p Op3nStack ssh $ssh_options root@${controller} 'ls'" &> /dev/null
108 if [ $? -ne 0 ]; then
109 error "The controller ${controller} is not up. Please check that the POD is correctly deployed."
112 info "Fetching openrc from a Foreman Controller '${controller}'..."
113 sshpass -p vagrant ssh $ssh_options root@${installer_ip} \
114 "sshpass -p Op3nStack scp $ssh_options root@${controller}:~/keystonerc_admin ." &> /dev/null
115 sshpass -p vagrant scp $ssh_options root@${installer_ip}:~/keystonerc_admin $dest_path &> /dev/null
117 #This file contains the mgmt keystone API, we need the public one for our rc file
118 public_ip=$(sshpass -p vagrant ssh $ssh_options root@${installer_ip} \
119 "sshpass -p Op3nStack ssh $ssh_options root@${controller} \
120 'source keystonerc_admin;keystone endpoint-list'" \
121 | grep http | head -1 | cut -d '|' -f 4 | sed 's/ //g') &> /dev/null
124 error "Installer $installer is not supported by this script"
129 if [ "$public_ip" == "" ]; then
130 error "Cannot retrieve the public IP from keystone"
133 info "Keystone public IP is $public_ip"
134 sed -i "/OS_AUTH_URL/c\export OS_AUTH_URL=\'$public_ip'" $dest_path
136 echo "-------- Credentials: --------"