Debian Codegraph @

lib/ansible/playbook/role/requirement.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 from ansible.errors import AnsibleError
21 from ansible.playbook.role.definition import RoleDefinition
22 from ansible.utils.display import Display
23 from ansible.utils.galaxy import scm_archive_resource
24
25 __all__ = ['RoleRequirement']
26
27 VALID_SPEC_KEYS = [
28 'name',
29 'role',
30 'scm',
31 'src',
32 'version',
33 ]
34
35 display = Display()
36
37
38 class RoleRequirement(RoleDefinition):
39
40 """
41 Helper class for Galaxy, which is used to parse both dependencies
42 specified in meta/main.yml and requirements.yml files.
43 """
44
45 def __init__(self):
46 pass
47
48 @staticmethod
49 def repo_url_to_role_name(repo_url):
50 # gets the role name out of a repo like
51 # http://git.example.com/repos/repo.git" => "repo"
52
53 if '://' not in repo_url and '@' not in repo_url:
54 return repo_url
55 trailing_path = repo_url.split('/')[-1]
56 if trailing_path.endswith('.git'):
57 trailing_path = trailing_path[:-4]
58 if trailing_path.endswith('.tar.gz'):
59 trailing_path = trailing_path[:-7]
60 if ',' in trailing_path:
61 trailing_path = trailing_path.split(',')[0]
62 return trailing_path
63
64 @staticmethod
65 def role_yaml_parse(role):
66
67 if isinstance(role, str):
68 name = None
69 scm = None
70 src = None
71 version = None
72 if ',' in role:
73 if role.count(',') == 1:
74 (src, version) = role.strip().split(',', 1)
75 elif role.count(',') == 2:
76 (src, version, name) = role.strip().split(',', 2)
77 else:
78 raise AnsibleError("Invalid role line (%s). Proper format is 'role_name[,version[,name]]'" % role)
79 else:
80 src = role
81
82 if name is None:
83 name = RoleRequirement.repo_url_to_role_name(src)
84 if '+' in src:
85 (scm, src) = src.split('+', 1)
86
87 return dict(name=name, src=src, scm=scm, version=version)
88
89 if 'role' in role:
90 name = role['role']
91 if ',' in name:
92 raise AnsibleError("Invalid old style role requirement: %s" % name)
93 else:
94 del role['role']
95 role['name'] = name
96 else:
97 role = role.copy()
98
99 if 'src' in role:
100 # New style: { src: 'galaxy.role,version,name', other_vars: "here" }
101 if 'github.com' in role["src"] and 'http' in role["src"] and '+' not in role["src"] and not role["src"].endswith('.tar.gz'):
102 role["src"] = "git+" + role["src"]
103
104 if '+' in role["src"]:
105 role["scm"], dummy, role["src"] = role["src"].partition('+')
106
107 if 'name' not in role:
108 role["name"] = RoleRequirement.repo_url_to_role_name(role["src"])
109
110 if 'version' not in role:
111 role['version'] = ''
112
113 if 'scm' not in role:
114 role['scm'] = None
115
116 for key in list(role.keys()):
117 if key not in VALID_SPEC_KEYS:
118 role.pop(key)
119
120 return role
121
122 @staticmethod
123 def scm_archive_role(src, scm='git', name=None, version='HEAD', keep_scm_meta=False):
124
125 return scm_archive_resource(src, scm=scm, name=name, version=version, keep_scm_meta=keep_scm_meta)