Beispiel #1
0
def pick_host(inp, query):
    '''
    Select host from db list
    :param inp: string of hostname
    :param query: prompt for input string if no match
    :return: Host_id of matching hostname
    '''
    if str(inp).lower() not in db_fetch_hostlist(''):
        print("Defined hosts:")
        print_out(print_cols(db_fetch_hostlist('')))
        inp = input(query)
    return db_fetch_hostid('', str(inp).lower())
Beispiel #2
0
 def do_load(self, inp):
     '''Load specified hosts from database - \'all\' for all'''
     if self.config is None:
         self.config = get_sudo()
     if inp == 'all':
         hosts = db_fetch_hostlist('')
     else:
         hosts = str(inp).split(',')
     for host in hosts:
         id = pick_host(host, f'{host} not found - load which host? ')
         host = db_read_host('', id, self.config)
         self.hosts[host.name] = host
     print("Loaded hosts:")
     print_out(print_cols((list(self.hosts.keys()))))
Beispiel #3
0
 def do_delete(self, inp):
     '''Delete host entry from database'''
     id = pick_host(inp, "Delete which host: ")
     if str(db_fetch_children('', id)[0]) != "0":
         print("WARNING: selected host contains defined children")
     print(f'Deleting host {inp}: Continue?')
     ans = input("Type \'yes\' to confirm deletion: ")
     if ans.strip() == "yes":
         parent = db_fetch_parent_id('', id)
         if parent:
             db_delete_child('', parent, id)
         db_delete_host('', id)
         print("Deleted. Defined hosts:")
         print_out(print_cols(db_fetch_hostlist('')))
     else:
         print("Deletion aborted.")
Beispiel #4
0
 def do_list(self, inp):
     '''List configured hosts in database'''
     print(f'Defined hosts:')
     print_out(print_cols(db_fetch_hostlist('')))
Beispiel #5
0
def setup():
    config = get_sudo()
    hosts = db_fetch_hostlist('')
    return config, hosts