1
1
# Some useful functions for your environment.py
2
2
import tempfile
3
3
import shutil
4
- import ansible .runner
5
4
import logging
6
5
import re
7
6
import os
8
7
import glob
9
8
import stat
10
9
10
+ from ansible_runner import Runner
11
+
11
12
12
13
def sample_before_all (context ):
13
14
docker_setup (context )
@@ -31,22 +32,18 @@ def docker_setup(context):
31
32
32
33
# Read ansible inventory from config
33
34
ansible_cfg = None
34
- inventory = None
35
35
36
36
logging .info ("Reading ansible config" )
37
37
try :
38
38
ansible_cfg = context .config .userdata ['ANSIBLE' ]
39
39
except KeyError :
40
40
raise Exception ("-D ANSIBLE missing" )
41
41
42
- inventory = ansible .inventory .Inventory (ansible_cfg )
43
- logging .info ("Ansible inventory is set\n " )
44
-
45
42
def open_file (path ):
46
43
context .temp_dir = tempfile .mkdtemp ()
47
- ret = ansible . runner . Runner (
44
+ ret = Runner (
48
45
module_name = 'fetch' ,
49
- inventory = inventory ,
46
+ inventory_file = ansible_cfg ,
50
47
module_args = 'src={0} dest={1}' .format (
51
48
path , context .temp_dir )).run ()
52
49
for _ , value in ret ['contacted' ].iteritems ():
@@ -61,19 +58,21 @@ def open_file(path):
61
58
def run (command ):
62
59
if '{{' in command :
63
60
command = command .replace ("{{" , "{{ '{{" ).replace ("}}" , "}}' }}" )
61
+ if '=' in command :
62
+ command = command .replace ('=' , '\=' )
64
63
logging .info ("Running '%s'" , command )
65
- context .result = ansible . runner . Runner (
64
+ context .result = Runner (
66
65
module_name = "shell" ,
67
- inventory = inventory ,
66
+ inventory_file = ansible_cfg ,
68
67
module_args = "{0} chdir={1}" .format (command , context .remote_dir )
69
68
).run ()
69
+
70
70
# dark means not responding
71
- if context . result [ 'dark' ] :
71
+ if 'dark' in context . result :
72
72
print (context .result )
73
- if not context .result ['contacted' ]:
74
- print ("no contacted hosts" )
75
73
for host , values in context .result ['contacted' ].iteritems ():
76
- logging .info ("On {0} returned {1}" .format (host , values ['rc' ]))
74
+ retvalue = values .get ('rc' )
75
+ logging .info ("On {0} returned {1}" .format (host , retvalue ))
77
76
78
77
if 'cmd' in values :
79
78
logging .info ("cmd: {0}" .format (values ['cmd' ]))
@@ -88,7 +87,7 @@ def run(command):
88
87
logging .info ('stdout:%s' , values ['stdout' ])
89
88
result = values ['stdout' ]
90
89
91
- if values [ 'rc' ] != 0 :
90
+ if 'failed' in values :
92
91
assert False
93
92
return result
94
93
context .run = run
@@ -99,25 +98,25 @@ def copy_dockerfile():
99
98
dockerfile = context .config .userdata ['DOCKERFILE' ]
100
99
dockerfile_dir = os .path .dirname (dockerfile )
101
100
# create remote directory
102
- ansible .runner .Runner (
101
+ Runner (
102
+ inventory_file = ansible_cfg ,
103
103
module_name = 'file' ,
104
- inventory = inventory ,
105
104
module_args = 'dest={0} state=directory' .format (context .remote_dir )
106
105
).run ()
107
106
# copy dockerfile
108
- ansible .runner .Runner (
107
+ Runner (
108
+ inventory_file = ansible_cfg ,
109
109
module_name = 'copy' ,
110
- inventory = inventory ,
111
110
module_args = 'src={0} dest={1}' .format (dockerfile , context .remote_dir )
112
111
).run ()
113
112
# copy files from dockerfile
114
113
f_in = open (dockerfile )
115
114
for path in re .findall ('(?:ADD|COPY) ([^ ]+) ' , f_in .read ()):
116
115
for glob_path in glob .glob (os .path .join (dockerfile_dir , path )):
117
116
# TODO Is there a nicer way to keep permissions?
118
- ansible .runner .Runner (
117
+ Runner (
118
+ inventory_file = ansible_cfg ,
119
119
module_name = 'copy' ,
120
- inventory = inventory ,
121
120
module_args = 'src={0} dest={1} directory_mode mode={2}' .format (
122
121
glob_path , context .remote_dir ,
123
122
oct (stat .S_IMODE (os .stat (glob_path ).st_mode )))
0 commit comments