diff --git a/ucs-school-umc-installer/umc/python/schoolinstaller/__init__.py b/ucs-school-umc-installer/umc/python/schoolinstaller/__init__.py index 9709f21..97e02e2 100644 --- a/ucs-school-umc-installer/umc/python/schoolinstaller/__init__.py +++ b/ucs-school-umc-installer/umc/python/schoolinstaller/__init__.py @@ -40,7 +40,6 @@ import tempfile import glob import subprocess import traceback -import ast import urllib import filecmp @@ -54,6 +53,7 @@ import paramiko # univention #from univention.lib import escape_value from univention.lib.package_manager import PackageManager +from univention.lib.umc_connection import UMCConnection from univention.management.console.modules import Base from univention.management.console.log import MODULE from univention.management.console.config import ucr @@ -119,7 +119,7 @@ def get_ssh_connection(username, password, host): def move_slave_into_ou(master, username, password, ou, slave): '''Make sure that the slave object exists in the right OU.''' MODULE.info('Trying to move the slave entry in the right OU structure...''') - result = umc(username, password, master, ['schoolwizards/schools/move_dc', '-o', 'schooldc=%s' % slave , '-o', 'schoolou=%s' % ou, '-f', 'schoolwizards/schools']) + result = umc(username, password, master, path='schoolwizards/schools/move_dc', options={'schooldc': slave , 'schoolou': ou}, flavor='schoolwizards/schools') if not result.get('success'): MODULE.warn('Could not successfully move the slave DC into its correct OU structure:\n%s' % result.get('message')) return False @@ -200,34 +200,14 @@ def get_master_dns_lookup(): regUMCResult = re.compile(r'.*^\s*RESULT\s*:\s*(?P.*)', re.MULTILINE | re.DOTALL) -def umc(username, password, master, options = [], requestType='command'): - with tempfile.NamedTemporaryFile() as passwordFile: - # write password to temp file - passwordFile.write('%s' % password) - passwordFile.flush() - - # UMC call - cmd = ['/usr/sbin/umc-%s' % requestType, '-U', username, '-y', passwordFile.name, '-s', master] - cmd += options - MODULE.info('Executing: %s' % ' '.join(cmd)) - process = subprocess.Popen(cmd, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE) - stdout, stderr = process.communicate() - - # parse output - match = regUMCResult.match(stdout) - - # check for errors - if process.returncode != 0 or not match: - # error case... should not happen - MODULE.error('Failed to launch UMC query: %s\n%s%s' % (cmd, stderr, stdout)) - raise RuntimeError(_('Cannot connect to UMC server %s.') % master) - - # parse the result and filter for exact matches (UMC search for '*pattern*') - return ast.literal_eval(match.groupdict().get('result')) +def umc(username, password, master, requestType='command', path='', options=None, flavor=None): + connection = UMCConnection(master, username, password, error_handler=MODULE.warn) + MODULE.info('Executing on %r: %r %r flavor=%r options=%r' % (master, requestType, path, flavor, options)) + return connection.request(path or '', options, flavor, command=requestType) def get_user_dn(username, password, master): """Get the LDAP DN for the given username.""" - result = umc(username, password, master, ['-f', 'users/user', 'udm/query', '-o', 'objectProperty=username', '-o', 'objectPropertyValue=%s' % username ]) + result = umc(username, password, master, flavor='users/user', path='udm/query', options={"objectProperty": "username", "objectPropertyValue": username}) result = [ ientry for ientry in result if ientry.get('username') == username ] if not result: return None @@ -258,17 +238,14 @@ def create_ou_remote(master, username, password, ou, display_name, educational_s opts = [{'object' : {'name' : ou, 'display_name' : display_name, 'dc_name' : educational_slave}}] if administrative_slave: opts[0]['object']['dc_name_administrative'] = administrative_slave - umc(username, password, master, ['schoolwizards/schools/create', '-e', '-o', repr(opts), '-f', 'schoolwizards/schools']) + umc(username, password, master, path='schoolwizards/schools/create', options=opts, flavor='schoolwizards/schools') except RuntimeError: return False return True def get_ucr_master(username, password, master, *ucrVariables): '''Read the LDAP base from the master system via UMC.''' - options = ['ucr', '-l'] - for ivar in ucrVariables: - options += ['-o', ivar] - return umc(username, password, master, options, 'get') + return umc(username, password, master, 'get', path='ucr', options=list(ucrVariables)) def restoreOrigCertificate(certOrigFile): # try to restore the original certificate file