def run(self, argv): root = get_playbook_root(os.getcwd()) if not root: print ''' can't find playbook. use `arm init` to create recommended structure. or use the `--no-dependencies` option.''' return 1 roles = odict() if getattr(argv, 'requirements', ''): for role_ident in open(argv.requirements[0], 'r'): roles = self._fetch(role_ident, argv.no_deps, roles) else: roles = self._fetch(argv.role_or_module, argv.no_deps, roles) for alias, role in roles.items(): self._install_and_link(alias, role, getattr(argv, 'upgrade', False)) print "\nrole(s) '%s' installed succesfully.\n" % (", ".join( roles.keys())) exit(0)
def _install_and_link(self, alias, role, upgrade=False): root = get_playbook_root(os.getcwd()) source_path = role.get_path() library_path = os.path.join(root, LIBRARY_ROLE_PATH, role.get_name()) link_path = os.path.join(root, 'roles', alias) if os.path.exists(library_path): if os.path.exists(link_path) and not os.path.islink(link_path): raise Exception("role '%s' already exists as a non-library role") if upgrade: if os.path.exists(library_path): shutil.rmtree(library_path) if os.path.islink(link_path): print "unlinking: %s" % link_path os.unlink(link_path) else: raise CommandException("'%s' already installed in library, use --upgrade to install latest" % role.get_name()) shutil.copytree(source_path, library_path) os.symlink( os.path.relpath(os.path.join(LIBRARY_ROLE_PATH,role.get_name()), 'roles/'), os.path.join(root,'roles',os.path.basename(alias)) )
def run(self, argv): root = get_playbook_root(os.getcwd()) if not root: print ''' can't find playbook. use `arm init` to create recommended structure. or use the `--no-dependencies` option.''' return 1 roles = odict() if getattr(argv, 'requirements', ''): for role_ident in open(argv.requirements,'r'): roles = self._fetch(role_ident, argv.no_dependencies, roles) else: roles = self._fetch(argv.role, argv.no_dependencies, roles ) for alias,role in roles.items(): self._install_and_link(alias, role, getattr(argv, 'upgrade', False)) print "\nrole(s) '%s' installed succesfully.\n" % (", ".join(roles.keys())) return 0
def run(self, argv): _root = get_playbook_root(os.getcwd()) _roles_directory = os.path.join(_root, 'roles') for _item in os.listdir(_roles_directory): _item = os.path.join(_roles_directory, _item) if not os.path.islink(_item): continue # _item vs realpath(_item) alias = None if os.path.basename(_item) != os.path.basename(os.path.realpath(_item)): alias = os.path.basename(_item) # if git if os.path.exists(os.path.join(os.path.realpath(_item),'.git')): repo = git_repo(os.path.realpath(_item)) origin = repo.remotes[0].config_reader.config.get('remote "origin"','url') commit = repo.head.commit.hexsha if not alias: print "git+%s@%s" % (origin,commit) continue print "git+%s@%s#alias=%s" % (origin,commit,alias) # if mercurial if os.path.exists(os.path.join(os.path.realpath(_item),'.hg')): repo = hg_repo(os.path.realpath(_item)) print repo if not alias: print "hg+%s@%s" % (origin,commit) continue print "hg+%s@%s#alias=%s" % (origin,commit,alias)
def _install_and_link(self, alias, rmp, upgrade=False): root = get_playbook_root(os.getcwd()) source_path = rmp.get_path() library_path = None link_path = None if type(rmp) == Role: installed_rmp_dir = settings.installed_roles_dir ansible_rmp_dir = settings.ansible_roles_dir elif type(rmp) == Module: installed_rmp_dir = settings.installed_modules_dir ansible_rmp_dir = settings.ansible_modules_dir installed_rmp_path = os.path.join(installed_rmp_dir, rmp.get_name()) library_path = os.path.join(root, installed_rmp_path) link_path = os.path.join(root, ansible_rmp_dir, alias) # TODO : test if a 'local' route makes sense for a role dependency # if the library path is also the role, local role dependency #if os.path.realpath(link_path) == os.path.realpath(library_path): #return if os.path.exists(library_path) and not upgrade: raise CommandException("'%s' already installed in library, use --upgrade to install latest" % rmp.get_name()) if os.path.exists(link_path): if not os.path.islink(link_path): if type(rmp) == Role: raise Exception("role '%s' already exists as a non-installed role" % rmp) elif type(rmp) == Module: raise Exception("module '%s' aleady exists as a non-installed module" % rmp) if not upgrade: raise CommandException("'%s' already installed in library, use --upgrade to install latest" % rmp.get_name()) if upgrade: if os.path.exists(library_path): print "\t upgrading :: removing old version" shutil.rmtree(library_path) if os.path.islink(link_path): print "\t upgrading :: unlinking old version" os.unlink(link_path) shutil.copytree(source_path, library_path) ansible_rmp_path = os.path.join(root,ansible_rmp_dir) if not os.path.exists(ansible_rmp_path): os.mkdir(ansible_rmp_path) os.symlink( os.path.relpath(installed_rmp_path, ansible_rmp_dir), os.path.join(link_path) )
def run(self, argv): _root = get_playbook_root(os.getcwd()) _role = os.path.join(_root, 'roles', argv.role) if not os.path.exists(_role): print "error :: the role `%s` does not exist in the playbook's library" % _role return 1 if not os.path.islink(_role): print "error :: the role `%s` was not installed using ARM. refusing to delete." % _role return 1 _library = os.path.join(os.path.realpath(_role)) repo = Repo(_library) # check to see if the role in the library has non-committed changes if repo.git.status(porcelain=True).strip() != '': print "error :: the role `%s` has non-commited changes" % _role return 1 # check to see if the library role has non-pushed changes # http://stackoverflow.com/questions/3844801 def checkEqual(lst): return not lst or lst.count(lst[0]) == len(lst) if not checkEqual([ref.commit for ref in repo.refs]): print "error :: the role `%s` has commits different from the origin" return 1 for root, dir, files in os.walk(_library): if '.git' in root: continue for f in files: print "%s/%s" % (root, f) if query_true_false('Are you sure you want to remove `%s`?' % argv.role, default=True): os.unlink(_role) shutil.rmtree(_library) else: print "exiting"
def run(self, argv): _root = get_playbook_root(os.getcwd()) if not _root: print( textwrap.dedent('''\ can't find playbook. use `arm init` to create recommended structure.''')) return 1 _roles_directory = os.path.join(_root, 'roles') for _item in os.listdir(_roles_directory): _item = os.path.join(_roles_directory, _item) if not os.path.islink(_item): continue # _item vs realpath(_item) alias = None if os.path.basename(_item) != os.path.basename( os.path.realpath(_item)): alias = os.path.basename(_item) # if git if os.path.exists(os.path.join(os.path.realpath(_item), '.git')): repo = git_repo(os.path.realpath(_item)) origin = repo.remotes[0].config_reader.config.get( 'remote "origin"', 'url') commit = repo.head.commit.hexsha if not alias: print "git+%s@%s" % (origin, commit) continue print "git+%s@%s#alias=%s" % (origin, commit, alias) # if mercurial if os.path.exists(os.path.join(os.path.realpath(_item), '.hg')): repo = hg_repo(os.path.realpath(_item)) print repo if not alias: print "hg+%s@%s" % (origin, commit) continue print "hg+%s@%s#alias=%s" % (origin, commit, alias)
def run(self, argv): _root = get_playbook_root(os.getcwd()) _role = os.path.join(_root, 'roles', argv.role) if not os.path.exists(_role): print "error :: the role `%s` does not exist in the playbook's library" % _role return 1 if not os.path.islink(_role): print "error :: the role `%s` was not installed using ARM. refusing to delete." % _role return 1 _library = os.path.join(os.path.realpath(_role)) repo = Repo(_library) # check to see if the role in the library has non-committed changes if repo.git.status(porcelain=True).strip() != '': print "error :: the role `%s` has non-commited changes" % _role return 1 # check to see if the library role has non-pushed changes # http://stackoverflow.com/questions/3844801 def checkEqual(lst): return not lst or lst.count(lst[0]) == len(lst) if not checkEqual([ ref.commit for ref in repo.refs ] ): print "error :: the role `%s` has commits different from the origin" return 1 for root, dir, files in os.walk(_library): if '.git' in root: continue for f in files: print "%s/%s" % (root, f) if query_true_false('Are you sure you want to remove `%s`?' % argv.role, default=True): os.unlink(_role) shutil.rmtree(_library) else: print "exiting"
def fetch(self, identifier): _repo = self.vcs(identifier) _uid = self._uid(identifier) _destination = os.path.join(get_playbook_root(), '.cache', self._uid(identifier)) if os.path.exists(_destination): shutil.rmtree(_destination) try: _repo.obtain(_destination) except InstallationError as e: raise RouteException("could not retrieve '%s' " % identifier) playbook_tests = ('roles', '.arm', 'group_vars', 'host_vars',) role_tests = ('tasks', 'meta', 'files', 'handlers') def _test_fetched(tests): return len([True for test in tests if os.path.exists(os.path.join(_destination, test))]) > 0 is_playbook = _test_fetched(playbook_tests) is_role = _test_fetched(role_tests) if is_role: return Role(_destination, uid=_uid) elif is_playbook: return Playbook(_destination, uid=_uid) else: return Module(_destination, uid=_uid) #if is_role and type(pod) != Role: #print "Warning: potential install of module or playbook as role" #if is_playbook and type(pod) != Playbook: #print "Warning: potential install of module or role as playbook" #if not is_playbook and not is_role and type(pod) != Module: #print "Warning: potential install of role or playbook as module" return pod(_destination, uid=_uid)
def _install_and_link(self, alias, rmp, upgrade=False): root = get_playbook_root(os.getcwd()) source_path = rmp.get_path() library_path = None link_path = None if type(rmp) == Role: installed_rmp_dir = settings.installed_roles_dir ansible_rmp_dir = settings.ansible_roles_dir elif type(rmp) == Module: installed_rmp_dir = settings.installed_modules_dir ansible_rmp_dir = settings.ansible_modules_dir installed_rmp_path = os.path.join(installed_rmp_dir, rmp.get_name()) library_path = os.path.join(root, installed_rmp_path) link_path = os.path.join(root, ansible_rmp_dir, alias) # TODO : test if a 'local' route makes sense for a role dependency # if the library path is also the role, local role dependency #if os.path.realpath(link_path) == os.path.realpath(library_path): #return if os.path.exists(library_path) and not upgrade: raise CommandException( "'%s' already installed in library, use --upgrade to install latest" % rmp.get_name()) if os.path.exists(link_path): if not os.path.islink(link_path): if type(rmp) == Role: raise Exception( "role '%s' already exists as a non-installed role" % rmp) elif type(rmp) == Module: raise Exception( "module '%s' aleady exists as a non-installed module" % rmp) if not upgrade: raise CommandException( "'%s' already installed in library, use --upgrade to install latest" % rmp.get_name()) if upgrade: if os.path.exists(library_path): print "\t upgrading :: removing old version" shutil.rmtree(library_path) if os.path.islink(link_path): print "\t upgrading :: unlinking old version" os.unlink(link_path) shutil.copytree(source_path, library_path) ansible_rmp_path = os.path.join(root, ansible_rmp_dir) if not os.path.exists(ansible_rmp_path): os.mkdir(ansible_rmp_path) os.symlink(os.path.relpath(installed_rmp_path, ansible_rmp_dir), os.path.join(link_path))
def run(self, argv): patterns = os.path.join(os.path.dirname(__file__),'init') current = os.getcwd() if argv.playbook and argv.editable: raise CommandException("combination of flags '-e' and '-p' not allowed.") def _default(item): return item if item else '' root = _default(get_playbook_root(current)) playbook = _default(argv.playbook) role = _default(argv.role) module = _default(argv.module) sources = ( os.path.join(patterns, 'playbook'), None, os.path.join(patterns, 'role'), os.path.join(patterns, 'role'), os.path.join(patterns, 'module'), os.path.join(patterns, 'module'), ) destinations = ( os.path.join(current, playbook), None, os.path.join(root, settings.paths.ansible_roles_dir, role), os.path.join(root, settings.paths.installed_roles_dir, role), os.path.join(root, settings.paths.ansible_modules_dir, module), os.path.join(root, settings.paths.installed_modules_dir, module) ) links = ( None, None, None, os.path.join(root, settings.paths.ansible_roles_dir, role), None, os.path.join(root, settings.paths.ansible_modules_dir, module) ) def _eval(item,value): return bool(item) * value index = (_eval(playbook, 0) | _eval(role,2) | _eval(module,4)) + _eval(argv.editable,1) _source = sources[index] _destination = destinations[index] env = Environment(loader=FileSystemLoader(_source)) def _install_template(_path): os.makedirs(os.path.join(_destination, _path)) files = os.listdir(os.path.join(_source,_path)) for f in files: _s = os.path.join(_source,_path,f) _d = os.path.join(_destination, _path, f) if os.path.isdir(_s): # recurse through any subdirectory _install_template(os.path.join(_path,f)) elif '.armj2' in f: # render template to _destination + _path +f template = env.get_template(os.path.join(_path,f)) _d = _d.replace('.armj2','') with open(_d, 'wb') as fh: fh.write(template.render(name=_d)) else: # copy to _destination + _path + f shutil.copy(_s,_d) _name = ( 'playbook', None, 'role', 'role', 'module', 'module' ) if os.path.exists(_destination) or \ (links[index] and os.path.exists(links[index])): raise CommandException("%s '%s' already exists." % (_name[index],os.path.basename(_destination))) _install_template('') if links[index]: os.symlink( os.path.relpath(_destination, os.path.dirname(links[index])), links[index]) print "ansible %s created successfully" % (_name[index]) return 0