move web config functions to yunohost_domain.py

This commit is contained in:
Kload 2013-02-27 22:11:10 +01:00
parent e0036030d4
commit bf2bfe626e
4 changed files with 81 additions and 78 deletions

View file

@ -172,8 +172,11 @@ domain:
domains:
help: Domain name to add
nargs: '*'
ask: "New domain"
pattern: '^([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])*)$'
web:
help: Auto-configure Apache and LemonLDAP for the domain
action: store_true
default: False
### domain_remove()
remove:
@ -484,11 +487,6 @@ tools:
### tools_ldapinit()
ldapinit:
action_help: YunoHost LDAP initialization
arguments:
-d:
full: --domain
help: YunoHost main domain
pattern: '^([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])*)$'
### tools_adminpw()
adminpw:

View file

@ -15,9 +15,6 @@ apps_path = '/usr/share/yunohost/apps'
apps_setting_path= '/etc/yunohost/apps/'
install_tmp = '/tmp/yunohost/install'
app_tmp_folder = install_tmp + '/from_file'
a2_template_path = '/etc/yunohost/apache/templates'
a2_app_conf_path = '/etc/yunohost/apache/domains'
lemon_tmp_conf = '/tmp/tmplemonconf'
def app_listlists():
"""
@ -205,10 +202,8 @@ def app_install(app, domain, path='/', label=None, public=False, protected=True)
try:
domain_list(filter="virtualdomain="+ domain)
except YunoHostError:
domain_add([domain])
domain_add([domain], web=True)
_apache_config(domain)
_lemon_config(domain)
# Copy files to the right place
@ -411,65 +406,6 @@ def _exec_app_script(step, path, var_dict, app_type):
break
def _apache_config(domain):
"""
Fill Apache configuration templates
Keyword arguments:
domain -- Domain to configure Apache around
"""
# TMP: remove old conf
if os.path.exists(a2_app_conf_path +'/'+ domain +'.conf'): os.remove(a2_app_conf_path +'/'+ domain +'.conf')
if os.path.exists(a2_app_conf_path +'/'+ domain +'.d/'): shutil.rmtree(a2_app_conf_path +'/'+ domain +'.d/')
try: os.listdir(a2_app_conf_path +'/'+ domain +'.d/')
except OSError: os.makedirs(a2_app_conf_path +'/'+ domain +'.d/')
with open(a2_app_conf_path +'/'+ domain +'.conf', 'a') as a2_conf:
for line in open(a2_template_path +'/template.conf.tmp'):
line = line.replace('[domain]',domain)
a2_conf.write(line)
if os.system('service apache2 reload') == 0:
win_msg(_("Apache configured"))
else:
raise YunoHostError(1, _("An error occured during Apache configuration"))
def _lemon_config(domain):
"""
Configure LemonLDAP
Keyword arguments:
domain -- Domain to configure LemonLDAP around
"""
if os.path.exists(lemon_tmp_conf): os.remove(lemon_tmp_conf)
lemon_conf_lines = [
"$tmp->{'exportedHeaders'}->{'"+ domain +"'}->{'Auth-User'} = '$uid';",
"$tmp->{'exportedHeaders'}->{'"+ domain +"'}->{'Remote-User'} = '$uid';",
"$tmp->{'exportedHeaders'}->{'"+ domain +"'}->{'Desc'} = '$description';",
"$tmp->{'exportedHeaders'}->{'"+ domain +"'}->{'Email'} = '$uid';",
"$tmp->{'exportedHeaders'}->{'"+ domain +"'}->{'Name'} = '$cn';",
"$tmp->{'exportedHeaders'}->{'"+ domain +"'}->{'Authorization'} = '\"Basic \".encode_base64(\"$uid:$_password\")';",
"$tmp->{'vhostOptions'}->{'"+ domain +"'}->{'vhostMaintenance'} = 0;",
"$tmp->{'vhostOptions'}->{'"+ domain +"'}->{'vhostPort'} = -1;",
"$tmp->{'vhostOptions'}->{'"+ domain +"'}->{'vhostHttps'} = -1;",
"$tmp->{'locationRules'}->{'"+ domain +"'}->{'default'} = 'accept';",
"$tmp->{'locationRules'}->{'"+ domain +"'}->{'(?#logout)^/logout'} = 'logout_app_sso https://"+ domain +"/';",
]
with open(lemon_tmp_conf,'a') as lemon_conf:
for line in lemon_conf_lines:
lemon_conf.write(line + '\n')
if os.system('/usr/share/lemonldap-ng/bin/lmYnhMoulinette') == 0:
win_msg(_("LemonLDAP configured"))
else:
raise YunoHostError(1, _("An error occured during LemonLDAP configuration"))
def _installed_instance_number(app):
"""
Check if application is installed and return instance number

View file

