def _jobs_status(self): self._connect() for job in self.J.keys(): # Check if this job has last build try: build = self.J[job].get_last_build() except jenkinsapi.custom_exceptions.NoBuildData: build = None if build: buildno = build.buildno # Status color status = build.get_status() if build.get_status() == "SUCCESS": status = TColors.green(status) elif build.get_status() == "FAILURE": status = TColors.red(status) else: status = TColors.yellow(status) # Color duration duration = format_last_run(build.get_duration(), color=False) # Color timestamp last_run = format_last_run(build.get_timestamp(), color=False) else: buildno = " - " status = " - " duration = " - " last_run = " - " # update results self.results.append([job, buildno, last_run, status, duration]) # Print results headers = ["Name", "no", "Last Run", "Status", "Exec Time"] print_table(self.results, headers=headers)
def _delete_index_by_day(self, retain_days=7, confirm=True): self._get_all_indecies() # Match any pattern with "somethingYYYY.MM.DD pattern = ".(\d{4}\.\d{2}\.\d{2})$" notpattern = "([A-Za-z\-\_]+)" match_indcies = dict() for index in self.indices: match = re.findall(pattern, index) if match: index_name = re.findall(notpattern, index)[0] if not match_indcies.get(index_name): match_indcies.update({index_name: []}) match_indcies[index_name].append(index) else: print TColors.red(" ? Index name '%s' does not mach date pattern. Ignoring" % index) to_be_deleted = [] for index_name in match_indcies: new_list = sorted(match_indcies.get(index_name)) index_len = len(new_list) if index_len > retain_days: print "%s, Count: %d, to be delete: %s" % (index_name, index_len, TColors.yellow(index_len-retain_days)) for delete_index in new_list[0:index_len-retain_days]: print " > X ", TColors.yellow(delete_index) to_be_deleted.append(delete_index) if len(to_be_deleted) > 0: self.to_be_deleted = ",".join(to_be_deleted) self._delete_index() else: print "Nothing to be deleted"
def _print_results(self): question = "Q> Ansible Summary status| host(s) '{}' returned '{}' sort '{}' order '{}' filter '{}'"\ .format(TColors.yellow(self.answers["host"]), TColors.yellow(len(self.results)), TColors.yellow(self.answers["sort"]), TColors.yellow(self.answers["order"]), TColors.yellow(self.answers["filter"])) headers = ["Host", "Last Run", "Play Name", "Ok", "CH", "UR", "FL", "Time(s)", "+Tag", "-Tag", "User"] print_table(self.results, headers=headers, question=question)
def _format_changed_item(self, item, truncate): item["skip_tags"] = ",".join(item["skip_tags"]) # Flatten list # Tags color and flatten list if "all" in item["only_tags"]: item["only_tags"] = ",".join(item["only_tags"]) else: item["only_tags"] = TColors.yellow(",".join(item["only_tags"])) # Color last last_run = format_last_run(item["@timestamp"], color=False) # Truncate changed_module_args_tr = truncate_str(item["changed_module_args"], truncate) changed_msg_tr = truncate_str(item["changed_msg"], truncate) # Update our results table with all values self.results.append( [ item["host"], last_run, item["play_name"], item["user"], item["only_tags"], item["skip_tags"], item["extra_vars"], item["changed_module_name"], changed_module_args_tr, changed_msg_tr, ] )
def _projects_console(self): self._connect() job_name = self.arguments.get("<jobname>") buildnober = self.arguments.get("<buildnumber>") try: if buildnober is None: build = self.J[job_name].get_last_build() else: build = self.J[job_name].get_build(int(buildnober)) except jenkinsapi.custom_exceptions.UnknownJob: print "Error '" + TColors.red(job_name) + "' does not exist" exit(1) except KeyError: print "Error unknown job build '" + TColors.red(buildnober) + " for '" + TColors.red(job_name) exit(1) else: print build.get_console()
def _projects_detail(self): self._connect() job_name = self.arguments.get("<jobname>") try: project = self.J[job_name] except jenkinsapi.custom_exceptions.UnknownJob: print "Error '" + TColors.red(job_name) + "' does not exist" exit(1) first_build = project.get_first_buildnumber() last_build = project.get_last_buildnumber() r_limit = self.arguments.get("--rlimit") if (last_build - first_build) > int(r_limit): first_build = last_build - int(r_limit) for buildno in range(first_build, last_build): try: build = project.get_build(buildno) except jenkinsapi.custom_exceptions.NoBuildData: build = None if build: # Status color status = build.get_status() if build.get_status() == "SUCCESS": status = TColors.green(status) elif build.get_status() == "FAILURE": status = TColors.red(status) if build: status = TColors.yellow(status) # Color duration duration = format_last_run(build.get_duration()) # Color timestamp last_run = format_last_run(build.get_timestamp(), color=False) self.results.append([buildno, last_run, duration, status]) # Print results headers = ["No.", "Last run", "Exec time", "Status"] print_table(self.results, headers=headers)
def _projects_status(self): self._connect() for job in self.J.keys(): name = job.split("-") if len(name) != 2: print "Error Jenkins job '{}' violates name SKIPPING JOB. Name example 'ansible-env-project_name.yml'"\ .format(name) return False # Check if this job has last build try: build = self.J[job].get_last_build() except jenkinsapi.custom_exceptions.NoBuildData: build = None if build: buildno = build.buildno # Status color status = build.get_status() if build.get_status() == "SUCCESS": status = TColors.green(status) elif build.get_status() == "FAILURE": status = TColors.red(status) else: status = TColors.yellow(status) # Color duration duration = format_last_run(build.get_duration()) # Color timestamp last_run = format_last_run(build.get_timestamp()) else: buildno = " - " status = " - " duration = " - " last_run = " - " # update results self.results.append([name[0], name[1], buildno, last_run, status, duration]) # Print results headers = ["ENV", "Name", "no", "Last Run", "Status", "Exec Time"] print_table(self.results, headers=headers)
def _format_summary_item(self, item): # TODO: smart print of time # Status Flag if len(self.inventory_hosts) > 1 and (self.answers["filter"] is None or self.answers["filter"] == "tag=all"): try: pop_host = self.inventory_hosts.index(item["host"]) self.inventory_hosts.pop(pop_host) item["host"] = "* " + item["host"] except: item["host"] = "? " + item["host"] # Tags color if "all" in item["only_tags"]: item["only_tags"] = ",".join(item["only_tags"]) else: item["only_tags"] = TColors.yellow(",".join(item["only_tags"])) item["skip_tags"] = ",".join(item["skip_tags"]) # Color recap if item["ok"] > 0: item["ok"] = TColors.green(item["ok"]) if item["failures"] > 0: item["failures"] = TColors.red(item["failures"]) if item["changed"] > 0: item["changed"] = TColors.yellow(item["changed"]) if item["unreachable"] > 0: item["unreachable"] = TColors.red(item["unreachable"]) if item["failures"] > 0 or item["unreachable"] > 0: item["host"] = TColors.red(item["host"]) elif item["changed"] > 0: item["host"] = TColors.yellow(item["host"]) else: item["host"] = TColors.green(item["host"]) # Color last run last_run = format_last_run(item["@timestamp"]) self.results.append([item["host"], last_run, item["play_name"], item["ok"], item["changed"], item["unreachable"], item["failures"], item["time"], item["only_tags"], item["skip_tags"], item["user"]])
def _print_results(self): question = "Q> Ansbile failures | host(s) '{}' returned '{}' limit '{}' sort '{}' order '{}' filter '{}'"\ .format(TColors.yellow(self.answers["host"]), TColors.yellow(len(self.results)), TColors.yellow(self.arguments.get("--rlimit")), TColors.yellow(self.answers["sort"]), TColors.yellow(self.answers["order"]), TColors.yellow(self.answers["filter"])) headers = ["Host", "Last Run", "Play Name", "User", "+Tag", "-Tag", "vars", "Module", "IERR", "Args", "Msg"] print_table(self.results, headers=headers, question=question)
def _print_results(self): question = "Q> Ansible inventory status| host(s) '{}' returned '{}' sort '{}' order '{}' filter '{}'"\ .format(TColors.yellow(self.inventory_files), TColors.yellow(len(self.results)), TColors.yellow(self.answers["sort"]), TColors.yellow(self.answers["order"]), TColors.yellow(self.answers["filter"])) headers = ["Host", "Last Run", "Play Name", "Ok", "CH", "UR", "FL", "Time(s)", "+Tag", "-Tag", "User"] footer = "" for host_run in self.inventory_hosts: if len(host_run) > 0: # make sure list is not "" footer += TColors.blue("r " + host_run) + "\n" print_table(self.results, headers=headers, question=question, footer=footer)
def _format_summary_item(self, item): # TODO: smart print of time # Tags color if "all" in item["only_tags"]: item["only_tags"] = ",".join(item["only_tags"]) else: item["only_tags"] = TColors.yellow(",".join(item["only_tags"])) item["skip_tags"] = ",".join(item["skip_tags"]) # Color recap if item["ok"] > 0: item["ok"] = TColors.green(item["ok"]) if item["failures"] > 0: item["failures"] = TColors.red(item["failures"]) if item["changed"] > 0: item["changed"] = TColors.yellow(item["changed"]) if item["unreachable"] > 0: item["unreachable"] = TColors.red(item["unreachable"]) if item["failures"] > 0 or item["unreachable"] > 0: item["host"] = TColors.red(item["host"]) elif item["changed"] > 0: item["host"] = TColors.yellow(item["host"]) else: item["host"] = TColors.green(item["host"]) # Color last run last_run = format_last_run(item["@timestamp"], color=False) self.results.append([item["host"], last_run, item["play_name"], item["ok"], item["changed"], item["unreachable"], item["failures"], item["time"], item["only_tags"], item["skip_tags"], item["user"]])
def _delete_index(self): try: user_input = raw_input("Are you sure you want to delete the above indices. " + TColors.red("NO UNDO") + " (yes/no): ") if user_input != "yes": print "you said no bye!!" exit(1) print "Starting deleting" self.client_indices.delete(self.to_be_deleted) print "Finished deleting" except Exception, e: print e