From 2900995b8dd777049829ff17df5309948082568d Mon Sep 17 00:00:00 2001 From: William Brown Date: Fri, 20 May 2016 18:37:38 +1000 Subject: [PATCH] Ticket ??? - Pre-release, consume the new python installer Bug Description: We need to be able to install a rest server to use it. This integrates the support between rest389 and lib389 to install the DS server and the new rest admin. Author: wibrown Review by: ??? --- cli/ds-rest-setup | 152 ++++++++++++++++++++++++++++++++++++++ cli/setup-ds-rest | 154 --------------------------------------- examples/ds-setup-rest-admin.inf | 48 ++++++++++++ examples/setup-ds-admin.inf | 47 ------------ rest389/LDAPOps.py | 2 +- setup.py | 4 +- 6 files changed, 203 insertions(+), 204 deletions(-) create mode 100755 cli/ds-rest-setup delete mode 100755 cli/setup-ds-rest create mode 100644 examples/ds-setup-rest-admin.inf delete mode 100644 examples/setup-ds-admin.inf diff --git a/cli/ds-rest-setup b/cli/ds-rest-setup new file mode 100755 index 0000000..598b42c --- /dev/null +++ b/cli/ds-rest-setup @@ -0,0 +1,152 @@ +#!/usr/bin/python3 + +# --- BEGIN COPYRIGHT BLOCK --- +# Copyright (C) 2015 Red Hat, Inc. +# All rights reserved. +# +# License: GPL (version 3 or any later version). +# See LICENSE for details. +# --- END COPYRIGHT BLOCK --- + +# Author: firstyear at redhat.com + +import sys +import os +import pwd +import grp +import time +from argparse import ArgumentParser + +import rest389 +import lib389 + +from lib389._constants import * +from lib389.properties import * +from lib389 import DirSrv, Entry + +from lib389.tools import SetupDs + +# There must be a way we can dynamically determine this path ... +TEMPLATE_HTTPD_CONFIG = '/usr/share/rest389/examples/rest389_httpd.conf' + +### A large chunk of this will move to lib389 in the future. +# For now we just need to be able to setup the rest part, and then +# send the remaining data to the lib389 system. + +def get_module_path(mod): + initpath = os.path.abspath(mod.__file__) + modpath = os.path.dirname(initpath) + return os.path.dirname(modpath) + +class SetupDsRest(SetupDs): + """ + This will setup a 389-ds instance along with an appropriate rest389 + administration server. + """ + + def _validate_config_2(self, config): + # Check that httpd is indeed installed and avaliable + assert(os.path.exists('/etc/httpd/conf.d/')) + # Get our path and the 389 path + assert(config.has_section('rest')) + assert(config.has_option('rest', 'user')) + assert(config.has_option('rest', 'group')) + + if not config.has_option('rest', 'rest389_path'): + config.set('rest', 'rest389_path', get_module_path(rest389)) + if not config.has_option('rest', 'lib389_path'): + config.set('rest', 'lib389_path', get_module_path(lib389)) + # Write these into the config? + # We pass in via arg, so we can re-use this as a lib if we want + # Create the config + # Set httpd to start on boot. + # How do we manage SSL? + + # Push the data to extra for later. + self.extra = { + 'user' : config.get('rest', 'user'), + 'group': config.get('rest', 'group'), + 'rest389_path': config.get('rest', 'rest389_path'), + 'lib389_path': config.get('rest', 'lib389_path'), + 'prefix': config.get('slapd', 'prefix'), + } + + def _prepare(self, extra): + pass + + def _install(self, extra): + # Read in the contents of the tmplate. This might move. + # Does the dirsrv user / group exist? + + template_data = '' + with open(TEMPLATE_HTTPD_CONFIG, 'r') as template_file: + template_data = template_file.read() + template_data = template_data.format( + USER=extra['user'], + GROUP=extra['group'], + REST389PATH=extra['rest389_path'], + LIB389PATH=extra['lib389_path'], + PREFIX=extra['prefix'], + ) + with open('/etc/httpd/conf.d/dirsrv_rest.conf', 'w') as httpd_conf: + httpd_conf.write(template_data) + # Give GROUP access to the DS instances? + +def ds_rest_setup_main(): + """ + Get and extract the information. Pass it to the setupds tool. + """ + # Check the CLI arguments. + parser = ArgumentParser() + parser.add_argument('-v', '--verbose', + help="Display extra debugging information during setup", action='store_true') + parser.add_argument('-n', '--dryrun', + help="Dryrun: Validate system and configurations only", action='store_true') + parser.add_argument('-f', '--file', + help="Inf file to use with out user interaction",required=True) + parser.add_argument('--IsolemnlyswearthatIamuptonogood', dest="ack", + help="""You are here likely here by mistake! You want setup-ds.pl! +By setting this value you acknowledge and take responsibility for the fact this command is UNTESTED and NOT READY. You are ON YOUR OWN! +""", + action='store_true') + + args = parser.parse_args() + # Create the setupDs object + if not args.ack: + sys.exit(0) + else: + print(""" + _________________________________________ +/ This is not what you want! Press ctrl-c \\ +\ now ... / + ----------------------------------------- + \\ / \\ //\\ + \\ |\\___/| / \\// \\\\ + /0 0 \\__ / // | \\ \\ + / / \\/_/ // | \\ \\ + @_^_@'/ \\/_ // | \\ \\ + //_^_/ \\/_ // | \\ \\ + ( //) | \\/// | \\ \\ + ( / /) _|_ / ) // | \\ _\\ + ( // /) '/,_ _ _/ ( ; -. | _ _\\.-~ .-~~~^-. + (( / / )) ,-{ _ `-.|.-~-. .~ `. + (( // / )) '/\\ / ~-. _ .-~ .-~^-. \\ + (( /// )) `. { } / \\ \\ + (( / )) .----~-.\\ \\-' .~ \\ `. \\^-. + ///.----..> \\ _ -~ `. ^-` ^-_ + ///-._ _ _ _ _ _ _}^ - - - - ~ ~-- ,.-~ + /.-~ + """) + for i in range(1,11): + print('%s ...' % (10 - int(i))) + time.sleep(1) + print('Launching ...') + sd = SetupDsRest(args.verbose, args.dryrun) + if sd.create_from_inf(args.file): + print("Sucessfully created instance") + else: + print("Failed to create instance") + sys.exit(1) + +if __name__ == '__main__': + ds_rest_setup_main() diff --git a/cli/setup-ds-rest b/cli/setup-ds-rest deleted file mode 100755 index 2b06dc1..0000000 --- a/cli/setup-ds-rest +++ /dev/null @@ -1,154 +0,0 @@ -#!/usr/bin/python - -# --- BEGIN COPYRIGHT BLOCK --- -# Copyright (C) 2015 Red Hat, Inc. -# All rights reserved. -# -# License: GPL (version 3 or any later version). -# See LICENSE for details. -# --- END COPYRIGHT BLOCK --- - -# Author: firstyear at redhat.com - -import sys -import os -import pwd -import grp -#This changes in py3 -import ConfigParser as configparser - -import rest389 -import lib389 - -from lib389._constants import * -from lib389.properties import * -from lib389 import DirSrv, Entry - -# There must be a way we can dynamically determine this path ... -TEMPLATE_HTTPD_CONFIG = '/usr/share/rest389/examples/rest389_httpd.conf' - -### A large chunk of this will move to lib389 in the future. -# For now we just need to be able to setup the rest part, and then -# send the remaining data to the lib389 system. - -def get_module_path(mod): - initpath = os.path.abspath(mod.__file__) - modpath = os.path.dirname(initpath) - return os.path.dirname(modpath) - -class SetupDsRest(object): - """ - This will setup a 389-ds instance along with an appropriate rest389 - administration server. - """ - - def __init__(self, config_file): - # We need a verbose flag - if not os.path.isfile(config_file): - os.stderr.write('Invalid file path\n') - sys.exit(1) - try: - self.config = configparser.SafeConfigParser() - with open(config_file, 'rb') as cf: - self.config.readfp(cf) - # Now we actually begin the setup - except Exception as e: - print(e) - sys.exit(1) - - def _main(self): - # First, validate both configs - # IF there is a DS config validate it - if self.config.has_section('slapd'): - self._validate_ds_config(self.config) - self._validate_rest_config(self.config) - # now setup the DirSrv instance via.allocate + create - # For now, disable this to test the rest setup - # If there is a DS config, create it. - if self.config.has_section('slapd'): - self._create_ds(self.config) - # Now setup the rest instance from a template. - self._create_rest() - return - - def _validate_ds_config(self, config): - # This will move to lib389 later. - # Check we have all the sections. - # Make sure we have needed keys. - assert(config.has_section('general')) - assert(config.has_option('general', 'config_version')) - assert(config.get('general', 'config_version') == '2') - # What about optional DS install? - assert(config.has_section('slapd')) - assert(config.has_option('slapd', 'instance_name')) - assert(config.has_option('slapd', 'root_password')) - assert(config.has_option('slapd', 'port')) - if not config.has_option('slapd', 'prefix'): - config.set('slapd', 'prefix', '/') - - def _validate_rest_config(self, config): - # Check that httpd is indeed installed and avaliable - assert(os.path.exists('/etc/httpd/conf.d/')) - # Get our path and the 389 path - assert(config.has_section('rest')) - assert(config.has_option('rest', 'user')) - assert(config.has_option('rest', 'group')) - - if not config.has_option('rest', 'rest389_path'): - config.set('rest', 'rest389_path', get_module_path(rest389)) - if not config.has_option('rest', 'lib389_path'): - config.set('rest', 'lib389_path', get_module_path(lib389)) - # Write these into the config? - # We pass in via arg, so we can re-use this as a lib if we want - # Create the config - # Set httpd to start on boot. - # How do we manage SSL? - pass - - def _create_ds(self, config): - # This method will likely see a lot of churn soon ... - instance = DirSrv(verbose=True) - instance.allocate({ - SER_SERVERID_PROP: config.get('slapd', 'instance_name'), - SER_HOST: config.get('general', 'full_machine_name'), - SER_PORT: config.get('slapd', 'port'), - #SER_SECURE_PORT: - #SER_ROOT_DN: - #SER_ROOT_PW: - #SER_USER_ID: - #SER_GROUP_ID: - #SER_REALM: - #SER_BACKUP_INST_DIR: - #SER_CREATION_SUFFIX: - SER_DEPLOYED_DIR: config.get('slapd', 'prefix'), - SER_STRICT_HOSTNAME_CHECKING: False - }) - if instance.exists(): - sys.stderr.write('Instance already exists') - return - # Bail!!! - instance.create() - - def _create_rest(self): - # Read in the contents of the tmplate. This might move. - # Does the dirsrv user / group exist? - - template_data = '' - with open(TEMPLATE_HTTPD_CONFIG, 'rb') as template_file: - template_data = template_file.read() - template_data = template_data.format( - USER=self.config.get('rest', 'user'), - GROUP=self.config.get('rest', 'group'), - REST389PATH=self.config.get('rest', 'rest389_path'), - LIB389PATH=self.config.get('rest', 'lib389_path'), - PREFIX=self.config.get('slapd', 'prefix'), - ) - with open('/etc/httpd/conf.d/dirsrv_rest.conf', 'wb') as httpd_conf: - httpd_conf.write(template_data) - # Give GROUP access to the DS instances? - -if __name__ == '__main__': - # Handle this better .... - sdr = SetupDsRest(sys.argv[1]) - sdr._main() - diff --git a/examples/ds-setup-rest-admin.inf b/examples/ds-setup-rest-admin.inf new file mode 100644 index 0000000..4964a03 --- /dev/null +++ b/examples/ds-setup-rest-admin.inf @@ -0,0 +1,48 @@ +; --- BEGIN COPYRIGHT BLOCK --- +; Copyright (C) 2015 Red Hat, Inc. +; All rights reserved. +; +; License: GPL (version 3 or any later version). +; See LICENSE for details. +; --- END COPYRIGHT BLOCK --- + +; Author: firstyear at redhat.com + +; This is a version 2 ds setup inf file. +; It is used by the python versions of setup-ds-* +; Most options map 1 to 1 to the original .inf file. +; However, there are some differences that I envision +; For example, note the split backend section. +; You should be able to create, one, many or no backends in an install + +[general] +config_version=2 +full_machine_name=localhost.localdomain +strict_host_checking=False + +[slapd] +instance_name=localhost +user=dirsrv +group=dirsrv +port=389 +secure_port=636 +root_dn=cn=Directory Manager +root_password=password +prefix=/opt/dirsrv +defaults=latest + +[rest] +user=dirsrv_rest +group=dirsrv_rest +port=5000 +;rest389_path +;lib389_path + +[backend-userRoot] +suffix=dc=example,dc=com +; this is controlled by slapd.InstallLdifFile == none, suggest or path in setup-ds.pl +sample_entries=yes + + + + diff --git a/examples/setup-ds-admin.inf b/examples/setup-ds-admin.inf deleted file mode 100644 index a8256a1..0000000 --- a/examples/setup-ds-admin.inf +++ /dev/null @@ -1,47 +0,0 @@ -; --- BEGIN COPYRIGHT BLOCK --- -; Copyright (C) 2015 Red Hat, Inc. -; All rights reserved. -; -; License: GPL (version 3 or any later version). -; See LICENSE for details. -; --- END COPYRIGHT BLOCK --- - -; Author: firstyear at redhat.com - -; This is a version 2 ds setup inf file. -; It is used by the python versions of setup-ds-* -; Most options map 1 to 1 to the original .inf file. -; However, there are some differences that I envision -; For example, note the split backend section. -; You should be able to create, one, many or no backends in an install - -[general] -config_version=2 -full_machine_name=localhost.localdomain -strict_host_checking=False - -[slapd] -instance_name=localhost -user=dirsrv -group=dirsrv -port=389 -secure_port=636 -root_dn=cn=Directory Manager -root_password=password -prefix=/opt/dirsrv - -[rest] -user=dirsrv_rest -group=dirsrv_rest -port=5000 -;rest389_path -;lib389_path - -[backend-userRoot] -suffix=dc=example,dc=com -; this is controlled by slapd.InstallLdifFile == none, suggest or path in setup-ds.pl -sample_entries=yes - - - - diff --git a/rest389/LDAPOps.py b/rest389/LDAPOps.py index 880c8df..6486e20 100644 --- a/rest389/LDAPOps.py +++ b/rest389/LDAPOps.py @@ -66,7 +66,7 @@ def ldap_req(ds, dn): try: entry = ds.search_s(dn, ldap.SCOPE_BASE, '(objectclass=*)') except ldap.LDAPError as e: - print str(e) + print(str(e)) if request.method == 'HEAD': # diff --git a/setup.py b/setup.py index 0133808..9ec2902 100644 --- a/setup.py +++ b/setup.py @@ -76,8 +76,8 @@ setup( 'rest389.wsgi', ]}, data_files=[ - ('/usr/share/rest389/examples/', ['examples/rest389_httpd.conf', 'examples/setup-ds-admin.inf']), - ('/usr/sbin/', ['cli/dsadm', 'cli/setup-ds-rest']), + ('/usr/share/rest389/examples/', ['examples/rest389_httpd.conf', 'examples/ds-setup-rest-admin.inf']), + ('/usr/sbin/', ['cli/dsadm', 'cli/ds-rest-setup']), ], install_requires=['python-ldap', 'python-lib389', 'flask'], ) -- 2.5.5