2015-04-23 03:41:05 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
########################################################################
|
|
|
|
#
|
|
|
|
# (C) 2013, James Cammarata <jcammarata@ansible.com>
|
|
|
|
#
|
|
|
|
# This file is part of Ansible
|
|
|
|
#
|
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Ansible is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
########################################################################
|
|
|
|
|
|
|
|
import datetime
|
|
|
|
import json
|
|
|
|
import os
|
|
|
|
import os.path
|
|
|
|
import shutil
|
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
import tarfile
|
|
|
|
import tempfile
|
|
|
|
import urllib
|
|
|
|
import urllib2
|
|
|
|
import yaml
|
|
|
|
|
|
|
|
from collections import defaultdict
|
|
|
|
from distutils.version import LooseVersion
|
|
|
|
from jinja2 import Environment
|
|
|
|
from optparse import OptionParser
|
|
|
|
|
|
|
|
import ansible.constants as C
|
|
|
|
import ansible.utils
|
|
|
|
import ansible.galaxy
|
2015-04-27 11:31:41 +00:00
|
|
|
from ansible.errors import AnsibleError, AnsibleOptionsError
|
|
|
|
from ansible.galaxy import Galaxy
|
|
|
|
from ansible.galaxy.api import GalaxyAPI
|
|
|
|
from ansible.galaxy.role import GalaxyRole
|
|
|
|
from ansible.utils.display import Display
|
|
|
|
from ansible.utils.cli import CLI
|
2015-04-23 03:41:05 +00:00
|
|
|
|
2015-04-27 11:31:41 +00:00
|
|
|
class GalaxyCLI(CLI):
|
2015-04-23 03:41:05 +00:00
|
|
|
|
|
|
|
VALID_ACTIONS = ("init", "info", "install", "list", "remove")
|
|
|
|
SKIP_INFO_KEYS = ("platforms","readme_html", "related", "summary_fields", "average_aw_composite", "average_aw_score", "url" )
|
|
|
|
|
2015-04-27 11:31:41 +00:00
|
|
|
def __init__(self, args, display=None):
|
2015-04-23 03:41:05 +00:00
|
|
|
|
2015-04-27 11:31:41 +00:00
|
|
|
self.api = None
|
|
|
|
self.galaxy = None
|
|
|
|
super(GalaxyCLI, self).__init__(args, display)
|
2015-04-23 03:41:05 +00:00
|
|
|
|
|
|
|
def parse(self):
|
|
|
|
''' create an options parser for bin/ansible '''
|
2015-04-27 11:31:41 +00:00
|
|
|
|
|
|
|
usage = "usage: %%prog [%s] [--help] [options] ..." % "|".join(self.VALID_ACTIONS)
|
2015-04-23 03:41:05 +00:00
|
|
|
epilog = "\nSee '%s <command> --help' for more information on a specific command.\n\n" % os.path.basename(sys.argv[0])
|
|
|
|
OptionParser.format_epilog = lambda self, formatter: self.epilog
|
|
|
|
parser = OptionParser(usage=usage, epilog=epilog)
|
|
|
|
|
2015-04-27 11:31:41 +00:00
|
|
|
self.parser = parser
|
|
|
|
self.set_action()
|
2015-04-23 03:41:05 +00:00
|
|
|
|
|
|
|
# options specific to actions
|
|
|
|
if self.action == "info":
|
2015-04-27 11:31:41 +00:00
|
|
|
self.parser.set_usage("usage: %prog info [options] role_name[,version]")
|
2015-04-23 03:41:05 +00:00
|
|
|
elif self.action == "init":
|
2015-04-27 11:31:41 +00:00
|
|
|
self.parser.set_usage("usage: %prog init [options] role_name")
|
|
|
|
self.parser.add_option(
|
2015-04-23 03:41:05 +00:00
|
|
|
'-p', '--init-path', dest='init_path', default="./",
|
|
|
|
help='The path in which the skeleton role will be created. '
|
|
|
|
'The default is the current working directory.')
|
2015-04-27 11:31:41 +00:00
|
|
|
self.parser.add_option(
|
2015-04-23 03:41:05 +00:00
|
|
|
'--offline', dest='offline', default=False, action='store_true',
|
|
|
|
help="Don't query the galaxy API when creating roles")
|
|
|
|
elif self.action == "install":
|
2015-04-27 11:31:41 +00:00
|
|
|
self.parser.set_usage("usage: %prog install [options] [-r FILE | role_name(s)[,version] | scm+role_repo_url[,version] | tar_file(s)]")
|
|
|
|
self.parser.add_option(
|
2015-04-23 03:41:05 +00:00
|
|
|
'-i', '--ignore-errors', dest='ignore_errors', action='store_true', default=False,
|
|
|
|
help='Ignore errors and continue with the next specified role.')
|
2015-04-27 11:31:41 +00:00
|
|
|
self.parser.add_option(
|
2015-04-23 03:41:05 +00:00
|
|
|
'-n', '--no-deps', dest='no_deps', action='store_true', default=False,
|
|
|
|
help='Don\'t download roles listed as dependencies')
|
2015-04-27 11:31:41 +00:00
|
|
|
self.parser.add_option(
|
2015-04-23 03:41:05 +00:00
|
|
|
'-r', '--role-file', dest='role_file',
|
|
|
|
help='A file containing a list of roles to be imported')
|
|
|
|
elif self.action == "remove":
|
2015-04-27 11:31:41 +00:00
|
|
|
self.parser.set_usage("usage: %prog remove role1 role2 ...")
|
2015-04-23 03:41:05 +00:00
|
|
|
elif self.action == "list":
|
2015-04-27 11:31:41 +00:00
|
|
|
self.parser.set_usage("usage: %prog list [role_name]")
|
2015-04-23 03:41:05 +00:00
|
|
|
|
|
|
|
# options that apply to more than one action
|
|
|
|
if self.action != "init":
|
2015-04-27 11:31:41 +00:00
|
|
|
self.parser.add_option(
|
2015-04-23 03:41:05 +00:00
|
|
|
'-p', '--roles-path', dest='roles_path', default=C.DEFAULT_ROLES_PATH,
|
|
|
|
help='The path to the directory containing your roles. '
|
|
|
|
'The default is the roles_path configured in your '
|
|
|
|
'ansible.cfg file (/etc/ansible/roles if not configured)')
|
|
|
|
|
|
|
|
if self.action in ("info","init","install"):
|
2015-04-27 11:31:41 +00:00
|
|
|
self.parser.add_option(
|
2015-04-23 03:41:05 +00:00
|
|
|
'-s', '--server', dest='api_server', default="galaxy.ansible.com",
|
|
|
|
help='The API server destination')
|
|
|
|
|
|
|
|
if self.action in ("init","install"):
|
2015-04-27 11:31:41 +00:00
|
|
|
self.parser.add_option(
|
2015-04-23 03:41:05 +00:00
|
|
|
'-f', '--force', dest='force', action='store_true', default=False,
|
|
|
|
help='Force overwriting an existing role')
|
|
|
|
|
2015-04-27 11:31:41 +00:00
|
|
|
# get options, args and galaxy object
|
|
|
|
self.options, self.args =self.parser.parse_args()
|
|
|
|
self.galaxy = Galaxy(self.options, self.display)
|
|
|
|
|
|
|
|
if len(self.args) != 1:
|
|
|
|
raise AnsibleOptionsError("Missing arguments")
|
2015-04-23 03:41:05 +00:00
|
|
|
|
2015-04-27 11:31:41 +00:00
|
|
|
return True
|
2015-04-23 03:41:05 +00:00
|
|
|
|
2015-04-27 11:31:41 +00:00
|
|
|
def run(self):
|
2015-04-23 03:41:05 +00:00
|
|
|
|
2015-04-27 11:31:41 +00:00
|
|
|
#self.display.verbosity = self.options.verbosity
|
|
|
|
api_server = self.get_opt("api_server", "galaxy.ansible.com")
|
2015-04-23 03:41:05 +00:00
|
|
|
|
2015-04-27 11:31:41 +00:00
|
|
|
# if not offline, get connect to galaxy api
|
|
|
|
if self.action == 'init' and not self.options.offline:
|
|
|
|
self.api = GalaxyAPI(self.galaxy, api_server)
|
|
|
|
if not self.api:
|
|
|
|
raise AnsibleError("The API server (%s) is not responding, please try again later." % api_server)
|
2015-04-23 03:41:05 +00:00
|
|
|
|
2015-04-27 11:31:41 +00:00
|
|
|
self.execute()
|
2015-04-23 03:41:05 +00:00
|
|
|
|
2015-04-27 11:31:41 +00:00
|
|
|
def get_opt(self, k, defval=""):
|
2015-04-23 03:41:05 +00:00
|
|
|
"""
|
|
|
|
Returns an option from an Optparse values instance.
|
|
|
|
"""
|
|
|
|
try:
|
2015-04-27 11:31:41 +00:00
|
|
|
data = getattr(self.options, k)
|
2015-04-23 03:41:05 +00:00
|
|
|
except:
|
|
|
|
return defval
|
|
|
|
if k == "roles_path":
|
|
|
|
if os.pathsep in data:
|
|
|
|
data = data.split(os.pathsep)[0]
|
|
|
|
return data
|
|
|
|
|
2015-04-27 11:31:41 +00:00
|
|
|
def exit_without_ignore(self, rc=1):
|
2015-04-23 03:41:05 +00:00
|
|
|
"""
|
|
|
|
Exits with the specified return code unless the
|
|
|
|
option --ignore-errors was specified
|
|
|
|
"""
|
|
|
|
|
2015-04-27 11:31:41 +00:00
|
|
|
if not self.get_opt("ignore_errors", False):
|
|
|
|
self.display.error('- you can use --ignore-errors to skip failed tasks/roles.')
|
|
|
|
return rc
|
2015-04-23 03:41:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2015-04-27 11:31:41 +00:00
|
|
|
def execute_init(self):
|
2015-04-23 03:41:05 +00:00
|
|
|
"""
|
|
|
|
Executes the init action, which creates the skeleton framework
|
|
|
|
of a role that complies with the galaxy metadata format.
|
|
|
|
"""
|
|
|
|
|
2015-04-27 11:31:41 +00:00
|
|
|
init_path = self.get_opt('init_path', './')
|
|
|
|
force = self.get_opt('force', False)
|
|
|
|
offline = self.get_opt('offline', False)
|
|
|
|
|
|
|
|
role_name = self.args.pop(0).strip()
|
|
|
|
if role_name == "":
|
|
|
|
raise AnsibleOptionsError("- no role name specified for init")
|
|
|
|
role_path = os.path.join(init_path, role_name)
|
|
|
|
if os.path.exists(role_path):
|
|
|
|
if os.path.isfile(role_path):
|
|
|
|
raise AnsibleError("- the path %s already exists, but is a file - aborting" % role_path)
|
|
|
|
elif not force:
|
|
|
|
raise AnsibleError("- the directory %s already exists." % role_path + \
|
|
|
|
"you can use --force to re-initialize this directory,\n" + \
|
|
|
|
"however it will reset any main.yml files that may have\n" + \
|
|
|
|
"been modified there already.")
|
2015-04-23 03:41:05 +00:00
|
|
|
|
|
|
|
# create the default README.md
|
|
|
|
if not os.path.exists(role_path):
|
|
|
|
os.makedirs(role_path)
|
|
|
|
readme_path = os.path.join(role_path, "README.md")
|
|
|
|
f = open(readme_path, "wb")
|
|
|
|
f.write(default_readme_template)
|
|
|
|
f.close
|
|
|
|
|
2015-04-27 11:31:41 +00:00
|
|
|
for dir in self.ROLE_DIRS:
|
2015-04-23 03:41:05 +00:00
|
|
|
dir_path = os.path.join(init_path, role_name, dir)
|
|
|
|
main_yml_path = os.path.join(dir_path, 'main.yml')
|
|
|
|
# create the directory if it doesn't exist already
|
|
|
|
if not os.path.exists(dir_path):
|
|
|
|
os.makedirs(dir_path)
|
|
|
|
|
|
|
|
# now create the main.yml file for that directory
|
|
|
|
if dir == "meta":
|
|
|
|
# create a skeleton meta/main.yml with a valid galaxy_info
|
|
|
|
# datastructure in place, plus with all of the available
|
|
|
|
# tags/platforms included (but commented out) and the
|
|
|
|
# dependencies section
|
|
|
|
platforms = []
|
2015-04-27 11:31:41 +00:00
|
|
|
if not offline and self.api:
|
|
|
|
platforms = self.api.get_list("platforms") or []
|
2015-04-23 03:41:05 +00:00
|
|
|
categories = []
|
2015-04-27 11:31:41 +00:00
|
|
|
if not offline and self.api:
|
|
|
|
categories = self.api.get_list("categories") or []
|
2015-04-23 03:41:05 +00:00
|
|
|
|
|
|
|
# group the list of platforms from the api based
|
|
|
|
# on their names, with the release field being
|
|
|
|
# appended to a list of versions
|
|
|
|
platform_groups = defaultdict(list)
|
|
|
|
for platform in platforms:
|
|
|
|
platform_groups[platform['name']].append(platform['release'])
|
|
|
|
platform_groups[platform['name']].sort()
|
|
|
|
|
|
|
|
inject = dict(
|
|
|
|
author = 'your name',
|
|
|
|
company = 'your company (optional)',
|
|
|
|
license = 'license (GPLv2, CC-BY, etc)',
|
|
|
|
issue_tracker_url = 'http://example.com/issue/tracker',
|
|
|
|
min_ansible_version = '1.2',
|
|
|
|
platforms = platform_groups,
|
|
|
|
categories = categories,
|
|
|
|
)
|
|
|
|
rendered_meta = Environment().from_string(default_meta_template).render(inject)
|
|
|
|
f = open(main_yml_path, 'w')
|
|
|
|
f.write(rendered_meta)
|
|
|
|
f.close()
|
|
|
|
pass
|
|
|
|
elif dir not in ('files','templates'):
|
|
|
|
# just write a (mostly) empty YAML file for main.yml
|
|
|
|
f = open(main_yml_path, 'w')
|
|
|
|
f.write('---\n# %s file for %s\n' % (dir,role_name))
|
|
|
|
f.close()
|
|
|
|
print "- %s was created successfully" % role_name
|
|
|
|
|
2015-04-27 11:31:41 +00:00
|
|
|
def execute_info(self):
|
2015-04-23 03:41:05 +00:00
|
|
|
"""
|
|
|
|
Executes the info action. This action prints out detailed
|
|
|
|
information about an installed role as well as info available
|
|
|
|
from the galaxy API.
|
|
|
|
"""
|
|
|
|
|
2015-04-27 11:31:41 +00:00
|
|
|
if len(self.args) == 0:
|
2015-04-23 03:41:05 +00:00
|
|
|
# the user needs to specify a role
|
2015-04-27 11:31:41 +00:00
|
|
|
raise AnsibleOptionsError("- you must specify a user/role name")
|
2015-04-23 03:41:05 +00:00
|
|
|
|
2015-04-27 11:31:41 +00:00
|
|
|
roles_path = self.get_opt("roles_path")
|
2015-04-23 03:41:05 +00:00
|
|
|
|
2015-04-27 11:31:41 +00:00
|
|
|
for role in self.args:
|
2015-04-23 03:41:05 +00:00
|
|
|
|
|
|
|
role_info = {}
|
|
|
|
|
|
|
|
install_info = get_galaxy_install_info(role, options)
|
|
|
|
if install_info:
|
|
|
|
if 'version' in install_info:
|
|
|
|
install_info['intalled_version'] = install_info['version']
|
|
|
|
del install_info['version']
|
|
|
|
role_info.update(install_info)
|
|
|
|
|
2015-04-27 11:31:41 +00:00
|
|
|
remote_data = self.api.lookup_role_by_name(role, False)
|
2015-04-23 03:41:05 +00:00
|
|
|
if remote_data:
|
|
|
|
role_info.update(remote_data)
|
|
|
|
|
2015-04-27 11:31:41 +00:00
|
|
|
metadata = get_metadata(role, options)
|
2015-04-23 03:41:05 +00:00
|
|
|
if metadata:
|
|
|
|
role_info.update(metadata)
|
|
|
|
|
|
|
|
role_spec = ansible.utils.role_spec_parse(role)
|
|
|
|
if role_spec:
|
|
|
|
role_info.update(role_spec)
|
|
|
|
|
|
|
|
if role_info:
|
|
|
|
print "- %s:" % (role)
|
|
|
|
for k in sorted(role_info.keys()):
|
|
|
|
|
|
|
|
if k in SKIP_INFO_KEYS:
|
|
|
|
continue
|
|
|
|
|
|
|
|
if isinstance(role_info[k], dict):
|
|
|
|
print "\t%s: " % (k)
|
|
|
|
for key in sorted(role_info[k].keys()):
|
|
|
|
if key in SKIP_INFO_KEYS:
|
|
|
|
continue
|
|
|
|
print "\t\t%s: %s" % (key, role_info[k][key])
|
|
|
|
else:
|
|
|
|
print "\t%s: %s" % (k, role_info[k])
|
|
|
|
else:
|
|
|
|
print "- the role %s was not found" % role
|
|
|
|
|
2015-04-27 11:31:41 +00:00
|
|
|
def execute_install(self):
|
2015-04-23 03:41:05 +00:00
|
|
|
"""
|
|
|
|
Executes the installation action. The args list contains the
|
|
|
|
roles to be installed, unless -f was specified. The list of roles
|
|
|
|
can be a name (which will be downloaded via the galaxy API and github),
|
|
|
|
or it can be a local .tar.gz file.
|
|
|
|
"""
|
|
|
|
|
2015-04-27 11:31:41 +00:00
|
|
|
role_file = self.get_opt("role_file", None)
|
2015-04-23 03:41:05 +00:00
|
|
|
|
2015-04-27 11:31:41 +00:00
|
|
|
if len(self.args) == 0 and role_file is None:
|
2015-04-23 03:41:05 +00:00
|
|
|
# the user needs to specify one of either --role-file
|
|
|
|
# or specify a single user/role name
|
2015-04-27 11:31:41 +00:00
|
|
|
raise AnsibleOptionsError("- you must specify a user/role name or a roles file")
|
|
|
|
elif len(self.args) == 1 and not role_file is None:
|
2015-04-23 03:41:05 +00:00
|
|
|
# using a role file is mutually exclusive of specifying
|
|
|
|
# the role name on the command line
|
2015-04-27 11:31:41 +00:00
|
|
|
raise AnsibleOptionsError("- please specify a user/role name, or a roles file, but not both")
|
2015-04-23 03:41:05 +00:00
|
|
|
|
2015-04-27 11:31:41 +00:00
|
|
|
no_deps = self.get_opt("no_deps", False)
|
|
|
|
roles_path = self.get_opt("roles_path")
|
2015-04-23 03:41:05 +00:00
|
|
|
|
|
|
|
roles_done = []
|
|
|
|
if role_file:
|
|
|
|
f = open(role_file, 'r')
|
|
|
|
if role_file.endswith('.yaml') or role_file.endswith('.yml'):
|
|
|
|
roles_left = map(ansible.utils.role_yaml_parse, yaml.safe_load(f))
|
|
|
|
else:
|
|
|
|
# roles listed in a file, one per line
|
2015-04-27 11:31:41 +00:00
|
|
|
roles_left = map(gr.get_spec, f.readlines())
|
2015-04-23 03:41:05 +00:00
|
|
|
f.close()
|
|
|
|
else:
|
|
|
|
# roles were specified directly, so we'll just go out grab them
|
|
|
|
# (and their dependencies, unless the user doesn't want us to).
|
2015-04-27 11:31:41 +00:00
|
|
|
roles_left = map(gr.get_spec, self.args)
|
2015-04-23 03:41:05 +00:00
|
|
|
|
|
|
|
while len(roles_left) > 0:
|
|
|
|
# query the galaxy API for the role data
|
|
|
|
role_data = None
|
|
|
|
role = roles_left.pop(0)
|
|
|
|
role_src = role.get("src")
|
|
|
|
role_scm = role.get("scm")
|
|
|
|
role_path = role.get("path")
|
|
|
|
|
|
|
|
if role_path:
|
|
|
|
options.roles_path = role_path
|
|
|
|
else:
|
|
|
|
options.roles_path = roles_path
|
|
|
|
|
|
|
|
if os.path.isfile(role_src):
|
|
|
|
# installing a local tar.gz
|
|
|
|
tmp_file = role_src
|
|
|
|
else:
|
|
|
|
if role_scm:
|
|
|
|
# create tar file from scm url
|
|
|
|
tmp_file = scm_archive_role(role_scm, role_src, role.get("version"), role.get("name"))
|
|
|
|
elif '://' in role_src:
|
|
|
|
# just download a URL - version will probably be in the URL
|
|
|
|
tmp_file = fetch_role(role_src, None, None, options)
|
|
|
|
else:
|
2015-04-27 11:31:41 +00:00
|
|
|
role_data = self.api.lookup_role_by_name(role_src)
|
2015-04-23 03:41:05 +00:00
|
|
|
if not role_data:
|
2015-04-27 11:31:41 +00:00
|
|
|
print "- sorry, %s was not found on %s." % (role_src, self.options.api_server)
|
2015-04-23 03:41:05 +00:00
|
|
|
exit_without_ignore(options)
|
|
|
|
continue
|
|
|
|
|
2015-04-27 11:31:41 +00:00
|
|
|
role_versions = self.api.fetch_role_related('versions', role_data['id'])
|
2015-04-23 03:41:05 +00:00
|
|
|
if "version" not in role or role['version'] == '':
|
|
|
|
# convert the version names to LooseVersion objects
|
|
|
|
# and sort them to get the latest version. If there
|
|
|
|
# are no versions in the list, we'll grab the head
|
|
|
|
# of the master branch
|
|
|
|
if len(role_versions) > 0:
|
|
|
|
loose_versions = [LooseVersion(a.get('name',None)) for a in role_versions]
|
|
|
|
loose_versions.sort()
|
|
|
|
role["version"] = str(loose_versions[-1])
|
|
|
|
else:
|
|
|
|
role["version"] = 'master'
|
|
|
|
elif role['version'] != 'master':
|
|
|
|
if role_versions and role["version"] not in [a.get('name', None) for a in role_versions]:
|
|
|
|
print 'role is %s' % role
|
|
|
|
print "- the specified version (%s) was not found in the list of available versions (%s)." % (role['version'], role_versions)
|
|
|
|
exit_without_ignore(options)
|
|
|
|
continue
|
|
|
|
|
|
|
|
# download the role. if --no-deps was specified, we stop here,
|
|
|
|
# otherwise we recursively grab roles and all of their deps.
|
|
|
|
tmp_file = fetch_role(role_src, role["version"], role_data, options)
|
|
|
|
installed = False
|
|
|
|
if tmp_file:
|
|
|
|
installed = install_role(role.get("name"), role.get("version"), tmp_file, options)
|
|
|
|
# we're done with the temp file, clean it up
|
|
|
|
if tmp_file != role_src:
|
|
|
|
os.unlink(tmp_file)
|
|
|
|
# install dependencies, if we want them
|
|
|
|
if not no_deps and installed:
|
|
|
|
if not role_data:
|
2015-04-27 11:31:41 +00:00
|
|
|
role_data = gr.get_metadata(role.get("name"), options)
|
2015-04-23 03:41:05 +00:00
|
|
|
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:
|
|
|
|
if isinstance(dep, basestring):
|
|
|
|
dep = ansible.utils.role_spec_parse(dep)
|
|
|
|
else:
|
|
|
|
dep = ansible.utils.role_yaml_parse(dep)
|
|
|
|
if not get_role_metadata(dep["name"], options):
|
|
|
|
if dep not in roles_left:
|
|
|
|
print '- adding dependency: %s' % dep["name"]
|
|
|
|
roles_left.append(dep)
|
|
|
|
else:
|
|
|
|
print '- dependency %s already pending installation.' % dep["name"]
|
|
|
|
else:
|
|
|
|
print '- dependency %s is already installed, skipping.' % dep["name"]
|
|
|
|
if not tmp_file or not installed:
|
|
|
|
print "- %s was NOT installed successfully." % role.get("name")
|
|
|
|
exit_without_ignore(options)
|
2015-04-27 11:31:41 +00:00
|
|
|
return 0
|
2015-04-23 03:41:05 +00:00
|
|
|
|
2015-04-27 11:31:41 +00:00
|
|
|
def execute_remove(self):
|
2015-04-23 03:41:05 +00:00
|
|
|
"""
|
|
|
|
Executes the remove action. The args list contains the list
|
|
|
|
of roles to be removed. This list can contain more than one role.
|
|
|
|
"""
|
|
|
|
|
2015-04-27 11:31:41 +00:00
|
|
|
if len(self.args) == 0:
|
|
|
|
raise AnsibleOptionsError('- you must specify at least one role to remove.')
|
2015-04-23 03:41:05 +00:00
|
|
|
|
2015-04-27 11:31:41 +00:00
|
|
|
for role in self.args:
|
2015-04-23 03:41:05 +00:00
|
|
|
if get_role_metadata(role, options):
|
|
|
|
if remove_role(role, options):
|
2015-04-27 11:31:41 +00:00
|
|
|
self.display.display('- successfully removed %s' % role)
|
2015-04-23 03:41:05 +00:00
|
|
|
else:
|
2015-04-27 11:31:41 +00:00
|
|
|
self.display.display("- failed to remove role: %s" % role)
|
2015-04-23 03:41:05 +00:00
|
|
|
else:
|
2015-04-27 11:31:41 +00:00
|
|
|
self.display.display('- %s is not installed, skipping.' % role)
|
|
|
|
return 0
|
2015-04-23 03:41:05 +00:00
|
|
|
|
2015-04-27 11:31:41 +00:00
|
|
|
def execute_list(self):
|
2015-04-23 03:41:05 +00:00
|
|
|
"""
|
|
|
|
Executes the list action. The args list can contain zero
|
|
|
|
or one role. If one is specified, only that role will be
|
|
|
|
shown, otherwise all roles in the specified directory will
|
|
|
|
be shown.
|
|
|
|
"""
|
|
|
|
|
2015-04-27 11:31:41 +00:00
|
|
|
if len(self.args) > 1:
|
|
|
|
raise AnsibleOptionsError("- please specify only one role to list, or specify no roles to see a full list")
|
2015-04-23 03:41:05 +00:00
|
|
|
|
2015-04-27 11:31:41 +00:00
|
|
|
if len(self.args) == 1:
|
2015-04-23 03:41:05 +00:00
|
|
|
# show only the request role, if it exists
|
2015-04-27 11:31:41 +00:00
|
|
|
role_name = self.args[0]
|
|
|
|
gr = GalaxyRole(self.galaxy, role_name)
|
|
|
|
metadata = gr.get_metadata()
|
2015-04-23 03:41:05 +00:00
|
|
|
if metadata:
|
2015-04-27 11:31:41 +00:00
|
|
|
install_info = gr.get_galaxy_install_info()
|
2015-04-23 03:41:05 +00:00
|
|
|
version = None
|
|
|
|
if install_info:
|
|
|
|
version = install_info.get("version", None)
|
|
|
|
if not version:
|
|
|
|
version = "(unknown version)"
|
|
|
|
# show some more info about single roles here
|
2015-04-27 11:31:41 +00:00
|
|
|
self.display.display("- %s, %s" % (role_name, version))
|
2015-04-23 03:41:05 +00:00
|
|
|
else:
|
2015-04-27 11:31:41 +00:00
|
|
|
self.display.display("- the role %s was not found" % role_name)
|
2015-04-23 03:41:05 +00:00
|
|
|
else:
|
|
|
|
# show all valid roles in the roles_path directory
|
2015-04-27 11:31:41 +00:00
|
|
|
roles_path = self.get_opt('roles_path')
|
2015-04-23 03:41:05 +00:00
|
|
|
roles_path = os.path.expanduser(roles_path)
|
|
|
|
if not os.path.exists(roles_path):
|
2015-04-27 11:31:41 +00:00
|
|
|
raise AnsibleOptionsError("- the path %s does not exist. Please specify a valid path with --roles-path" % roles_path)
|
2015-04-23 03:41:05 +00:00
|
|
|
elif not os.path.isdir(roles_path):
|
2015-04-27 11:31:41 +00:00
|
|
|
raise AnsibleOptionsError("- %s exists, but it is not a directory. Please specify a valid path with --roles-path" % roles_path)
|
2015-04-23 03:41:05 +00:00
|
|
|
path_files = os.listdir(roles_path)
|
|
|
|
for path_file in path_files:
|
|
|
|
if get_role_metadata(path_file, options):
|
|
|
|
install_info = get_galaxy_install_info(path_file, options)
|
|
|
|
version = None
|
|
|
|
if install_info:
|
|
|
|
version = install_info.get("version", None)
|
|
|
|
if not version:
|
|
|
|
version = "(unknown version)"
|
2015-04-27 11:31:41 +00:00
|
|
|
self.display.display("- %s, %s" % (path_file, version))
|
|
|
|
return 0
|
2015-04-23 03:41:05 +00:00
|
|
|
|
|
|
|
#-------------------------------------------------------------------------------------
|
|
|
|
# The main entry point
|
|
|
|
#-------------------------------------------------------------------------------------
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
|
|
|
display = Display()
|
|
|
|
try:
|
2015-04-27 11:31:41 +00:00
|
|
|
cli = GalaxyCLI(sys.argv, display=display)
|
|
|
|
cli.parse()
|
|
|
|
sys.exit(cli.run())
|
|
|
|
except AnsibleOptionsError as e:
|
|
|
|
cli.parser.print_help()
|
|
|
|
display.display(str(e), stderr=True, color='red')
|
2015-04-23 03:41:05 +00:00
|
|
|
sys.exit(1)
|
2015-04-27 11:31:41 +00:00
|
|
|
except AnsibleError as e:
|
|
|
|
display.display(str(e), stderr=True, color='red')
|
|
|
|
sys.exit(2)
|
2015-04-23 03:41:05 +00:00
|
|
|
except KeyboardInterrupt:
|
|
|
|
display.error("interrupted")
|
2015-04-27 11:31:41 +00:00
|
|
|
sys.exit(3)
|