Fix galaxy install dep failure

Also fixes issue where force does not force reinstall of deps

Fixes #10425
This commit is contained in:
James Cammarata 2015-09-15 17:30:36 -04:00
parent 1a39e32a13
commit 2a50957ad8
4 changed files with 126 additions and 106 deletions

View file

@ -352,6 +352,7 @@ class GalaxyCLI(CLI):
raise AnsibleOptionsError("- please specify a user/role name, or a roles file, but not both") raise AnsibleOptionsError("- please specify a user/role name, or a roles file, but not both")
no_deps = self.get_opt("no_deps", False) no_deps = self.get_opt("no_deps", False)
force = self.get_opt('force', False)
roles_path = self.get_opt("roles_path") roles_path = self.get_opt("roles_path")
roles_done = [] roles_done = []
@ -389,6 +390,9 @@ class GalaxyCLI(CLI):
role = roles_left.pop(0) role = roles_left.pop(0)
role_path = role.path role_path = role.path
if role.install_info is not None and not force:
self.display.display('- %s is already installed, skipping.' % role.name)
continue
if role_path: if role_path:
self.options.roles_path = role_path self.options.roles_path = role_path
@ -443,25 +447,20 @@ class GalaxyCLI(CLI):
os.unlink(tmp_file) os.unlink(tmp_file)
# install dependencies, if we want them # install dependencies, if we want them
if not no_deps and installed: if not no_deps and installed:
if not role_data: role_dependencies = role.metadata.get('dependencies', [])
role_data = gr.get_metadata(role.get("name"), options)
role_dependencies = role_data['dependencies']
else:
role_dependencies = role_data['summary_fields']['dependencies'] # api_fetch_role_related(api_server, 'dependencies', role_data['id'])
for dep in role_dependencies: for dep in role_dependencies:
self.display.debug('Installing dep %s' % dep) self.display.debug('Installing dep %s' % dep)
if isinstance(dep, basestring): dep_req = RoleRequirement()
dep = ansible.utils.role_spec_parse(dep) __, dep_name, __ = dep_req.parse(dep)
dep_role = GalaxyRole(self.galaxy, name=dep_name)
if dep_role.install_info is None or force:
if dep_role not in roles_left:
self.display.display('- adding dependency: %s' % dep_name)
roles_left.append(GalaxyRole(self.galaxy, name=dep_name))
else: else:
dep = ansible.utils.role_yaml_parse(dep) self.display.display('- dependency %s already pending installation.' % dep_name)
if not get_role_metadata(dep["name"], options):
if dep not in roles_left:
self.display.display('- adding dependency: %s' % dep["name"])
roles_left.append(dep)
else: else:
self.display.display('- dependency %s already pending installation.' % dep["name"]) self.display.display('- dependency %s is already installed, skipping.' % dep_name)
else:
self.display.display('- dependency %s is already installed, skipping.' % dep["name"])
if not tmp_file or not installed: if not tmp_file or not installed:
self.display.warning("- %s was NOT installed successfully." % role.name) self.display.warning("- %s was NOT installed successfully." % role.name)

View file

@ -22,6 +22,8 @@
import os import os
from six import string_types
from ansible.errors import AnsibleError from ansible.errors import AnsibleError
from ansible.utils.display import Display from ansible.utils.display import Display
@ -40,9 +42,9 @@ class Galaxy(object):
self.display = display self.display = display
self.options = options self.options = options
self.roles_path = getattr(self.options, 'roles_path', None) roles_paths = getattr(self.options, 'roles_path', [])
if self.roles_path: if isinstance(roles_paths, string_types):
self.roles_path = os.path.expanduser(self.roles_path) self.roles_paths = [os.path.expanduser(roles_path) for roles_path in roles_paths.split(os.pathsep)]
self.roles = {} self.roles = {}

View file

@ -39,7 +39,7 @@ class GalaxyRole(object):
ROLE_DIRS = ('defaults','files','handlers','meta','tasks','templates','vars') ROLE_DIRS = ('defaults','files','handlers','meta','tasks','templates','vars')
def __init__(self, galaxy, name, src=None, version=None, scm=None): def __init__(self, galaxy, name, src=None, version=None, scm=None, role_path=None):
self._metadata = None self._metadata = None
self._install_info = None self._install_info = None
@ -52,7 +52,20 @@ class GalaxyRole(object):
self.src = src or name self.src = src or name
self.scm = scm self.scm = scm
self.path = (os.path.join(galaxy.roles_path, self.name)) if role_path is not None:
self.path = role_path
else:
for path in galaxy.roles_paths:
role_path = os.path.join(path, self.name)
if os.path.exists(role_path):
self.path = role_path
break
else:
# use the first path by default
self.path = os.path.join(galaxy.roles_paths[0], self.name)
def __eq__(self, other):
return self.name == other.name
def fetch_from_scm_archive(self): def fetch_from_scm_archive(self):

View file

