|
23 | 23 | {project_env_py_import}
|
24 | 24 | """
|
25 | 25 |
|
26 |
| -common_environment_py_content = """ |
27 |
| -# Some useful functions for your environment.py |
28 |
| -from types import ModuleType |
29 |
| -import tempfile |
30 |
| -import shutil |
31 |
| -import ansible.runner |
32 |
| -import logging |
33 |
| -import re |
34 |
| -import os |
35 |
| -import glob |
36 |
| -import stat |
37 |
| -import inspect |
38 |
| -import copy |
39 |
| -
|
40 |
| -def _invoke_other_environment_hooks(*args): |
41 |
| - # we need a copy of current frame becase it will change |
42 |
| - frame_globals = copy.copy(inspect.currentframe().f_globals) |
43 |
| - # get which function invoked us |
44 |
| - current_function = inspect.stack()[1][3] |
45 |
| - # iterate for all modules wich ends with _environemnt |
46 |
| - # and execute current method from them |
47 |
| - for g in frame_globals: |
48 |
| - if isinstance(frame_globals[g], ModuleType): |
49 |
| - if frame_globals[g].__name__.endswith("_environment"): |
50 |
| - for func_name, func in inspect.getmembers(frame_globals[g], inspect.isfunction): |
51 |
| - if func_name == current_function: |
52 |
| - func(*args) |
53 |
| -
|
54 |
| -
|
55 |
| -def before_all(context): |
56 |
| - _invoke_other_environment_hooks(context) |
57 |
| - try: |
58 |
| - ansible_cfg = context.config.userdata['ANSIBLE'] |
59 |
| - inventory = ansible.inventory.Inventory(ansible_cfg) |
60 |
| - except KeyError: |
61 |
| - raise Exception("-D ANSIBLE missing") |
62 |
| - remote_dir = '/var/tmp/dkrfile' |
63 |
| -
|
64 |
| - def open_file(path): |
65 |
| - context.temp_dir = tempfile.mkdtemp() |
66 |
| - ret = ansible.runner.Runner( |
67 |
| - module_name='fetch', |
68 |
| - inventory=inventory, |
69 |
| - module_args='src={0} dest={1}'.format( |
70 |
| - path, context.temp_dir)).run() |
71 |
| - for host, value in ret['contacted'].iteritems(): |
72 |
| - try: |
73 |
| - ret_file = open(value['dest']) |
74 |
| - return ret_file |
75 |
| - except KeyError: |
76 |
| - print ("ansible output: {0}".format(ret)) |
77 |
| - raise Exception(value['msg']) |
78 |
| - context.open_file = open_file |
79 |
| -
|
80 |
| - def run(command): |
81 |
| - if '{{' in command: |
82 |
| - command = command.replace("{{", "{{ '{{").replace("}}", "}}' }}") |
83 |
| - logging.info("Running '%s'" % command) |
84 |
| - context.result = ansible.runner.Runner( |
85 |
| - module_name="shell", |
86 |
| - inventory=inventory, |
87 |
| - module_args="{0} chdir={1}".format(command, remote_dir) |
88 |
| - ).run() |
89 |
| - passed = True |
90 |
| - # dark means not responding |
91 |
| - if context.result['dark']: |
92 |
| - passed = False |
93 |
| - print(context.result) |
94 |
| - if not context.result['contacted']: |
95 |
| - passed = False |
96 |
| - print ("no contacted hosts") |
97 |
| - for host, values in context.result['contacted'].iteritems(): |
98 |
| - if values['rc'] != 0: |
99 |
| - print("On {0} returned {1}".format(host, values['rc'])) |
100 |
| - print("stderr: {0}".format(values['stderr'])) |
101 |
| - print("cmd: {0}".format(values['cmd'])) |
102 |
| - assert False |
103 |
| - logging.info('stdout:\\n%s' % values['stdout']) |
104 |
| - if values['stderr']: |
105 |
| - logging.info('stderr\\n:%s' % values['stderr']) |
106 |
| - return values['stdout'] |
107 |
| - context.run = run |
108 |
| -
|
109 |
| - def copy_dockerfile(): |
110 |
| -
|
111 |
| - try: |
112 |
| - # copy dockerfile |
113 |
| - dockerfile = context.config.userdata['DOCKERFILE'] |
114 |
| - dockerfile_dir = os.path.dirname(dockerfile) |
115 |
| - # create remote directory |
116 |
| - ansible.runner.Runner( |
117 |
| - module_name='file', |
118 |
| - inventory=inventory, |
119 |
| - module_args='dest={0} state=directory'.format(remote_dir) |
120 |
| - ).run() |
121 |
| - # copy dockerfile |
122 |
| - ansible.runner.Runner( |
123 |
| - module_name='copy', |
124 |
| - inventory=inventory, |
125 |
| - module_args='src={0} dest={1}'.format(dockerfile, remote_dir) |
126 |
| - ).run() |
127 |
| - # copy files from dockerfile |
128 |
| - f_in = open(dockerfile) |
129 |
| - for path in re.findall('(?:ADD|COPY) ([^ ]+) ', f_in.read()): |
130 |
| - for glob_path in glob.glob(os.path.join(dockerfile_dir,path)): |
131 |
| - # TODO Is there a nicer way to keep permissions? |
132 |
| - ansible.runner.Runner( |
133 |
| - module_name='copy', |
134 |
| - inventory=inventory, |
135 |
| - module_args='src={0} dest={1} directory_mode mode={2}'.format(glob_path, remote_dir, |
136 |
| - oct(stat.S_IMODE(os.stat(glob_path).st_mode))) |
137 |
| - ).run() |
138 |
| - except Exception as e: |
139 |
| - logging.warning("copy_dockerfile:%s" % e) |
140 |
| -
|
141 |
| - copy_dockerfile() |
142 |
| -
|
143 |
| - # build image if not exist |
144 |
| - try: |
145 |
| - context.image = context.config.userdata['IMAGE'] |
146 |
| - run('docker pull {0}'.format(context.image)) |
147 |
| - except AssertionError: |
148 |
| - pass |
149 |
| - except KeyError: |
150 |
| - context.image = 'ctf' |
151 |
| - try: |
152 |
| - run('docker build -t {0} .'.format(context.image)) |
153 |
| - except AssertionError: |
154 |
| - pass |
155 |
| -
|
156 |
| - cid_file_name = re.sub(r'\W+', '', context.image) |
157 |
| - context.cid_file = "/tmp/%s.cid" % cid_file_name |
158 |
| -
|
159 |
| -def after_scenario(context, scenario): |
160 |
| - _invoke_other_environment_hooks(context ,scenario) |
161 |
| - try: |
162 |
| - if context.config.userdata['KEEP_CONTAINER_AFTER_TEST']: |
163 |
| - return |
164 |
| - except KeyError, e: |
165 |
| - pass |
166 |
| -
|
167 |
| - try: |
168 |
| - cid = context.run('cat %s' % context.cid_file) |
169 |
| - except AssertionError, e: |
170 |
| - logging.info("before_scenario: {0}".format(e)) |
171 |
| - return |
172 |
| - if cid: |
173 |
| - context.run("docker logs %s" % cid) |
174 |
| - try: |
175 |
| - context.run("docker stop %s" % cid) |
176 |
| - context.run("docker kill %s" % cid) |
177 |
| - context.run("docker rm -v %s" % cid) |
178 |
| - except AssertionError: |
179 |
| - pass |
180 |
| - if hasattr(context, 'cid'): |
181 |
| - del context.cid |
182 |
| - context.run('rm {0}'.format(context.cid_file)) |
183 |
| -
|
184 |
| -
|
185 |
| -def after_all(context): |
186 |
| - _invoke_other_environment_hooks(context) |
187 |
| - if hasattr(context, 'temp_dir'): |
188 |
| - shutil.rmtree(context.temp_dir) #FIXME catch exception |
189 |
| -
|
190 |
| -""" |
191 | 26 |
|
192 | 27 | sample_ctl_ctf_config = """
|
193 | 28 | [ctf]
|
|
0 commit comments