Add promise for dashboard
[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 # 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}
59
60 if [ -z $dest_path ] || [ -z $installer_type ] || [ -z $installer_ip ]; then
61     usage
62     exit 2
63 fi
64
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"
68 else
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."
71 fi
72
73
74 ssh_options="-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no"
75
76 # Start fetching the files
77 if [ "$installer_type" == "fuel" ]; then
78     #ip_fuel="10.20.0.2"
79     verify_connectivity $installer_ip
80
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
84
85     if [ -z $controller_ip ]; then
86         error "The controller $controller_ip is not up. Please check that the POD is correctly deployed."
87     fi
88
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
93
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) &> /dev/null
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
102
103 elif [ "$installer_type" == "apex" ]; then
104     verify_connectivity $installer_ip
105
106     # The credentials file is located in the Instack VM (192.0.2.1)
107     # NOTE: This might change for bare metal deployments
108     info "Fetching rc file from Instack VM $installer_ip..."
109     if [ -f /root/.ssh/id_rsa ]; then
110         chmod 600 /root/.ssh/id_rsa
111     fi
112     sudo scp $ssh_options root@$installer_ip:/home/stack/overcloudrc $dest_path
113
114 elif [ "$installer_type" == "compass" ]; then
115     verify_connectivity $installer_ip
116     controller_ip=$(sshpass -p'root' ssh 2>/dev/null $ssh_options root@${installer_ip} \
117         'mysql -ucompass -pcompass -Dcompass -e"select *  from cluster;"' \
118         | awk -F"," '{for(i=1;i<NF;i++)if($i~/\"host[1-5]\"/) {print $(i+1);break;}}'  \
119         | grep -oP "\d+.\d+.\d+.\d+")
120
121     if [ -z $controller_ip ]; then
122         error "The controller $controller_ip is not up. Please check that the POD is correctly deployed."
123     fi
124
125     info "Fetching rc file from controller $controller_ip..."
126     sshpass -p root ssh 2>/dev/null $ssh_options root@${installer_ip} \
127         "scp $ssh_options ${controller_ip}:/opt/admin-openrc.sh ." &> /dev/null
128     sshpass -p root scp 2>/dev/null $ssh_options root@${installer_ip}:~/admin-openrc.sh $dest_path &> /dev/null
129     echo 'export OS_REGION_NAME=regionOne' >> $dest_path
130
131     info "This file contains the mgmt keystone API, we need the public one for our rc file"
132     admin_ip=$(cat $dest_path | grep "OS_AUTH_URL" | sed 's/^.*\=//' | sed "s/^\([\"']\)\(.*\)\1\$/\2/g" | sed s'/\/$//')
133     info "admin_ip: $admin_ip"
134     public_ip=$(sshpass -p root ssh $ssh_options root@${installer_ip} \
135         "ssh ${controller_ip} 'source /opt/admin-openrc.sh; keystone endpoint-list'" \
136         | grep $admin_ip | sed 's/ /\n/g' | grep ^http | head -1)
137     info "public_ip: $public_ip"
138
139
140 elif [ "$installer_type" == "joid" ]; then
141     # do nothing...for the moment
142     # we can either do a scp from the jumphost or use the -v option to transmit the param to the docker file
143     echo "Do nothing, creds will be provided through volume option at docker creation for joid"
144
145 elif [ "$installer_type" == "foreman" ]; then
146     #ip_foreman="172.30.10.73"
147     controller="oscontroller1.opnfv.com"
148     verify_connectivity $installer_ip
149
150     # Check if controller is alive (here is more difficult to get the ip from a command like "fuel node")
151     sshpass -p vagrant ssh $ssh_options root@${installer_ip} \
152         "sshpass -p Op3nStack ssh $ssh_options root@${controller} 'ls'" &> /dev/null
153     if [ $? -ne 0 ]; then
154         error "The controller ${controller} is not up. Please check that the POD is correctly deployed."
155     fi
156
157     info "Fetching openrc from a Foreman Controller '${controller}'..."
158     sshpass -p vagrant ssh $ssh_options root@${installer_ip} \
159         "sshpass -p Op3nStack scp $ssh_options root@${controller}:~/keystonerc_admin ." &> /dev/null
160     sshpass -p vagrant scp $ssh_options root@${installer_ip}:~/keystonerc_admin $dest_path &> /dev/null
161
162     #This file contains the mgmt keystone API, we need the public one for our rc file
163     admin_ip=$(cat $dest_path | grep "OS_AUTH_URL" | sed 's/^.*\=//' | sed "s/^\([\"']\)\(.*\)\1\$/\2/g" | sed s'/\/$//')
164     public_ip=$(sshpass -p vagrant ssh $ssh_options root@${installer_ip} \
165         "sshpass -p Op3nStack ssh $ssh_options root@${controller} \
166         'source keystonerc_admin;keystone endpoint-list'" \
167         | grep $admin_ip | sed 's/ /\n/g' | grep ^http | head -1) &> /dev/null
168
169 else
170     error "Installer $installer is not supported by this script"
171 fi
172
173
174 if [ ! -f $dest_path ]; then
175     error "There has been an error retrieving the credentials"
176 fi
177
178 if [ "$public_ip" != "" ]; then
179     info "Exchanging keystone public IP in rc file to $public_ip"
180     sed -i  "/OS_AUTH_URL/c\export OS_AUTH_URL=\'$public_ip'" $dest_path
181 fi
182
183
184
185 echo "-------- Credentials: --------"
186 cat $dest_path
187
188 exit 0