From 3f8d673d60f9d996ddaf330042d7e471cc07eff1 Mon Sep 17 00:00:00 2001 From: Kloadut Date: Fri, 26 Oct 2012 15:26:50 +0200 Subject: [PATCH] Almost done postinstall --- action_map.yml | 19 ++++++++++++--- yunohost.py | 34 ++++++++++++++++++++++++++ yunohost_tools.py | 62 +++++++++++++++++++++++++++++++++++++++++++++-- yunohost_user.py | 30 ++++++----------------- 4 files changed, 118 insertions(+), 27 deletions(-) diff --git a/action_map.yml b/action_map.yml index e75ced4e..cb921a27 100644 --- a/action_map.yml +++ b/action_map.yml @@ -437,8 +437,21 @@ tools: category_help: Specific tools actions: - ### tools_init() - init: - action_help: Install YunoHost LDAP initialization + ### tools_ldapinit() + ldapinit: + action_help: YunoHost LDAP initialization connections: - ldap + + ### tools_postinstall() + postinstall: + action_help: YunoHost post-install + connections: + - ldap + arguments: + -d: + full: --domain + help: YunoHost main domain + -p: + full: --password + help: YunoHost admin password diff --git a/yunohost.py b/yunohost.py index d8622021..73e2d0bb 100644 --- a/yunohost.py +++ b/yunohost.py @@ -104,6 +104,40 @@ def validate(regex_dict): raise YunoHostError(22, _('Invalid attribute') + ' ' + attr) return True +def get_required_args(args, required_args, password=False): + """ + Input missing values or raise Exception + + Keyword arguments: + args -- Available arguments + required_args -- Dictionary of required arguments and input phrase + password -- True|False Hidden password double-input needed + + Returns: + args + + """ + try: + for arg, phrase in required_args.items(): + if not args[arg] and arg != 'password': + if os.isatty(1): + args[arg] = raw_input(colorize(phrase + ': ', 'cyan')) + else: + raise Exception #FIX + # Password + if not args['password'] and password and required_args['password']: + if os.isatty(1): + args['password'] = getpass.getpass(colorize(required_args['password'] + ': ', 'cyan')) + pwd2 = getpass.getpass(colorize('Retype ' + required_args['password'][0].lower() + required_args['password'][1:] + ': ', 'cyan')) + if args['password'] != pwd2: + raise YunoHostError(22, _("Passwords doesn't match")) + else: + raise YunoHostError(22, _("Missing arguments")) + except KeyboardInterrupt, EOFError: + raise YunoHostError(125, _("Interrupted, YunoHost not configured")) + + return args + def display_error(error): """ diff --git a/yunohost_tools.py b/yunohost_tools.py index cddc5919..4c4ae8d5 100644 --- a/yunohost_tools.py +++ b/yunohost_tools.py @@ -1,9 +1,15 @@ # -*- coding: utf-8 -*- +import os +import sys import yaml +import re +from yunohost import validate, colorize, get_required_args -def tools_init(args, connections): +def tools_ldapinit(args, connections): yldap = connections['ldap'] + + # TODO: Check if LDAP is already initialized with open('ldap_scheme.yml') as f: ldap_map = yaml.load(f) @@ -27,4 +33,56 @@ def tools_init(args, connections): yldap.update('cn=admin', admin_dict) - return { 'Success' : _("LDAP successfully initialized") } + return { 'Success' : _("LDAP has been successfully initialized") } + +def tools_adminpw(args, connections): #FIX + # Validate password length + if len(args['new']) < 4: + raise YunoHostError(22, _("Password is too short")) + print args + +def tools_maindomain(args): #FIX + validate({ args['new'] : r'^([a-zA-Z0-9]{1}([a-zA-Z0-9\-]*[a-zA-Z0-9])*)(\.[a-zA-Z0-9]{1}([a-zA-Z0-9\-]*[a-zA-Z0-9])*)*(\.[a-zA-Z]{1}([a-zA-Z0-9\-]*[a-zA-Z0-9])*)$' }) + + config_files = [ + '/etc/postfix/main.cf', + '/etc/mailname', + '/etc/ejabberd/ejabberd.cfg', + '/etc/lemonldap-ng/lemonldap-ng.ini', + '/etc/hosts', + ] + + config_dir = [ + '/var/lib/lemonldap-ng/conf', + '/etc/apache2/sites-available', + ] + + for dir in config_dir: + for file in os.listdir(dir): + config_files.append(dir + '/' + file) + + for file in config_files: + with open(file, "r") as sources: + lines = sources.readlines() + with open(file, "w") as sources: + for line in lines: + sources.write(re.sub(r'yunohost.org', args['domain'], line)) + + os.system('/etc/init.d/hostname.sh') + # TODO: Regenerate certificate + return { 'Success' : _("YunoHost main domain has been successfully configured") } + +def tools_postinstall(args, connections): + + args = get_required_args(args, {'domain' : _('Domain name'), 'password' : _('Admin new password') }, True) + + # Initialize YunoHost LDAP base + #tools_ldapinit(None, connections) + + # Change LDAP admin password + #tools_adminpw({ 'old' : 'yunohost', 'new' : args['password']}) + + # New domain config + #tools_maindomain({ 'old' : 'yunohost.org', 'new' : args['domain']}) + + return { 'Success' : _("YunoHost has been successfully configured") } diff --git a/yunohost_user.py b/yunohost_user.py index b1682152..c5b2be68 100644 --- a/yunohost_user.py +++ b/yunohost_user.py @@ -7,7 +7,7 @@ import crypt import random import string import getpass -from yunohost import YunoHostError, win_msg, colorize, validate +from yunohost import YunoHostError, win_msg, colorize, validate, get_required_args def user_list(args, connections): # TODO : fix print(args) @@ -24,27 +24,13 @@ def user_create(args, connections): Boolean """ yldap = connections['ldap'] - required_args = ['username', 'mail', 'firstname', 'lastname'] - - # Input missing values - try: - for arg in required_args: - if not args[arg]: - if os.isatty(1): - args[arg] = raw_input(colorize(arg.capitalize()+': ', 'cyan')) - else: - raise Exception - # Password - if not args['password']: - if os.isatty(1): - args['password'] = getpass.getpass(colorize('Password: ', 'cyan')) - pwd2 = getpass.getpass(colorize('Retype password:', 'cyan')) - if args['password'] != pwd2: - raise YunoHostError(22, _("Passwords doesn't match")) - else: - raise YunoHostError(22, _("Missing arguments")) - except KeyboardInterrupt, EOFError: - raise YunoHostError(125, _("Interrupted, user not created")) + args = get_required_args(args, { + 'username': _('Username'), + 'mail': _('Mail address'), + 'firstname': _('Firstname'), + 'lastname': _('Lastname'), + 'password': _('Password') + }, True) # Validate password length if len(args['password']) < 4: