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