48498b70c8f43772a6361ba336408f5996839d45
[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
11
12 usage() {
13     echo "usage: $0 -d <destination> -i <installer_type> -a <installer_ip>" >&2
14 }
15
16 info ()  {
17     logger -s -t "fetch_os_creds.info" "$*"
18 }
19
20
21 error () {
22     logger -s -t "fetch_os_creds.error" "$*"
23     exit 1
24 }
25
26
27 verify_connectivity() {
28     local ip=$1
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!"
33             return 0
34         fi
35         sleep 1
36     done
37     error "Can not talk to $ip."
38 }
39
40
41
42 #Get options
43 while getopts ":d:i:a:h:" optchar; do
44     case "${optchar}" in
45         d) dest_path=${OPTARG} ;;
46         i) installer_type=${OPTARG} ;;
47         a) installer_ip=${OPTARG} ;;
48         *) echo "Non-option argument: '-${OPTARG}'" >&2
49            usage
50            exit 2
51            ;;
52     esac
53 done
54
55 if [ -z $dest_path ] || [ -z $installer_type ] || [ -z $installer_ip ]; then
56     usage
57     exit 2
58 fi
59
60
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"
64 else
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."
67 fi
68
69
70 ssh_options="-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no"
71
72 # Start fetching the files
73 if [ "$installer_type" == "fuel" ]; then
74     #ip_fuel="10.20.0.2"
75     verify_connectivity $installer_ip
76
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
80
81     if [ -z $controller_ip ]; then
82         error "The controller $controller_ip is not up. Please check that the POD is correctly deployed."
83     fi
84
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
89
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 | sed 's/ /\n/g' | grep ^http | head -1)
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
98
99
100 elif [ "$installer_type" == "foreman" ]; then
101     #ip_foreman="172.30.10.73"
102     controller="oscontroller1.opnfv.com"
103     verify_connectivity $installer_ip
104
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."
110     fi
111
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
116
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
122
123 else
124     error "Installer $installer is not supported by this script"
125 fi
126
127
128
129 if [ "$public_ip" == "" ]; then
130     error "Cannot retrieve the public IP from keystone"
131 fi
132
133 info "Keystone public IP is $public_ip"
134 sed -i  "/OS_AUTH_URL/c\export OS_AUTH_URL=\'$public_ip'" $dest_path
135
136 echo "-------- Credentials: --------"
137 cat $dest_path
138
139 exit 0