Debian Codegraph @

ansible_collections/community/general/plugins/connection/iocage.py

raw
1 # Based on jail.py
2 # (c) 2013, Michael Scherer <[email protected]>
3 # (c) 2015, Toshio Kuratomi <[email protected]>
4 # (c) 2016, Stephan Lohse <[email protected]>
5 # Copyright (c) 2017 Ansible Project
6 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
7 # SPDX-License-Identifier: GPL-3.0-or-later
8
9 from __future__ import annotations
10
11 DOCUMENTATION = r"""
12 author: Stephan Lohse (!UNKNOWN) <[email protected]>
13 name: iocage
14 short_description: Run tasks in iocage jails
15 description:
16 - Run commands or put/fetch files to an existing iocage jail.
17 options:
18 remote_addr:
19 description:
20 - Path to the jail.
21 type: string
22 vars:
23 - name: ansible_host
24 - name: ansible_iocage_host
25 remote_user:
26 description:
27 - User to execute as inside the jail.
28 type: string
29 vars:
30 - name: ansible_user
31 - name: ansible_iocage_user
32 """
33
34 import subprocess
35
36 from ansible.errors import AnsibleError
37 from ansible.module_utils.common.text.converters import to_native
38 from ansible.utils.display import Display
39
40 from ansible_collections.community.general.plugins.connection.jail import Connection as Jail
41
42 display = Display()
43
44
45 class Connection(Jail):
46 """Local iocage based connections"""
47
48 transport = "community.general.iocage"
49
50 def __init__(self, play_context, new_stdin, *args, **kwargs):
51 self.ioc_jail = play_context.remote_addr
52
53 self.iocage_cmd = Jail._search_executable("iocage")
54
55 jail_uuid = self.get_jail_uuid()
56
57 kwargs[Jail.modified_jailname_key] = f"ioc-{jail_uuid}"
58
59 display.vvv(
60 f"Jail {self.ioc_jail} has been translated to {kwargs[Jail.modified_jailname_key]}",
61 host=kwargs[Jail.modified_jailname_key],
62 )
63
64 super().__init__(play_context, new_stdin, *args, **kwargs)
65
66 def get_jail_uuid(self):
67 p = subprocess.Popen(
68 [self.iocage_cmd, "get", "host_hostuuid", self.ioc_jail],
69 stdin=subprocess.PIPE,
70 stdout=subprocess.PIPE,
71 stderr=subprocess.STDOUT,
72 )
73
74 stdout, stderr = p.communicate()
75
76 if stdout is not None:
77 stdout = to_native(stdout)
78
79 if stderr is not None:
80 stderr = to_native(stderr)
81
82 # otherwise p.returncode would not be set
83 p.wait()
84
85 if p.returncode != 0:
86 raise AnsibleError(f"iocage returned an error: {stdout}")
87
88 return stdout.strip("\n")