Skip to content

Commit 4fedf88

Browse files
authored
Merge pull request #595 from jordemort/buildah
Add buildah transport
2 parents de12097 + d71bdd1 commit 4fedf88

File tree

9 files changed

+186
-2
lines changed

9 files changed

+186
-2
lines changed

ansible_mitogen/connection.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,20 @@ def _connect_ssh(spec):
154154
}
155155
}
156156

157+
def _connect_buildah(spec):
158+
"""
159+
Return ContextService arguments for a Buildah connection.
160+
"""
161+
return {
162+
'method': 'buildah',
163+
'kwargs': {
164+
'username': spec.remote_user(),
165+
'container': spec.remote_addr(),
166+
'python_path': spec.python_path(),
167+
'connect_timeout': spec.ansible_ssh_timeout() or spec.timeout(),
168+
'remote_name': get_remote_name(spec),
169+
}
170+
}
157171

158172
def _connect_docker(spec):
159173
"""
@@ -373,6 +387,7 @@ def _connect_mitogen_doas(spec):
373387
#: generating ContextService keyword arguments matching a connection
374388
#: specification.
375389
CONNECTION_METHOD = {
390+
'buildah': _connect_buildah,
376391
'docker': _connect_docker,
377392
'kubectl': _connect_kubectl,
378393
'jail': _connect_jail,
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Copyright 2019, David Wilson
2+
#
3+
# Redistribution and use in source and binary forms, with or without
4+
# modification, are permitted provided that the following conditions are met:
5+
#
6+
# 1. Redistributions of source code must retain the above copyright notice,
7+
# this list of conditions and the following disclaimer.
8+
#
9+
# 2. Redistributions in binary form must reproduce the above copyright notice,
10+
# this list of conditions and the following disclaimer in the documentation
11+
# and/or other materials provided with the distribution.
12+
#
13+
# 3. Neither the name of the copyright holder nor the names of its contributors
14+
# may be used to endorse or promote products derived from this software without
15+
# specific prior written permission.
16+
#
17+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18+
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20+
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21+
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22+
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23+
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24+
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25+
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26+
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27+
# POSSIBILITY OF SUCH DAMAGE.
28+
29+
from __future__ import absolute_import
30+
import os.path
31+
import sys
32+
33+
try:
34+
import ansible_mitogen
35+
except ImportError:
36+
base_dir = os.path.dirname(__file__)
37+
sys.path.insert(0, os.path.abspath(os.path.join(base_dir, '../../..')))
38+
del base_dir
39+
40+
import ansible_mitogen.connection
41+
42+
43+
class Connection(ansible_mitogen.connection.Connection):
44+
transport = 'buildah'

ansible_mitogen/strategy.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,8 @@ def wrap_connection_loader__get(name, *args, **kwargs):
139139
While the strategy is active, rewrite connection_loader.get() calls for
140140
some transports into requests for a compatible Mitogen transport.
141141
"""
142-
if name in ('docker', 'kubectl', 'jail', 'local', 'lxc',
143-
'lxd', 'machinectl', 'setns', 'ssh'):
142+
if name in ('buildah', 'docker', 'kubectl', 'jail', 'local',
143+
'lxc', 'lxd', 'machinectl', 'setns', 'ssh'):
144144
name = 'mitogen_' + name
145145
return connection_loader__get(name, *args, **kwargs)
146146

ansible_mitogen/transport_config.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,12 @@ def mitogen_mask_remote_name(self):
240240
undesirable in some circumstances.
241241
"""
242242

243+
@abc.abstractmethod
244+
def mitogen_buildah_path(self):
245+
"""
246+
The path to the "buildah" program for the 'buildah' transport.
247+
"""
248+
243249
@abc.abstractmethod
244250
def mitogen_docker_path(self):
245251
"""
@@ -424,6 +430,9 @@ def mitogen_kind(self):
424430
def mitogen_mask_remote_name(self):
425431
return self._connection.get_task_var('mitogen_mask_remote_name')
426432

433+
def mitogen_buildah_path(self):
434+
return self._connection.get_task_var('mitogen_buildah_path')
435+
427436
def mitogen_docker_path(self):
428437
return self._connection.get_task_var('mitogen_docker_path')
429438

@@ -647,6 +656,9 @@ def mitogen_kind(self):
647656
def mitogen_mask_remote_name(self):
648657
return self._host_vars.get('mitogen_mask_remote_name')
649658

