/
autofog.py
134 lines (121 loc) · 4.8 KB
/
autofog.py
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
#!/usr/bin/env python
import utils
import json
endpoint = 'https://ord.autoscale.api.rackspacecloud.com/v1.0/%s/%s'
auth_data = utils.auth('samsonite', '07f2ad0df034b776570fe77853159920',
servicename='cloudServersOpenStack', region='ord')
def findservers(serv):
'''Returns a server name when given a server uuid.'''
resp = utils.get('%s/servers/%s' % (auth_data['uri'], serv),
auth_data['token'])
if resp.status_code != 404:
return resp.json['server']['name']
else:
return None
def get_groups():
'''List out current scaling groups.'''
resp = utils.get(endpoint % (auth_data['id'], 'groups'),
auth_data['token']).json
groups = resp['groups']
for group in groups:
utils.printvtable([('Group ID', group['id']),
('Desired Capacity', group['desiredCapacity']),
('Pending Capacity', group['pendingCapacity']),
('Active Capacity', group['activeCapacity']),
('Active Nodes',
utils.string_a_list(
[findservers(i) for i in
[i['id'] for i in group['active']]]))])
def make_group(**kwargs):
'''Needs the name of the group, cooldown, min and max entities, servName
and lbid'''
data = {
"groupConfiguration": {},
"launchConfiguration": {
"type": "launch_server",
"args": {
"server": {
"flavorRef": 3,
"imageRef": "c11fdbb2-808a-4aec-bf12-d531ae053139"
},
"loadBalancers": [
{
"loadBalancerId": 141023,
"port": 80
}
]
}
},
"scalingPolicies": []
}
data['groupConfiguration'] = kwargs
# print json.dumps(data, indent=4)
resp = utils.post(endpoint % (auth_data['id'], 'groups'),
data=data, token=auth_data['token'])
print resp.status_code, resp.json
def delete_group(groupid):
'''Delete a scaling group.'''
data = {
"name": "workers",
"cooldown": 60,
"minEntities": 0,
"maxEntities": 0,
"metadata": {
"firstkey": "this is a string",
"secondkey": "1"
}
}
print 'Updating config for group %s...' % groupid
put = utils.put(endpoint % (auth_data['id'], 'groups/%s/config' % groupid),
data=data, token=auth_data['token'])
print put.status_code, json.dumps(put.json, indent=4)
delete = utils.delete(endpoint % (auth_data['id'],
'groups/%s' % groupid), token=auth_data['token'])
print delete.status_code, json.dumps(delete.json, indent=4)
def make_policy(groupid, **kwargs):
'''Make a scaling policy.'''
data = []
data.append(kwargs)
resp = utils.post(endpoint % (auth_data['id'], 'groups/%s/policies' % groupid),
data=json.dumps(data), token=auth_data['token'])
print resp.status_code, resp.json
def list_policies(groupid):
'''List out current scaling policies.'''
resp = utils.get(endpoint % (auth_data['id'], 'groups/%s/policies' % groupid), token=auth_data['token'])
data = []
for i in resp.json['policies']:
if 'changePercent' in i:
chng = '%s%%' % i['changePercent']
else:
chng = '%s server(s)' % i['change']
utils.printvtable([('ID', i['id']), ('Name', i['name']),
('Change', chng), ('Cooldown', i['cooldown']),
('Type', i['type']),
('Link', utils.string_a_list([l['href'] for l in i['links']]))])
def delete_policy(polid, groupid):
'''Deletes a scaling policy'''
resp = utils.delete(endpoint % (auth_data['id'], 'groups/%s/policies/%s' % (groupid, polid)), token=auth_data['token'])
print resp.status_code, json.dumps(resp.json, indent=4)
def main():
'''Parse args and main functions'''
# make_group('new group test')
get_groups()
# findservers('7f037d0c-52be-407d-b900-0e9563ec8227')
# make_policy('c2964772-d567-4bf7-b522-dc841419b6c7', name='test policy',
# change=1, cooldown=10, type='webhook')
# list_policies('c2964772-d567-4bf7-b522-dc841419b6c7')
# delete_policy('fb6afd6d-74c2-49d6-b1bd-f322d32c56fe',
# 'c2964772-d567-4bf7-b522-dc841419b6c7')
# delete_group('c2964772-d567-4bf7-b522-dc841419b6c7')
# delete_group('2b55d518-d66a-402a-8b39-b46393bfd994')
# delete_group('e159d98b-6e1b-4486-876c-3295cef79811')
# print auth_data
# parser = argparse.ArgumentParser()
# subparsers = parser.add_subparsers(dest='subparsers')
# list_parser = subparsers.add_parser('list')
# subsubparsers = subparsers.add_subparsers(dest='subsubparsers')
# user_subparser = list_parser.add_subparsers()
# list_parser.add_argument('users')
# args = parser.parse_args()
if __name__ == '__main__':
main()