|
|
@ -28,9 +28,16 @@ import json
|
|
|
|
from urllib2 import quote as urlquote, HTTPError
|
|
|
|
from urllib2 import quote as urlquote, HTTPError
|
|
|
|
from urlparse import urlparse
|
|
|
|
from urlparse import urlparse
|
|
|
|
|
|
|
|
|
|
|
|
from ansible.errors import AnsibleError, AnsibleOptionsError
|
|
|
|
from ansible.errors import AnsibleError
|
|
|
|
from ansible.module_utils.urls import open_url
|
|
|
|
from ansible.module_utils.urls import open_url
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
from __main__ import display
|
|
|
|
|
|
|
|
except ImportError:
|
|
|
|
|
|
|
|
from ansible.utils.display import Display
|
|
|
|
|
|
|
|
display = Display()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class GalaxyAPI(object):
|
|
|
|
class GalaxyAPI(object):
|
|
|
|
''' This class is meant to be used as a API client for an Ansible Galaxy server '''
|
|
|
|
''' This class is meant to be used as a API client for an Ansible Galaxy server '''
|
|
|
|
|
|
|
|
|
|
|
@ -52,7 +59,7 @@ class GalaxyAPI(object):
|
|
|
|
if server_version in self.SUPPORTED_VERSIONS:
|
|
|
|
if server_version in self.SUPPORTED_VERSIONS:
|
|
|
|
self.baseurl = '%s/api/%s' % (api_server, server_version)
|
|
|
|
self.baseurl = '%s/api/%s' % (api_server, server_version)
|
|
|
|
self.version = server_version # for future use
|
|
|
|
self.version = server_version # for future use
|
|
|
|
self.galaxy.display.vvvvv("Base API: %s" % self.baseurl)
|
|
|
|
display.vvvvv("Base API: %s" % self.baseurl)
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
raise AnsibleError("Unsupported Galaxy server API version: %s" % server_version)
|
|
|
|
raise AnsibleError("Unsupported Galaxy server API version: %s" % server_version)
|
|
|
|
|
|
|
|
|
|
|
@ -68,7 +75,7 @@ class GalaxyAPI(object):
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
data = json.load(open_url(api_server, validate_certs=self.galaxy.options.validate_certs))
|
|
|
|
data = json.load(open_url(api_server, validate_certs=self.galaxy.options.validate_certs))
|
|
|
|
return data.get("current_version", 'v1')
|
|
|
|
return data.get("current_version", 'v1')
|
|
|
|
except Exception as e:
|
|
|
|
except Exception:
|
|
|
|
# TODO: report error
|
|
|
|
# TODO: report error
|
|
|
|
return None
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
@ -83,12 +90,12 @@ class GalaxyAPI(object):
|
|
|
|
user_name = ".".join(parts[0:-1])
|
|
|
|
user_name = ".".join(parts[0:-1])
|
|
|
|
role_name = parts[-1]
|
|
|
|
role_name = parts[-1]
|
|
|
|
if notify:
|
|
|
|
if notify:
|
|
|
|
self.galaxy.display.display("- downloading role '%s', owned by %s" % (role_name, user_name))
|
|
|
|
display.display("- downloading role '%s', owned by %s" % (role_name, user_name))
|
|
|
|
except:
|
|
|
|
except:
|
|
|
|
raise AnsibleError("- invalid role name (%s). Specify role as format: username.rolename" % role_name)
|
|
|
|
raise AnsibleError("- invalid role name (%s). Specify role as format: username.rolename" % role_name)
|
|
|
|
|
|
|
|
|
|
|
|
url = '%s/roles/?owner__username=%s&name=%s' % (self.baseurl, user_name, role_name)
|
|
|
|
url = '%s/roles/?owner__username=%s&name=%s' % (self.baseurl, user_name, role_name)
|
|
|
|
self.galaxy.display.vvvv("- %s" % (url))
|
|
|
|
display.vvvv("- %s" % (url))
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
data = json.load(open_url(url, validate_certs=self.galaxy.options.validate_certs))
|
|
|
|
data = json.load(open_url(url, validate_certs=self.galaxy.options.validate_certs))
|
|
|
|
if len(data["results"]) != 0:
|
|
|
|
if len(data["results"]) != 0:
|
|
|
@ -109,13 +116,13 @@ class GalaxyAPI(object):
|
|
|
|
url = '%s/roles/%d/%s/?page_size=50' % (self.baseurl, int(role_id), related)
|
|
|
|
url = '%s/roles/%d/%s/?page_size=50' % (self.baseurl, int(role_id), related)
|
|
|
|
data = json.load(open_url(url, validate_certs=self.galaxy.options.validate_certs))
|
|
|
|
data = json.load(open_url(url, validate_certs=self.galaxy.options.validate_certs))
|
|
|
|
results = data['results']
|
|
|
|
results = data['results']
|
|
|
|
done = (data.get('next', None) == None)
|
|
|
|
done = (data.get('next', None) is None)
|
|
|
|
while not done:
|
|
|
|
while not done:
|
|
|
|
url = '%s%s' % (self.baseurl, data['next'])
|
|
|
|
url = '%s%s' % (self.baseurl, data['next'])
|
|
|
|
self.galaxy.display.display(url)
|
|
|
|
display.display(url)
|
|
|
|
data = json.load(open_url(url, validate_certs=self.galaxy.options.validate_certs))
|
|
|
|
data = json.load(open_url(url, validate_certs=self.galaxy.options.validate_certs))
|
|
|
|
results += data['results']
|
|
|
|
results += data['results']
|
|
|
|
done = (data.get('next', None) == None)
|
|
|
|
done = (data.get('next', None) is None)
|
|
|
|
return results
|
|
|
|
return results
|
|
|
|
except:
|
|
|
|
except:
|
|
|
|
return None
|
|
|
|
return None
|
|
|
@ -134,13 +141,13 @@ class GalaxyAPI(object):
|
|
|
|
results = data
|
|
|
|
results = data
|
|
|
|
done = True
|
|
|
|
done = True
|
|
|
|
if "next" in data:
|
|
|
|
if "next" in data:
|
|
|
|
done = (data.get('next', None) == None)
|
|
|
|
done = (data.get('next', None) is None)
|
|
|
|
while not done:
|
|
|
|
while not done:
|
|
|
|
url = '%s%s' % (self.baseurl, data['next'])
|
|
|
|
url = '%s%s' % (self.baseurl, data['next'])
|
|
|
|
self.galaxy.display.display(url)
|
|
|
|
display.display(url)
|
|
|
|
data = json.load(open_url(url, validate_certs=self.galaxy.options.validate_certs))
|
|
|
|
data = json.load(open_url(url, validate_certs=self.galaxy.options.validate_certs))
|
|
|
|
results += data['results']
|
|
|
|
results += data['results']
|
|
|
|
done = (data.get('next', None) == None)
|
|
|
|
done = (data.get('next', None) is None)
|
|
|
|
return results
|
|
|
|
return results
|
|
|
|
except Exception as error:
|
|
|
|
except Exception as error:
|
|
|
|
raise AnsibleError("Failed to download the %s list: %s" % (what, str(error)))
|
|
|
|
raise AnsibleError("Failed to download the %s list: %s" % (what, str(error)))
|
|
|
@ -168,7 +175,7 @@ class GalaxyAPI(object):
|
|
|
|
for plat in platforms:
|
|
|
|
for plat in platforms:
|
|
|
|
search_url += '&chain__platforms__name=' + urlquote(plat)
|
|
|
|
search_url += '&chain__platforms__name=' + urlquote(plat)
|
|
|
|
|
|
|
|
|
|
|
|
self.galaxy.display.debug("Executing query: %s" % search_url)
|
|
|
|
display.debug("Executing query: %s" % search_url)
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
data = json.load(open_url(search_url, validate_certs=self.galaxy.options.validate_certs))
|
|
|
|
data = json.load(open_url(search_url, validate_certs=self.galaxy.options.validate_certs))
|
|
|
|
except HTTPError as e:
|
|
|
|
except HTTPError as e:
|
|
|
|