-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsoftlayer_autoscale.py
More file actions
executable file
·382 lines (319 loc) · 12.9 KB
/
softlayer_autoscale.py
File metadata and controls
executable file
·382 lines (319 loc) · 12.9 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
import SoftLayer
from SoftLayer import Client
import os, sys, time, simplejson
from datetime import timedelta, datetime
username = os.environ.get('SOFTLAYER_USERNAME', '')
api_key = os.environ.get('SOFTLAYER_APIKEY','')
endpoint_url = SoftLayer.API_PUBLIC_ENDPOINT
client = Client(username=username, api_key=api_key, endpoint_url=endpoint_url)
def getDatacenterKeyName(name):
datacenters = getDatacenters()
for datacenter in datacenters:
if datacenter['name'] == name:
print simplejson.dumps(datacenter, sort_keys=True, indent=4 * ' ')
return datacenter['regions'][0]['keyname']
return None
def getDatacenterRegionDescription(name):
regions = getRegionalGroups()
for region in regions:
for location in region['locations']:
if location['name'] == name:
print simplejson.dumps(location, sort_keys=True, indent=4 * ' ')
return region['id'], region['description']
return None
def getDatacenters():
object_mask = 'regions'
datacenters = client['SoftLayer_Location'].getDatacenters(mask=object_mask)
#print simplejson.dumps(datacenters, sort_keys=True, indent=4 * ' ')
return datacenters
def getPackagePrices():
prices = client['SoftLayer_Product_Package'].getItemPrices(id=194)
print simplejson.dumps(prices, sort_keys=True, indent=4 * ' ')
def getBlockDeviceTemplateGroups():
imgs = client['SoftLayer_Account'].getBlockDeviceTemplateGroups()
print simplejson.dumps(imgs, sort_keys=True, indent=4 * ' ')
def orderLoadBalancer(location, price_id, place_order):
orderData = {
'prices': [
{
'id': price_id
}
],
'packageId': 194,
'location': location
}
orderId = 0
if place_order:
load_balancer = client['SoftLayer_Product_Order'].placeOrder(orderData)
else:
load_balancer = client['SoftLayer_Product_Order'].verifyOrder(orderData)
print simplejson.dumps(load_balancer, sort_keys=True, indent=4 * ' ')
return load_balancer
def getBillingOrderDetail(id):
mask = 'id,status,items'
order = client['SoftLayer_Billing_Order'].getObject(id=id,mask=mask)
print simplejson.dumps(order, sort_keys=True, indent=4 * ' ')
return order
def getBillingOrderItem(id):
mask = 'billingItem,order'
order = client['SoftLayer_Billing_Order_Item'].getObject(id=id,mask=mask)
print simplejson.dumps(order, sort_keys=True, indent=4 * ' ')
return order
def getVirtualServer(id):
server = client['SoftLayer_Virtual_Guest'].getObject(id=id)
print simplejson.dumps(server, sort_keys=True, indent=4 * ' ')
return server
def getLoadBalancers():
object_mask = 'loadBalancerHardware,virtualServers,ipAddress,billingItem, billingItem[orderItem]'
load_balancers = client['Account'].getAdcLoadBalancers(mask=object_mask)
#print simplejson.dumps(load_balancers, sort_keys=True, indent=4 * ' ')
return load_balancers
def findLoadBalancer(order_item_id):
load_balancers = getLoadBalancers()
for load_balancer in load_balancers: #reversed(load_balancers)
#print '\n LOAD BALANCERS \n'
#print simplejson.dumps(load_balancer, sort_keys=True, indent=4 * ' ')
if load_balancer['billingItem']['orderItemId'] == order_item_id:
#print simplejson.dumps(load_balancer, sort_keys=True, indent=4 * ' ')
return load_balancer
return None
def getLoadBalancer(id):
mask = 'virtualServers[serviceGroups[services[groupReferences]]],billingItem'
load_balancer = client['Network_Application_Delivery_Controller_LoadBalancer_VirtualIpAddress'].getObject(id=id, mask=mask)
print simplejson.dumps(load_balancer, sort_keys=True, indent=4 * ' ')
return load_balancer
def getRoutingMethods():
methods = client['Network_Application_Delivery_Controller_LoadBalancer_Routing_Method'].getAllObjects()
print simplejson.dumps(methods, sort_keys=True, indent=4 * ' ')
return methods
def getRoutingTypes():
types = client['Network_Application_Delivery_Controller_LoadBalancer_Routing_Type'].getAllObjects()
print simplejson.dumps(types, sort_keys=True, indent=4 * ' ')
return types
def addLoadBalancerServiceGroup(id,port,allocation,routing_type,routing_method):
mask = 'virtualServers[serviceGroups[services[groupReferences]]]'
load_balancer = client['Network_Application_Delivery_Controller_LoadBalancer_VirtualIpAddress'].getObject(id=id, mask=mask)
service_template = {
'port': port,
'allocation': allocation,
'serviceGroups': [
{
'routingTypeId': routing_type,
'routingMethodId': routing_method
}
]
}
load_balancer['virtualServers'].append(service_template)
return client['Network_Application_Delivery_Controller_LoadBalancer_VirtualIpAddress'].editObject(load_balancer, id=id)
## Scale
def getTerminationPolicies():
items = client['SoftLayer_Scale_Termination_Policy'].getAllObjects()
print simplejson.dumps(items, sort_keys=True, indent=4 * ' ')
return items
def getRegionalGroups():
mask = 'locations'
items = client['SoftLayer_Location_Group_Regional'].getAllObjects(mask=mask)
#print simplejson.dumps(items, sort_keys=True, indent=4 * ' ')
return items
def getScaleActionTypes():
items = client['SoftLayer_Scale_Policy_Action'].getAllObjects()
print simplejson.dumps(items, sort_keys=True, indent=4 * ' ')
return items
def createScaleGroup(vip_id, regional_group_id, scale_group_name, hostname, domain, datacenter, cpu, memory, img, port, desiredcount, mincount, maxcount):
group = {
'name': scale_group_name,
'desiredMemberCount': desiredcount,
'minimumMemberCount': mincount,
'maximumMemberCount': maxcount,
'cooldown': 120,
'terminationPolicyId': 1,
'regionalGroupId': regional_group_id,
'suspendedFlag': False,
'virtualGuestMemberTemplate': {
'hostname': hostname,
'domain': domain,
'startCpus': cpu,
'maxMemory': memory,
'hourlyBillingFlag': True,
'localDiskFlag': True,
'blockDeviceTemplateGroup': {
'globalIdentifier': img
},
'datacenter': {
'name': datacenter
}
},
'loadBalancers':[
{
'healthCheck': {
'healthCheckTypeId': 21
},
'port': port,
'virtualServerId': vip_id #252313
}
]
}
#print simplejson.dumps(group, sort_keys=True, indent=4 * ' ')
order = client['SoftLayer_Scale_Group'].createObject(group)
print simplejson.dumps(order, sort_keys=True, indent=4 * ' ')
return order
def createScaleUpPolicy(scale_group_id, scale_name, scale_amount):
policy = {
'name': scale_name,
'scaleGroupId': scale_group_id,
'scaleActions': [
{
'amount': scale_amount,
'deleteFlag': '',
'scaleType': 'RELATIVE',
'typeId': 1
}
],
'resourceUseTriggers': [
{
'typeId': 3,
'watches': [
{
'algorithm': 'EWMA',
'metric': 'host.cpu.percent',
'modifyDate': '',
'operator': '>',
'period': 120,
'value': '60'
}
]
}
]
}
order = client['SoftLayer_Scale_Policy'].createObject(policy)
print simplejson.dumps(order, sort_keys=True, indent=4 * ' ')
return order
def createScaleDownPolicy(scale_group_id, scale_name, scale_amount):
policy = {
'name': scale_name,
'scaleGroupId': scale_group_id,
'scaleActions': [
{
'amount': scale_amount,
'deleteFlag': '',
'scaleType': 'RELATIVE',
'typeId': 1
}
],
'resourceUseTriggers': [
{
'typeId': 3,
'watches': [
{
'algorithm': 'EWMA',
'metric': 'host.cpu.percent',
'modifyDate': '',
'operator': '<',
'period': 120,
'value': '40'
}
]
}
]
}
order = client['SoftLayer_Scale_Policy'].createObject(policy)
print simplejson.dumps(order, sort_keys=True, indent=4 * ' ')
return order
def getScaleGroups():
mask = 'loadBalancers,loadBalancers[healthCheck[attributes]],loadBalancers[healthCheck[services]],loadBalancers[healthCheck[type]],policies,policies[resourceUseTriggers],policies[triggers],policies[triggers[type]],policies[actions]'
groups = client['SoftLayer_Account'].getScaleGroups(mask=mask)
print simplejson.dumps(groups, sort_keys=True, indent=4 * ' ')
return groups
def getScaleGroup(id):
mask = 'loadBalancers,loadBalancers[healthCheck[attributes]],loadBalancers[healthCheck[services]],loadBalancers[healthCheck[type]],policies,policies[resourceUseTriggers],policies[triggers],policies[triggers[type]],policies[resourceUseTriggers[watches]],policies[actions]'
group = client['SoftLayer_Scale_Group'].getObject(id=id,mask=mask)
print simplejson.dumps(group, sort_keys=True, indent=4 * ' ')
return group
#SoftLayer_Scale_Policy_Trigger_ResourceUse_Watch
def getScaleLoadBalancer(id):
load_balancers = client['SoftLayer_Scale_LoadBalancer'].getObject(id=id)
print simplejson.dumps(load_balancers, sort_keys=True, indent=4 * ' ')
return load_balancers
def scale(id, amount):
client['SoftLayer_Scale_Group'].scale(amount,id=id)
# Setup Params
# --------------------------
scale_group_name = 'scale-mcltn-1'
datacenter = 'sjc03'
hostname = 'scale'
domain = 'colton.cc'
cpu = 1
memory = 1024
img = '4df4c463-5e29-4773-9ddb-fdda6ea5b2d8'
lb_price_id = 2079 # 250 VIP Connections
mincount = 0
desiredcount = 0
maxcount = 3
port = 8080
allocation = 100
routing_type = 2
routing_method = 10
# GET INFO CALLS
# ----------------------------------
#getDatacenters()
#getRegionalGroups()
#getBlockDeviceTemplateGroups()
#getPackagePrices()
#getLoadBalancers()
#getRoutingMethods()
#getRoutingTypes()
# #getTerminationPolicies()
# #getScaleActionTypes()
# #getScaleLoadBalancer(55458)
# #getScaleGroups()
# #getScaleGroup(990877)
#scale_group_id = 1012657
#getScaleGroup(scale_group_id)
#createScaleUpPolicy(scale_group_id,'xx-scale-cpu-up',1)
#createScaleDownPolicy(scale_group_id,'xx-scale-cpu-down',-1)
#scale(scale_group_id,-1)
def setup_autoscale():
# PLACE ORDER
# ----------------------------------------------------
# order = orderLoadBalancer(region_keyname, lb_price_id, True)
# order_id = order['orderId']
# print 'Order Id: ' + str(order_id)
# time.sleep(10)
order_id = 8432665 #8478513
order_details = getBillingOrderDetail(order_id)
billing_item_order_item_id = order_details['items'][0]['id']
print 'Billing Item Order Item Id: ' + str(billing_item_order_item_id)
load_balancer = findLoadBalancer(billing_item_order_item_id)
lb_id = load_balancer['id']
print 'Load Balancer Id: ' + str(lb_id)
# addLoadBalancerServiceGroup(lb_id, port, allocation, routing_type, routing_method)
# time.sleep(10)
load_balancer = getLoadBalancer(lb_id)
vip_id = load_balancer['virtualServers'][0]['id']
print 'Virtual Server Id: ' + str(vip_id)
time.sleep(1)
region_keyname = getDatacenterKeyName(datacenter)
regional_group_id, regional_group_description = getDatacenterRegionDescription(datacenter)
#print region_keyname
#print regional_group_id
#print regional_group_description
scale_group = createScaleGroup(vip_id, regional_group_id, scale_group_name, hostname, domain, datacenter, cpu, memory, img, port, desiredcount, mincount, maxcount)
scale_group_id = scale_group['id']
print 'Scale Group Id: ' + str(scale_group_id)
# time.sleep(10)
def scale_autoscale(scale_group_id):
getScaleGroup(scale_group_id)
createScaleUpPolicy(scale_group_id,'xx-scale-cpu-up',1)
time.sleep(5)
createScaleDownPolicy(scale_group_id,'xx-scale-cpu-down',-1)
time.sleep(5)
#setup_autoscale()
#scale_autoscale(1015461)
#scale_group_id = 1015461
#scale(scale_group_id,2)
#scale(scale_group_id,-1)
# sudo apt-get install stress
# stress -c 1
# stress -c 1 -m 1 --vm-bytes 300M
# stress -c 2 -t 240s &
# top