def execute_help(self, line, indent=0): self._init() method = self._find_method(line) if method: try: try: method_name = method.__name__ except Exception: method_name = None if method_name == DEFAULT: # Print controller help CommandHelp.display(self, indent=indent) if self.modifiers: CommandHelp.print_text( "%sModifiers%s: %s" % (terminal.underline(), terminal.reset(), ", ".join( sorted(self.modifiers))), indent=indent) if CommandHelp.has_help(method): CommandHelp.display(method, indent=indent) indent += 2 for command in sorted(self.commands.keys()): CommandHelp.print_text( "- %s%s%s:" % (terminal.bold(), command, terminal.reset()), indent=indent - 1) self.execute_help([command], indent=indent) return elif isinstance(method, ShellException): # Method not implemented pass elif method_name is None: # Nothing to print yet method.execute_help(line, indent=indent) else: # Print help for a command CommandHelp.display(method, indent=indent) return except IOError as e: raise ShellException(str(e)) else: raise ShellException("Method was not set? %s" % (line))
def execute_help(self, line, indent=0): self._init() method = self._find_method(line) if method: try: try: method_name = method.__name__ except Exception: method_name = None if method_name == DEFAULT: # Print controller help CommandHelp.display(self, indent=indent) if self.modifiers: CommandHelp.print_text( "%sModifiers%s: %s" % (terminal.underline(), terminal.reset(), ", ".join( sorted(self.modifiers))), indent=indent) if CommandHelp.has_help(method): CommandHelp.display(method, indent=indent) indent += 2 for command in sorted(self.commands.keys()): CommandHelp.print_text( "- %s%s%s:" % (terminal.bold(), command, terminal.reset()), indent=indent - 1) self.execute_help([command], indent=indent) return elif isinstance(method, ShellException): # Method not implemented pass elif method_name is None: # Nothing to print yet method.execute_help(line, indent=indent) else: # Print help for a command CommandHelp.display(method, indent=indent) return except IOError as e: raise ShellException(str(e)) else: raise ShellException( "Method was not set? %s" % (line))
def gen_description(self, line_width, desc_width): if self._description == '': return [] tdesc = self._description[:].split(' ') lines = [] words = [] while tdesc != []: words.append(tdesc.pop(0)) line = ' '.join(words) if len(line) >= desc_width: if len(words) > 1: tdesc.insert(0, words.pop()) line = ' '.join(words) words = [] lines.append(line) else: if words: line = ' '.join(words) lines.append(line) description = [ "%s%s%s" % (terminal.dim(), l.center(line_width), terminal.reset()) for l in lines ] description = "\n".join(description) return [ description, ]
def gen_description(self, line_width, desc_width): if self._description == '': return [] tdesc = self._description[:].split(' ') lines = [] words = [] while tdesc != []: words.append(tdesc.pop(0)) line = ' '.join(words) if len(line) >= desc_width: if len(words) > 1: tdesc.insert(0, words.pop()) line = ' '.join(words) words = [] lines.append(line) else: if words: line = ' '.join(words) lines.append(line) description = ["%s%s%s" % ( terminal.dim(), l.center(line_width), terminal.reset()) for l in lines] description = "\n".join(description) return [description, ]
def __call__(self, func): try: if func.__name__ == DEFAULT: self.message[0] = "%sDefault%s: %s" % ( terminal.underline(), terminal.reset(), self.message[0]) except Exception: pass func._command_help = self.message return func
def __call__(self, func): try: if func.__name__ == DEFAULT: self.message[0] = "%sDefault%s: %s" % ( terminal.underline(), terminal.reset(), self.message[0]) except Exception: pass func._command_help = self.message return func
def precmd(self, line, max_commands_to_print_header=1, command_index_to_print_from=1): lines = None try: lines = self.clean_line(line) if not lines: # allow empty lines return "" except Exception as e: logger.error(e) return "" for line in lines: if line[0] in self.commands: return " ".join(line) if len(lines) > max_commands_to_print_header: if len(line) > 1 and any( cmd.startswith(line[0]) for cmd in MULTILEVEL_COMMANDS): index = command_index_to_print_from else: # If single level command then print from first index. For example: health, features, grep etc. index = 0 print "\n~~~ %s%s%s ~~~" % (terminal.bold(), ' '.join( line[index:]), terminal.reset()) sys.stdout.write(terminal.reset()) try: response = self.ctrl.execute(line) if response == "EXIT": return "exit" except Exception as e: logger.error(e) return "" # line was handled by execute
def _get_horizontal_header(self, title_every_nth=0): width = sum(self._render_column_widths) total_repeat_titles = 0 if title_every_nth: total_columns = len(self._render_column_display_names ) - 1 # Ignoring first columns of Row Header total_repeat_titles = (total_columns - 1) / title_every_nth width += total_repeat_titles * self._render_column_widths[ 0] # Width is same as first column width += len(self._column_padding) * \ (len(self._render_column_widths) + total_repeat_titles - 1) column_name_lines = map(lambda h: h.split(" "), self._render_column_display_names) max_deep = max(map(len, column_name_lines)) output = [terminal.bold()] output.append(self._get_title(self._title, width=width)) output.append(terminal.reset()) output = [''.join(output)] output.extend(self.gen_description(width, width - 10)) for r in range(max_deep): row = [] for i, c in enumerate(column_name_lines): if title_every_nth and ( i - 1) > 0 and (i - 1) % title_every_nth == 0: try: row.append(column_name_lines[0][r].rjust( self._render_column_widths[0])) except IndexError: row.append(".".rjust(self._render_column_widths[0])) row.append(self._column_padding) try: row.append(c[r].rjust(self._render_column_widths[i])) except IndexError: row.append(".".rjust(self._render_column_widths[i])) row.append(self._column_padding) output.append(row) output = "\n".join(map(lambda r: "".join(r), output)) return output
def do_cake(self, line): msg = """ * * * * * * ( ) (*) (*) ) | | ( * (*) |~| |~| (*) | |S| |A| | * |~| |P| |D| |~| |A| |I| |M| |U| ,|E|a@@@@|K|@@@@@@@@@@@|I|@@@@a|T|. .,a@@@|R|@@@@@|E|@@@@@@@@@@@|N|@@@@@|I|@@@@a,. ,a@@@@@@|O|@@@@@@@@@@@@.@@@@@@@@@@@@@@|L|@@@@@@@a, a@@@@@@@@@@@@@@@@@@@@@\' . `@@@@@@@@@@@@@@@@@@@@@@@@a ;`@@@@@@@@@@@@@@@@@@\' . `@@@@@@@@@@@@@@@@@@@@@\'; ;@@@`@@@@@@@@@@@@@\' . `@@@@@@@@@@@@@@@@\'@@@; ;@@@;,.aaaaaaaaaa . aaaaa,,aaaaaaa,;@@@; ;;@;;;;@@@@@@@@;@ @.@ ;@@@;;;@@@@@@;;;;@@; ;;;;;;;@@@@;@@;;@ @@ . @@ ;;@;;;;@@;@@@;;;;;;; ;;;;;;;;@@;;;;;;; @@ . @@ ;;;;;;;;;;;@@;;;;@;; ;;;;;;;;;;;;;;;;;@@ . @@;;;;;;;;;;;;;;;;@@@; ,%%%;;;;;;;;@;;;;;;;; . ;;;;;;;;;;;;;;;;@@;;%%%, .%%%%%%;;;;;;;@@;;;;;;;; ,%%%, ;;;;;;;;;;;;;;;;;;;;%%%%%%, .%%%%%%%;;;;;;;@@;;;;;;;; ,%%%%%%%, ;;;;;;;;;;;;;;;;;;;;%%%%%%%, %%%%%%%%`;;;;;;;;;;;;;;;; %%%%%%%%%%% ;;;;;;;;;;;;;;;;;;;\'%%%%%%%% %%%%%%%%%%%%`;;;;;;;;;;;;,%%%%%%%%%%%%%,;;;;;;;;;;;;;;;\'%%%%%%%%%%%% `%%%%%%%%%%%%%%%%%,,,,,,,%%%%%%%%%%%%%%%,,,,,,,%%%%%%%%%%%%%%%%%%%%\' `%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\' `%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\' """ from time import sleep s = 0.5 for line in msg.split('\n'): print line sleep(s) s = s / 1.2 print terminal.bold() + \ "Let there be CAKE!".center(80) + \ terminal.reset()
def _get_horizontal_header(self, title_every_nth=0): width = sum(self._render_column_widths) total_repeat_titles = 0 if title_every_nth: total_columns = len(self._render_column_display_names) - 1 # Ignoring first columns of Row Header total_repeat_titles = (total_columns-1)/title_every_nth width += total_repeat_titles * self._render_column_widths[0] # Width is same as first column width += len(self._column_padding) * \ (len(self._render_column_widths) + total_repeat_titles - 1) column_name_lines = map( lambda h: h.split(" "), self._render_column_display_names) max_deep = max(map(len, column_name_lines)) output = [terminal.bold()] output.append(self._get_title(self._title, width=width)) output.append(terminal.reset()) output = [''.join(output)] output.extend(self.gen_description(width, width - 10)) for r in range(max_deep): row = [] for i, c in enumerate(column_name_lines): if title_every_nth and (i-1) >0 and (i-1)%title_every_nth == 0: try: row.append(column_name_lines[0][r].rjust(self._render_column_widths[0])) except IndexError: row.append(".".rjust(self._render_column_widths[0])) row.append(self._column_padding) try: row.append(c[r].rjust(self._render_column_widths[i])) except IndexError: row.append(".".rjust(self._render_column_widths[i])) row.append(self._column_padding) output.append(row) output = "\n".join(map(lambda r: "".join(r), output)) return output
def __init__(self, admin_version, seeds, user=None, password=None, use_services_alumni=False, use_services_alt=False, log_path="", log_analyser=False, collectinfo=False, ssl_context=None, only_connect_seed=False, execute_only_mode=False, timeout=5): # indicates shell created successfully and connected to cluster/collectinfo/logfile self.connected = True self.execute_only_mode = execute_only_mode if log_analyser: self.name = 'Aerospike Log Analyzer Shell' elif collectinfo: self.name = 'Aerospike Collectinfo Shell' else: self.name = 'Aerospike Interactive Shell' if not execute_only_mode: print terminal.bold() + self.name + ', version ' +\ admin_version + terminal.reset() + "\n" cmd.Cmd.__init__(self) try: if log_analyser: if not log_path: log_path = " " self.ctrl = LogRootController(admin_version, log_path) self.prompt = "Log-analyzer> " elif collectinfo: if not log_path: logger.error( "You have not specified any collectinfo path. Usage: asadm -c -f <collectinfopath>" ) self.do_exit('') exit(1) self.ctrl = CollectinfoRootController(admin_version, clinfo_path=log_path) self.prompt = "Collectinfo-analyzer> " if not execute_only_mode: self.intro = str(self.ctrl.loghdlr) else: if user is not None: if password == "prompt" or password is None: if sys.stdin.isatty(): password = getpass.getpass("Enter Password:"******"Not able to connect any cluster with " + str(seeds) + ".") self.connected = False return else: logger.critical( "Not able to connect any cluster with " + str(seeds) + ".") self.prompt = "Admin> " self.intro = "" if not execute_only_mode: self.intro += str(self.ctrl.cluster) + "\n" cluster_visibility_error_nodes = self.ctrl.cluster.get_visibility_error_nodes( ) if cluster_visibility_error_nodes: self.intro += terminal.fg_red( ) + "Cluster Visibility error (Please check services list): %s" % ( ", ".join(cluster_visibility_error_nodes) ) + terminal.fg_clear() + "\n" cluster_down_nodes = self.ctrl.cluster.get_down_nodes() if cluster_down_nodes: self.intro += terminal.fg_red( ) + "Extra nodes in alumni list: %s" % (", ".join( cluster_down_nodes)) + terminal.fg_clear() + "\n" if self.use_rawinput: self.prompt = "\001" + terminal.bold() + terminal.fg_red() + "\002" +\ self.prompt + "\001" +\ terminal.unbold() + terminal.fg_clear() + "\002" except Exception as e: self.do_exit('') logger.critical(str(e)) if not execute_only_mode: try: readline.read_history_file(ADMINHIST) except Exception: readline.write_history_file(ADMINHIST) self.commands = set() regex = re.compile("^do_(.*)$") commands = map(lambda v: regex.match(v).groups()[0], filter(regex.search, dir(self))) for command in commands: if command != 'help': self.commands.add(command)
def _server_log_output_merger(self, file_streams, output_page_size=3, return_strings=False, end_key=END_ROW_KEY, default_value=[]): latency_end = {} result = {} merge_result = {} tm_keys = {} need_to_process = False keys_in_input = [] result_count = 0 for key in file_streams.keys(): if not return_strings: merge_result[key] = {} try: tm, res = file_streams[key].next() if not tm: continue if tm == end_key: latency_end[key] = res continue except Exception: continue need_to_process = True result[key] = {} tm_keys[key] = {} if not return_strings: if not keys_in_input: keys_in_input = res.keys() tm_keys[key] = tm result[key] = res if return_strings: colors = self._get_fg_bg_color_index_list(len(file_streams)) while need_to_process: need_to_process = False try: min_keys = [ k for k, x in tm_keys.items() if not any(y < x for y in tm_keys.values())] except Exception: break if not min_keys: break current_tm = tm_keys[min_keys[0]] for file_key in sorted(file_streams.keys()): if file_key in min_keys: if return_strings: try: merge_result[SHOW_RESULT_KEY] += "%s %s%s::" % ( self.bg_colors[colors[( file_streams.keys().index(file_key))][0]][1](), terminal.reset(), file_key) except Exception: merge_result[SHOW_RESULT_KEY] = "%s %s%s::" %( self.bg_colors[colors[( file_streams.keys().index(file_key))][0]][1](), terminal.reset(), file_key) merge_result[SHOW_RESULT_KEY] += result[file_key] else: if merge_result[file_key]: for k in keys_in_input: merge_result[file_key][k].update( result[file_key][k]) else: merge_result[file_key].update(result[file_key]) del result[file_key] del tm_keys[file_key] try: tm, res = file_streams[file_key].next() if not tm: continue if tm == end_key: latency_end[file_key] = res continue except Exception: continue need_to_process = True tm_keys[file_key] = tm result[file_key] = res else: if file_key in tm_keys and tm_keys[file_key]: need_to_process = True if return_strings: continue for k in keys_in_input: if k not in merge_result[file_key]: merge_result[file_key][k] = {} merge_result[file_key][k][ current_tm.strftime(DT_FMT)] = default_value result_count += 1 if result_count == output_page_size: yield merge_result result_count = 0 merge_result = {} if return_strings: continue for key in file_streams.keys(): merge_result[key] = {} if not latency_end: yield merge_result else: self._balance_dict(latency_end, file_streams.keys(), default_value) for file_key in latency_end: if file_key not in merge_result or not merge_result[file_key]: merge_result[file_key] = latency_end[file_key] else: for sub_key in latency_end[file_key]: if (sub_key not in merge_result[file_key] or not merge_result[file_key][sub_key]): merge_result[file_key][ sub_key] = latency_end[file_key][sub_key] else: merge_result[file_key][sub_key].update( latency_end[file_key][sub_key]) yield merge_result
def _str_vertical(self, title_every_nth=0): output = [] title_width = [] total_titles = 1 if title_every_nth: total_columns = len(self._render_column_widths) - 1 extra_header_columns = (total_columns - 1) / title_every_nth total_titles = extra_header_columns + 1 # 1 for default if total_titles > 1: slice_title_width = self._render_column_widths[0] + 1 n_columns = 0 for i, c_width in enumerate(self._render_column_widths[1:]): slice_title_width += c_width n_columns += 1 if (i + 1) % title_every_nth == 0: temp_width = slice_title_width temp_width += len(self._column_padding) * (n_columns) if i != len(self._render_column_widths) - 2: temp_width += len(self._column_padding) title_width.append(temp_width) slice_title_width = self._render_column_widths[0] + 1 n_columns = 0 if n_columns: temp_width = slice_title_width temp_width += len(self._column_padding) * (n_columns) title_width.append(temp_width) else: temp_width = sum(self._render_column_widths) + 1 # 1 for ":" temp_width += len(self._column_padding) * \ (len(self._render_column_widths) - 1) title_width.append(temp_width) output = [terminal.bold()] for t_width in title_width: output.append(self._get_title(self._title, width=t_width)) output.append(terminal.reset()) output = [''.join(output)] output.extend( self.gen_description(sum(title_width), sum(title_width) - 10)) for i, column_name in enumerate(self._render_column_names): row = [] row.append(terminal.style(terminal.bg_clear, terminal.fg_clear)) column_title = column_name if column_name == "NODE": row.append(terminal.bold()) row.append(column_title.ljust(self._render_column_widths[0])) row.append(":") row.append(self._column_padding) added_columns = 0 for j, (cell_format, cell) in enumerate( (raw_data[i] for raw_data in self._data), 1): if (title_every_nth and added_columns > 0 and added_columns % title_every_nth == 0): row.append( column_title.ljust(self._render_column_widths[0])) row.append(":") row.append(self._column_padding) cell = cell.ljust(self._render_column_widths[j]) row.append("%s%s" % (cell_format(), cell)) row.append(self._column_padding) added_columns += 1 if column_name == "NODE": row.append(terminal.reset()) output.append(''.join(row)) return '\n'.join(output) + '\n'
def _str_vertical(self, title_every_nth=0): output = [] title_width = [] total_titles = 1 if title_every_nth: total_columns = len(self._render_column_widths) - 1 extra_header_columns = (total_columns - 1) / title_every_nth total_titles = extra_header_columns + 1 # 1 for default if total_titles > 1: slice_title_width = self._render_column_widths[0] + 1 n_columns = 0 for i, c_width in enumerate(self._render_column_widths[1:]): slice_title_width += c_width n_columns += 1 if (i + 1) % title_every_nth == 0: temp_width = slice_title_width temp_width += len(self._column_padding) * (n_columns) if i != len(self._render_column_widths) - 2: temp_width += len(self._column_padding) title_width.append(temp_width) slice_title_width = self._render_column_widths[0] + 1 n_columns = 0 if n_columns: temp_width = slice_title_width temp_width += len(self._column_padding) * (n_columns) title_width.append(temp_width) else: temp_width = sum(self._render_column_widths) + 1 # 1 for ":" temp_width += len(self._column_padding) * \ (len(self._render_column_widths) - 1) title_width.append(temp_width) output = [terminal.bold()] for t_width in title_width: output.append(self._get_title(self._title, width=t_width)) output.append(terminal.reset()) output = [''.join(output)] output.extend( self.gen_description(sum(title_width), sum(title_width) - 10)) for i, column_name in enumerate(self._render_column_names): row = [] row.append(terminal.style( terminal.bg_clear, terminal.fg_clear)) column_title = column_name if column_name == "NODE": row.append(terminal.bold()) row.append(column_title.ljust(self._render_column_widths[0])) row.append(":") row.append(self._column_padding) added_columns = 0 for j, (cell_format, cell) in enumerate((raw_data[i] for raw_data in self._data), 1): if (title_every_nth and added_columns > 0 and added_columns % title_every_nth == 0): row.append( column_title.ljust(self._render_column_widths[0])) row.append(":") row.append(self._column_padding) cell = cell.ljust(self._render_column_widths[j]) row.append("%s%s" % (cell_format(), cell)) row.append(self._column_padding) added_columns += 1 if column_name == "NODE": row.append(terminal.reset()) output.append(''.join(row)) return '\n'.join(output) + '\n'