diff options
author | Jeroen van Meeuwen (Kolab Systems) <vanmeeuwen@kolabsys.com> | 2011-10-28 12:18:43 +0100 |
---|---|---|
committer | Jeroen van Meeuwen (Kolab Systems) <vanmeeuwen@kolabsys.com> | 2011-10-28 12:18:43 +0100 |
commit | 22cfcd2406a44a49bbd263a2600f923a28f8be4c (patch) | |
tree | d5ac8c9903857f79cab46783a48182da67c5f9bc /pykolab | |
parent | 45d4f21fe57ba47f97bf29e714bacdecdd2edca2 (diff) | |
download | pykolab-22cfcd2406a44a49bbd263a2600f923a28f8be4c.tar.gz |
Use the CLI command registration mechanism for setup components as well
Diffstat (limited to 'pykolab')
-rw-r--r-- | pykolab/setup/__init__.py | 31 | ||||
-rw-r--r-- | pykolab/setup/components.py | 182 | ||||
-rw-r--r-- | pykolab/setup/imap_setup.py | 32 | ||||
-rw-r--r-- | pykolab/setup/ldap_setup.py | 144 | ||||
-rw-r--r-- | pykolab/setup/setup_ldap.py | 79 |
5 files changed, 283 insertions, 185 deletions
diff --git a/pykolab/setup/__init__.py b/pykolab/setup/__init__.py index 2cc09da..df9bd07 100644 --- a/pykolab/setup/__init__.py +++ b/pykolab/setup/__init__.py @@ -1,5 +1,4 @@ # -*- coding: utf-8 -*- -# # Copyright 2010-2011 Kolab Systems AG (http://www.kolabsys.com) # # Jeroen van Meeuwen (Kolab Systems) <vanmeeuwen a kolabsys.com> @@ -18,14 +17,28 @@ # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # -from pykolab import constants +import sys + +import pykolab + +log = pykolab.getLogger('pykolab.setup') +conf = pykolab.getConf() + +class Setup(object): + def __init__(self): + import components + components.__init__() + + to_execute = [] + + arg_num = 0 + for arg in sys.argv[1:]: + arg_num += 1 + if not arg.startswith('-') and len(sys.argv) >= arg_num: + if components.components.has_key(sys.argv[arg_num].replace('-','_')): + to_execute.append(sys.argv[arg_num].replace('-','_')) -__all__ = [ - ] + components.execute('_'.join(to_execute)) -for component in constants.COMPONENTS: - try: - exec("from %s_setup import setup as %s_setup" % (component,component)) - __all__.append("%s_setup" % component) - except: + def run(self): pass diff --git a/pykolab/setup/components.py b/pykolab/setup/components.py new file mode 100644 index 0000000..87f4e85 --- /dev/null +++ b/pykolab/setup/components.py @@ -0,0 +1,182 @@ +# -*- coding: utf-8 -*- +# +# Copyright 2010-2011 Kolab Systems AG (http://www.kolabsys.com) +# +# Jeroen van Meeuwen (Kolab Systems) <vanmeeuwen a kolabsys.com> +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; version 3 or, at your option, any later version +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Library General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +# + +import os + +import pykolab + +from pykolab.constants import * +from pykolab.translate import _ + +log = pykolab.getLogger('pykolab.setup') +conf = pykolab.getConf() + +components = {} +component_groups = {} + +def __init__(): + # We only want the base path + components_base_path = os.path.dirname(__file__) + + for components_path, dirnames, filenames in os.walk(components_base_path): + if not components_path == components_base_path: + continue + + for filename in filenames: + if filename.startswith('setup_') and filename.endswith('.py'): + module_name = filename.replace('.py','') + component_name = module_name.replace('setup_', '') + #print "exec(\"from %s import __init__ as %s_register\"" %(module_name,component_name) + exec("from %s import __init__ as %s_register" %(module_name,component_name)) + exec("%s_register()" %(component_name)) + + for dirname in dirnames: + register_group(components_path, dirname) + + register('help', list_setup, description=_("Display this help.")) + +def list_setup(*args, **kw): + """ + List components + """ + + __components = {} + + for component in components.keys(): + if isinstance(component, tuple): + component_group, component = component + __components[component_group] = { + component: components[(component_group,component)] + } + else: + __components[component] = components[component] + + _components = __components.keys() + _components.sort() + + for _component in _components: + if __components[_component].has_key('function'): + # This is a top-level component + if not __components[_component]['description'] == None: + print "%-25s - %s" %(_component.replace('_','-'),__components[_component]['description']) + else: + print "%-25s" %(_component.replace('_','-')) + + for _component in _components: + if not __components[_component].has_key('function'): + # This is a nested component + print "\n" + _("Command Group: %s") %(_component) + "\n" + ___components = __components[_component].keys() + ___components.sort() + for __component in ___components: + if not __components[_component][__component]['description'] == None: + print "%-4s%-21s - %s" %('',__component.replace('_','-'),__components[_component][__component]['description']) + else: + print "%-4s%-21s" %('',__component.replace('_','-')) + +def execute(component_name, *args, **kw): + if not components.has_key(component_name): + log.error(_("No such component.")) + sys.exit(1) + + if not components[component_name].has_key('function') and \ + not components[component_name].has_key('group'): + log.error(_("No such component.")) + sys.exit(1) + + if components[component_name].has_key('group'): + group = components[component_name]['group'] + component_name = components[component_name]['component_name'] + try: + exec("from %s.cmd_%s import cli_options as %s_%s_cli_options" %(group,component_name,group,component_name)) + exec("%s_%s_cli_options()" %(group,component_name)) + except ImportError, e: + pass + + else: + try: + exec("from cmd_%s import cli_options as %s_cli_options" %(component_name,component_name)) + exec("%s_cli_options()" %(component_name)) + except ImportError, e: + pass + + conf.finalize_conf() + _component_name = conf.cli_args.pop(0) + components[component_name]['function'](conf.cli_args, kw) + +def register_group(dirname, module): + components_base_path = os.path.join(os.path.dirname(__file__), module) + + components[module] = {} + + for components_path, dirnames, filenames in os.walk(components_base_path): + if not components_path == components_base_path: + continue + + for filename in filenames: + if filename.startswith('cmd_') and filename.endswith('.py'): + module_name = filename.replace('.py','') + component_name = module_name.replace('cmd_', '') + #print "exec(\"from %s.%s import __init__ as %s_%s_register\"" %(module,module_name,module,component_name) + exec("from %s.%s import __init__ as %s_%s_register" %(module,module_name,module,component_name)) + exec("%s_%s_register()" %(module,component_name)) + +def register(component_name, func, group=None, description=None, aliases=[]): + if not group == None: + component = "%s_%s" %(group,component_name) + else: + component = component_name + + if isinstance(aliases, basestring): + aliases = [aliases] + + if components.has_key(component): + log.fatal(_("Command '%s' already registered") %(component)) + sys.exit(1) + + if callable(func): + if group == None: + components[component_name] = { + 'function': func, + 'description': description + } + else: + components[group][component_name] = { + 'function': func, + 'description': description + } + + components[component] = components[group][component_name] + components[component]['group'] = group + components[component]['component_name'] = component_name + + for alias in aliases: + components[alias] = { + 'function': func, + 'description': _("Alias for %s") %(component_name) + } + +## +## Commands not yet implemented +## + +def not_yet_implemented(*args, **kw): + print _("Not yet implemented") + sys.exit(1)
\ No newline at end of file diff --git a/pykolab/setup/imap_setup.py b/pykolab/setup/imap_setup.py deleted file mode 100644 index 3f21c93..0000000 --- a/pykolab/setup/imap_setup.py +++ /dev/null @@ -1,32 +0,0 @@ -# -*- coding: utf-8 -*- -# -# Copyright 2010-2011 Kolab Systems AG (http://www.kolabsys.com) -# -# Jeroen van Meeuwen (Kolab Systems) <vanmeeuwen a kolabsys.com> -# -# This program is free software; you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation; version 3 or, at your option, any later version -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Library General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software -# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. -# - -import logging -import os -import sys - -from pykolab.translate import _ -from pykolab import utils - -from pykolab.setup import package - -def setup(): - pkgmgr = package.Package('cyrus-imapd') - #pkgmgr = package.Package('kolabd') diff --git a/pykolab/setup/ldap_setup.py b/pykolab/setup/ldap_setup.py deleted file mode 100644 index 8874753..0000000 --- a/pykolab/setup/ldap_setup.py +++ /dev/null @@ -1,144 +0,0 @@ -# -*- coding: utf-8 -*- -# -# Copyright 2010-2011 Kolab Systems AG (http://www.kolabsys.com) -# -# Jeroen van Meeuwen (Kolab Systems) <vanmeeuwen a kolabsys.com> -# -# This program is free software; you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation; version 3 or, at your option, any later version -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Library General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software -# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. -# - -import getpass -import logging -import os -import sys - -try: - import ldap -except ImportError, e: - print >> sys.stderr, _("Cannot load Python LDAP libraries.") - -from pykolab.translate import _ -from pykolab import constants -from pykolab import utils - -def setup(): - """ - Setup LDAP from here. - - # Register with existing LDAP tree? - #* Verify schema loaded - #* Forget about flexibility - # Create new LDAP tree - #* OpenLDAP - """ - - (service, other_services) = utils.is_service([ - 'dirsrv', - 'ldap', - 'slapd' - ]) - - for item in other_services: - print >> sys.stderr, _("Warning: LDAP Service '%s' is available on " + \ - "this system as well.") % item - - print _("Found system service %s.") % service - - #ldap_uri = utils.ask_question(_("LDAP URI (read/write)"), "ldap://ldap.%s" %(constants.domainname)) - ldap_uri = utils.ask_question(_("LDAP URI (read/write)"), "ldap://localhost") - manager_dn = utils.ask_question("Manager DN", "cn=Directory Manager") - #manager_pw = utils.ask_question("Manager Password", password=True) - manager_pw = utils.ask_question("Manager Password", "verysecret", password=True) - - try: - con = ldap.initialize(ldap_uri) - con.bind(manager_dn, manager_pw, ldap.AUTH_SIMPLE) - except TypeError: - # This is a funny input error ("") - print >> sys.stderr, _("Could not connect to LDAP server due to " + \ - "invalid LDAP URI format or no local socket") - sys.exit(1) - except ldap.INVALID_CREDENTIALS, e: - print >> sys.stderr, _("Your username or password are incorrect") - sys.exit(1) - except ldap.LDAPError, e: - print >> sys.stderr, _("Could not connect to LDAP server due to " + \ - "invalid LDAP URI (or invalid format) or no local socket") - sys.exit(1) - except ldap.SERVER_DOWN, e: - print >> sys.stderr, e['desc'] - sys.exit(1) - - # Returns a list of dicts (empty list if not found) - kolab_config_dn_results = con.search_s('cn=kolab,cn=config', ldap.SCOPE_SUBTREE, '(cn=kolab)', ['cn']) - - if len(kolab_config_dn_results) == 1: - print >> sys.stdout, "Success: Found cn=kolab,cn=config" - - else: - initialize_kolab_config_dn(con) - - #if not service == "": - #if service in constants.SERVICE_MAP.keys(): - #exec("setup_%s()" % constants.SERVICE_MAP['%s' % service]['type']) - #else: - ## No service found on the local system, so ask a bunch of questions. - ## - ## - ldap uri - ## - manager dn - ## - manager pw - #pass - -def setup_389ds(): - """ - Executes for a local 389 Directory Server installation. - """ - - for (path, directories, files) in os.walk("/etc/dirsrv/"): - for directory in directories: - if directory.startswith('slapd-'): - print "Found a dirsrv instance %r" % directory - dirsrv_instance = directory - -# if dirsrv_instance == '': -# # Apparently we're working with a remote dirsrv... are we going to have -# # to set up the local directory service as well?? -# raise NotImplementedError, _("Initializing a 389 Directory Server has not been implemented yet. Please use setup-ds-admin") -# -# elif dirsrv_instance == 'slapd-localhost': -# # The server is on localhost -# ldap_conn = ldap.initialize(uri="ldap://localhost:389") -# try: -# ldap_conn.start_tls_s() -# except ldap.LDAPError, e: -# pass -# -# else: -# pass - -def setup_openldap(): - print "im an openldap system!" - - -def initialize_kolab_config_dn(ldap_con=None): - if ldap_con == None: - return - - ldif = """ -dn: cn=kolab,cn=config -cirUpdateSchedule: New -cn: kolab -objectClass: top -objectClass: extensibleobject -""" diff --git a/pykolab/setup/setup_ldap.py b/pykolab/setup/setup_ldap.py new file mode 100644 index 0000000..e7ccb8a --- /dev/null +++ b/pykolab/setup/setup_ldap.py @@ -0,0 +1,79 @@ +# -*- coding: utf-8 -*- +# Copyright 2010-2011 Kolab Systems AG (http://www.kolabsys.com) +# +# Jeroen van Meeuwen (Kolab Systems) <vanmeeuwen a kolabsys.com> +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; version 3 or, at your option, any later version +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Library General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +# + +import components + +import pykolab + +from pykolab import utils +from pykolab.constants import * +from pykolab.translate import _ + +log = pykolab.getLogger('pykolab.setup') +conf = pykolab.getConf() + +def __init__(): + components.register('ldap', execute, description=description()) + +def description(): + return _("Setup LDAP.") + +def execute(*args, **kw): + _input = {} + + _input['admin_pass'] = utils.ask_question(_("Administrator password"), password=True) + _input['dirmgr_pass'] = utils.ask_question(_("Directory Manager password"), password=True) + + _input['userid'] = utils.ask_question(_("User"), default="nobody") + _input['group'] = utils.ask_question(_("Group"), default="nobody") + + _input['fqdn'] = fqdn + _input['hostname'] = hostname + _input['domain'] = domainname + + _input['nodotdomain'] = domainname.replace('.','_') + + _input['rootdn'] = utils.standard_root_dn(domainname) + + print """ +[General] +FullMachineName = %(fqdn)s +SuiteSpotUserID = %(userid)s +SuiteSpotGroup = %(group)s +AdminDomain = %(domain)s +ConfigDirectoryLdapURL = ldap://%(fqdn)s:389/o=NetscapeRoot +ConfigDirectoryAdminID = admin +ConfigDirectoryAdminPwd = %(admin_pass)s + +[slapd] +SlapdConfigForMC = Yes +UseExistingMC = 0 +ServerPort = 389 +ServerIdentifier = %(hostname)s +Suffix = dc=test90,dc=kolabsys,dc=com +RootDN = cn=Directory Manager +RootDNPwd = %(dirmgr_pass)s +ds_bename = %(nodotdomain)s +AddSampleEntries = No + +[admin] +Port = 9830 +ServerAdminID = admin +ServerAdminPwd = %(admin_pass)s +""" %(_input) |