-
Notifications
You must be signed in to change notification settings - Fork 112
Expand file tree
/
Copy pathcontroller.py
More file actions
49 lines (40 loc) · 1.76 KB
/
controller.py
File metadata and controls
49 lines (40 loc) · 1.76 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
from __future__ import unicode_literals
import subprocess
from celery import task
from celerytasks.chef import ChefAPI
from deis import settings
@task(name='controller.update_gitosis')
def update_gitosis(databag_item_value):
# update the data bag
client = ChefAPI(settings.CHEF_SERVER_URL,
settings.CHEF_CLIENT_NAME,
settings.CHEF_CLIENT_KEY)
client.update_databag_item('deis-build', 'gitosis', databag_item_value)
# call a chef update
subprocess.check_call(
['sudo', 'chef-client', '--override-runlist', 'recipe[deis::gitosis]'])
@task(name='controller.update_formation')
def update_formation(formation_id, databag_item_value):
# update the data bag
client = ChefAPI(settings.CHEF_SERVER_URL,
settings.CHEF_CLIENT_NAME,
settings.CHEF_CLIENT_KEY)
# TODO: move this logic into the chef API
resp, code = client.update_databag_item(
'deis-formations', formation_id, databag_item_value)
if code == 200:
return resp, code
elif code == 404:
resp, code = client.create_databag_item('deis-formations',
formation_id, databag_item_value)
if code != 201:
raise RuntimeError('Failed to create data bag: {code} => {resp}'.format(**locals()))
else:
raise RuntimeError('Failed to update data bag: {code} => {resp}'.format(**locals()))
@task(name='controller.destroy_formation')
def destroy_formation(formation_id):
client = ChefAPI(settings.CHEF_SERVER_URL,
settings.CHEF_CLIENT_NAME,
settings.CHEF_CLIENT_KEY)
_resp, _code = client.delete_databag_item('deis-formations', formation_id)
subprocess.check_call(['sudo', 'chef-client'])