diff options
Diffstat (limited to 'src/lib/Server/Admin/Client.py')
-rw-r--r-- | src/lib/Server/Admin/Client.py | 55 |
1 files changed, 40 insertions, 15 deletions
diff --git a/src/lib/Server/Admin/Client.py b/src/lib/Server/Admin/Client.py index 9d75a6ab3..c4f931350 100644 --- a/src/lib/Server/Admin/Client.py +++ b/src/lib/Server/Admin/Client.py @@ -1,50 +1,75 @@ import lxml.etree - +import fcntl import Bcfg2.Server.Admin class Client(Bcfg2.Server.Admin.Mode): __shorthelp__ = 'bcfg2-admin client add <client> attr1=val1 attr2=val2\nbcfg2-admin client del <client>' __longhelp__ = __shorthelp__ + '\n\tCreate or delete client entries' - def __init__(self): - Bcfg2.Server.Admin.Mode.__init__(self) + def __init__(self, configfile): + Bcfg2.Server.Admin.Mode.__init__(self, configfile) try: - self.bcore = Bcfg2.Server.Core.Core(self.repo, [], ['Metadata'], + self.bcore = Bcfg2.Server.Core.Core(self.get_repo_path(), [], ['Metadata'], 'foo', False) except Bcfg2.Server.Core.CoreInitError, msg: self.errExit("Core load failed because %s" % msg) - [bcore.fam.Service() for _ in range(5)] - while bcore.fam.Service(): + [self.bcore.fam.Service() for _ in range(5)] + while self.bcore.fam.Service(): pass + self.tree = lxml.etree.parse(self.get_repo_path() + "/Metadata/clients.xml") + self.root = self.tree.getroot() def __call__(self, args): Bcfg2.Server.Admin.Mode.__call__(self, args) if args[0] == 'add': attr_d = {} - for i in args[1:]: + for i in args[2:]: attr, val = i.split('=', 1) if attr not in ['profile', 'uuid', 'password', 'address', 'secure', 'location']: print "Attribute %s unknown" % attr raise SystemExit(1) attr_d[attr] = val - self.AddClient(args[1], attr_d) + self.add_client(args[1], attr_d) elif args[0] in ['delete', 'remove', 'del', 'rm']: - self.DelClient(args[1]) + self.del_client(args[1]) else: print "No command specified" raise SystemExit(1) - self.tree.write(self.get_repo_path() + '/Metadata/clients.xml') + client_tree = open(self.get_repo_path() + "/Metadata/clients.xml","w") + fd = client_tree.fileno() + while True: + try: + fcntl.lockf(fd, fcntl.LOCK_EX | fcntl.LOCK_NB) + except IOError: + continue + else: + break + self.tree.write(client_tree) + fcntl.lockf(fd, fcntl.LOCK_UN) + client_tree.close() - def AddClient(self, client, attrs): + def add_client(self, client, attrs): '''add a new client''' - # FIXME add a dup client check element = lxml.etree.Element("Client", name=client) for key, val in attrs.iteritems(): element.set(key, val) + node = self.search_client(client) + if node != None: + print "Client \"%s\" already exists" % (client) + raise SystemExit(1) self.root.append(element) - def DelClient(self, client): + def del_client(self, client): '''delete an existing client''' - # FIXME DelClient not implemented - pass + node = self.search_client(client) + if node == None: + print "Client \"%s\" not found" % (client) + raise SystemExit(1) + self.root.remove(node) + def search_client(self, client): + '''find a client''' + for node in self.root: + if node.attrib["name"] == client: + return node + return None |