-
Notifications
You must be signed in to change notification settings - Fork 0
/
myhosts.sh
92 lines (84 loc) · 3.05 KB
/
myhosts.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/bin/bash
# script to list all fermicloud hosts from fermicloudui
OST_PROJECTS_LIST="osg glideinwms hepcloud scsservices"
help_msg() {
cat << EOF
$0 [options]
Print info about the Fermicloud VMs
Options:
-h print this message
-v verbose
-t Create OS_TOKEN only if needed (will ask for services password)
-r Create OS_TOKEN all the times (will ask for services password)
-p project (Default: glideinwms)
Projects: $OST_PROJECTS_LIST
Exit codes for errors:
1 wrong command option
2 invalid or missing OS token
3 openstack server command failed
EOF
}
get_token() {
# Get OS token and save it in the cache
# Globals: VERBOSE, OST_PROJECT
# Env: HOME USER
# The following command will ask for the service password
$VERBOSE && echo "No OS token. Requesting it ($USER, $OST_PROJECT). You vill be asked for the service passord."
OS_TOKEN=$(openstack --os-username="$USER" --os-user-domain-name=services --os-project-domain-name=services --os-project-name "$OST_PROJECT" --os-auth-url http://131.225.153.227:5000/v3 --os-system-scope all token issue --format json | jq -r '.id')
[ -z "$OS_TOKEN" ] && { echo "Unable to obtain OS token. Aborting." >&2; exit 2; }
rm -f "$HOME"/.fclcache/token
echo "OST_PROJECT=$OST_PROJECT" > "$HOME"/.fclcache/token
chmod 600 "$HOME"/.fclcache/token
echo "OS_TOKEN_DATE=$(date +%Y-%m-%dT%H:%M:%S%z)" >> "$HOME"/.fclcache/token
echo "OS_TOKEN=$OS_TOKEN" >> "$HOME"/.fclcache/token
}
REFRESH=false
VERBOSE=false
GET_TOKEN=false
OST_PROJECT=glideinwms
while getopts "hvrtp:" option
do
case "${option}"
in
h) help_msg; exit 0;;
v) VERBOSE=true;;
t) GET_TOKEN=true;;
r) REFRESH=true;;
p) OST_PROJECT=$OPTARG;;
*) echo "Wrong option" >&2; help_msg >&2; exit 1;;
esac
done
# Make sure that the OS token is available
if $REFRESH; then
# Force update of the token
get_token
else
if [ -z "$OS_TOKEN" ]; then
if [ -r "$HOME"/.fclcache/token ] && grep "OST_PROJECT=$OST_PROJECT" "$HOME"/.fclcache/token > /dev/null; then
# Source variable defining the OS_TOKEN
. "$HOME"/.fclcache/token
elif $GET_TOKEN; then
get_token
fi
fi
fi
[ -z "$OS_TOKEN" ] && { echo "Cannot proceed without OS token. Get one in advance or use '-t' or '-r'. Aborting." >&2; $VERBOSE && help_msg >&2; exit 2; }
export OS_TOKEN
# Run the commands to get the list of VMs (OS servers)
#alias osnova='openstack --os-endpoint http://131.225.153.227:8774/v2.1 --os-token $OS_TOKEN server'
#osnova list --format json > ptp.json
#csv_table=$(cat ptp.json | jq -r '.[]|(.ID+","+.Networks+","+.Name)')
my_hosts_table=$(openstack --os-endpoint http://131.225.153.227:8774/v2.1 --os-token $OS_TOKEN server list --format json | jq -r '.[]|(.ID+","+.Networks+","+.Name)')
if [ $? -ne 0 ] || [ -z "$my_hosts_table" ]; then
echo "Error in the openstack command. Aborting." >&2
exit 3
fi
for i in $my_hosts_table; do
mid=${i%%,*}
mname=${i##*,}
mip=${i#*,provider1=}
mip=${mip%%,*}
mhost=$(host $mip)
mhost=${mhost##* }
echo -e "$mid $mip\t ${mhost%.}\t $mname"
done