@ -7,6 +7,10 @@ import re
from urllib import urlopen
from yunohost import YunoHostError, YunoHostLDAP, win_msg, colorize, validate, get_required_args
a2_template_path = '/etc/yunohost/apache/templates'
a2_app_conf_path = '/etc/yunohost/apache/domains'
lemon_tmp_conf = '/tmp/tmplemonconf'
def domain_list(filter=None, limit=None, offset=None):
"""
List YunoHost domains
@ -41,12 +45,13 @@ def domain_list(filter=None, limit=None, offset=None):
return { 'Domains': result_list }
def domain_add(domains):
def domain_add(domains, web=False):
"""
Add one or more domains
Keyword argument:
domains -- List of domains to add
web -- Configure Apache and LemonLDAP for the domain too
Returns:
Dict
@ -105,6 +110,10 @@ def domain_add(domains):
else:
raise YunoHostError(169, _("An error occured during domain creation"))
if web:
_apache_config(domain)
_lemon_config(domain)
win_msg(_("Domain(s) successfully created"))
return { 'Domains' : result }
@ -153,3 +162,65 @@ def domain_remove(domains):
return { 'Domains' : result }
def _apache_config(domain):
"""
Fill Apache configuration templates
Keyword arguments:
domain -- Domain to configure Apache around
"""
# TMP: remove old conf
if os.path.exists(a2_app_conf_path +'/'+ domain +'.conf'): os.remove(a2_app_conf_path +'/'+ domain +'.conf')
if os.path.exists(a2_app_conf_path +'/'+ domain +'.d/'): shutil.rmtree(a2_app_conf_path +'/'+ domain +'.d/')
try: os.listdir(a2_app_conf_path +'/'+ domain +'.d/')
except OSError: os.makedirs(a2_app_conf_path +'/'+ domain +'.d/')
with open(a2_app_conf_path +'/'+ domain +'.conf', 'a') as a2_conf:
for line in open(a2_template_path +'/template.conf.tmp'):
line = line.replace('[domain]',domain)
a2_conf.write(line)
if os.system('service apache2 reload') == 0:
win_msg(_("Apache configured"))
else:
raise YunoHostError(1, _("An error occured during Apache configuration"))
def _lemon_config(domain):
"""
Configure LemonLDAP
Keyword arguments:
domain -- Domain to configure LemonLDAP around
"""
if os.path.exists(lemon_tmp_conf): os.remove(lemon_tmp_conf)
lemon_conf_lines = [
"$tmp->{'exportedHeaders'}->{'"+ domain +"'}->{'Auth-User'} = '$uid';",
"$tmp->{'exportedHeaders'}->{'"+ domain +"'}->{'Remote-User'} = '$uid';",
"$tmp->{'exportedHeaders'}->{'"+ domain +"'}->{'Desc'} = '$description';",
"$tmp->{'exportedHeaders'}->{'"+ domain +"'}->{'Email'} = '$uid';",
"$tmp->{'exportedHeaders'}->{'"+ domain +"'}->{'Name'} = '$cn';",
"$tmp->{'exportedHeaders'}->{'"+ domain +"'}->{'Authorization'} = '\"Basic \".encode_base64(\"$uid:$_password\")';",
"$tmp->{'vhostOptions'}->{'"+ domain +"'}->{'vhostMaintenance'} = 0;",
"$tmp->{'vhostOptions'}->{'"+ domain +"'}->{'vhostPort'} = -1;",
"$tmp->{'vhostOptions'}->{'"+ domain +"'}->{'vhostHttps'} = -1;",
"$tmp->{'locationRules'}->{'"+ domain +"'}->{'default'} = 'accept';",
"$tmp->{'locationRules'}->{'"+ domain +"'}->{'(?#logout)^/logout'} = 'logout_app_sso https://"+ domain +"/';",
]
with open(lemon_tmp_conf,'a') as lemon_conf:
for line in lemon_conf_lines:
lemon_conf.write(line + '\n')
if os.system('/usr/share/lemonldap-ng/bin/lmYnhMoulinette') == 0:
win_msg(_("LemonLDAP configured"))
else:
raise YunoHostError(1, _("An error occured during LemonLDAP configuration"))

View file

@ -8,13 +8,10 @@ import getpass
from yunohost import YunoHostError, YunoHostLDAP, validate, colorize, get_required_args, win_msg
from yunohost_domain import domain_add
def tools_ldapinit(domain):
def tools_ldapinit():
"""
Initialize YunoHost LDAP scheme
Keyword arguments:
domain -- Main domain name for initialization
Returns:
dict
@ -30,7 +27,6 @@ def tools_ldapinit(domain):
for rdn, attr_dict in ldap_map['childs'].items():
yldap.add(rdn, attr_dict)
domain_add([domain])
admin_dict = {
'cn': 'admin',
@ -113,6 +109,8 @@ def tools_maindomain(old_domain, new_domain):
for line in lines:
sources.write(re.sub(r''+ old_domain +'', new_domain, line))
domain_add([domain], web=True)
lemon_tmp_conf = '/tmp/tmplemonconf'
if os.path.exists(lemon_tmp_conf): os.remove(lemon_tmp_conf)
@ -181,7 +179,7 @@ def tools_postinstall(domain, password):
tools_maindomain(old_domain='yunohost.org', new_domain=domain)
# Initialize YunoHost LDAP base
tools_ldapinit(domain=domain)
tools_ldapinit()
# Change LDAP admin password
tools_adminpw(old_password='yunohost', new_password=password)