diff --git a/setup.py b/setup.py index d50ba48..ba37351 100644 --- a/setup.py +++ b/setup.py @@ -14,4 +14,9 @@ setup( 'Milla>=0.3', 'SQLAlchemy', ], + entry_points={ + 'console_scripts': [ + 'rouse=rouse.cli:main', + ], + }, ) diff --git a/src/rouse/cli.py b/src/rouse/cli.py new file mode 100644 index 0000000..412beae --- /dev/null +++ b/src/rouse/cli.py @@ -0,0 +1,128 @@ +from __future__ import print_function, unicode_literals +from . import client +import argparse +import sys + + +try: + input_ = raw_input +except NameError: + input_ = input + + +def print_host(host): + print('Host ID : {}'.format(host.id)) + print('MAC Address : {}'.format(host.macaddr)) + print('IP Address : {}'.format(host.ipaddr)) + print('Hostname : {}'.format(host.hostname)) + + +def add_host(args): + rouse = client.Rouse() + if args.hint: + host = client.Host.discover(args.hint) + if not host: + sys.stderr.write('No host found for {}\n'.format(args.host)) + raise SystemExit(1) + else: + host = client.Host() + if args.mac_address is not None: + host.macaddr = args.mac_address + if args.ip_address is not None: + host.ipaddr = args.ip_address + if args.hostname is not None: + host.hostname = args.hostname + host.add(rouse) + print_host(host) + + +def del_host(args): + rouse = client.Rouse() + keywords = {} + keywords['macaddr'] = args.host + keywords['ipaddr'] = args.host + keywords['hostname'] = args.host + hosts = client.Host.find(rouse=rouse, **keywords) + for host in hosts: + print_host(host) + try: + confirm = input_('Delete this host? ') + except (EOFError, KeyboardInterrupt): + raise SystemExit(1) + if confirm.lower() in ('yes', 'y'): + host.delete() + + +def find_host(args): + rouse = client.Rouse() + keywords = {} + if args.host: + keywords['macaddr'] = args.host + keywords['ipaddr'] = args.host + keywords['hostname'] = args.host + hosts = client.Host.find(rouse=rouse, **keywords) + for host in hosts: + print_host(host) + + +def set_host(args): + rouse = client.Rouse() + host = client.Host.get(rouse, args.id) + if args.mac_address is not None: + host.macaddr = args.mac_address + if args.ip_address is not None: + host.ipaddr = args.ip_address + if args.hostname is not None: + host.hostname = args.hostname + host.update() + print_host(host) + + +def parse_args(): + parser = argparse.ArgumentParser() + sp = parser.add_subparsers(metavar='command') + sp.required = True + + p_add = sp.add_parser('add', help='Add a host') + p_add.add_argument('hint', nargs='?', + help='Hostname, IP address, or MAC address of new host') + p_add.add_argument('--mac-address', '-M', + help='MAC Address') + p_add.add_argument('--ip-address', '-I', + help='IP Address') + p_add.add_argument('--hostname', '-H', + help='Hostname') + p_add.set_defaults(func=add_host) + + p_del = sp.add_parser('delete', help='Remove a host') + p_del.add_argument('host', + help='Hostname, IP address, or MAC address of host to ' + 'delete') + p_del.set_defaults(func=del_host) + + p_find = sp.add_parser('find', help='Find an existing host') + p_find.add_argument('host', nargs='?', + help='Hostname, IP address, or MAC address of a host') + p_find.set_defaults(func=find_host) + + p_set = sp.add_parser('set', help='Set host properties') + p_set.add_argument('id', type=int, + help='Host ID') + p_set.add_argument('--mac-address', '-M', + help='MAC Address') + p_set.add_argument('--ip-address', '-I', + help='IP Address') + p_set.add_argument('--hostname', '-H', + help='Hostname') + p_set.set_defaults(func=set_host) + + return parser.parse_args() + + +def main(): + args = parse_args() + args.func(args) + + +if __name__ == '__main__': + main()