add fetch os creds scripts, which is used before running the bottlenecks test cases
[bottlenecks.git] / utils / infra_setup / fetch_os_creds.sh
1 #!/bin/bash
2 ##############################################################################
3 # Copyright (c) 2015 Ericsson AB and others.
4 # jose.lausuch@ericsson.com matthew.lijun@huawei.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
104 elif [ "$installer_type" == "foreman" ]; then
105     #ip_foreman="172.30.10.73"
106     controller="oscontroller1.opnfv.com"
107     verify_connectivity $installer_ip
108
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."
114     fi
115
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
120
121     #This file contains the mgmt keystone API, we need the public one for our rc file
122     admin_ip=$(cat $dest_path | grep "OS_AUTH_URL" | sed 's/^.*\=//' | sed "s/^\([\"']\)\(.*\)\1\$/\2/g" | sed s'/\/$//')
123     public_ip=$(sshpass -p vagrant ssh $ssh_options root@${installer_ip} \
124         "sshpass -p Op3nStack ssh $ssh_options root@${controller} \
125         'source keystonerc_admin;keystone endpoint-list'" \
126         | grep $admin_ip | sed 's/ /\n/g' | grep ^http | head -1) &> /dev/null
127
128 elif [ "$installer_type" == "compass" ]; then
129     verify_connectivity $installer_ip
130     controller_ip=$(sshpass -p'root' ssh 2>/dev/null $ssh_options root@${installer_ip} \
131         'mysql -ucompass -pcompass -Dcompass -e"select *  from cluster;"' \
132         | awk -F"," '{for(i=1;i<NF;i++)if($i~/\"host1\"/) {print $(i+1);break;}}'  \
133         | grep -oP "\d+.\d+.\d+.\d+")
134
135     if [ -z $controller_ip ]; then
136         error "The controller $controller_ip is not up. Please check that the POD is correctly deployed."
137     fi
138
139     info "Fetching rc file from controller $controller_ip..."
140     sshpass -p root ssh 2>/dev/null $ssh_options root@${installer_ip} \
141         "scp $ssh_options ${controller_ip}:/opt/admin-openrc.sh ." &> /dev/null
142     sshpass -p root scp 2>/dev/null $ssh_options root@${installer_ip}:~/admin-openrc.sh $dest_path &> /dev/null
143     echo 'export OS_REGION_NAME=regionOne' >> $dest_path
144
145     info "This file contains the mgmt keystone API, we need the public one for our rc file"
146     admin_ip=$(cat $dest_path | grep "OS_AUTH_URL" | sed 's/^.*\=//' | sed "s/^\([\"']\)\(.*\)\1\$/\2/g" | sed s'/\/$//')
147     info "admin_ip: $admin_ip"
148     public_ip=$(sshpass -p root ssh $ssh_options root@${installer_ip} \
149         "ssh ${controller_ip} 'source /opt/admin-openrc.sh; keystone endpoint-list'" \
150         | grep $admin_ip | sed 's/ /\n/g' | grep ^http | head -1)
151     info "public_ip: $public_ip"
152
153 else
154     error "Installer $installer is not supported by this script"
155 fi
156
157
158
159 if [ "$public_ip" == "" ]; then
160     error "Cannot retrieve the public IP from keystone"
161 fi
162
163 info "Keystone public IP is $public_ip"
164 sed -i  "/OS_AUTH_URL/c\export OS_AUTH_URL=\'$public_ip'" $dest_path
165
166 echo "-------- Credentials: --------"
167 cat $dest_path
168
169 exit 0