Debian Codegraph @

lib/ansible/utils/galaxy.py

raw
1 # (c) 2014 Michael DeHaan, <[email protected]>
2 #
3 # This file is part of Ansible
4 #
5 # Ansible is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Ansible is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Ansible. If not, see <http://www.gnu.org/licenses/>.
17
18 from __future__ import annotations
19
20 import os
21 import tempfile
22 from subprocess import Popen, PIPE
23 import tarfile
24
25 import ansible.constants as C
26 from ansible import context
27 from ansible.errors import AnsibleError
28 from ansible.utils.display import Display
29 from ansible.module_utils.common.process import get_bin_path
30 from ansible.module_utils.common.text.converters import to_text, to_native
31
32
33 display = Display()
34
35
36 def scm_archive_collection(src, name=None, version='HEAD'):
37 return scm_archive_resource(src, scm='git', name=name, version=version, keep_scm_meta=False)
38
39
40 def scm_archive_resource(src, scm='git', name=None, version='HEAD', keep_scm_meta=False):
41
42 def run_scm_cmd(cmd, tempdir):
43 try:
44 stdout = ''
45 stderr = ''
46 popen = Popen(cmd, cwd=tempdir, stdout=PIPE, stderr=PIPE)
47 stdout, stderr = popen.communicate()
48 except Exception as e:
49 ran = " ".join(cmd)
50 display.debug("ran %s:" % ran)
51 raise AnsibleError("when executing %s: %s" % (ran, to_native(e)))
52 if popen.returncode != 0:
53 raise AnsibleError("- command %s failed in directory %s (rc=%s) - %s" % (' '.join(cmd), tempdir, popen.returncode, to_native(stderr)))
54
55 if scm not in ['hg', 'git']:
56 raise AnsibleError("- scm %s is not currently supported" % scm)
57
58 try:
59 scm_path = get_bin_path(scm)
60 except (ValueError, OSError) as ex:
61 raise AnsibleError(f"Could not find/use {scm!r}, it is required to continue with installing {src!r}.") from ex
62
63 tempdir = tempfile.mkdtemp(dir=C.DEFAULT_LOCAL_TMP)
64 clone_cmd = [scm_path, 'clone']
65
66 # Add specific options for ignoring certificates if requested
67 ignore_certs = context.CLIARGS['ignore_certs'] or C.GALAXY_IGNORE_CERTS
68
69 if ignore_certs:
70 if scm == 'git':
71 clone_cmd.extend(['-c', 'http.sslVerify=false'])
72 elif scm == 'hg':
73 clone_cmd.append('--insecure')
74
75 clone_cmd.extend(['--', src, name])
76
77 run_scm_cmd(clone_cmd, tempdir)
78
79 if scm == 'git' and version:
80 checkout_cmd = [scm_path, 'checkout', to_text(version)]
81 run_scm_cmd(checkout_cmd, os.path.join(tempdir, name))
82
83 temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.tar', dir=C.DEFAULT_LOCAL_TMP)
84 archive_cmd = None
85 if keep_scm_meta:
86 display.vvv('tarring %s from %s to %s' % (name, tempdir, temp_file.name))
87 with tarfile.open(temp_file.name, "w") as tar:
88 tar.add(os.path.join(tempdir, name), arcname=name)
89 elif scm == 'hg':
90 archive_cmd = [scm_path, 'archive', '--prefix', "%s/" % name]
91 if version:
92 archive_cmd.extend(['-r', version])
93 archive_cmd.append(temp_file.name)
94 elif scm == 'git':
95 archive_cmd = [scm_path, 'archive', '--prefix=%s/' % name, '--output=%s' % temp_file.name]
96 if version:
97 archive_cmd.append(version)
98 else:
99 archive_cmd.append('HEAD')
100
101 if archive_cmd is not None:
102 display.vvv('archiving %s' % archive_cmd)
103 run_scm_cmd(archive_cmd, os.path.join(tempdir, name))
104
105 return temp_file.name