def cleanup_counter(): logging.debug('cleaning up existing counters...') remote.execute('iptables -F COUNTER') rules = list_rules() while ('0 references' not in rules) and ('No chain' not in rules): # left behind by previous runs rules = list_rules() remote.execute('iptables -D FORWARD -j COUNTER')
def main(config_filename=None): description = 'Version %s. \nScript to read cluster hostnames from /etc/hosts and pass it to the remote program' % version parser = argparse.ArgumentParser(description=description) parser.add_argument('-u', '--username', help='SSH username for the host', required=True) parser.add_argument('-p', '--password', help='Password for the SSH user', required=True) parser.add_argument('-f', '--conf-file', dest='conf_file', help='Config file to pass to remote program', required=False) parser.add_argument('-i', '--include-prefix', dest='include_prefix', help='Hostname prefixes to be included', required=False) parser.add_argument( '-e', '--exclude-prefix', dest='exclude_prefix', help= 'Hostname prefixes to be excluded. If include_prefix is provided, that takes preference', required=False) args = parser.parse_args() username = args.username password = args.password conf_file = args.conf_file #password = getpass.getpass('Ambari password for username [{0}]: '.format(username)) #if not password: # password = default_ambari_password usernames = [] passwords = [] hosts = get_cluster_hosts(etc_host_file, args.include_prefix, args.exclude_prefix) for host in hosts: usernames.append(username) passwords.append(password) if conf_file: config = remote.load_config(conf_file) if 'variables' not in config: config['variables'] = {} config['variables']['credentials.hostname'] = hosts config['variables']['credentials.username'] = usernames config['variables']['credentials.password'] = passwords remote.execute(config) else: pretty_print(hosts)
def install(machine_name, build, domain=None): current = installed_version(machine_name) install_output = [] if current != build: remove(machine_name) install_output = remote.execute(machine_name, "/mnt/vas/dev-builds/QAS-%s/install.sh -qa vasclnt" % build) join_output = [] if domain: join_output = join(machine_name, domain) return [install_output, join_output]
def addresses(): """Return list of IP,MAC pairs""" # normally 'arp -n' output = remote.execute('arp show') try: result = output.splitlines(False) lines = [ x.split() for x in result if len(x) > 0 and is_ip(x.split()[0]) ] pairs = [(x[0], x[3].upper()) for x in lines] return pairs except AttributeError: return []
def read_counters(tries=3): """Returns dictionary of IP address keys and data usage values""" response = None while not response and tries > 0: response = remote.execute( 'iptables -L COUNTER -vx; iptables -Z COUNTER') lines = [x for x in response.splitlines(False) if 'anywhere' in x] counters = {} for line in lines: address = line.split()[-1] counter = line.split()[1] # bytes counters[address] = int(counter) return counters
def unjoin(machine_name): return remote.execute(machine_name, "/opt/quest/bin/vastool -u Administrator -w test123 unjoin")
def join(machine_name, domain): return remote.execute(machine_name, "/opt/quest/bin/vastool -u Administrator -w test123 join -f %s" % domain)
def installed_version(machine_name): build = remote.execute(machine_name, "/opt/quest/bin/vastool -v") if build and not build[1]: # If there is no stderr output return build[0].split('\n')[0].split()[-1].strip()
def remove(machine_name, remove=None): if not remove: remove = installed_version(machine_name) if remove: return remote.execute(machine_name, "/mnt/vas/dev-builds/QAS-%s/install.sh remove 2> /dev/null" % remove)
def list_rules(): return remote.execute('iptables -L COUNTER')
def add_counter(address): remote.execute('iptables -A COUNTER --dst ' + address)
def init_counter(): logging.debug('initializing counter...') remote.execute('iptables -N COUNTER; iptables -I FORWARD -j COUNTER')
def execute(username, password, clustername, base_url, selected_service, selected_component, conf_file, ssh_user, ssh_pass, run_id): if not clustername: clustername = get_default_cluster_name(base_url) cluster_url = '{0}/clusters/{1}'.format(base_url, clustername) service_group_type_dict = get_all_current_properties(cluster_url) services = list(service_group_type_dict) if not selected_service: selected_service = get_selection(services, 'a service') service_log_dirs = get_service_log_dir(cluster_url, selected_service) hostnames_components = get_service_components_hostname( cluster_url, selected_service, selected_component) print '\nBelow are the hosts/nodes for {0}:'.format(selected_service) for index, host in enumerate(sorted(hostnames_components)): print '{0}. {1}'.format(index + 1, host) for component in hostnames_components[host]: print ' - {0}'.format(component) print '' if not conf_file: selection = raw_input( 'Do you want to get the config variables to run the script manually? [n]: ' ) if not (selection.upper() == 'Y' or selection.upper() == 'YES'): sys.exit(0) variables = {} variables['group1.service'] = [] variables['group1.log_dir'] = [] for log_dir in service_log_dirs[selected_service]: variables['group1.service'].append(selected_service) variables['group1.log_dir'].append(log_dir) variables['credential.hostname'] = [] variables['credential.username'] = [] variables['credential.password'] = [] prompt_cleared = False for host in sorted(hostnames_components): variables['credential.hostname'].append(host) if ssh_user: variables['credential.username'].append(ssh_user) else: username = raw_input( "\nEnter SSH username for '{0}' [{1}]: ".format( host, default_ssh_user)) if not username: username = default_ssh_user variables['credential.username'].append(username) if ssh_pass: variables['credential.password'].append(ssh_pass) else: if ssh_user: username = ssh_user password = getpass.getpass( 'Password for {0} [Exit]: '.format(username)) if not password: print 'Exiting...' sys.exit(0) variables['credential.password'].append(password) if not prompt_cleared and (not ssh_user or not ssh_pass ) and len(hostnames_components) > 1: prompt_cleared = True selection = raw_input( 'Use the same credentials for all the host{s}? [y]: ') if not selection or selection.upper() == 'Y' or selection.upper( ) == 'YES': ssh_user = username ssh_pass = password if run_id: variables['run_id'] = run_id if conf_file: config = remote.load_config(conf_file) config['variables'] = variables remote.execute(config) else: output = {'variables': variables} print_json(output)