Skip to content

Commit 123739f

Browse files
committed
Update and cleanup common_environment.py
1 parent c1d84ef commit 123739f

File tree

1 file changed

+61
-40
lines changed

1 file changed

+61
-40
lines changed

ctf_cli/common_environment_content.py

+61-40
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import inspect
1212
import copy
1313

14+
1415
def _invoke_other_environment_hooks(*args):
1516
# we need a copy of current frame becase it will change
1617
frame_globals = copy.copy(inspect.currentframe().f_globals)
@@ -47,89 +48,109 @@ def open_file(path):
4748
ret_file = open(value['dest'])
4849
return ret_file
4950
except KeyError:
50-
print ("ansible output: {0}".format(ret))
51+
print("ansible output: {0}".format(ret))
5152
raise Exception(value['msg'])
5253
context.open_file = open_file
5354

5455
def run(command):
55-
logging.debug("Running '%s'" % command)
56+
if '{{' in command:
57+
command = command.replace("{{", "{{ '{{").replace("}}", "}}' }}")
58+
logging.info("Running '%s'" % command)
5659
context.result = ansible.runner.Runner(
57-
module_name='shell',
60+
module_name="shell",
5861
inventory=inventory,
5962
module_args="{0} chdir={1}".format(command, remote_dir)
6063
).run()
61-
passed = True
6264
# dark means not responding
6365
if context.result['dark']:
64-
passed = False
65-
print("dark")
6666
print(context.result)
6767
if not context.result['contacted']:
68-
passed = False
69-
print ("no contacted hosts")
68+
print("no contacted hosts")
7069
for host, values in context.result['contacted'].iteritems():
7170
if values['rc'] != 0:
7271
print("On {0} returned {1}".format(host, values['rc']))
7372
print("stderr: {0}".format(values['stderr']))
7473
print("cmd: {0}".format(values['cmd']))
7574
assert False
75+
logging.info('stdout:\\n%s' % values['stdout'])
76+
if values['stderr']:
77+
logging.info('stderr\\n:%s' % values['stderr'])
7678
return values['stdout']
7779
context.run = run
7880

79-
# copy dockerfile
80-
dockerfile = context.config.userdata['DOCKERFILE']
81-
dockerfile_dir = os.path.dirname(dockerfile)
82-
# create remote directory
83-
ansible.runner.Runner(
84-
module_name='file',
85-
inventory=inventory,
86-
module_args='dest={0} state=directory'.format(remote_dir)
87-
).run()
88-
# copy dockerfile
89-
ansible.runner.Runner(
90-
module_name='copy',
91-
inventory=inventory,
92-
module_args='src={0} dest={1}'.format(dockerfile, remote_dir)
93-
).run()
94-
# copy files from dockerfile
95-
f_in = open(dockerfile)
96-
for path in re.findall('(?:ADD|COPY) ([^ ]+) ', f_in.read()):
97-
for glob_path in glob.glob(os.path.join(dockerfile_dir,path)):
98-
# TODO Is there a nicer way to keep permissions?
81+
def copy_dockerfile():
82+
83+
try:
84+
# copy dockerfile
85+
dockerfile = context.config.userdata['DOCKERFILE']
86+
dockerfile_dir = os.path.dirname(dockerfile)
87+
# create remote directory
88+
ansible.runner.Runner(
89+
module_name='file',
90+
inventory=inventory,
91+
module_args='dest={0} state=directory'.format(remote_dir)
92+
).run()
93+
# copy dockerfile
9994
ansible.runner.Runner(
10095
module_name='copy',
10196
inventory=inventory,
102-
module_args='src={0} dest={1} directory_mode mode={2}'.format(glob_path, remote_dir,
103-
oct(stat.S_IMODE(os.stat(glob_path).st_mode)))
104-
).run()
97+
module_args='src={0} dest={1}'.format(dockerfile, remote_dir)
98+
).run()
99+
# copy files from dockerfile
100+
f_in = open(dockerfile)
101+
for path in re.findall('(?:ADD|COPY) ([^ ]+) ', f_in.read()):
102+
for glob_path in glob.glob(os.path.join(dockerfile_dir, path)):
103+
# TODO Is there a nicer way to keep permissions?
104+
ansible.runner.Runner(
105+
module_name='copy',
106+
inventory=inventory,
107+
module_args='src={0} dest={1} directory_mode mode={2}'.format(
108+
glob_path, remote_dir,
109+
oct(stat.S_IMODE(os.stat(glob_path).st_mode)))
110+
).run()
111+
except Exception as e:
112+
logging.warning("copy_dockerfile:%s" % e)
113+
114+
copy_dockerfile()
105115

106116
# build image if not exist
107117
try:
108118
context.image = context.config.userdata['IMAGE']
119+
run('docker pull {0}'.format(context.image))
120+
except AssertionError:
121+
pass
109122
except KeyError:
110123
context.image = 'ctf'
111-
run('docker build -t {0} .'.format(context.image))
124+
try:
125+
run('docker build -t {0} .'.format(context.image))
126+
except AssertionError:
127+
pass
112128

113129
cid_file_name = re.sub(r'\W+', '', context.image)
114130
context.cid_file = "/tmp/%s.cid" % cid_file_name
115131

132+
116133
def after_scenario(context, scenario):
117-
_invoke_other_environment_hooks(context ,scenario)
134+
_invoke_other_environment_hooks(context, scenario)
118135
try:
119136
if context.config.userdata['KEEP_CONTAINER_AFTER_TEST']:
120137
return
121-
except KeyError, e:
138+
except KeyError as e:
122139
pass
123140

124141
try:
125142
cid = context.run('cat %s' % context.cid_file)
126-
except AssertionError, e:
127-
logging.debug("before_scenario: {0}".format(e))
143+
except AssertionError as e:
144+
logging.info("before_scenario: {0}".format(e))
128145
return
129146
if cid:
130-
context.run("docker stop %s" % cid)
131-
context.run("docker kill %s" % cid)
132-
context.run("docker rm %s" % cid)
147+
context.run("docker logs %s" % cid)
148+
try:
149+
context.run("docker stop %s" % cid)
150+
context.run("docker kill %s" % cid)
151+
context.run("docker rm -v %s" % cid)
152+
except AssertionError:
153+
pass
133154
if hasattr(context, 'cid'):
134155
del context.cid
135156
context.run('rm {0}'.format(context.cid_file))
@@ -138,4 +159,4 @@ def after_scenario(context, scenario):
138159
def after_all(context):
139160
_invoke_other_environment_hooks(context)
140161
if hasattr(context, 'temp_dir'):
141-
shutil.rmtree(context.temp_dir) #FIXME catch exception
162+
shutil.rmtree(context.temp_dir) # FIXME catch exception

0 commit comments

Comments
 (0)