def popup(original, orig_type, thr, title): """ Start the thread and display a popup of info until the thread is finished """ thr.start() info_str = "" while thr.is_alive(): if orig_type == 'containers': info = diff(Containers(), original) elif orig_type == 'images': info = diff(Images(), original) if info: info_str = "" for entry in info: info_str = entry[0] + ": " + entry[1] + "\n" + info_str if self.action['action_name'] != 'configure': npyscreen.notify_wait(info_str, title=title) time.sleep(1) thr.join() try: result = self.api_action.queue.get(False) if isinstance(result, tuple) and isinstance(result[1], tuple): running, failed = result[1] r_str = '' for container in running: r_str += container + ": successful\n" for container in failed: r_str += container + ": failed\n" npyscreen.notify_confirm(r_str) except Exception as e: # pragma: no cover pass return
def on_ok(self): """ Take the tool selections and build them """ def diff(first, second): """ Get the elements that exist in the first list and not in the second """ second = set(second) return [item for item in first if item not in second] def popup(original, orig_type, thr, title): """ Start the thread and display a popup of info until the thread is finished """ thr.start() info_str = "" while thr.is_alive(): if orig_type == 'containers': info = diff(Containers(), original) elif orig_type == 'images': info = diff(Images(), original) if info: info_str = "" for entry in info: # TODO limit length of info_str to fit box info_str += entry[0] + ": " + entry[1] + "\n" npyscreen.notify_wait(info_str, title=title) time.sleep(1) return original_images = Images() api_action = Action() for repo in self.tools_tc: for tool in self.tools_tc[repo]: self.logger.info(tool) if self.tools_tc[repo][tool].value: t = tool if t.startswith('/:'): t = " " + t[1:] t = t.split(":") thr = threading.Thread(target=api_action.build, args=(), kwargs={ 'name': t[0], 'branch': t[1], 'version': t[2] }) popup(original_images, "images", thr, 'Please wait, building images...') npyscreen.notify_confirm("Done building images.", title='Built images') self.quit()
def core_tools(self, action): """ Perform actions for core tools """ def diff(first, second): """ Get the elements that exist in the first list and not in the second """ second = set(second) return [item for item in first if item not in second] def popup(original, orig_type, thr, title): """ Start the thread and display a popup of info until the thread is finished """ thr.start() info_str = "" while thr.is_alive(): if orig_type == 'containers': info = diff(Containers(), original) elif orig_type == 'images': info = diff(Images(), original) if info: info_str = "" for entry in info: # TODO limit length of info_str to fit box info_str += entry[0] + ": " + entry[1] + "\n" npyscreen.notify_wait(info_str, title=title) time.sleep(1) return if action == 'install': original_images = Images() thr = threading.Thread(target=self.api_action.cores, args=(), kwargs={"action": "install"}) popup(original_images, "images", thr, 'Please wait, installing core containers...') npyscreen.notify_confirm("Done installing core containers.", title='Installed core containers') elif action == 'build': self.parentApp.change_form('BUILDCORETOOLS') elif action == 'start': self.parentApp.change_form('STARTCORETOOLS') elif action == 'stop': self.parentApp.change_form('STOPCORETOOLS') elif action == 'clean': self.parentApp.change_form('CLEANCORETOOLS') elif action == "inventory": self.parentApp.change_form('COREINVENTORY') elif action == 'update': self.parentApp.change_form('UPDATECORETOOLS') elif action == 'remove': self.parentApp.change_form('REMOVECORETOOLS') return
def core_tools(action): """ Perform actions for core tools """ def diff(first, second): """ Get the elements that exist in the first list and not in the second """ second = set(second) return [item for item in first if item not in second] def popup(original, orig_type, thr, title): """ Start the thread and display a popup of info until the thread is finished """ thr.start() info_str = '' while thr.is_alive(): if orig_type == 'containers': info = diff(Containers(), original) elif orig_type == 'images': info = diff(Images(), original) if info: info_str = '' for entry in info: # TODO limit length of info_str to fit box info_str += entry[0] + ': ' + entry[1] + '\n' npyscreen.notify_wait(info_str, title=title) time.sleep(1) return if action == 'install': original_images = Images() m_helper = MenuHelper() thr = Thread(target=m_helper.cores, args=(), kwargs={'action': 'install'}) popup(original_images, 'images', thr, 'Please wait, installing core containers...') notify_confirm( 'Done installing core containers (any' ' already installed tools untouched).', title='Installed core containers') return
def popup(original, orig_type, thr, title): """ Start the thread and display a popup of info until the thread is finished """ thr.start() info_str = "" while thr.is_alive(): if orig_type == 'containers': info = diff(Containers(), original) elif orig_type == 'images': info = diff(Images(), original) if info: info_str = "" for entry in info: # TODO limit length of info_str to fit box info_str += entry[0] + ": " + entry[1] + "\n" npyscreen.notify_wait(info_str, title=title) time.sleep(1) return
def test_images(): """ Test the images function """ images = Images() images = Images(vent=False) assert isinstance(images, list)
def on_ok(self): """ Take the tool selections and perform the provided action on them """ def diff(first, second): """ Get the elements that exist in the first list and not in the second """ second = set(second) return [item for item in first if item not in second] def popup(original, orig_type, thr, title): """ Start the thread and display a popup of info until the thread is finished """ thr.start() info_str = "" while thr.is_alive(): if orig_type == 'containers': info = diff(Containers(), original) elif orig_type == 'images': info = diff(Images(), original) if info: info_str = "" for entry in info: info_str = entry[0] + ": " + entry[1] + "\n" + info_str if self.action['action_name'] != 'configure': npyscreen.notify_wait(info_str, title=title) time.sleep(1) thr.join() try: result = self.api_action.queue.get(False) if isinstance(result, tuple) and isinstance(result[1], tuple): running, failed = result[1] r_str = '' for container in running: r_str += container + ": successful\n" for container in failed: r_str += container + ": failed\n" npyscreen.notify_confirm(r_str) except Exception as e: # pragma: no cover pass return if self.action['type'] == 'images': originals = Images() else: originals = Containers() tool_d = {} if self.action['action_name'] in ['clean', 'remove', 'stop', 'update']: reconfirmation_str = "" if self.action['cores']: reconfirmation_str = "Are you sure you want to " reconfirmation_str += self.action['action_name'] reconfirmation_str += " core containers?" else: reconfirmation_str = "Are you sure you want to " reconfirmation_str += self.action['action_name'] reconfirmation_str += " plugin containers?" perform = npyscreen.notify_ok_cancel(reconfirmation_str, title="Confirm command") if not perform: return tools_to_configure = [] for repo in self.tools_tc: for tool in self.tools_tc[repo]: self.logger.info(tool) if self.tools_tc[repo][tool].value: t = tool.split(":", 2)[2].split("/")[-1] if t.startswith('/:'): t = " " + t[1:] t = t.split(":") if self.action['action_name'] == 'start': status = self.action['action_object2'](name=t[0], branch=t[1], version=t[2]) if status[0]: tool_d.update(status[1]) elif self.action['action_name'] == 'configure': constraints = { 'name': t[0], 'branch': t[1], 'version': t[2], 'repo': repo } options = ['type'] action = self.action['api_action'] tool = action.p_helper.constraint_options( constraints, options)[0] # only one tool should be returned name = tool.keys()[0] if tool[name]['type'] == 'registry': registry_image = True else: registry_image = False kargs = { 'name': 'Configure ' + t[0], 'tool_name': t[0], 'branch': t[1], 'version': t[2], 'repo': repo, 'next_tool': None, 'get_configure': action.get_configure, 'save_configure': action.save_configure, 'restart_tools': action.restart_tools, 'clean': action.clean, 'prep_start': action.prep_start, 'start_tools': action.start, 'from_registry': registry_image } if tools_to_configure: kargs['next_tool'] = tools_to_configure[-1] self.parentApp.addForm("EDITOR" + t[0], EditorForm, **kargs) tools_to_configure.append("EDITOR" + t[0]) else: kargs = {'name': t[0], 'branch': t[1], 'version': t[2]} # add core recognition if self.action['cores']: kargs.update({'groups': 'core'}) # use latest version for update, not necessarily # version in manifest if self.action['action_name'] == 'update': if t[2] != 'HEAD': repo_commits = self.m_helper.repo_commits( repo)[1] for branch in repo_commits: if branch[0] == t[1]: kargs.update( {'new_version': branch[1][0]}) else: kargs.update({'new_version': 'HEAD'}) thr = Thread(target=self.action['action_object1'], args=(), kwargs=kargs) popup( originals, self.action['type'], thr, 'Please wait, ' + self.action['present_t'] + '...') if self.action['action_name'] == 'start': thr = Thread(target=self.action['action_object1'], args=(), kwargs={'tool_d': tool_d}) popup(originals, self.action['type'], thr, 'Please wait, ' + self.action['present_t'] + '...') if self.action['action_name'] != 'configure': npyscreen.notify_confirm('Done ' + self.action['present_t'] + '.', title=self.action['past_t']) self.quit() else: if len(tools_to_configure) > 0: self.parentApp.change_form(tools_to_configure[-1]) else: npyscreen.notify_confirm( "No tools selected, returning to" " main menu", title="No action taken") self.quit()
def test_images(): """ Test the images function """ images = Images() images = Images(vent=False) assert type(images) == list
def inventory(self, choices=None): """ Return a dictionary of the inventory items and status """ self.logger.info("Starting: inventory") status = (True, None) self.logger.info("choices specified: " + str(choices)) try: # choices: repos, core, tools, images, built, running, enabled items = { 'repos': [], 'core': [], 'tools': [], 'images': [], 'built': [], 'running': [], 'enabled': [] } tools = self.plugin.tools() self.logger.info("found tools: " + str(tools)) for choice in choices: for tool in tools: try: if choice == 'repos': if 'repo' in tool: if tool['repo'] and tool['repo'] not in items[ choice]: items[choice].append(tool['repo']) elif choice == 'core': if 'groups' in tool: if 'core' in tool['groups']: items[choice].append( (tool['section'], tool['name'])) elif choice == 'tools': items[choice].append( (tool['section'], tool['name'])) elif choice == 'images': # TODO also check against docker images = Images() items[choice].append( (tool['section'], tool['name'], tool['image_name'])) elif choice == 'built': items[choice].append( (tool['section'], tool['name'], tool['built'])) elif choice == 'running': containers = Containers() status = 'not running' for container in containers: image_name = tool['image_name'].rsplit( ":" + tool['version'], 1)[0] image_name = image_name.replace(':', '-') image_name = image_name.replace('/', '-') if container[0] == image_name: status = container[1] items[choice].append( (tool['section'], tool['name'], status)) elif choice == 'enabled': items[choice].append( (tool['section'], tool['name'], tool['enabled'])) else: # unknown choice pass except Exception as e: # pragma: no cover self.logger.error( "unable to grab information about tool: " + str(tool) + " because: " + str(e)) pass status = (True, items) except Exception as e: self.logger.error("inventory failed with error: " + str(e)) status = (False, e) self.logger.info("Status of inventory: " + str(status)) self.logger.info("Finished: inventory") return status
def update(self, repo=None, name=None, groups=None, enabled="yes", branch="master", version="HEAD"): """ Update a set of tools that match the parameters given, if no parameters are given, updated all installed tools on the master branch at verison HEAD that are enabled """ args = locals() self.logger.info("Starting: update") self.logger.info(args) status = (True, None) try: options = ['path', 'image_name', 'image_id'] sections, template = self.plugin.constraint_options(args, options) # get existing containers and images and states running_containers = Containers() built_images = Images() self.logger.info("running docker containers: " + str(running_containers)) self.logger.info("built docker images: " + str(built_images)) # if repo, pull and build # if registry image, pull for section in sections: try: cwd = os.getcwd() self.logger.info("current working directory: " + str(cwd)) os.chdir(sections[section]['path']) self.plugin.version = version self.plugin.branch = branch c_status = self.plugin.checkout() self.logger.info(c_status) try: os.chdir(cwd) except Exception as e: # pragma: no cover self.logger.error("unable to change directory: " + str(e)) pass template = self.plugin.builder( template, sections[section]['path'], sections[section]['image_name'], section, build=True, branch=branch, version=version) self.logger.info(template) # stop and remove old containers and images if image_id updated # !! TODO # start containers if they were running # !! TODO # TODO logging except Exception as e: # pragma: no cover self.logger.error("unable to update: " + str(section) + " because: " + str(e)) template.write_config() except Exception as e: self.logger.error("update failed with error: " + str(e)) status = (False, e) self.logger.info("Status of update: " + str(status)) self.logger.info("Finished: update") return status