Almost done postinstall

This commit is contained in:
Kloadut 2012-10-26 15:26:50 +02:00
parent 3b216d6a26
commit 3f8d673d60
4 changed files with 118 additions and 27 deletions

View file

@ -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

View file

@ -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):
"""

View file

@ -1,10 +1,16 @@
# -*- 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") }

View file

@ -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: