summaryrefslogtreecommitdiffstats
path: root/src/lib/Server/Admin/Client.py
diff options
context:
space:
mode:
authorAndrew Brestick <brestick@mcs.anl.gov>2008-06-19 15:11:06 +0000
committerAndrew Brestick <brestick@mcs.anl.gov>2008-06-19 15:11:06 +0000
commitc4de45d84a95ec7e3b61780eae6a591885fc9bbf (patch)
tree34fdab3104bd25c1d53955a47b24ad00f1fb87ff /src/lib/Server/Admin/Client.py
parentaf4dffe87b4c185fb0b5f43cdfdd8a05cb2b0ab6 (diff)
downloadbcfg2-c4de45d84a95ec7e3b61780eae6a591885fc9bbf.tar.gz
bcfg2-c4de45d84a95ec7e3b61780eae6a591885fc9bbf.tar.bz2
bcfg2-c4de45d84a95ec7e3b61780eae6a591885fc9bbf.zip
Resolves ticket #541 and fixes client adding/deleting
git-svn-id: https://svn.mcs.anl.gov/repos/bcfg/trunk/bcfg2@4701 ce84e21b-d406-0410-9b95-82705330c041
Diffstat (limited to 'src/lib/Server/Admin/Client.py')
-rw-r--r--src/lib/Server/Admin/Client.py55
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