Friendly Role Names and roles from URLs

* Roles can now be given a friendly name as third field in role spec csv
* Roles can be installed from URL (not just from archived SCMs)
* Integration tests to demonstrate this
* Unit tests to ensure that role spec parsing works as expected
This commit is contained in:
Will Thames
2014-08-15 21:19:40 +10:00
committed by Michael DeHaan
parent 4803e923ff
commit 46b59b02ed
6 changed files with 81 additions and 67 deletions

View File

@@ -178,13 +178,6 @@ class Play(object):
returns any variables that were included with the role
"""
orig_path = template(self.basedir,role,self.vars)
if '+' in orig_path and '://' in orig_path:
# dependency name pointing to SCM URL
# assume role name is last part of the URL
orig_path = utils.repo_url_to_role_name(orig_path)
if ',' in orig_path:
# version information for role dependency used by galaxy
orig_path = orig_path.split(',')[0]
role_vars = {}
if type(orig_path) == dict:
@@ -193,20 +186,21 @@ class Play(object):
if role_name is None:
raise errors.AnsibleError("expected a role name in dictionary: %s" % orig_path)
role_vars = orig_path
orig_path = role_name
else:
(scm, role_src, role_version, role_name) = utils.role_spec_parse(orig_path)
role_path = None
possible_paths = [
utils.path_dwim(self.basedir, os.path.join('roles', orig_path)),
utils.path_dwim(self.basedir, orig_path)
utils.path_dwim(self.basedir, os.path.join('roles', role_name)),
utils.path_dwim(self.basedir, role_name)
]
if C.DEFAULT_ROLES_PATH:
search_locations = C.DEFAULT_ROLES_PATH.split(os.pathsep)
for loc in search_locations:
loc = os.path.expanduser(loc)
possible_paths.append(utils.path_dwim(loc, orig_path))
possible_paths.append(utils.path_dwim(loc, role_name))
for path_option in possible_paths:
if os.path.isdir(path_option):
@@ -419,8 +413,7 @@ class Play(object):
role_name = role['role']
else:
role_name = role
if '+' in role_name and '://' in role_name:
role_name = utils.repo_url_to_role_name(role_name)
(scm, role_src, role_version, role_name) = utils.role_spec_parse(role_name)
role_names.append(role_name)
if os.path.isfile(task):

View File

@@ -356,10 +356,33 @@ def repo_url_to_role_name(repo_url):
trailing_path = repo_url.split('/')[-1]
if trailing_path.endswith('.git'):
trailing_path = trailing_path[:-4]
if trailing_path.endswith('.tar.gz'):
trailing_path = trailing_path[:-7]
if ',' in trailing_path:
trailing_path = trailing_path.split(',')[0]
return trailing_path
def role_spec_parse(role_spec):
role_spec = role_spec.strip()
role_version = ''
if role_spec == "" or role_spec.startswith("#"):
return (None, None, None, None)
tokens = map(lambda s: s.strip(), role_spec.split(','))
if '+' in tokens[0]:
(scm, role_url) = tokens[0].split('+')
else:
scm = None
role_url = tokens[0]
if len(tokens) >= 2:
role_version = tokens[1]
if len(tokens) == 3:
role_name = tokens[2]
else:
role_name = repo_url_to_role_name(tokens[0])
return (scm, role_url, role_version, role_name)
def json_loads(data):
''' parse a JSON string and return a data structure '''