243 lines
8.9 KiB
Python
243 lines
8.9 KiB
Python
import SocketServer
|
|
import threading
|
|
import cmd
|
|
import sys
|
|
import time
|
|
|
|
import Entry
|
|
import Customer
|
|
from logger import Logger
|
|
|
|
|
|
class LocalMyCmdException(Exception):
|
|
def __init__(self, msg):
|
|
self.msg = msg
|
|
|
|
class MyCmd(cmd.Cmd):
|
|
@classmethod
|
|
def setClassParams(cls, entries, customers):
|
|
cls.entries = entries
|
|
cls.customers = customers
|
|
|
|
def __init__(self, i, o):
|
|
cmd.Cmd.__init__(self, completekey=None, stdin=i, stdout=o)
|
|
cmd.Cmd.use_rawinput = False
|
|
self.intro = "Hello at the yadyn cli\n"
|
|
self.prompt = "yadyn> "
|
|
|
|
def do_list(self, l):
|
|
"""list all or list <dynid>
|
|
List the details of all entries or of the entry specified by <dynid>"""
|
|
try:
|
|
parts = l.split(' ')
|
|
if len(parts) != 1:
|
|
raise LocalMyCmdException("illegal number of arguments")
|
|
x = parts[0]
|
|
if x == 'all':
|
|
for entry in MyCmd.entries.values():
|
|
self.stdout.write(str(entry) + "\n")
|
|
elif MyCmd.entries.has_key(x):
|
|
self.stdout.write(str(MyCmd.entries[x]) + "\n")
|
|
else:
|
|
raise LocalMyCmdException("unknown dynid")
|
|
except LocalMyCmdException, e:
|
|
self.stdout.write("Failure: %s\n" % str(e.msg))
|
|
|
|
def do_reset(self, l):
|
|
"""reset all or reset <dynid>
|
|
Reset the address details of all or of the entry specified by <dynid>."""
|
|
try:
|
|
parts = l.split(' ')
|
|
if len(parts) != 1:
|
|
raise LocalMyCmdException("illegal number of arguments")
|
|
x = parts[0]
|
|
if x == 'all':
|
|
for entry in MyCmd.entries.values():
|
|
entry.address = '0.0.0.0'
|
|
entry.addressInDns = '0.0.0.0'
|
|
entry.lastEventTime = int(time.time())
|
|
self.stdout.write(str(entry) + "\n")
|
|
else:
|
|
if MyCmd.entries.has_key(x):
|
|
MyCmd.entries[x].address = '0.0.0.0'
|
|
MyCmd.entries[x].addressInDns = '0.0.0.0'
|
|
MyCmd.entries[x].lastEventTime = int(time.time())
|
|
self.stdout.write(str(MyCmd.entries[x]) + "\n")
|
|
else:
|
|
raise LocalMyCmdException("unknown dynid")
|
|
except LocalMyCmdException, e:
|
|
self.stdout.write("Failure: %s\n" % str(e.msg))
|
|
|
|
def do_add(self, l):
|
|
"""add <dynid> <name> <zone> <shared secret>
|
|
dynid is the key of the entry, name is the left-most part of the hostname to be set, zone is the domain part of the hostname to be set, shared secret is the password."""
|
|
try:
|
|
parts = l.split(' ')
|
|
if len(parts) != 4:
|
|
raise LocalMyCmdException("illegal number of arguments")
|
|
(dynid, name, zone, sharedSecret) = parts
|
|
if MyCmd.entries.has_key(dynid):
|
|
raise LocalMyCmdException("duplicate dynid")
|
|
for entry in MyCmd.entries.values():
|
|
if entry.name == name and entry.zone == zone:
|
|
raise LocalMyCmdException("duplicate full name")
|
|
newEntry = Entry.Entry(dynid, sharedSecret, name, zone)
|
|
MyCmd.entries[dynid] = newEntry
|
|
self.stdout.write("Done\n")
|
|
except LocalMyCmdException, e:
|
|
self.stdout.write("Failure: %s\n" % str(e.msg))
|
|
|
|
def do_edit(self, l):
|
|
"""edit <dynid> <key> <value>
|
|
Change the name or the shared secret of the entry."""
|
|
try:
|
|
parts = l.split(' ')
|
|
if len(parts) != 3:
|
|
raise LocalMyCmdException("illegal number of arguments")
|
|
(dynid, attr, value) = parts
|
|
if not MyCmd.entries.has_key(dynid):
|
|
raise LocalMyCmdException("unknown dynid")
|
|
if attr == 'sharedSecret':
|
|
MyCmd.entries[dynid].sharedSecret = value
|
|
elif attr == 'name':
|
|
MyCmd.entries[dynid].name = value
|
|
else:
|
|
raise LocalMyCmdException("unknown attribute to change")
|
|
self.stdout.write("Done\n")
|
|
except LocalMyCmdException, e:
|
|
self.stdout.write("Failure: %s\n" % str(e.msg))
|
|
|
|
def do_delete(self, l):
|
|
"""delete <dynid>
|
|
delete the entry specified by dynid"""
|
|
try:
|
|
parts = l.split(' ')
|
|
if len(parts) != 1:
|
|
raise LocalMyCmdException("illegal number of arguments")
|
|
x = parts[0]
|
|
if not MyCmd.entries.has_key(x):
|
|
raise LocalMyCmdException("unknown dynid")
|
|
del MyCmd.entries[x]
|
|
self.stdout.write("Done\n")
|
|
except LocalMyCmdException, e:
|
|
self.stdout.write("Failure: %s\n" % str(e.msg))
|
|
|
|
def do_debug(self, l):
|
|
try:
|
|
parts = l.split(' ')
|
|
if len(parts) != 1:
|
|
raise LocalMyCmdException("illegal number of arguments")
|
|
if parts[0] == 'on':
|
|
Logger.debugEnable()
|
|
elif parts[0] == 'off':
|
|
Logger.debugDisable()
|
|
else:
|
|
raise LocalMyCmdException("illegal argument")
|
|
except LocalMyCmdException, e:
|
|
self.stdout.write("Failure: %s\n" % str(e.msg))
|
|
|
|
|
|
def do_quit(self, l):
|
|
"""quit
|
|
Quit the shell"""
|
|
self.stdout.write("Bye\n")
|
|
return True
|
|
|
|
def do_addcust(self, l):
|
|
try:
|
|
parts = l.split(' ')
|
|
if len(parts) != 5:
|
|
raise LocalMyCmdException("illegal number of arguments")
|
|
(zone, adminPwd, contact, email, maxEntries) = parts
|
|
if MyCmd.customers.has_key(zone):
|
|
raise LocalMyCmdException("duplicate zone")
|
|
try:
|
|
maxEntries = int(maxEntries)
|
|
except ValueError:
|
|
raise LocalMyCmdException("maxEntries is no number")
|
|
newCustomer = Customer.Customer(zone, adminPwd, contact, email, maxEntries)
|
|
MyCmd.customers[zone] = newCustomer
|
|
self.stdout.write("Done\n")
|
|
except LocalMyCmdException, e:
|
|
self.stdout.write("Failure: %s\n" % str(e.msg))
|
|
|
|
def do_editcust(self, l):
|
|
try:
|
|
parts = l.split(' ')
|
|
if len(parts) != 3:
|
|
raise LocalMyCmdException("illegal number of arguments")
|
|
(zone, attr, value) = parts
|
|
if not MyCmd.customers.has_key(zone):
|
|
raise LocalMyCmdException("unknown zone")
|
|
if attr == 'adminPwd':
|
|
MyCmd.customers[zone].adminPwd = value
|
|
elif attr == 'contact':
|
|
MyCmd.customers[zone].contact = value
|
|
elif attr == 'email':
|
|
MyCmd.customers[zone].email = value
|
|
elif attr == 'maxEntries':
|
|
try:
|
|
MyCmd.customers[zone].maxEntries = int(value)
|
|
except ValueError:
|
|
raise LocalMyCmdException("value is no number")
|
|
else:
|
|
raise LocalMyCmdException("unknown attribute to change")
|
|
self.stdout.write("Done\n")
|
|
except LocalMyCmdException, e:
|
|
self.stdout.write("Failure: %s\n" % str(e.msg))
|
|
|
|
def do_deletecust(self, l):
|
|
try:
|
|
parts = l.split(' ')
|
|
if len(parts) != 1:
|
|
raise LocalMyCmdException("illegal number of arguments")
|
|
zone = parts[0]
|
|
if not MyCmd.customers.has_key(zone):
|
|
raise LocalMyCmdException("unknown zone")
|
|
del MyCmd.customers[zone]
|
|
self.stdout.write("Done\n")
|
|
except LocalMyCmdException, e:
|
|
self.stdout.write("Failure: %s\n" % str(e.msg))
|
|
|
|
|
|
def do_listcust(self, l):
|
|
try:
|
|
parts = l.split(' ')
|
|
if len(parts) != 1:
|
|
raise LocalMyCmdException("illegal number of arguments")
|
|
x = parts[0]
|
|
if x == 'all':
|
|
for customer in MyCmd.customers.values():
|
|
self.stdout.write(str(customer) + "\n")
|
|
elif MyCmd.customers.has_key(x):
|
|
self.stdout.write(str(MyCmd.customers[x]) + "\n")
|
|
else:
|
|
raise LocalMyCmdException("unknown zone")
|
|
except LocalMyCmdException, e:
|
|
self.stdout.write("Failure: %s\n" % str(e.msg))
|
|
|
|
|
|
|
|
class CmdHandler(SocketServer.StreamRequestHandler):
|
|
def handle(self):
|
|
myCmd = MyCmd(self.rfile, self.wfile)
|
|
myCmd.cmdloop()
|
|
|
|
|
|
class CmdServer(SocketServer.ThreadingTCPServer):
|
|
def __init__(self, server_address, RequestHandlerClass):
|
|
self.allow_reuse_address = True
|
|
SocketServer.ThreadingTCPServer.__init__(self, server_address, RequestHandlerClass)
|
|
|
|
|
|
class CmdReceiver(threading.Thread):
|
|
def __init__(self, cmdAddr, entries, adminPwd):
|
|
threading.Thread.__init__(self)
|
|
self.cmdAddr = cmdAddr
|
|
MyCmd.setClassParams(entries, adminPwd)
|
|
self.setDaemon(True)
|
|
|
|
def run(self):
|
|
CmdServer(self.cmdAddr, CmdHandler).serve_forever()
|
|
|