summaryrefslogtreecommitdiffstats
path: root/pykolab/cli/__init__.py
diff options
context:
space:
mode:
authorJeroen van Meeuwen (Kolab Systems) <vanmeeuwen@kolabsys.com>2010-10-08 13:32:18 +0200
committerJeroen van Meeuwen (Kolab Systems) <vanmeeuwen@kolabsys.com>2010-10-08 13:32:18 +0200
commitd60eebbdfe20859dd29fa3d6f0ad0daea77bc0f3 (patch)
treea5ac30918e90978f2dc3777c52db2fb6b43e9f0b /pykolab/cli/__init__.py
parentad572e9d144c603d0af4e17a2510ef0c2c1f5f24 (diff)
downloadpykolab-d60eebbdfe20859dd29fa3d6f0ad0daea77bc0f3.tar.gz
Check in the first set of actions to execute
Diffstat (limited to 'pykolab/cli/__init__.py')
-rw-r--r--pykolab/cli/__init__.py128
1 files changed, 124 insertions, 4 deletions
diff --git a/pykolab/cli/__init__.py b/pykolab/cli/__init__.py
index 1b960b2..d71dca1 100644
--- a/pykolab/cli/__init__.py
+++ b/pykolab/cli/__init__.py
@@ -1,3 +1,4 @@
+# -*- coding: utf-8 -*-
# Copyright 2010 Kolab Systems AG (http://www.kolabsys.com)
#
# Jeroen van Meeuwen (Kolab Systems) <vanmeeuwen a kolabsys.com>
@@ -16,22 +17,141 @@
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
+import ldap
+import ldif
import traceback
import shutil
import sys
+from ldap.modlist import addModlist
+
import pykolab
import pykolab.plugins
-from pykolab.translate import _
-
+from pykolab import utils
from pykolab.conf import Conf
from pykolab.constants import *
+from pykolab.translate import _
class Cli(object):
def __init__(self):
self.conf = Conf()
- print "%r" %(self.conf.args)
+
+ domain_group = self.conf.parser.add_option_group(_("Domain Options"))
+ domain_group.add_option( '--review',
+ dest = "review",
+ action = "store_true",
+ default = False,
+ help = _("Review LDIF before committed"))
+
+ self.conf.finalize_conf()
+
+ # The first argument has to be a command
+ action = self.conf.args.pop(0)
+ action_function = action.replace('-','_')
+ action_components = action.split('-')
+
+ if hasattr(self, "action_%s" %(action_function)):
+ self.conf.log.info(_("TODO: self.check_%s()") %(action_function))
+ exec("self.action_%s()" %(action_function))
+ else:
+ try:
+ action_domain = action_components.pop()
+ action_action = action_components.pop()
+ exec("from pykolab.cli import action_%s" %(action_domain))
+ if hasattr("action_%s" %(action_domain), "%s" %(action_action)):
+ exec("result = action_%s.%s(%r)" %(action_domain,action_action,self.conf.args))
+ except ImportError, e:
+ pass
+
+ self.print_usage()
+
+ def action_list_domains(self):
+ ldap_con = ldap.initialize(self.conf.get('ldap', 'uri'))
+ ldap_con.bind_s(self.conf.get('ldap', 'bind_dn'), self.conf.get('ldap', 'bind_pw'))
+
+ results = ldap_con.search_s('cn=kolab,cn=config', ldap.SCOPE_ONELEVEL, '(objectClass=*)', ['associatedDomain'])
+
+ for dn,entry in results:
+ print ''.join(entry['associatedDomain'])
+
+ def action_del_domain(self):
+ domainname = self.conf.args.pop(0)
+
+ self.conf.log.info(_("Deleting domain %s") %(domainname))
+
+ dn = "associateddomain=%s,cn=kolab,cn=config" %(domainname)
+
+ ldap_con = ldap.initialize(self.conf.get('ldap', 'uri'))
+ ldap_con.bind_s(self.conf.get('ldap', 'bind_dn'), self.conf.get('ldap', 'bind_pw'))
+
+ # The try/except should actually be in check_del_domain()
+ try:
+ # Do the actual synchronous add-operation to the ldapserver
+ ldap_con.delete_s(dn)
+ except ldap.NO_SUCH_OBJECT, e:
+ self.conf.log.error(_("No domain %s exists.") %(domainname))
+
+ # Its nice to the server to disconnect and free resources when done
+ ldap_con.unbind_s()
+
+ def action_add_domain(self):
+ self.conf.log.info(_("TODO: Figure out where the domain should actually be added."))
+
+ domainname = self.conf.args.pop(0)
+
+ self.conf.log.info(_("Adding domain %s") %(domainname))
+
+ # The dn of our new entry/object
+ self.conf.log.info(_("TODO: Make the format for a new domain configurable."))
+ dn = "associateddomain=%s,cn=kolab,cn=config" %(domainname)
+
+ # A dict to help build the "body" of the object
+ self.conf.log.info(_("TODO: Make what a domain looks like configurable."))
+ attrs = {}
+ attrs['objectclass'] = [
+ 'top',
+ 'domainrelatedobject',
+ 'organization',
+ 'inetdomain'
+ ]
+ attrs['associatedDomain'] = ['%s' %(domainname)]
+ domainname_components = domainname.split('.')
+ attrs['inetDomainBaseDN'] = ['dc=%s,dc=%s' %(domainname_components[0],domainname_components[1])]
+
+ self.conf.log.info(_("TODO: Prompt for organization name/description. For now, use domain name."))
+ attrs['o'] = ['%s' %(domainname)]
+
+ go_ahead = True
+
+ if self.conf.cli_options.review:
+ ldif_writer = ldif.LDIFWriter(sys.stdout)
+ ldif_writer.unparse(dn,attrs)
+ if not utils.ask_confirmation(_("Please ACK or NACK the above LDIF:"), default="y", all_inclusive_no=True):
+ go_ahead = False
+
+ if go_ahead:
+ # Convert our dict to nice syntax for the add-function using modlist-module
+ _ldif = addModlist(attrs)
+
+ # Now build an ldap connection and execute the motherf.
+ ldap_con = ldap.initialize(self.conf.get('ldap', 'uri'))
+ ldap_con.bind_s(self.conf.get('ldap', 'bind_dn'), self.conf.get('ldap', 'bind_pw'))
+
+ # The try/except should actually be in check_add_domain
+ try:
+ # Do the actual synchronous add-operation to the ldapserver
+ ldap_con.add_s(dn,_ldif)
+ except ldap.ALREADY_EXISTS, e:
+ self.conf.log.error(_("Domain %s already exists.") %(domainname))
+
+ # Its nice to the server to disconnect and free resources when done
+ ldap_con.unbind_s()
+#
def run(self):
- print >> sys.stderr, _("Running")
+ pass
+
+ def print_usage(self):
+ print >> sys.stderr, _("Actions") + ":"
+ print >> sys.stderr, "add-domain <domainname>" \ No newline at end of file