-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathorder_svc.sh
executable file
·154 lines (138 loc) · 3.95 KB
/
order_svc.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/bin/bash
IFS=";"
# CloudForms OrderGanza - Patrick Rutledge [email protected]
# Defaults
uri="https://cf.example.com"
totalRequests=10 # Total number of requests
groupCount=5 # Number to order at one time
groupWait=1 # Minutes between groups
apiWait=3 # Seconds between API calls in a group
# Group to use, if empty group set in UI will be used by CF
miqgroup=""
# Dont touch from here on
usage() {
echo "Error: Usage $0 -c <catalog name> -i <item name> [ -u <username> -P <password> -t <totalRequests> -g <groupCount> -p <groupWait> -a <apiWait> -w <uri> -d <key1=value;key2=value> -n -N]"
}
while getopts Nnu:P:c:i:t:g:p:a:w:d: FLAG; do
case $FLAG in
n) noni=1;;
u) username="$OPTARG";;
P) password="$OPTARG";;
N) insecure=1;;
c) catalogName="$OPTARG";;
i) itemName="$OPTARG";;
t) totalRequests="$OPTARG";;
g) groupCount="$OPTARG";;
p) groupWait="$OPTARG";;
a) apiWait="$OPTARG";;
w) uri="$OPTARG";;
d) keypairs="$OPTARG";;
*) usage;exit;;
esac
done
if [ -z "$catalogName" -o -z "$itemName" ]
then
usage
exit 1
fi
if [ -z "$username" ]
then
echo -n "Enter CF Username: ";read username
fi
if [ -z "$password" ]
then
echo -n "Enter CF Password: "
stty -echo
read password
stty echo
echo
fi
if [ "$insecure" == 1 ]
then
ssl="-k"
else
ssl=""
fi
tok=`curl -s $ssl --user $username:$password -X GET -H "Accept: application/json" $uri/api/auth`
err=$?
tt=`echo $tok|grep error`
if [ $err != 0 -o -n "$tt" ]
then
echo "ERROR: Authentication failed to CloudForms."
echo "$tok"
exit $err
fi
tok=`echo $tok|python -m json.tool|grep auth_token|cut -f4 -d\"`
#echo "tok is '$tok'"
if [ -z "$tok" ]
then
echo "ERROR: Authentication failed to CloudForms."
exit 1
fi
catalogName=`echo $catalogName|sed "s/ /+/g"`
catalogName=`echo $catalogName|sed "s/&/%26/g"`
catalogID=`curl -s $ssl -H "X-Auth-Token: $tok" -H "Content-Type: application/json" -H "X-MIQ-Group: $miqgroup" -X GET "$uri/api/service_catalogs?attributes=name,id&expand=resources&filter%5B%5D=name='$catalogName'" | python -m json.tool |grep '"id"' | cut -f2 -d:|sed "s/[ ,\"]//g"`
if [ -z "$catalogID" ]
then
echo "ERROR: No such catalog $catalogName"
exit 1
fi
echo "catalogID is $catalogID"
itemName=`echo $itemName|sed "s/ /+/g"`
itemName=`echo $itemName|sed "s/&/%26/g"`
itemID=`curl -s $ssl -H "X-Auth-Token: $tok" -H "Content-Type: application/json" -H "X-MIQ-Group: $miqgroup" -X GET "$uri/api/service_templates?attributes=service_template_catalog_id,id,name&expand=resources&filter%5B%5D=name='$itemName'&filter%5B%5D=service_template_catalog_id='$catalogID'" | python -m json.tool |grep '"id"' | cut -f2 -d:|sed "s/[ ,\"]//g"`
if [ -z "$itemID" ]
then
echo "ERROR: No such catalog item $itemName"
exit 1
fi
echo "itemID is $itemID"
if [ "$noni" != 1 ]
then
echo -n "Are you sure you wish to deploy $totalRequests instances of this catalog item? (y/N): ";read yn
if [ "$yn" != "y" ]
then
echo "Exiting."
exit
fi
fi
KPS=""
if [ -n "$keypairs" ]
then
for kp in $keypairs
do
k=`echo $kp|cut -f1 -d=`
v=`echo $kp|cut -f2 -d=`
KPS="${KPS}, \"${k}\" : \"${v}\""
done
fi
PAYLOAD="{ \"action\": \"order\", \"resource\": { \"href\": \"https://$uri/api/service_templates/$itemID\"${KPS} } }"
((slp=$groupWait * 60))
#echo "PAYLOAD Is ${PAYLOAD}"
t=1
g=1
while [ $t -le $totalRequests ]
do
c=1
while [ $c -le $groupCount -a $t -le $totalRequests ]
do
echo "Deploying request $t in group $g"
out=`curl -s $ssl --user $username:$password -H "Content-Type: application/json" -H "X-MIQ-Group: $miqgroup" -X POST $uri/api/service_catalogs/$catalogID/service_templates -d "$PAYLOAD"`
testerr=`echo $out|grep '{"error":'`
if [ -n "$testerr" ]
then
echo "Deployment Failed!"
echo $out | python -m json.tool
exit 1
fi
(( c = $c + 1 ))
(( t = $t + 1 ))
sleep $apiWait
done
if [ $t -le $totalRequests ]
then
echo "Sleeping $slp seconds..."
(( g = $g + 1 ))
sleep $slp
fi
done