-
Notifications
You must be signed in to change notification settings - Fork 0
/
TaskHandler.py
65 lines (55 loc) · 2.5 KB
/
TaskHandler.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
import subprocess
import shutil
import os
import jinja2
class TaskHandler(object):
"""
Actions to configure the local node as described by role configuration file
:param attributes: dict of attributes including template variables
"""
def __init__(self, runbook, roles_dir):
self.attributes = runbook['attributes']
self.templates_dir = '{}/templates'.format(roles_dir)
self.jinja_environmenent = jinja2.Environment(loader=jinja2.FileSystemLoader(self.templates_dir))
self.roles_dir = roles_dir
def handle_task(self, task):
if task['action'] == 'commands':
output = ""
for command in task['commands']:
try:
output += self.run_command(command)
except Exception as e:
raise e
return output
elif task['action'] == 'file':
output = self.copy_file(source=task['source'], destination=task['destination'], owner=task['owner'],
group=task['group'], mode=task['mode'])
return output
elif task['action'] == 'template':
output = self.copy_template_to_file(source=task['source'],
destination=task['destination'], attributes=self.attributes)
return output
def run_command(self, command):
try:
output = subprocess.check_output(command, universal_newlines=True, shell=True, timeout=60)
except subprocess.CalledProcessError:
raise
except subprocess.TimeoutExpired:
raise
return '<- {}'.format(output)
def copy_file(self, source, destination, owner='root', group='root', mode='640'):
'''
:param source: location of file relative to repo root
:param destination: full path to destination on local fs
:return: 0 if success else exception
'''
path = '{}/files/{}'.format(self.roles_dir, source)
shutil.copyfile(path, destination)
shutil.chown(destination, user=owner, group=group)
os.chmod(destination, mode)
return '<- Copied {} to {}'.format(source, destination)
def copy_template_to_file(self, source, destination, attributes):
template = self.jinja_environmenent.get_template(source)
with open(destination, 'w') as f:
f.write(template.render(attributes=attributes))
return '<- wrote file from template: {}'.format(destination)