-
Notifications
You must be signed in to change notification settings - Fork 1
/
heroku.py
370 lines (306 loc) · 11.1 KB
/
heroku.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
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
#! /usr/bin/env python
# -*- coding: utf-8; -*-
from __future__ import unicode_literals, print_function
import json
import sys
from ansible.module_utils.basic import AnsibleModule
# WANT_JSON
import heroku3
from heroku3.models import BaseResource
from requests.exceptions import HTTPError
__all__ = ['main']
DOCUMENTATION = '''
---
module: heroku
short_description: Manages Heroku applications
description:
- Let's you create and delete Heroku applications
- Let's you query and or update application's configuration
version_added: "2.1"
author: "Nicolas CANIART, @cans"
requirements:
- heroku3 Python module
# ... snip ...
'''
EXAMPLES = '''
- name: Create a new application
heroku:
apikey: "<your-heroku-api-key>"
app: "my-app"
command: create
region: "us"
- name:
heroku:
apikey: "<your-heroku-api-key>"
app: "my-app"
command: config
settings:
variable: "value"
path: "/to/some/place"
uppercase: true
- name: Delete an application
heroku:
apikey: "<your-heroku-api-key>"
app: "my-app"
command: delete
'''
RETURN = '''
'''
_COMMANDS = ('apps',
'config',
'create',
'delete',
'facts',
'scale',
'start',
'stop',
)
_DYNO_SIZES = ('free',
'hobby',
'standard-1x',
'standard-2x',
'performance-m',
'performance-l',
)
# _DYNO_WORKLOADS = ('web',
# 'worker',
# )
_HEROKU_REGIONS = ('eu',
'frankfurt',
'oregon',
'tokyo',
'us',
'virginia',
)
_ARGS_SPEC = {'command': {'default': 'facts',
'choices': _COMMANDS,
},
'app': {'required': False,
# 'type': 'list',
},
'apikey': {'required': True,
},
'count': {'required': False,
'type': 'int',
'default': None,
},
'region': {'default': 'us',
'choices': _HEROKU_REGIONS,
},
'size': {'default': None,
'choices': _DYNO_SIZES,
},
'stack': {'default': 'cedar',
'choices': ('cedar', ),
},
'settings': {'required': False,
'type': 'dict',
'default': {},
},
'uppercase': {'required': False,
'type': 'bool',
'default': False,
},
# Dyno types comes from the Procile, no possibility to
# change it.
# 'workload': {'required': False,
# 'choices': _DYNO_WORKLOADS,
# }
}
def _passive(verb):
if verb[-1] in ['p']:
return "{}{}ed".format(verb, verb[-1])
elif verb[-1] in ['t']:
return '{}ed'.format(verb)
return '{}d'.format(verb)
def _check_app(module, client, app, exists=True):
apps = client.apps()
if app not in apps and exists:
module.fail_json(msg="No such application: `{}'".format(app))
else:
return apps.get(app)
def _check_args(module, command, kwargs):
extras = []
from StringIO import StringIO
for arg, value in kwargs.iteritems():
if value != _ARGS_SPEC[arg].get('default'):
extras.append(arg)
if extras:
module.fail_json(msg=("Unexpected arguments for command '{}': {}"
.format(command, ', '.join(extras))
)
)
def _scale_app(module, hk_app, quantity=None, size=None, **kwargs):
"""Given an app, up- or down-scales it.
If original quantity was 0 and the new one is positive, then this
starts the application.
Conversely, if the original quantity is positive and the new one is
zero, then this stops the application.
Args:
module (AnsibleModule): the Ansible module instance
hk_app (heroku3.models.App): the application to scale
quantity (int): the number of dynos required to be present.
size (int): the type of Dyno to use.
Returns:
None:
"""
action = None
try:
formation = hk_app.process_formation() # Get the "formation"
# Encapsulation violation: implement __len__ on KeyedListResources
formation_count = len(formation._items)
if formation_count > 1:
module.fail_json(msg="Unhandled use case: application with several process formations")
if formation_count == 0:
module.fail_json(msg="App `{app}' has process formation: have you deployed some code on it ?")
formation = formation[0]
if formation.quantity < quantity:
if formation.quantity == 0:
action = 'start'
else:
action = 'up-scale'
elif formation.quantity > quantity:
if quantity == 0:
action = 'stop'
else:
action = 'down-scale'
if formation and (formation.quantity != quantity or size != formation.size):
formation.update(size=size, quantity=quantity)
except HTTPError as e:
module.fail_json(msg="Could not {action} app `{app}': {error}"
.format(action=action, app=hk_app.name, error=e))
else:
if action is not None:
msg = ('App {app} successfully {action}.'
.format(app=hk_app.name, action=_passive(action))
)
else:
msg = "App `{app}' left unchanged.".format(app=hk_app.name)
module.exit_json(changed=action is not None, msg=msg)
def _apps(module, client, app=None, **kwargs):
pass
def _config(module, client, app=None, settings=None, uppercase=True, **kwargs):
"""
"""
_check_args(module, 'config', kwargs)
hk_app = _check_app(module, client, app)
results = []
unknown = object()
success = True
try:
current_settings = hk_app.config()
current_settings = current_settings.data
except Exception as e: # Better handle error
success = False
message = ("Failed to update `{}' application's configuration: {}"
.format(app, e)
)
if not success:
module.fail_json(msg=message)
else:
changed = False
actual_settings = {}
for variable, new_value in settings.iteritems():
if uppercase is True:
actual_variable = variable.upper()
else:
actual_variable = variable
actual_settings[actual_variable] = str(new_value)
old_value = current_settings.get(variable, unknown)
changed = old_value is unknown or str(new_value) != old_value
hk_app.update_config(actual_settings)
module.exit_json(changed=changed,
)
def _create(module, client, app=None, region=None, stack=None, **kwargs):
"""Creates a new Heroku application
The presence of the ``stack`` argument is a feeble attempt to make
this module future-proof: there is only one stack available right
now.
Args:
module (AnsibleModule): the Ansible module instance
client (heroku3.api.Heroku): the Heroku API client
app (str): the name of the app to create
stack (str): the name of the Heroku stack to use
region (str): the Heroku region in which host the the app
"""
_check_args(module, 'create', kwargs)
hk_app = _check_app(module, client, app, exists=False)
if hk_app is None:
try:
hk_app = client.create_app(name=app, stack_id_or_name=stack, region_id_or_name=region)
module.exit_json(changed=True, heroku_app=_convert_facts(hk_app))
except HTTPError as e:
module.fail_json(msg="Could not create App {app}: {error}"
.format(app=app, error=e))
else:
module.exit_json(changed=False,
msg="App `{app}' already exists".format(app=app),
heroku_app=_convert_facts(hk_app))
def _convert_facts(fact, exclude=None):
result = {}
exclude = exclude or ['app', 'info', 'order_by', ]
attributes = dir(fact)
for attr in attributes:
if attr.startswith('_') or attr in exclude:
continue
value = getattr(fact, attr)
if callable(value):
continue
elif isinstance(value, BaseResource):
result[attr] = _convert_facts(value, exclude=exclude)
else:
result[attr] = value
return result
def _delete(module, client, app=None, **kwargs):
_check_args(module, 'delete', kwargs)
hk_app = _check_app(module, client, app)
try:
hk_app.delete()
except HTTPError as e:
module.fail_json(msg="Fail to delete App `{}'.".format(app))
else:
module.exit_json(changed=True, msg="App `{}' deleted.".format(app))
def _facts(module, client, app=None, **kwargs):
_check_args(module, 'facts', kwargs)
hk_app = _check_app(module, client, app)
facts = _convert_facts(hk_app)
module.exit_json(changed=True, heroku_app=facts)
def _get_facts(hk_app):
results = dict()
return {'heroku_app': {'name': app,
'id': hk_app.id,
'raw': data,
},
}
def _scale(module, client, app=None, size=None, count=None, workload=None, **kwargs):
_check_args(module, 'scale', kwargs)
hk_app = _check_app(module, client, app)
if count < 0:
module.fail_json(msg="Value of the `count' argument must be positive.")
_scale_app(module, hk_app, quantity=count, size=size)
def _start(module, client, app=None, size=None, count=None, **kwargs):
_check_args(module, 'start', kwargs)
if count <= 0:
module.fail_json(msg="Value of the `count' argument must be strictly positive.")
_scale(module, client, app=app, size=size, count=count, **kwargs)
def _stop(module, client, app=None, **kwargs):
print(kwargs, file=sys.stderr)
_check_args(module, 'stop', kwargs)
kwargs.pop('count')
_scale(module, client, app=app, count=0, **kwargs)
def main():
global _ARGS_SPEC
global _COMMANDS
_HANDLERS = [_apps, _config, _create, _delete, _facts, _scale, _start, _stop, ]
_COMMAND_HANDLERS = dict(zip(_COMMANDS, _HANDLERS))
module = AnsibleModule(argument_spec=_ARGS_SPEC,
supports_check_mode=False,
)
p = module.params
client = heroku3.from_key(p['apikey'])
del p['apikey'] # So it does not appear in logs.
command = _COMMAND_HANDLERS[p['command']]
results = command(module, client, **{k: v for k, v in module.params.iteritems() if k != 'command'})
if __name__ == '__main__':
main()
# vim: syntax=python:sws=4:sw=4:et: