mirror of
https://github.com/YunoHost/moulinette.git
synced 2024-09-03 20:06:31 +02:00
moving functions
This commit is contained in:
parent
cd9681459d
commit
bb5bdc2f17
3 changed files with 88 additions and 78 deletions
94
parse_args
94
parse_args
|
@ -31,7 +31,7 @@ if not __debug__:
|
||||||
gettext.install('YunoHost')
|
gettext.install('YunoHost')
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from yunohost import YunoHostError, YunoHostLDAP, str_to_func, colorize, pretty_print_dict
|
from yunohost import YunoHostError, YunoHostLDAP, str_to_func, colorize, pretty_print_dict, display_error, connect_services, disconnect_services
|
||||||
except ImportError:
|
except ImportError:
|
||||||
sys.stderr.write('Require YunoHost lib')
|
sys.stderr.write('Require YunoHost lib')
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
@ -229,18 +229,18 @@ action_map = {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
### domain_create()
|
### domain_add()
|
||||||
'create' : {
|
'add' : {
|
||||||
'action_help' : _("Create a custom domain"),
|
'action_help' : _("Create a custom domain"),
|
||||||
'connections' : ['ldap'],
|
'connections' : ['ldap'],
|
||||||
'arguments' : {
|
'arguments' : {
|
||||||
'domain name' : {
|
'domain' : {
|
||||||
'help' : _("Domain name to create"),
|
'help' : _("Domain name to add"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
### domain_delete()
|
### domain_remove()
|
||||||
'delete' : {
|
'remove' : {
|
||||||
'action_help' : _("Delete domains"),
|
'action_help' : _("Delete domains"),
|
||||||
'connections' : ['ldap'],
|
'connections' : ['ldap'],
|
||||||
'arguments' : {
|
'arguments' : {
|
||||||
|
@ -255,14 +255,14 @@ action_map = {
|
||||||
'action_help' : _("Get domain informations"),
|
'action_help' : _("Get domain informations"),
|
||||||
'connections' : ['ldap'],
|
'connections' : ['ldap'],
|
||||||
'arguments' : {
|
'arguments' : {
|
||||||
'domain-name' : {}
|
'domain' : {}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
### domain_renewcert()
|
### domain_renewcert()
|
||||||
'renewcert' : {
|
'renewcert' : {
|
||||||
'action_help' : _("Renew domain certificate"),
|
'action_help' : _("Renew domain certificate"),
|
||||||
'arguments' : {
|
'arguments' : {
|
||||||
'domain-name' : {}
|
'domain' : {}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@ -301,7 +301,7 @@ action_map = {
|
||||||
'action_help' : _("Install apps"),
|
'action_help' : _("Install apps"),
|
||||||
'connections' : ['ldap', 'dns'],
|
'connections' : ['ldap', 'dns'],
|
||||||
'arguments' : {
|
'arguments' : {
|
||||||
'appname' : {
|
'app' : {
|
||||||
'nargs' : '+',
|
'nargs' : '+',
|
||||||
},
|
},
|
||||||
'-d' : {
|
'-d' : {
|
||||||
|
@ -338,8 +338,8 @@ action_map = {
|
||||||
'connections' : ['ldap'],
|
'connections' : ['ldap'],
|
||||||
'arguments' : {
|
'arguments' : {
|
||||||
'app' : {
|
'app' : {
|
||||||
'help' : _("App(s) to upgrade"),
|
'help' : _("App(s) to upgrade (default all)"),
|
||||||
'nargs' : '+',
|
'nargs' : '*',
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -406,6 +406,7 @@ action_map = {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
### repo_add()
|
||||||
'add' : {
|
'add' : {
|
||||||
'action_help' : _("Add app repository"),
|
'action_help' : _("Add app repository"),
|
||||||
'connections' : ['repo'],
|
'connections' : ['repo'],
|
||||||
|
@ -419,6 +420,7 @@ action_map = {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
### repo_remove()
|
||||||
'remove' : {
|
'remove' : {
|
||||||
'action_help' : _("Remove repository"),
|
'action_help' : _("Remove repository"),
|
||||||
'connections' : ['repo'],
|
'connections' : ['repo'],
|
||||||
|
@ -480,10 +482,12 @@ action_map = {
|
||||||
'firewall' : {
|
'firewall' : {
|
||||||
'category_help' : _("Manage firewall rules"),
|
'category_help' : _("Manage firewall rules"),
|
||||||
'actions' : {
|
'actions' : {
|
||||||
|
### firewall_list()
|
||||||
'list' : {
|
'list' : {
|
||||||
'action_help' : _("List all firewall rules"),
|
'action_help' : _("List all firewall rules"),
|
||||||
'connections' : ['firewall'],
|
'connections' : ['firewall'],
|
||||||
},
|
},
|
||||||
|
### firewall_allow()
|
||||||
'allow' : {
|
'allow' : {
|
||||||
'action_help' : _("Allow connection port/protocol"),
|
'action_help' : _("Allow connection port/protocol"),
|
||||||
'connections' : ['firewall'],
|
'connections' : ['firewall'],
|
||||||
|
@ -500,6 +504,7 @@ action_map = {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
### firewall_disallow()
|
||||||
'disallow' : {
|
'disallow' : {
|
||||||
'action_help' : _("Disallow connection"),
|
'action_help' : _("Disallow connection"),
|
||||||
'connections' : ['firewall'],
|
'connections' : ['firewall'],
|
||||||
|
@ -573,71 +578,6 @@ def parse_dict(action_map):
|
||||||
|
|
||||||
return parsers['general'].parse_args()
|
return parsers['general'].parse_args()
|
||||||
|
|
||||||
def display_error(error):
|
|
||||||
"""
|
|
||||||
Nice error displaying
|
|
||||||
|
|
||||||
"""
|
|
||||||
if not __debug__ :
|
|
||||||
traceback.print_exc()
|
|
||||||
if os.isatty(1):
|
|
||||||
print('\n' + colorize(_("Error: "), 'red') + error.message)
|
|
||||||
else:
|
|
||||||
print(json.dumps({ 'error' : error.message }))
|
|
||||||
|
|
||||||
def connect_services(action_map):
|
|
||||||
"""
|
|
||||||
Connect to different services needed by the action
|
|
||||||
|
|
||||||
Keyword arguments:
|
|
||||||
action_map -- Map of actions
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
Dict -- openned connections or error code
|
|
||||||
|
|
||||||
"""
|
|
||||||
action_dict = action_map[sys.argv[1]]['actions'][sys.argv[2]]
|
|
||||||
connections = {}
|
|
||||||
required_connections = []
|
|
||||||
|
|
||||||
if 'connections' in action_dict:
|
|
||||||
required_connections = action_dict['connections']
|
|
||||||
|
|
||||||
try:
|
|
||||||
# Connect to different services if the action is requiring it
|
|
||||||
if 'ldap' in required_connections:
|
|
||||||
connections['ldap'] = YunoHostLDAP()
|
|
||||||
if 'firewall' in required_connections:
|
|
||||||
connections['firewall'] = open('/etc/init.d/iptables', 'w')
|
|
||||||
# TODO: Add other services connections
|
|
||||||
except YunoHostError, error:
|
|
||||||
display_error(error)
|
|
||||||
sys.exit(error.code)
|
|
||||||
else:
|
|
||||||
return connections
|
|
||||||
|
|
||||||
def disconnect_services(connections):
|
|
||||||
"""
|
|
||||||
Disconnect openned connections
|
|
||||||
|
|
||||||
Keyword arguments:
|
|
||||||
connections -- Dictionnary of openned connections
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
Boolean
|
|
||||||
|
|
||||||
"""
|
|
||||||
try:
|
|
||||||
if 'ldap' in connections:
|
|
||||||
connections['ldap'].disconnect()
|
|
||||||
if 'firewall' in connections:
|
|
||||||
connections['firewall'].close()
|
|
||||||
# TODO: Add other services deconnections
|
|
||||||
except YunoHostError, error:
|
|
||||||
display_error(error)
|
|
||||||
sys.exit(error.code)
|
|
||||||
else:
|
|
||||||
return True
|
|
||||||
|
|
||||||
def main(action_map):
|
def main(action_map):
|
||||||
"""
|
"""
|
||||||
|
|
69
yunohost.py
69
yunohost.py
|
@ -97,6 +97,75 @@ def validate(regex_dict):
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def display_error(error):
|
||||||
|
"""
|
||||||
|
Nice error displaying
|
||||||
|
|
||||||
|
"""
|
||||||
|
if not __debug__ :
|
||||||
|
traceback.print_exc()
|
||||||
|
if os.isatty(1):
|
||||||
|
print('\n' + colorize(_("Error: "), 'red') + error.message)
|
||||||
|
else:
|
||||||
|
print(json.dumps({ 'error' : error.message }))
|
||||||
|
|
||||||
|
|
||||||
|
def connect_services(action_map):
|
||||||
|
"""
|
||||||
|
Connect to different services needed by the action
|
||||||
|
|
||||||
|
Keyword arguments:
|
||||||
|
action_map -- Map of actions
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Dict -- openned connections or error code
|
||||||
|
|
||||||
|
"""
|
||||||
|
action_dict = action_map[sys.argv[1]]['actions'][sys.argv[2]]
|
||||||
|
connections = {}
|
||||||
|
required_connections = []
|
||||||
|
|
||||||
|
if 'connections' in action_dict:
|
||||||
|
required_connections = action_dict['connections']
|
||||||
|
|
||||||
|
try:
|
||||||
|
# Connect to different services if the action is requiring it
|
||||||
|
if 'ldap' in required_connections:
|
||||||
|
connections['ldap'] = YunoHostLDAP()
|
||||||
|
if 'firewall' in required_connections:
|
||||||
|
connections['firewall'] = open('/etc/init.d/iptables', 'w')
|
||||||
|
# TODO: Add other services connections
|
||||||
|
except YunoHostError, error:
|
||||||
|
display_error(error)
|
||||||
|
sys.exit(error.code)
|
||||||
|
else:
|
||||||
|
return connections
|
||||||
|
|
||||||
|
|
||||||
|
def disconnect_services(connections):
|
||||||
|
"""
|
||||||
|
Disconnect openned connections
|
||||||
|
|
||||||
|
Keyword arguments:
|
||||||
|
connections -- Dictionnary of openned connections
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Boolean
|
||||||
|
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
if 'ldap' in connections:
|
||||||
|
connections['ldap'].disconnect()
|
||||||
|
if 'firewall' in connections:
|
||||||
|
connections['firewall'].close()
|
||||||
|
# TODO: Add other services deconnections
|
||||||
|
except YunoHostError, error:
|
||||||
|
display_error(error)
|
||||||
|
sys.exit(error.code)
|
||||||
|
else:
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
class YunoHostError(Exception):
|
class YunoHostError(Exception):
|
||||||
"""
|
"""
|
||||||
Custom exception
|
Custom exception
|
||||||
|
|
|
@ -65,7 +65,7 @@ def user_create(args, connections):
|
||||||
'userPassword' : pwd
|
'userPassword' : pwd
|
||||||
}
|
}
|
||||||
|
|
||||||
# Validate values
|
# Validate values TODO: validate other values
|
||||||
validate({
|
validate({
|
||||||
args['username'] : r'^[a-z0-9_]+$',
|
args['username'] : r'^[a-z0-9_]+$',
|
||||||
args['mail'] : r'^[\w.-]+@[\w.-]+\.[a-zA-Z]{2,6}$'
|
args['mail'] : r'^[\w.-]+@[\w.-]+\.[a-zA-Z]{2,6}$'
|
||||||
|
@ -82,6 +82,7 @@ def user_create(args, connections):
|
||||||
|
|
||||||
if yldap.add(rdn, attr_dict):
|
if yldap.add(rdn, attr_dict):
|
||||||
win_msg(_("User successfully created"))
|
win_msg(_("User successfully created"))
|
||||||
|
#TODO: Send a welcome mail to user
|
||||||
return { _("Fullname") : fullname, _("Username") : args['username'], _("Mail") : args['mail'] }
|
return { _("Fullname") : fullname, _("Username") : args['username'], _("Mail") : args['mail'] }
|
||||||
else:
|
else:
|
||||||
raise YunoHostError(169, _('An error occured during user creation'))
|
raise YunoHostError(169, _('An error occured during user creation'))
|
||||||
|
|
Loading…
Add table
Reference in a new issue