This repository has been archived by the owner on Nov 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
conf_handler.py
93 lines (74 loc) · 2.61 KB
/
conf_handler.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
from string import Template
import os
import subprocess
import time
import numpy as np
from ..config import APPLY_CONF_TEMPLATE_PATH, ARG_KEYS
from ..utils.logger import Logger
logger = Logger(__name__)
_ = logger.get_logger()
class PuppetTemplate(Template):
"""
PuppetTemplate
It inherits from string.Template
reference: https://docs.python.org/3.4/library/string.html#format-examples
This Template will treat %foo as variable named foo.
For example:
PuppetTemplate(abc.txt).substitute(foo="bar") will render abc.txt
with '%foo' replaced by 'bar'
:param template: template of string to be renderred
:type template: str
"""
delimiter = '%'
class ConfHandler():
"""
ConfHandler
:param arg: argument dict to be applied
:type arg: dict
arg = dict(zip(ARG_KEYS, values))
"""
def __init__(self, arg_dict):
self.arg_validator(arg_dict)
arg = {key: str(arg_dict[key]) for key in arg_dict}
try:
self.apply_command = self._build_command(arg)
except FileNotFoundError as e:
_.error(str(e))
def apply(self):
return self._issue_command(self.apply_command)
def arg_validator(self, arg):
for key in ARG_KEYS:
try:
assert (
key in arg), "Invalid arg: missing %s in arguments" % (key)
assert (
type(arg[key]) in [float, np.float32, np.float64]
), "Invalid tye: %s should be float" % (key)
except AssertionError as e:
_.error(str(e))
raise
_.info("Arguments is valid: %s" % (str(arg)))
def _build_command(self, arg):
template_path = os.path.join(
os.path.dirname(__file__),
'..',
APPLY_CONF_TEMPLATE_PATH)
with open(template_path, 'r') as template_file:
template_string = template_file.read()
puppet_string = PuppetTemplate(template_string).substitute(**arg)
command_string = "puppet apply --modulepath=/etc/puppet/modules -e '" + puppet_string + "'"
return command_string
def _issue_command(self, command):
output, error = subprocess.Popen(
command,
universal_newlines=True,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
).communicate()
return output, error
def cleanup(self):
values = [0] * len(ARG_KEYS)
arg = dict(zip(ARG_KEYS, values))
restore_command = self._build_command(arg)
return self._issue_command(restore_command)