Make ansible-galaxy work with galaxy.ansible.com

Now works with role files that use Ansible Galaxy roles
Still work to do on making this work with internal roles
This commit is contained in:
Will Thames 2015-07-04 13:00:30 +10:00 committed by Brian Coca
parent 014f3e8f7a
commit 9abd9a8f57
2 changed files with 25 additions and 34 deletions

View file

@ -320,9 +320,6 @@ class GalaxyCLI(CLI):
roles_done = [] roles_done = []
roles_left = [] roles_left = []
role_name = self.args.pop(0).strip()
gr = GalaxyRole(self.galaxy, role_name)
if role_file: if role_file:
f = open(role_file, 'r') f = open(role_file, 'r')
if role_file.endswith('.yaml') or role_file.endswith('.yml'): if role_file.endswith('.yaml') or role_file.endswith('.yml'):
@ -330,20 +327,18 @@ class GalaxyCLI(CLI):
else: else:
# roles listed in a file, one per line # roles listed in a file, one per line
for rname in f.readlines(): for rname in f.readlines():
roles_left.append(GalaxyRole(self.galaxy, rname)) roles_left.append(GalaxyRole(self.galaxy, rname.strip()))
f.close() f.close()
else: else:
# roles were specified directly, so we'll just go out grab them # roles were specified directly, so we'll just go out grab them
# (and their dependencies, unless the user doesn't want us to). # (and their dependencies, unless the user doesn't want us to).
for rname in self.args: for rname in self.args:
roles_left.append(GalaxyRole(self.galaxy, rname)) roles_left.append(GalaxyRole(self.galaxy, rname.strip()))
while len(roles_left) > 0: while len(roles_left) > 0:
# query the galaxy API for the role data # query the galaxy API for the role data
role_data = None role_data = None
role = roles_left.pop(0) role = roles_left.pop(0)
role_src = role.src
role_scm = role.scm
role_path = role.path role_path = role.path
if role_path: if role_path:
@ -352,21 +347,19 @@ class GalaxyCLI(CLI):
self.options.roles_path = roles_path self.options.roles_path = roles_path
tmp_file = None tmp_file = None
if role_src and os.path.isfile(role_src): installed = False
if role.src and os.path.isfile(role.src):
# installing a local tar.gz # installing a local tar.gz
tmp_file = role_src tmp_file = role.src
else: else:
if role_scm: if role.scm:
# create tar file from scm url # create tar file from scm url
tmp_file = scm_archive_role(role_scm, role_src, role.version, role.name) tmp_file = scm_archive_role(role.scm, role.src, role.version, role.name)
if role_src: if role.src:
if '://' in role_src: if '://' not in role.src:
# just download a URL - version will probably be in the URL role_data = self.api.lookup_role_by_name(role.src)
tmp_file = gr.fetch()
else:
role_data = self.api.lookup_role_by_name(role_src)
if not role_data: if not role_data:
self.display.warning("- sorry, %s was not found on %s." % (role_src, self.options.api_server)) self.display.warning("- sorry, %s was not found on %s." % (role.src, self.options.api_server))
self.exit_without_ignore() self.exit_without_ignore()
continue continue
@ -379,24 +372,23 @@ class GalaxyCLI(CLI):
if len(role_versions) > 0: if len(role_versions) > 0:
loose_versions = [LooseVersion(a.get('name',None)) for a in role_versions] loose_versions = [LooseVersion(a.get('name',None)) for a in role_versions]
loose_versions.sort() loose_versions.sort()
role["version"] = str(loose_versions[-1]) role.version = str(loose_versions[-1])
else: else:
role["version"] = 'master' role.version = 'master'
elif role['version'] != 'master': elif role.version != 'master':
if role_versions and role.version not in [a.get('name', None) for a in role_versions]: if role_versions and role.version not in [a.get('name', None) for a in role_versions]:
self.display.warning('role is %s' % role) self.display.warning('role is %s' % role)
self.display.warning("- the specified version (%s) was not found in the list of available versions (%s)." % (role.version, role_versions)) self.display.warning("- the specified version (%s) was not found in the list of available versions (%s)." % (role.version, role_versions))
self.exit_without_ignore() self.exit_without_ignore()
continue continue
# download the role. if --no-deps was specified, we stop here, # download the role. if --no-deps was specified, we stop here,
# otherwise we recursively grab roles and all of their deps. # otherwise we recursively grab roles and all of their deps.
tmp_file = gr.fetch(role_data) tmp_file = role.fetch(role_data)
installed = False
if tmp_file: if tmp_file:
installed = install_role(role.name, role.version, tmp_file, options) installed = role.install(tmp_file)
# we're done with the temp file, clean it up # we're done with the temp file, clean it up
if tmp_file != role_src: if tmp_file != role.src:
os.unlink(tmp_file) os.unlink(tmp_file)
# install dependencies, if we want them # install dependencies, if we want them

View file

@ -49,7 +49,7 @@ class GalaxyRole(object):
self.name = name self.name = name
self.version = version self.version = version
self.src = src self.src = src or name
self.scm = scm self.scm = scm
self.path = (os.path.join(galaxy.roles_path, self.name)) self.path = (os.path.join(galaxy.roles_path, self.name))
@ -178,17 +178,16 @@ class GalaxyRole(object):
return False return False
def fetch(self, target, role_data): def fetch(self, role_data):
""" """
Downloads the archived role from github to a temp location, extracts Downloads the archived role from github to a temp location
it, and then copies the extracted role to the role library path.
""" """
# first grab the file and save it to a temp location # first grab the file and save it to a temp location
if self.src: if "github_user" in role_data and "github_repo" in role_data:
archive_url = self.src archive_url = 'https://github.com/%s/%s/archive/%s.tar.gz' % (role_data["github_user"], role_data["github_repo"], self.version)
else: else:
archive_url = 'https://github.com/%s/%s/archive/%s.tar.gz' % (role_data["github_user"], role_data["github_repo"], target) archive_url = self.src
self.display.display("- downloading role from %s" % archive_url) self.display.display("- downloading role from %s" % archive_url)
try: try: