@ -234,7 +234,7 @@ def exit_without_ignore(options, rc=1):
"""
"""
if not get_opt(options, "ignore_errors", False):
if not get_opt(options, "ignore_errors", False):
print 'Y ou can use --ignore-errors to skip failed roles.'
print '- y ou can use --ignore-errors to skip failed roles.'
sys.exit(rc)
sys.exit(rc)
#-------------------------------------------------------------------------------------
#-------------------------------------------------------------------------------------
@ -268,10 +268,10 @@ def api_lookup_role_by_name(api_server, role_name):
parts = role_name.split(".")
parts = role_name.split(".")
user_name = ".".join(parts[0:-1])
user_name = ".".join(parts[0:-1])
role_name = parts[-1]
role_name = parts[-1]
print " downloading role '%s', owned by %s" % (role_name, user_name)
print "- downloading role '%s', owned by %s" % (role_name, user_name)
except:
except:
parser.print_help()
parser.print_help()
print "Invalid role name (%s). You must specify username.rolename" % role_name
print "- invalid role name (%s). Specify role as format: username.rolename" % role_name
sys.exit(1)
sys.exit(1)
url = 'https://%s/api/v1/roles/?owner__username=%s&name=%s' % (api_server,user_name,role_name)
url = 'https://%s/api/v1/roles/?owner__username=%s&name=%s' % (api_server,user_name,role_name)
@ -329,7 +329,7 @@ def api_get_list(api_server, what):
done = (data.get('next', None) == None)
done = (data.get('next', None) == None)
return results
return results
except:
except:
print " - failed to download the %s list" % what
print "- failed to download the %s list" % what
return None
return None
#-------------------------------------------------------------------------------------
#-------------------------------------------------------------------------------------
@ -338,7 +338,7 @@ def api_get_list(api_server, what):
def scm_archive_role(scm, role_url, role_version, role_name):
def scm_archive_role(scm, role_url, role_version, role_name):
if scm not in ['hg', 'git']:
if scm not in ['hg', 'git']:
print "SCM %s is not currently supported" % scm
print "- scm %s is not currently supported" % scm
return False
return False
tempdir = tempfile.mkdtemp()
tempdir = tempfile.mkdtemp()
clone_cmd = [scm, 'clone', role_url, role_name]
clone_cmd = [scm, 'clone', role_url, role_name]
@ -476,7 +476,7 @@ def fetch_role(role_name, target, role_data, options):
archive_url = role_name
archive_url = role_name
else:
else:
archive_url = 'https://github.com/%s/%s/archive/%s.tar.gz' % (role_data["github_user"], role_data["github_repo"], target)
archive_url = 'https://github.com/%s/%s/archive/%s.tar.gz' % (role_data["github_user"], role_data["github_repo"], target)
print " - downloading role from %s" % archive_url
print "- downloading role from %s" % archive_url
try:
try:
url_file = urllib2.urlopen(archive_url)
url_file = urllib2.urlopen(archive_url)
@ -490,7 +490,7 @@ def fetch_role(role_name, target, role_data, options):
except Exception, e:
except Exception, e:
# TODO: better urllib2 error handling for error
# TODO: better urllib2 error handling for error
# messages that are more exact
# messages that are more exact
print "E rror: failed to download the file."
print "- e rror: failed to download the file."
return False
return False
def install_role(role_name, role_version, role_filename, options):
def install_role(role_name, role_version, role_filename, options):
@ -498,7 +498,7 @@ def install_role(role_name, role_version, role_filename, options):
# to the specified (or default) roles directory
# to the specified (or default) roles directory
if not tarfile.is_tarfile(role_filename):
if not tarfile.is_tarfile(role_filename):
print "E rror: the file downloaded was not a tar.gz"
print "- e rror: the file downloaded was not a tar.gz"
return False
return False
else:
else:
if role_filename.endswith('.gz'):
if role_filename.endswith('.gz'):
@ -514,13 +514,13 @@ def install_role(role_name, role_version, role_filename, options):
meta_file = member
meta_file = member
break
break
if not meta_file:
if not meta_file:
print "E rror: this role does not appear to have a meta/main.yml file."
print "- e rror: this role does not appear to have a meta/main.yml file."
return False
return False
else:
else:
try:
try:
meta_file_data = yaml.safe_load(role_tar_file.extractfile(meta_file))
meta_file_data = yaml.safe_load(role_tar_file.extractfile(meta_file))
except:
except:
print "E rror: this role does not appear to have a valid meta/main.yml file."
print "- e rror: this role does not appear to have a valid meta/main.yml file."
return False
return False
# we strip off the top-level directory for all of the files contained within
# we strip off the top-level directory for all of the files contained within
@ -528,20 +528,20 @@ def install_role(role_name, role_version, role_filename, options):
# to the specified role's name
# to the specified role's name
role_path = os.path.join(get_opt(options, 'roles_path', '/etc/ansible/roles'), role_name)
role_path = os.path.join(get_opt(options, 'roles_path', '/etc/ansible/roles'), role_name)
role_path = os.path.expanduser(role_path)
role_path = os.path.expanduser(role_path)
print " - extracting %s to %s" % (role_name, role_path)
print "- extracting %s to %s" % (role_name, role_path)
try:
try:
if os.path.exists(role_path):
if os.path.exists(role_path):
if not os.path.isdir(role_path):
if not os.path.isdir(role_path):
print "E rror: the specified roles path exists and is not a directory."
print "- e rror: the specified roles path exists and is not a directory."
return False
return False
elif not get_opt(options, "force", False):
elif not get_opt(options, "force", False):
print "E rror: the specified role %s appears to already exist. Use --force to replace it." % role_name
print "- e rror: the specified role %s appears to already exist. Use --force to replace it." % role_name
return False
return False
else:
else:
# using --force, remove the old path
# using --force, remove the old path
if not remove_role(role_name, options):
if not remove_role(role_name, options):
print "E rror: %s doesn't appear to contain a role." % role_path
print "- e rror: %s doesn't appear to contain a role." % role_path
print "P lease remove this directory manually if you really want to put the role here."
print " p lease remove this directory manually if you really want to put the role here."
return False
return False
else:
else:
os.makedirs(role_path)
os.makedirs(role_path)
@ -563,11 +563,11 @@ def install_role(role_name, role_version, role_filename, options):
# write out the install info file for later use
# write out the install info file for later use
write_galaxy_install_info(role_name, role_version, options)
write_galaxy_install_info(role_name, role_version, options)
except OSError, e:
except OSError, e:
print "E rror: you do not have permission to modify files in %s" % role_path
print "- e rror: you do not have permission to modify files in %s" % role_path
return False
return False
# return the parsed yaml metadata
# return the parsed yaml metadata
print "%s was installed successfully" % role_name
print "- %s was installed successfully" % role_name
return meta_file_data
return meta_file_data
#-------------------------------------------------------------------------------------
#-------------------------------------------------------------------------------------
@ -588,7 +588,7 @@ def execute_init(args, options, parser):
if not offline:
if not offline:
api_config = api_get_config(api_server)
api_config = api_get_config(api_server)
if not api_config:
if not api_config:
print "T he API server (%s) is not responding, please try again later." % api_server
print "- t he API server (%s) is not responding, please try again later." % api_server
sys.exit(1)
sys.exit(1)
try:
try:
@ -598,18 +598,17 @@ def execute_init(args, options, parser):
role_path = os.path.join(init_path, role_name)
role_path = os.path.join(init_path, role_name)
if os.path.exists(role_path):
if os.path.exists(role_path):
if os.path.isfile(role_path):
if os.path.isfile(role_path):
print "T he path %s already exists, but is a file - aborting" % role_path
print "- t he path %s already exists, but is a file - aborting" % role_path
sys.exit(1)
sys.exit(1)
elif not force:
elif not force:
print "The directory %s already exists." % role_path
print "- the directory %s already exists." % role_path
print ""
print " you can use --force to re-initialize this directory,\n" + \
print "You can use --force to re-initialize this directory,\n" + \
" however it will reset any main.yml files that may have\n" + \
"however it will reset any main.yml files that may have\n" + \
" been modified there already."
"been modified there already."
sys.exit(1)
sys.exit(1)
except Exception, e:
except Exception, e:
parser.print_help()
parser.print_help()
print "N o role name specified for init"
print "- n o role name specified for init"
sys.exit(1)
sys.exit(1)
ROLE_DIRS = ('defaults','files','handlers','meta','tasks','templates','vars')
ROLE_DIRS = ('defaults','files','handlers','meta','tasks','templates','vars')
@ -668,7 +667,7 @@ def execute_init(args, options, parser):
f = open(main_yml_path, 'w')
f = open(main_yml_path, 'w')
f.write('---\n# %s file for %s\n' % (dir,role_name))
f.write('---\n# %s file for %s\n' % (dir,role_name))
f.close()
f.close()
print "%s was created successfully" % role_name
print "- %s was created successfully" % role_name
def execute_info(args, options, parser):
def execute_info(args, options, parser):
"""
"""
@ -695,13 +694,13 @@ def execute_install(args, options, parser):
# the user needs to specify one of either --role-file
# the user needs to specify one of either --role-file
# or specify a single user/role name
# or specify a single user/role name
parser.print_help()
parser.print_help()
print "Y ou must specify a user/role name or a roles file"
print "- y ou must specify a user/role name or a roles file"
sys.exit()
sys.exit()
elif len(args) == 1 and role_file:
elif len(args) == 1 and role_file:
# using a role file is mutually exclusive of specifying
# using a role file is mutually exclusive of specifying
# the role name on the command line
# the role name on the command line
parser.print_help()
parser.print_help()
print "P lease specify a user/role name, or a roles file, but not both"
print "- p lease specify a user/role name, or a roles file, but not both"
sys.exit(1)
sys.exit(1)
roles_done = []
roles_done = []
@ -739,12 +738,12 @@ def execute_install(args, options, parser):
# installing from galaxy
# installing from galaxy
api_config = api_get_config(api_server)
api_config = api_get_config(api_server)
if not api_config:
if not api_config:
print "T he API server (%s) is not responding, please try again later." % api_server
print "- t he API server (%s) is not responding, please try again later." % api_server
sys.exit(1)
sys.exit(1)
role_data = api_lookup_role_by_name(api_server, role_src)
role_data = api_lookup_role_by_name(api_server, role_src)
if not role_data:
if not role_data:
print "S orry, %s was not found on %s." % (role_src, api_server)
print "- s orry, %s was not found on %s." % (role_src, api_server)
continue
continue
role_versions = api_fetch_role_related(api_server, 'versions', role_data['id'])
role_versions = api_fetch_role_related(api_server, 'versions', role_data['id'])
@ -761,14 +760,16 @@ def execute_install(args, options, parser):
role["version"] = 'master'
role["version"] = 'master'
else:
else:
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]:
print "T he specified version (%s) was not found in the list of available versions." % role.version
print "- t he specified version (%s) was not found in the list of available versions." % role.version
exit_without_ignore(options)
exit_without_ignore(options)
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 = fetch_role(role_src, role["version"], role_data, options)
tmp_file = fetch_role(role_src, role["version"], role_data, options)
if tmp_file and install_role(role.get("name"), role.get("version"), tmp_file, 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
# we're done with the temp file, clean it up
os.unlink(tmp_file)
os.unlink(tmp_file)
# install dependencies, if we want them
# install dependencies, if we want them
@ -784,14 +785,14 @@ def execute_install(args, options, parser):
else:
else:
dep = ansible.utils.role_yaml_parse(dep)
dep = ansible.utils.role_yaml_parse(dep)
if not get_role_metadata(dep["name"], options):
if not get_role_metadata(dep["name"], options):
print ' adding dependency: %s' % dep["name"]
print '- adding dependency: %s' % dep["name"]
roles_left.append(dep)
roles_left.append(dep)
else:
else:
print ' dependency %s is already installed, skipping.' % dep["name"]
print '- dependency %s is already installed, skipping.' % dep["name"]
else :
if not tmp_file or not installed :
if tmp_file:
if tmp_file:
os.unlink(tmp_file)
os.unlink(tmp_file)
print "%s was NOT installed successfully." % role.get("name")
print "- %s was NOT installed successfully." % role.get("name")
exit_without_ignore(options)
exit_without_ignore(options)
sys.exit(0)
sys.exit(0)
@ -803,17 +804,17 @@ def execute_remove(args, options, parser):
if len(args) == 0:
if len(args) == 0:
parser.print_help()
parser.print_help()
print 'Y ou must specify at least one role to remove.'
print '- y ou must specify at least one role to remove.'
sys.exit()
sys.exit()
for role in args:
for role in args:
if get_role_metadata(role, options):
if get_role_metadata(role, options):
if remove_role(role, options):
if remove_role(role, options):
print 'successfully removed %s' % role
print '- successfully removed %s' % role
else:
else:
print "failed to remove role: %s" % role
print "- failed to remove role: %s" % role
else:
else:
print '%s is not installed, skipping.' % role
print '- %s is not installed, skipping.' % role
sys.exit(0)
sys.exit(0)
def execute_list(args, options, parser):
def execute_list(args, options, parser):
@ -825,7 +826,7 @@ def execute_list(args, options, parser):
"""
"""
if len(args) > 1:
if len(args) > 1:
print "P lease specify only one role to list, or specify no roles to see a full list"
print "- p lease specify only one role to list, or specify no roles to see a full list"
sys.exit(1)
sys.exit(1)
if len(args) == 1:
if len(args) == 1:
@ -840,19 +841,19 @@ def execute_list(args, options, parser):
if not version:
if not version:
version = "(unknown version)"
version = "(unknown version)"
# show some more info about single roles here
# show some more info about single roles here
print " %s, %s" % (role_name, version)
print "- %s, %s" % (role_name, version)
else:
else:
print "T he role %s was not found" % role_name
print "- t he role %s was not found" % role_name
else:
else:
# show all valid roles in the roles_path directory
# show all valid roles in the roles_path directory
roles_path = get_opt(options, 'roles_path')
roles_path = get_opt(options, 'roles_path')
roles_path = os.path.expanduser(roles_path)
roles_path = os.path.expanduser(roles_path)
if not os.path.exists(roles_path):
if not os.path.exists(roles_path):
parser.print_help()
parser.print_help()
print "T he path %s does not exist. Please specify a valid path with --roles-path" % roles_path
print "- t he path %s does not exist. Please specify a valid path with --roles-path" % roles_path
sys.exit(1)
sys.exit(1)
elif not os.path.isdir(roles_path):
elif not os.path.isdir(roles_path):
print "%s exists, but it is not a directory. Please specify a valid path with --roles-path" % roles_path
print "- %s exists, but it is not a directory. Please specify a valid path with --roles-path" % roles_path
parser.print_help()
parser.print_help()
sys.exit(1)
sys.exit(1)
path_files = os.listdir(roles_path)
path_files = os.listdir(roles_path)
@ -864,7 +865,7 @@ def execute_list(args, options, parser):
version = install_info.get("version", None)
version = install_info.get("version", None)
if not version:
if not version:
version = "(unknown version)"
version = "(unknown version)"
print " %s, %s" % (path_file, version)
print "- %s, %s" % (path_file, version)
sys.exit(0)
sys.exit(0)
#-------------------------------------------------------------------------------------
#-------------------------------------------------------------------------------------
@ -882,7 +883,7 @@ def main():
fn = globals()["execute_%s" % action]
fn = globals()["execute_%s" % action]
fn(args, options, parser)
fn(args, options, parser)
#except KeyError, e:
#except KeyError, e:
# print "E rror: %s is not a valid action. Valid actions are: %s" % (action, ", ".join(VALID_ACTIONS))
# print "- e rror: %s is not a valid action. Valid actions are: %s" % (action, ", ".join(VALID_ACTIONS))
# sys.exit(1)
# sys.exit(1)
if __name__ == "__main__":
if __name__ == "__main__":