cli: Add basic command-line interface client

master
Dustin 2016-01-01 17:49:31 -06:00
parent 61eb0f1ee6
commit ef822f8855
2 changed files with 133 additions and 0 deletions

View File

@ -14,4 +14,9 @@ setup(
'Milla>=0.3',
'SQLAlchemy',
],
entry_points={
'console_scripts': [
'rouse=rouse.cli:main',
],
},
)

128
src/rouse/cli.py Normal file
View File

@ -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()