Add lemonrule function

This commit is contained in:
Kload 2013-10-10 12:52:21 +02:00
parent b84b7dc8f9
commit f0fbfb6bd6
5 changed files with 91 additions and 15 deletions

View file

@ -638,3 +638,29 @@ tools:
help: Subscribe domain to a DynDNS service
action: store_true
### tools_lemonrule()
lemonrule:
action_help: Add/remove LemonLDAP location rule
api: PUT /lemonrule
arguments:
-i:
full: --id
help: ID to attribute to the rule
-u:
full: --url
help: URL to apply to the rule
-v:
full: --value
help: Value of the rule to write to the configuration file
-p:
full: --priority
help: Priority of the rule
-d:
full: --delete
help: Delete the rule
action: store_true
-a:
full: --apply
help: Apply the LemonLDAP configuration by reloading Apache
action: store_true

View file

@ -88,18 +88,6 @@ def pretty_print_dict(d, depth=0):
else:
print((" ") * depth + "%s: %s" % (str(k), str(v)))
def lvl(*args):
dic = None
for arg in args:
if dic is None:
dic = arg
elif arg in dic:
dic = dic[arg]
else:
return False
return True
def is_true(arg):
true_list = ['yes', 'Yes', 'true', 'True' ]
for string in true_list:

View file

@ -30,7 +30,7 @@ import shutil
import stat
import yaml
import time
from yunohost import YunoHostError, YunoHostLDAP, win_msg, random_password, lvl, is_true, lemon_configuration
from yunohost import YunoHostError, YunoHostLDAP, win_msg, random_password, is_true, lemon_configuration
from yunohost_domain import domain_list, domain_add
from yunohost_user import user_info
@ -41,7 +41,6 @@ a2_settings_path = '/etc/yunohost/apache/domains'
a2_template_path = '/etc/yunohost/apache/templates'
install_tmp = '/tmp/yunohost/install'
app_tmp_folder = install_tmp + '/from_file'
lemon_tmp_conf = '/tmp/tmplemonconf'
def app_listlists():
"""

View file

@ -117,7 +117,7 @@ def domain_add(domains, raw=False, main=False):
('exportedHeaders', domain, 'Auth-User'): '$uid',
('exportedHeaders', domain, 'Remote-User'): '$uid',
('exportedHeaders', domain, 'Desc'): '$description',
('exportedHeaders', domain, 'Email'): '$mail',
('exportedHeaders', domain, 'Email'): "(ref($mail) eq 'ARRAY' ? $mail[0] : $mail)",
('exportedHeaders', domain, 'Name'): '$cn',
('exportedHeaders', domain, 'Authorization'): '"Basic ".encode_base64("$uid:$_password")',
('vhostOptions', domain, 'vhostMaintenance'): 0,

View file

@ -36,6 +36,8 @@ from yunohost_domain import domain_add
from yunohost_dyndns import dyndns_subscribe
from yunohost_backup import backup_init
lemon_tmp_conf = '/tmp/tmplemonconf'
def tools_ldapinit(password=None):
"""
YunoHost LDAP initialization
@ -298,3 +300,64 @@ def tools_postinstall(domain, password, dyndns=False):
os.system('service samba restart')
win_msg(_("YunoHost has been successfully configured"))
def tools_lemonrule(id=None, url=None, key=None, value=None, priority=None, delete=False, apply=False):
"""
"""
conf_lines = []
if delete: line = "delete $tmp"
else: line = "$tmp"
# locationRule formatter
if url is not None and id is not None:
# Remove potential "http://" or "https://"
if '://' in url:
url = url[url.index('://') + 3:]
# Split domain and path properly
if '/' in url:
domain = url[:url.index('/')]
path = url[url.index('/'):]
if path[-1:] is not '/':
path = path +'/'
else:
domain = url
path = '/'
line = line +"->{'locationRules'}->{'"+ domain +"'}"
if priority is not None:
line = line +"->{'(?#"+ priority + id +")^"+ path +"'}"
else:
line = line +"->{'(?#"+ id +"Z)^"+ path +"'}"
# Free key formatter from tuple
elif key is not None:
if not isinstance(key, tuple): key = (key,)
for level in key:
line = line +"->{'"+ level +"'}"
# Append value
if value is None: conf_lines.append(line +';')
elif isinstance(value, int): conf_lines.append(line +' = '+ str(value) +';')
else: conf_lines.append(line +' = \''+ value +'\';')
# Write configuration
with open(lemon_tmp_conf,'a+') as lemon_conf:
for conf_line in conf_lines:
lemon_conf.write(conf_line)
# Apply & reload configuration
if apply:
os.system('chown www-data '+ lemon_tmp_conf)
if os.system('/usr/share/lemonldap-ng/bin/lmYnhMoulinette') == 0:
os.system('service apache2 reload')
win_msg(_("LemonLDAP configured"))
else:
raise YunoHostError(1, _("An error occured during LemonLDAP configuration"))
os.system("echo '' > lemon_tmp_conf")