Allow ansible-galaxy to install roles from URLs

ansible-galaxy can now refer to SCM URLs (git and hg at this point)
for role names
Dependencies need to use the full SCM URLs too.
Otherwise all seems to work well

Test rolesfile
```
http://bitbucket.org/willthames/git-ansible-galaxy,v1.1
https://bitbucket.org/willthames/hg-ansible-galaxy
```
(works with ssh too)
This commit is contained in:
Will Thames 2014-08-13 21:32:36 +10:00 committed by Michael DeHaan
parent bae73e5793
commit 36177396c4

View file

@ -26,6 +26,7 @@ import json
import os
import os.path
import shutil
import subprocess
import sys
import tarfile
import tempfile
@ -326,6 +327,63 @@ def api_get_list(api_server, what):
print " - failed to download the %s list" % what
return None
#-------------------------------------------------------------------------------------
# scm repo utility functions
#-------------------------------------------------------------------------------------
def repo_fetch_role(role_name, role_version):
check_repo_cmd = { 'git': ['git', 'ls-remote', role_name],
'hg': ['hg', 'identify', role_name]}
with open('/dev/null', 'w') as devnull:
for (scm, cmd) in check_repo_cmd.items():
popen = subprocess.Popen(cmd, stdout=devnull, stderr=devnull)
rc = popen.wait()
if rc == 0:
return scm_archive_role(scm, role_name, role_version)
print "Repo doesn't seem to be hg or git"
sys.exit(2)
def scm_archive_role(scm, role_url, role_version):
tempdir = tempfile.mkdtemp()
role_name = role_url.split('/')[-1]
clone_cmd = [scm, 'clone', role_url]
with open('/dev/null', 'w') as devnull:
popen = subprocess.Popen(clone_cmd, cwd=tempdir, stdout=devnull, stderr=devnull)
rc = popen.wait()
if rc != 0:
print "Command %s failed" % ' '.join(clone_cmd)
print "in directory %s" % temp_dir
sys.exit(1)
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.tar.gz')
if scm == 'hg':
archive_cmd = ['hg', 'archive', '--prefix', "%s/" % role_name]
if role_version:
archive_cmd.extend(['-r', role_version])
archive_cmd.append(temp_file.name)
if scm == 'git':
archive_cmd = ['git', 'archive', '--prefix=%s/' % role_name, '--output=%s' % temp_file.name]
if role_version:
archive_cmd.append(role_version)
else:
archive_cmd.append('HEAD')
with open('/dev/null', 'w') as devnull:
popen = subprocess.Popen(archive_cmd, cwd=os.path.join(tempdir, role_name),
stderr=devnull, stdout=devnull)
rc = popen.wait()
if rc != 0:
print "Command %s failed" % ' '.join(archive_cmd)
print "in directory %s" % tempdir
sys.exit(1)
shutil.rmtree(tempdir)
return temp_file.name
#-------------------------------------------------------------------------------------
# Role utility functions
#-------------------------------------------------------------------------------------
@ -679,6 +737,12 @@ def execute_install(args, options, parser):
else:
print "%s (%s) was NOT installed successfully." % (role_name,tar_file)
exit_without_ignore(options)
else:
if '://' in role_name:
# installing from scm url
tmp_file = repo_fetch_role(role_name, role_version)
role_name = role_name.split('/')[-1]
role_data = None
else:
# installing remotely
role_data = api_lookup_role_by_name(api_server, role_name)
@ -713,6 +777,10 @@ def execute_install(args, options, parser):
os.unlink(tmp_file)
# install dependencies, if we want them
if not no_deps:
if not role_data:
role_data = get_role_metadata(role_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_name in role_dependencies:
#dep_name = "%s.%s" % (dep['owner'], dep['name'])