-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgnt-instance
359 lines (305 loc) · 11.7 KB
/
gnt-instance
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
DOCUMENTATION = '''
---
module: gnt-instance
author: Fabien Dupont
short_description: "Manage ganeti instances"
requirements: []
description:
- Manage ganeti instances
options:
name:
required: true
aliases: ["instance_name"]
description:
- Name of the instance being managed
state:
required: false
default: "present"
choices: ["present", "absent", "restarted", "started", "stopped", "migrated", "reinstalled"]
description:
- State of the instance. When C(absent), removes the instance.
cluster:
required: false
default: "localhost"
description:
- Domain name or IP address of the cluster.
port:
required: false
default: 5800
description:
- TCP port of the cluster's RAPI.
user:
required: false
description:
- User used to access the API (Must have write access).
password:
required: false
description:
- Password of the user used to access the API.
memory:
required: false
default: 256
description:
- Amount of memory awarded to the created instance
when state=present.
vcpus:
required: false
default: 1
description:
- Amount of vcpus awarded to the created instance
when state=present.
disk:
required: false
default: "2G"
description:
- Amount of disk space awarded to the created instance
when state=present. Accept a value in MB or a shortcut
like M, G, or T.
disk_template:
required: false
default: "drbd"
choices: ["sharedfile", "diskless", "plain", "blockdev", "drbd", "file", "rbd"]
description:
- Disk template used to create the instance's disk
when state=present.
iallocator:
required: false
default: "hail"
description:
- Plug'in used to select the instance's nodes when
state=present.
os_type:
required: false
default: "debootstrap+default"
description:
- Operating system installed on the instance when
state=present.
pnode:
required: false
description:
- Primary node where the instance is executed.
snode:
required: false
description:
- Secondary node where the instance is executed.
Required if disk_template = drbd.
'''
EXAMPLES = '''
# Shutdown the instance 'foo'
- gnt-instance: name=foo state=stopped
# Create or modify an instance named 'bar' with 512MB of RAM and 2 CPUs
- gnt-instance: name=bar memory=512 vcpus=2 state=present
# Restart the instance 'baz'
- gnt-instance: name=baz stated=restarted
'''
import os
import syslog
import platform
import urllib2
import urllib
import base64
import simplejson
import time
class MyRequest(urllib2.Request):
GET = 'get'
POST = 'post'
PUT = 'put'
DELETE = 'delete'
def __init__(self, url, data=None, headers={},
origin_req_host=None, unverifiable=False, method=None):
urllib2.Request.__init__(self, url, data, headers, origin_req_host, unverifiable)
self.method = method
def get_method(self):
if self.method:
return self.method
return urllib2.Request.get_method(self)
class Instance(object):
current = {}
def __new__(cls, *args, **kwargs):
return load_platform_subclass(Instance, args, kwargs)
def __init__(self, module):
self.module = module
self.changed = False
self.cluster = module.params["cluster"]
self.port = module.params["port"]
self.state = module.params["state"]
self.name = module.params["name"]
self.user = module.params["user"]
self.password = module.params["password"]
self.memory = module.params["memory"]
self.vcpus = module.params["vcpus"]
self.disk = module.params["disk"]
self.disk_template = module.params["disk_template"]
self.iallocator = module.params["iallocator"]
self.os_type = module.params["os_type"]
self.pnode = module.params["pnode"]
self.snode = module.params["snode"]
def query_api(self, method = 'get', **kwargs):
params = {}
url = 'https://' + self.cluster + ':' + str(self.port) + '/2'
if not('resource' in kwargs):
url = url + '/instances'
else:
url = url + kwargs['resource']
if 'params' in kwargs:
params = kwargs['params']
if self.module.check_mode and method != 'get':
return {'code': 200}
opener = urllib2.build_opener(urllib2.HTTPHandler)
request = MyRequest(url, data=simplejson.dumps(params), headers={'Content-Type': 'application/json'}, method=method)
if self.user != '' and self.password != '':
base64string = base64.encodestring('%s:%s' % (self.user, self.password)).replace('\n', '')
request.add_header("Authorization", "Basic %s" % base64string)
try:
ret = opener.open(request)
return simplejson.load(ret)
except urllib2.URLError as e:
if method != 'get':
self.module.fail_json(name=self.name, msg='(' + method + ') ' + url + ': ' + simplejson.load(e)['explain'], params=params, code=e.code)
return e.code
def populate(self):
ret = self.query_api('get', resource = '/instances/' + self.name)
if ret == 404:
self.current = {'code': 404, 'msg': 'Not found'}
else:
self.current = ret
self.current['code'] = 200
def startup(self):
self.changed = True
return self.query_api('put', resource = '/instances/' + self.name + '/startup')
def reboot(self):
self.changed = True
return self.query_api('post', resource = '/instances/' + self.name + '/reboot')
def migrate(self):
self.changed = True
return self.query_api('put', resource = '/instances/' + self.name + '/migrate', params = {'allow_failover': True})
def reinstall(self):
self.changed = True
return self.query_api('post', resource = '/instances/' + self.name + '/reinstall')
def shutdown(self):
self.changed = True
return self.query_api('put', resource = '/instances/' + self.name + '/shutdown')
def delete(self):
self.changed = True
return self.query_api('delete', resource = '/instances/' + self.name)
def create(self):
self.changed = True
return self.query_api('post', resource = '/instances', params = {
'__version__': 1,
'beparams': {
'memory': self.memory,
'vcpus': self.vcpus
},
'disk_template': self.disk_template,
'disks': [{'size': self.disk}],
'iallocator': self.iallocator,
'instance_name': self.name,
'mode': 'create',
'pnode': None if self.pnode == '' else self.pnode,
'snode': None if self.snode == '' else self.snode,
'nics': [{}],
'os_type': self.os_type if self.os_type != None else 'debootstrap+default'
})
def modify(self, new_params):
self.changed = True
return self.query_api('put', resource = '/instances/' + self.name + '/modify', params = new_params)
def wait_for(self, job_id):
if self.module.check_mode:
return
status = 'unk'
while status != 'success' and status != 'error' and status != 'cancelled':
status = self.query_api('get', resource = '/jobs/' + job_id)['status']
time.sleep(1)
if status != 'success':
module.fail_json(name=instance.name, msg=job_id + ' ' + status)
def main():
module = AnsibleModule(
argument_spec = dict(
cluster=dict(default='localhost', type='str', required=False),
port=dict(default='5080', type='int', required=False),
state=dict(default='present', choices=['present', 'absent', 'restarted', 'started', 'stopped', 'migrated', 'reinstalled'], type='str'),
name=dict(required=True, type='str', aliases=['instance_name']),
user=dict(required=False, type='str', default=''),
password=dict(required=False, type='str', default=''),
memory=dict(type='int'),
vcpus=dict(type='int'),
disk=dict(type='str', default='2G'),
disk_template=dict(type='str', default='drbd', choices=['sharedfile', 'diskless', 'plain', 'blockdev', 'drbd', 'file', 'rbd']),
iallocator=dict(type='str', default='hail'),
os_type=dict(type='str'),
pnode=dict(type='str', default=''),
snode=dict(type='str', default=''),
),
supports_check_mode=True
)
instance = Instance(module)
instance.populate()
last_job = None
if instance.current["code"] != 404 and instance.state != 'absent':
new_params = {}
changed = False
need_reinstall = False
need_shutdown = False
if instance.memory and instance.current['beparams']['memory'] != instance.memory:
try:
new_params['beparams']
except:
new_params['beparams'] = {}
new_params['beparams']['memory'] = instance.memory
changed = True
if instance.vcpus and instance.current['beparams']['vcpus'] != instance.vcpus:
try:
new_params['beparams']
except:
new_params['beparams'] = {}
new_params['beparams']['vcpus'] = instance.vcpus
changed = True
if instance.disk_template and instance.current['disk_template'] != instance.disk_template:
new_params['disk_template'] = instance.disk_template
if instance.disk_template == 'drbd':
if instance.snode == '':
module.fail_json(msg='No secondary node specified')
new_params['remote_node'] = instance.snode
changed = True
need_shutdown = True
if instance.os_type and instance.current['os'] != instance.os_type:
new_params['os_name'] = instance.os_type
changed = True
need_reinstall = True
if need_shutdown:
instance.wait_for(instance.shutdown())
if changed:
instance.wait_for(instance.modify(new_params))
if need_reinstall:
instance.reinstall()
elif need_shutdown:
instance.wait_for(instance.startup())
if instance.current["code"] == 404 and instance.state != 'absent' and instance.state != 'present':
module.fail_json(name=instance.name, msg=instance.current["msg"])
elif instance.state == 'stopped':
if instance.current['status'] == 'running':
instance.shutdown()
elif instance.state == 'started':
if instance.current['status'] != 'running':
instance.startup()
elif instance.state == 'restarted':
if instance.current['status'] == 'running':
instance.reboot()
elif instance.current['status'] == 'ADMIN_down':
instance.startup()
elif instance.state == 'migrated':
instance.migrate()
elif instance.state == 'reinstalled':
instance.reinstall()
elif instance.state == 'absent':
if instance.current['code'] != 404:
instance.delete()
elif instance.state == 'present':
if instance.current['code'] == 404:
instance.create()
module.exit_json(changed=instance.changed, name=instance.name, state=instance.state)
# include magic from lib/ansible/module_common.py
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
main()