659+
def mitogen_buildah_path(self):
660+
return self._host_vars.get('mitogen_buildah_path')
661+
650662
def mitogen_docker_path(self):
651663
return self._host_vars.get('mitogen_docker_path')
652664

mitogen/buildah.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# Copyright 2019, David Wilson
2+
#
3+
# Redistribution and use in source and binary forms, with or without
4+
# modification, are permitted provided that the following conditions are met:
5+
#
6+
# 1. Redistributions of source code must retain the above copyright notice,
7+
# this list of conditions and the following disclaimer.
8+
#
9+
# 2. Redistributions in binary form must reproduce the above copyright notice,
10+
# this list of conditions and the following disclaimer in the documentation
11+
# and/or other materials provided with the distribution.
12+
#
13+
# 3. Neither the name of the copyright holder nor the names of its contributors
14+
# may be used to endorse or promote products derived from this software without
15+
# specific prior written permission.
16+
#
17+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18+
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20+
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21+
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22+
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23+
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24+
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25+
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26+
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27+
# POSSIBILITY OF SUCH DAMAGE.
28+
29+
# !mitogen: minify_safe
30+
31+
import logging
32+
33+
import mitogen.core
34+
import mitogen.parent
35+
36+
37+
LOG = logging.getLogger(__name__)
38+
39+
40+
class Stream(mitogen.parent.Stream):
41+
child_is_immediate_subprocess = False
42+
43+
container = None
44+
username = None
45+
buildah_path = 'buildah'
46+
47+
# TODO: better way of capturing errors such as "No such container."
48+
create_child_args = {
49+
'merge_stdio': True
50+
}
51+
52+
def construct(self, container=None,
53+
buildah_path=None, username=None,
54+
**kwargs):
55+
assert container or image
56+
super(Stream, self).construct(**kwargs)
57+
if container:
58+
self.container = container
59+
if buildah_path:
60+
self.buildah_path = buildah_path
61+
if username:
62+
self.username = username
63+
64+
def _get_name(self):
65+
return u'buildah.' + self.container
66+
67+
def get_boot_command(self):
68+
args = []
69+
if self.username:
70+
args += ['--user=' + self.username]
71+
bits = [self.buildah_path, 'run'] + args + ['--', self.container]
72+
73+
return bits + super(Stream, self).get_boot_command()

mitogen/core.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1089,6 +1089,7 @@ class Importer(object):
10891089
# The Mitogen package is handled specially, since the child context must
10901090
# construct it manually during startup.
10911091
MITOGEN_PKG_CONTENT = [
1092+
'buildah',
10921093
'compat',
10931094
'debug',
10941095
'doas',

mitogen/parent.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2163,6 +2163,9 @@ def proxy_connect(self, via_context, method_name, name=None, **kwargs):
21632163
self._write_lock.release()
21642164
return context
21652165

2166+
def buildah(self, **kwargs):
2167+
return self.connect(u'buildah', **kwargs)
2168+
21662169
def doas(self, **kwargs):
21672170
return self.connect(u'doas', **kwargs)
21682171

tests/buildah_test.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import os
2+
3+
import mitogen
4+
5+
import unittest2
6+
7+
import testlib
8+
9+
10+
class ConstructorTest(testlib.RouterMixin, testlib.TestCase):
11+
def test_okay(self):
12+
buildah_path = testlib.data_path('stubs/stub-buildah.py')
13+
context = self.router.buildah(
14+
container='container_name',
15+
buildah_path=buildah_path,
16+
)
17+
stream = self.router.stream_by_id(context.context_id)
18+
19+
argv = eval(context.call(os.getenv, 'ORIGINAL_ARGV'))
20+
self.assertEquals(argv[0], buildah_path)
21+
self.assertEquals(argv[1], 'run')
22+
self.assertEquals(argv[2], '--')
23+
self.assertEquals(argv[3], 'container_name')
24+
self.assertEquals(argv[4], stream.python_path)
25+
26+
27+
if __name__ == '__main__':
28+
unittest2.main()

tests/data/stubs/stub-buildah.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/env python
2+
3+
import sys
4+
import os
5+
6+
os.environ['ORIGINAL_ARGV'] = repr(sys.argv)
7+
os.environ['THIS_IS_STUB_BUILDAH'] = '1'
8+
os.execv(sys.executable, sys.argv[sys.argv.index('--') + 2:])

0 commit comments

Comments
 (0)