Debian Codegraph @

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

raw
1 # Based on local.py (c) 2012, Michael DeHaan <[email protected]>
2 # Based on chroot.py (c) 2013, Maykel Moya <[email protected]>
3 # Copyright (c) 2013, Michael Scherer <[email protected]>
4 # Copyright (c) 2017 Ansible Project
5 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
6 # SPDX-License-Identifier: GPL-3.0-or-later
7
8 from __future__ import annotations
9
10 DOCUMENTATION = r"""
11 author: Michael Scherer (@mscherer) <[email protected]>
12 name: funcd
13 short_description: Use funcd to connect to target
14 description:
15 - This transport permits you to use Ansible over Func.
16 - For people who have already setup func and that wish to play with ansible, this permit to move gradually to ansible without
17 having to redo completely the setup of the network.
18 options:
19 remote_addr:
20 description:
21 - The path of the chroot you want to access.
22 type: string
23 default: inventory_hostname
24 vars:
25 - name: ansible_host
26 - name: ansible_func_host
27 """
28
29 HAVE_FUNC = False
30 try:
31 import func.overlord.client as fc
32
33 HAVE_FUNC = True
34 except ImportError:
35 pass
36
37 import os
38 import shutil
39 import tempfile
40
41 from ansible.errors import AnsibleError
42 from ansible.plugins.connection import ConnectionBase
43 from ansible.utils.display import Display
44
45 display = Display()
46
47
48 class Connection(ConnectionBase):
49 """Func-based connections"""
50
51 has_pipelining = False
52
53 def __init__(self, runner, host, port, *args, **kwargs):
54 self.runner = runner
55 self.host = host
56 # port is unused, this go on func
57 self.port = port
58 self.client = None
59
60 def connect(self, port=None):
61 if not HAVE_FUNC:
62 raise AnsibleError("func is not installed")
63
64 self.client = fc.Client(self.host)
65 return self
66
67 def exec_command(self, cmd, in_data=None, sudoable=True):
68 """run a command on the remote minion"""
69
70 if in_data:
71 raise AnsibleError("Internal Error: this module does not support optimized module pipelining")
72
73 # totally ignores privilege escalation
74 display.vvv(f"EXEC {cmd}", host=self.host)
75 p = self.client.command.run(cmd)[self.host]
76 return p[0], p[1], p[2]
77
78 @staticmethod
79 def _normalize_path(path, prefix):
80 if not path.startswith(os.path.sep):
81 path = os.path.join(os.path.sep, path)
82 normpath = os.path.normpath(path)
83 return os.path.join(prefix, normpath[1:])
84
85 def put_file(self, in_path, out_path):
86 """transfer a file from local to remote"""
87
88 out_path = self._normalize_path(out_path, "/")
89 display.vvv(f"PUT {in_path} TO {out_path}", host=self.host)
90 self.client.local.copyfile.send(in_path, out_path)
91
92 def fetch_file(self, in_path, out_path):
93 """fetch a file from remote to local"""
94
95 in_path = self._normalize_path(in_path, "/")
96 display.vvv(f"FETCH {in_path} TO {out_path}", host=self.host)
97 # need to use a tmp dir due to difference of semantic for getfile
98 # ( who take a # directory as destination) and fetch_file, who
99 # take a file directly
100 tmpdir = tempfile.mkdtemp(prefix="func_ansible")
101 self.client.local.getfile.get(in_path, tmpdir)
102 shutil.move(os.path.join(tmpdir, self.host, os.path.basename(in_path)), out_path)
103 shutil.rmtree(tmpdir)
104
105 def close(self):
106 """terminate the connection; nothing to do here"""
107 pass