@ -54,26 +54,26 @@ class RoleRequirement(RoleDefinition):
assert type(ds) == dict or isinstance(ds, string_types) assert type(ds) == dict or isinstance(ds, string_types)
role_name = '' role_name = None
role_params = dict() role_params = dict()
new_ds = dict() new_ds = dict()
if isinstance(ds, string_types): if isinstance(ds, string_types):
role_name = ds role_name = ds
else: else:
ds = self._preprocess_role_spec(ds) (new_ds, role_params) = self._split_role_params(self._preprocess_role_spec(ds))
(new_ds, role_params) = self._split_role_params(ds)
# pull the role name out of the ds # pull the role name out of the ds
role_name = new_ds.get('role_name') role_name = new_ds.pop('role_name', new_ds.pop('role', None))
del ds['role_name']
if role_name is None:
raise AnsibleError("Role requirement did not contain a role name!", obj=ds)
return (new_ds, role_name, role_params) return (new_ds, role_name, role_params)
def _preprocess_role_spec(self, ds): def _preprocess_role_spec(self, ds):
if 'role' in ds: if 'role' in ds:
# Old style: {role: "galaxy.role,version,name", other_vars: "here" } # Old style: {role: "galaxy.role,version,name", other_vars: "here" }
role_info = self._role_spec_parse(ds['role']) role_info = role_spec_parse(ds['role'])
if isinstance(role_info, dict): if isinstance(role_info, dict):
# Warning: Slight change in behaviour here. name may be being # Warning: Slight change in behaviour here. name may be being
# overloaded. Previously, name was only a parameter to the role. # overloaded. Previously, name was only a parameter to the role.
@ -96,7 +96,7 @@ class RoleRequirement(RoleDefinition):
ds["role"] = ds["name"] ds["role"] = ds["name"]
del ds["name"] del ds["name"]
else: else:
ds["role"] = self._repo_url_to_role_name(ds["src"]) ds["role"] = repo_url_to_role_name(ds["src"])
# set some values to a default value, if none were specified # set some values to a default value, if none were specified
ds.setdefault('version', '') ds.setdefault('version', '')
@ -104,7 +104,7 @@ class RoleRequirement(RoleDefinition):
return ds return ds
def _repo_url_to_role_name(self, repo_url): def repo_url_to_role_name(repo_url):
# gets the role name out of a repo like # gets the role name out of a repo like
# http://git.example.com/repos/repo.git" => "repo" # http://git.example.com/repos/repo.git" => "repo"
@ -119,7 +119,7 @@ class RoleRequirement(RoleDefinition):
trailing_path = trailing_path.split(',')[0] trailing_path = trailing_path.split(',')[0]
return trailing_path return trailing_path
def _role_spec_parse(self, role_spec): def role_spec_parse(role_spec):
# takes a repo and a version like # takes a repo and a version like
# git+http://git.example.com/repos/repo.git,v1.0 # git+http://git.example.com/repos/repo.git,v1.0
# and returns a list of properties such as: # and returns a list of properties such as:
@ -156,44 +156,50 @@ class RoleRequirement(RoleDefinition):
if len(tokens) == 3: if len(tokens) == 3:
role_name = tokens[2] role_name = tokens[2]
else: else:
role_name = self._repo_url_to_role_name(tokens[0]) role_name = repo_url_to_role_name(tokens[0])
if scm and not role_version: if scm and not role_version:
role_version = default_role_versions.get(scm, '') role_version = default_role_versions.get(scm, '')
return dict(scm=scm, src=role_url, version=role_version, role_name=role_name) return dict(scm=scm, src=role_url, version=role_version, role_name=role_name)
# FIXME: all of these methods need to be cleaned up/reorganized below this
def get_opt(options, k, defval=""):
"""
Returns an option from an Optparse values instance.
"""
try:
data = getattr(options, k)
except:
return defval
if k == "roles_path":
if os.pathsep in data:
data = data.split(os.pathsep)[0]
return data
def role_yaml_parse(role): def get_role_path(role_name, options):
if 'role' in role: """
# Old style: {role: "galaxy.role,version,name", other_vars: "here" } Returns the role path based on the roles_path option
role_info = role_spec_parse(role['role']) and the role name.
if isinstance(role_info, dict): """
# Warning: Slight change in behaviour here. name may be being roles_path = get_opt(options,'roles_path')
# overloaded. Previously, name was only a parameter to the role. roles_path = os.path.join(roles_path, role_name)
# Now it is both a parameter to the role and the name that roles_path = os.path.expanduser(roles_path)
# ansible-galaxy will install under on the local system. return roles_path
if 'name' in role and 'name' in role_info:
del role_info['name'] def get_role_metadata(role_name, options):
role.update(role_info) """
Returns the metadata as YAML, if the file 'meta/main.yml'
exists in the specified role_path
"""
role_path = os.path.join(get_role_path(role_name, options), 'meta/main.yml')
try:
if os.path.isfile(role_path):
f = open(role_path, 'r')
meta_data = yaml.safe_load(f)
f.close()
return meta_data
else: else:
# New style: { src: 'galaxy.role,version,name', other_vars: "here" } return None
if 'github.com' in role["src"] and 'http' in role["src"] and '+' not in role["src"] and not role["src"].endswith('.tar.gz'): except:
role["src"] = "git+" + role["src"] return None
if '+' in role["src"]:
(scm, src) = role["src"].split('+')
role["scm"] = scm
role["src"] = src
if 'name' not in role:
role["name"] = repo_url_to_role_name(role["src"])
if 'version' not in role:
role['version'] = ''
if 'scm' not in role:
role['scm'] = None
return role