2012-10-06 17:57:08 +02:00
|
|
|
#!/usr/bin/env python
|
2012-10-06 16:22:15 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2012-10-07 16:20:20 +02:00
|
|
|
__credits__ = """
|
|
|
|
Copyright (C) 2012 YunoHost
|
2012-10-06 20:29:00 +02:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Affero General Public License as published
|
|
|
|
by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
|
|
along with this program; if not, see http://www.gnu.org/licenses
|
2012-10-07 16:20:20 +02:00
|
|
|
"""
|
2012-10-07 18:20:06 +02:00
|
|
|
__author__ = 'Kload <kload@kload.fr>'
|
2012-10-07 13:37:59 +02:00
|
|
|
__version__ = '2.0 beta1'
|
2012-10-06 20:29:00 +02:00
|
|
|
|
2012-10-08 18:16:43 +02:00
|
|
|
import os
|
2012-10-06 16:22:15 +02:00
|
|
|
import sys
|
|
|
|
import argparse
|
2012-10-07 16:20:20 +02:00
|
|
|
import gettext
|
2012-10-08 18:16:43 +02:00
|
|
|
import json
|
|
|
|
if not __debug__:
|
|
|
|
import traceback
|
|
|
|
|
2012-10-07 16:20:20 +02:00
|
|
|
gettext.install('YunoHost')
|
2012-10-07 13:16:19 +02:00
|
|
|
|
2012-10-10 16:01:54 +02:00
|
|
|
try:
|
|
|
|
from yunohost import YunoHostError, YunoHostLDAP, str_to_func, colorize, pretty_print_dict
|
|
|
|
except ImportError:
|
|
|
|
sys.stderr.write('Require YunoHost lib')
|
|
|
|
sys.exit(1)
|
2012-10-08 20:46:52 +02:00
|
|
|
|
2012-10-08 23:02:26 +02:00
|
|
|
"""
|
2012-10-10 12:20:28 +02:00
|
|
|
Category/actions/arguments dictionnary
|
2012-10-08 20:46:52 +02:00
|
|
|
|
|
|
|
|
2012-10-10 12:20:28 +02:00
|
|
|
Except for general_arguments, this dictionary contains 3 levels
|
|
|
|
as in this sample command line :
|
|
|
|
|
|
|
|
yunohost monitor info --cpu --ram
|
|
|
|
^ ^ ^ ^
|
|
|
|
(script) | category | action | parameters
|
2012-10-08 20:46:52 +02:00
|
|
|
|
|
|
|
|
2012-10-10 12:20:28 +02:00
|
|
|
Above example will lead to the function 'monitor_info(args)'
|
2012-10-12 19:00:41 +02:00
|
|
|
in the file 'yunohost_monitor.py' with 'cpu' and 'ram'
|
2012-10-10 12:20:28 +02:00
|
|
|
stored in args dictionnary.
|
|
|
|
|
|
|
|
Usage:
|
|
|
|
You can add a category at the first level, action at the second one,
|
|
|
|
and arguments at the third one.
|
2012-10-12 19:00:41 +02:00
|
|
|
If a connexion is needed for the action, don't forget to add it to
|
|
|
|
the action parameters (ldap, repo, dns or firewall).
|
2012-10-10 12:20:28 +02:00
|
|
|
|
|
|
|
Documentation:
|
|
|
|
You can see all arguments settings at the argparse documentation:
|
|
|
|
http://docs.python.org/dev/library/argparse.html
|
|
|
|
#argparse.ArgumentParser.add_argument
|
|
|
|
|
2012-10-12 19:00:41 +02:00
|
|
|
Don't forget to turn argument as dictionnary ('setting' : 'value')
|
2012-10-08 23:02:26 +02:00
|
|
|
|
|
|
|
"""
|
2012-10-14 17:01:54 +02:00
|
|
|
action_map = {
|
2012-10-10 12:20:28 +02:00
|
|
|
#############################
|
|
|
|
# General args #
|
|
|
|
#############################
|
|
|
|
'general_arguments' : {
|
|
|
|
'-v' : {
|
|
|
|
'full' : '--version',
|
2012-10-10 19:13:38 +02:00
|
|
|
'help' : _("Display %(prog)s version"),
|
2012-10-10 12:20:28 +02:00
|
|
|
'action' : 'version',
|
|
|
|
'version' : '%(prog)s ' + __version__,
|
|
|
|
},
|
2012-10-10 14:47:11 +02:00
|
|
|
},
|
2012-10-10 11:10:57 +02:00
|
|
|
#############################
|
|
|
|
# User #
|
|
|
|
#############################
|
|
|
|
'user' : {
|
2012-10-10 19:13:38 +02:00
|
|
|
'category_help' : _("Manage users"),
|
2012-10-10 16:01:54 +02:00
|
|
|
'actions' : {
|
2012-10-10 11:10:57 +02:00
|
|
|
### user_list()
|
2012-10-10 19:47:57 +02:00
|
|
|
'list' : {
|
2012-10-10 19:13:38 +02:00
|
|
|
'action_help' : _("List users"),
|
2012-10-12 19:00:41 +02:00
|
|
|
'connections' : ['ldap'],
|
2012-10-10 16:01:54 +02:00
|
|
|
'arguments' : {
|
2012-10-10 19:13:38 +02:00
|
|
|
'--fields' : {
|
2012-10-12 19:00:41 +02:00
|
|
|
'help' : _("fields to fetch"),
|
2012-10-10 19:13:38 +02:00
|
|
|
'nargs' : '+',
|
|
|
|
},
|
|
|
|
'-f' : {
|
|
|
|
'full' : '--filter',
|
|
|
|
'help' : _("LDAP filter used to search"),
|
|
|
|
},
|
|
|
|
'-l' : {
|
|
|
|
'full' : '--limit',
|
2012-10-12 19:00:41 +02:00
|
|
|
'help' : _("Maximum number of user fetched"),
|
2012-10-10 19:13:38 +02:00
|
|
|
},
|
|
|
|
'-o' : {
|
|
|
|
'full' : '--offset',
|
|
|
|
'help' : _("Starting number for user fetching"),
|
2012-10-10 11:10:57 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
2012-10-10 19:47:57 +02:00
|
|
|
### user_create()
|
|
|
|
'create' : {
|
2012-10-10 19:13:38 +02:00
|
|
|
'action_help' : _("Create user"),
|
2012-10-12 19:00:41 +02:00
|
|
|
'connections' : ['ldap'],
|
2012-10-10 16:01:54 +02:00
|
|
|
'arguments' : {
|
2012-10-10 11:10:57 +02:00
|
|
|
'-u' : {
|
|
|
|
'full' : '--username',
|
2012-10-10 19:13:38 +02:00
|
|
|
'help' : _("Must be unique"),
|
2012-10-10 11:10:57 +02:00
|
|
|
},
|
|
|
|
'-f' : {
|
|
|
|
'full' : '--firstname',
|
|
|
|
},
|
|
|
|
'-l' : {
|
|
|
|
'full' : '--lastname',
|
|
|
|
},
|
|
|
|
'-m' : {
|
|
|
|
'full' : '--mail',
|
2012-10-10 19:13:38 +02:00
|
|
|
'help' : _("Main mail address, must be unique"),
|
2012-10-10 11:10:57 +02:00
|
|
|
},
|
|
|
|
'-p' : {
|
|
|
|
'full' : '--password',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
2012-10-10 19:13:38 +02:00
|
|
|
### user_delete()
|
2012-10-10 19:47:57 +02:00
|
|
|
'delete' : {
|
2012-10-10 19:13:38 +02:00
|
|
|
'action_help' : _("Delete user"),
|
2012-10-12 19:00:41 +02:00
|
|
|
'connections' : ['ldap'],
|
2012-10-10 19:13:38 +02:00
|
|
|
'arguments' : {
|
|
|
|
'users' : {
|
|
|
|
'help' : _("Username of users to delete"),
|
|
|
|
'nargs' : '+',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
### user_update()
|
2012-10-10 19:47:57 +02:00
|
|
|
'update' : {
|
2012-10-10 19:13:38 +02:00
|
|
|
'action_help' : _("Update user informations"),
|
2012-10-12 19:00:41 +02:00
|
|
|
'connections' : ['ldap'],
|
2012-10-10 19:13:38 +02:00
|
|
|
'arguments' : {
|
|
|
|
'user' : {
|
|
|
|
'help' : _("Username of user to update"),
|
|
|
|
},
|
|
|
|
'-f' : {
|
|
|
|
'full' : '--firstname',
|
|
|
|
},
|
|
|
|
'-l' : {
|
|
|
|
'full' : '--lastname',
|
|
|
|
},
|
|
|
|
'-m' : {
|
|
|
|
'full' : '--mail',
|
|
|
|
},
|
|
|
|
'-cp' : {
|
|
|
|
'full' : '--change-password',
|
|
|
|
'help' : _("New password to set"),
|
|
|
|
'metavar' : 'PASSWORD',
|
|
|
|
},
|
|
|
|
'--add-mailforward' : {
|
|
|
|
'help' : _("Mailforward addresses to add"),
|
|
|
|
'nargs' : '+',
|
|
|
|
'metavar' : 'MAIL',
|
|
|
|
},
|
|
|
|
'--remove-mailforward' : {
|
|
|
|
'help' : _("Mailforward addresses to remove"),
|
|
|
|
'nargs' : '+',
|
|
|
|
'metavar' : 'MAIL',
|
|
|
|
},
|
|
|
|
'-add-mailalias' : {
|
|
|
|
'help' : _("Mail aliases to add"),
|
|
|
|
'nargs' : '+',
|
|
|
|
'metavar' : 'MAIL',
|
|
|
|
},
|
|
|
|
'-remove-mailalias' : {
|
|
|
|
'help' : _("Mail aliases to remove"),
|
|
|
|
'nargs' : '+',
|
|
|
|
'metavar' : 'MAIL',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
### user_info()
|
2012-10-10 14:47:11 +02:00
|
|
|
'info' : {
|
2012-10-10 19:13:38 +02:00
|
|
|
'action_help' : _("Get user informations"),
|
2012-10-12 19:00:41 +02:00
|
|
|
'connections' : ['ldap'],
|
2012-10-10 19:13:38 +02:00
|
|
|
'arguments' : {
|
|
|
|
'user' : {
|
|
|
|
'nargs' : '?',
|
|
|
|
},
|
|
|
|
'-m' : {
|
|
|
|
'full' : '--mail',
|
|
|
|
},
|
|
|
|
'-cn' : {
|
|
|
|
'full' : '--fullname',
|
|
|
|
},
|
|
|
|
}
|
2012-10-10 14:47:11 +02:00
|
|
|
},
|
2012-10-10 11:34:36 +02:00
|
|
|
}
|
2012-10-08 18:16:43 +02:00
|
|
|
},
|
2012-10-10 19:47:57 +02:00
|
|
|
#############################
|
|
|
|
# Domain #
|
|
|
|
#############################
|
2012-10-08 18:16:43 +02:00
|
|
|
'domain' : {
|
2012-10-10 19:13:38 +02:00
|
|
|
'category_help' : _("Manage domains"),
|
2012-10-10 19:47:57 +02:00
|
|
|
'actions' : {
|
|
|
|
### domain_list()
|
|
|
|
'list' : {
|
|
|
|
'action_help' : _("List domains"),
|
2012-10-12 19:00:41 +02:00
|
|
|
'connections' : ['ldap'],
|
2012-10-10 19:47:57 +02:00
|
|
|
'arguments' : {
|
|
|
|
'-f' : {
|
|
|
|
'full' : '--filter',
|
|
|
|
'help' : _("LDAP filter used to search"),
|
|
|
|
},
|
|
|
|
'-l' : {
|
|
|
|
'full' : '--limit',
|
2012-10-12 19:00:41 +02:00
|
|
|
'help' : _("Maximum number of domain fetched"),
|
2012-10-10 19:47:57 +02:00
|
|
|
},
|
|
|
|
'-o' : {
|
|
|
|
'full' : '--offset',
|
2012-10-12 19:00:41 +02:00
|
|
|
'help' : _("Starting number for domain fetching"),
|
2012-10-10 19:47:57 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
### domain_create()
|
|
|
|
'create' : {
|
|
|
|
'action_help' : _("Create a custom domain"),
|
2012-10-12 19:00:41 +02:00
|
|
|
'connections' : ['ldap'],
|
2012-10-10 19:47:57 +02:00
|
|
|
'arguments' : {
|
|
|
|
'domain name' : {
|
|
|
|
'help' : _("Domain name to create"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
### domain_delete()
|
|
|
|
'delete' : {
|
|
|
|
'action_help' : _("Delete domains"),
|
2012-10-12 19:00:41 +02:00
|
|
|
'connections' : ['ldap'],
|
2012-10-10 19:47:57 +02:00
|
|
|
'arguments' : {
|
2012-10-12 19:00:41 +02:00
|
|
|
'domain' : {
|
|
|
|
'help' : _("Domain(s) to delete"),
|
2012-10-10 19:47:57 +02:00
|
|
|
'nargs' : '+',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
### domain_info()
|
|
|
|
'info' : {
|
|
|
|
'action_help' : _("Get domain informations"),
|
2012-10-12 19:00:41 +02:00
|
|
|
'connections' : ['ldap'],
|
2012-10-10 19:47:57 +02:00
|
|
|
'arguments' : {
|
|
|
|
'domain-name' : {}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
### domain_renewcert()
|
|
|
|
'renewcert' : {
|
|
|
|
'action_help' : _("Renew domain certificate"),
|
|
|
|
'arguments' : {
|
|
|
|
'domain-name' : {}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
2012-10-08 18:16:43 +02:00
|
|
|
},
|
2012-10-12 19:00:41 +02:00
|
|
|
#############################
|
|
|
|
# App #
|
|
|
|
#############################
|
2012-10-08 18:16:43 +02:00
|
|
|
'app' : {
|
2012-10-10 19:13:38 +02:00
|
|
|
'category_help' : _("Manage apps"),
|
2012-10-12 19:00:41 +02:00
|
|
|
'actions' : {
|
|
|
|
### app_list()
|
|
|
|
'list' : {
|
|
|
|
'action_help' : _("List apps"),
|
|
|
|
'connections' : ['ldap'],
|
|
|
|
'arguments' : {
|
|
|
|
'--fields' : {
|
|
|
|
'help' : _("fields to fetch"),
|
|
|
|
'nargs' : '+',
|
|
|
|
},
|
|
|
|
'-f' : {
|
|
|
|
'full' : '--filter',
|
|
|
|
'help' : _("LDAP filter used to search"),
|
|
|
|
},
|
|
|
|
'-l' : {
|
|
|
|
'full' : '--limit',
|
|
|
|
'help' : _("Maximum number of app fetched"),
|
|
|
|
},
|
|
|
|
'-o' : {
|
|
|
|
'full' : '--offset',
|
|
|
|
'help' : _("Starting number for app fetching"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
### app_install() TODO: Write help
|
|
|
|
'install' : {
|
|
|
|
'action_help' : _("Install apps"),
|
2012-10-14 20:56:04 +02:00
|
|
|
'connections' : ['ldap', 'dns'],
|
2012-10-12 19:00:41 +02:00
|
|
|
'arguments' : {
|
|
|
|
'appname' : {
|
|
|
|
'nargs' : '+',
|
|
|
|
},
|
|
|
|
'-d' : {
|
|
|
|
'full' : '--domain',
|
|
|
|
},
|
|
|
|
'-p' : {
|
|
|
|
'full' : '--path',
|
|
|
|
},
|
|
|
|
'-l' : {
|
|
|
|
'full' : '--label',
|
|
|
|
},
|
|
|
|
'--public' : {
|
|
|
|
'action' : 'store_true',
|
|
|
|
},
|
|
|
|
'--protected' : {
|
|
|
|
'action' : 'store_true',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
### app_remove() TODO: Write help
|
|
|
|
'remove' : {
|
|
|
|
'action_help' : _("Remove app"),
|
2012-10-14 20:56:04 +02:00
|
|
|
'connections' : ['ldap', 'dns'],
|
2012-10-12 19:00:41 +02:00
|
|
|
'arguments' : {
|
|
|
|
'app' : {
|
|
|
|
'help' : _("App(s) to delete"),
|
|
|
|
'nargs' : '+',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
### app_upgrade()
|
|
|
|
'upgrade' : {
|
|
|
|
'action_help' : _("Upgrade app"),
|
|
|
|
'connections' : ['ldap'],
|
|
|
|
'arguments' : {
|
|
|
|
'app' : {
|
|
|
|
'help' : _("App(s) to upgrade"),
|
|
|
|
'nargs' : '+',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
### app_info() TODO: Write help
|
|
|
|
'info' : {
|
|
|
|
'action_help' : _("Get app informations"),
|
|
|
|
'connections' : ['ldap'],
|
|
|
|
'arguments' : {
|
|
|
|
'app' : {},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
### app_addaccess() TODO: Write help
|
|
|
|
'addaccess' : {
|
|
|
|
'action_help' : _("Grant access right to users (everyone by default)"),
|
|
|
|
'connections' : ['ldap'],
|
|
|
|
'arguments' : {
|
|
|
|
'app' : {
|
|
|
|
'nargs' : '+',
|
|
|
|
},
|
|
|
|
'-u' : {
|
|
|
|
'full' : '--user',
|
|
|
|
'nargs' : '+',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
### app_removeaccess() TODO: Write help
|
|
|
|
'removeaccess' : {
|
|
|
|
'action_help' : _("Revoke access right to users (everyone by default)"),
|
|
|
|
'connections' : ['ldap'],
|
|
|
|
'arguments' : {
|
|
|
|
'app' : {
|
|
|
|
'nargs' : '+',
|
|
|
|
},
|
|
|
|
'-u' : {
|
|
|
|
'full' : '--user',
|
|
|
|
'nargs' : '+',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
#############################
|
|
|
|
# Repository #
|
|
|
|
#############################
|
|
|
|
'repo' : {
|
|
|
|
'category_help' : _("Manage app repositories"),
|
|
|
|
'actions' : {
|
|
|
|
### repo_list()
|
|
|
|
'list' : {
|
|
|
|
'action_help' : _("List repositories"),
|
2012-10-14 20:56:04 +02:00
|
|
|
'connections' : ['repo'],
|
2012-10-12 19:00:41 +02:00
|
|
|
'arguments' : {
|
|
|
|
'-f' : {
|
|
|
|
'full' : '--filter',
|
|
|
|
'help' : _("LDAP filter used to search"),
|
|
|
|
},
|
|
|
|
'-l' : {
|
|
|
|
'full' : '--limit',
|
|
|
|
'help' : _("Maximum number of repository fetched"),
|
|
|
|
},
|
|
|
|
'-o' : {
|
|
|
|
'full' : '--offset',
|
|
|
|
'help' : _("Starting number for repository fetching"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
2012-10-14 20:56:04 +02:00
|
|
|
'add' : {
|
|
|
|
'action_help' : _("Add app repository"),
|
|
|
|
'connections' : ['repo'],
|
|
|
|
'arguments' : {
|
|
|
|
'url' : {
|
|
|
|
'help' : _("URL of the repository"),
|
|
|
|
},
|
|
|
|
'-n' : {
|
|
|
|
'full' : '--name',
|
|
|
|
'help' : _("Unique name of the repository"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
'remove' : {
|
|
|
|
'action_help' : _("Remove repository"),
|
|
|
|
'connections' : ['repo'],
|
|
|
|
'arguments' : {
|
|
|
|
'repo' : {
|
|
|
|
'help' : _("Name or URL of the repository"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
'update' : {
|
|
|
|
'action_help' : _("Update app list from the repositories"),
|
|
|
|
'connections' : ['repo'],
|
|
|
|
},
|
2012-10-12 19:00:41 +02:00
|
|
|
}
|
2012-10-08 18:16:43 +02:00
|
|
|
},
|
2012-10-14 21:12:21 +02:00
|
|
|
#############################
|
|
|
|
# Monitor #
|
|
|
|
#############################
|
2012-10-08 18:16:43 +02:00
|
|
|
'monitor' : {
|
2012-10-10 19:13:38 +02:00
|
|
|
'category_help' : _("Monitoring functions"),
|
2012-10-14 11:56:57 +02:00
|
|
|
'actions' : {
|
|
|
|
'info': {
|
|
|
|
'action_help' : _("Check System"),
|
|
|
|
'connections' : ['ldap'],
|
|
|
|
'arguments' : {
|
|
|
|
'-m' : {
|
|
|
|
'full' : '--memory',
|
|
|
|
'help' : _("Check Memory"),
|
|
|
|
'action' : 'store_true',
|
|
|
|
},
|
2012-10-14 21:12:21 +02:00
|
|
|
'-c' : {
|
2012-10-14 11:56:57 +02:00
|
|
|
'full' : '--cpu',
|
|
|
|
'help' : _("Check CPU"),
|
2012-10-14 21:12:21 +02:00
|
|
|
'action' : 'store_true',
|
2012-10-14 11:56:57 +02:00
|
|
|
},
|
|
|
|
'-d' : {
|
|
|
|
'full' : '--disk',
|
|
|
|
'help' : _("Check Disk"),
|
|
|
|
'action' : 'store_true',
|
|
|
|
},
|
|
|
|
'-i' : {
|
|
|
|
'full' : '--ifconfig',
|
|
|
|
'help' : _("Ifconfig"),
|
|
|
|
'action' : 'store_true',
|
|
|
|
},
|
2012-10-14 13:42:22 +02:00
|
|
|
'-u' : {
|
|
|
|
'full' : '--uptime',
|
|
|
|
'help' : _("Show Uptime"),
|
|
|
|
'action' : 'store_true',
|
|
|
|
},
|
2012-10-14 11:56:57 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
2012-10-14 21:12:21 +02:00
|
|
|
},
|
|
|
|
#############################
|
|
|
|
# Firewall #
|
|
|
|
#############################
|
|
|
|
'firewall' : {
|
|
|
|
'category_help' : _("Manage firewall rules"),
|
|
|
|
'actions' : {
|
|
|
|
'list' : {
|
|
|
|
'action_help' : _("List all firewall rules"),
|
|
|
|
'connections' : ['firewall'],
|
|
|
|
},
|
|
|
|
'allow' : {
|
|
|
|
'action_help' : _("Allow connection port/protocol"),
|
|
|
|
'connections' : ['firewall'],
|
|
|
|
'arguments' : {
|
|
|
|
'port' : {
|
|
|
|
'help' : _("Port to open"),
|
|
|
|
},
|
|
|
|
'protocol' : {
|
|
|
|
'help' : _("Protocol associated with port"),
|
|
|
|
'choices' : ['UDP', 'TCP', 'Both'],
|
|
|
|
},
|
|
|
|
'name' : {
|
|
|
|
'help' : _("Reference name of the rule"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
'disallow' : {
|
|
|
|
'action_help' : _("Disallow connection"),
|
|
|
|
'connections' : ['firewall'],
|
|
|
|
'arguments' : {
|
|
|
|
'name' : {
|
|
|
|
'help' : _("Reference name of the rule to delete"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
#############################
|
|
|
|
# Tools #
|
|
|
|
#############################
|
2012-10-08 18:16:43 +02:00
|
|
|
'tools' : {
|
2012-10-10 19:13:38 +02:00
|
|
|
'category_help' : _("Specific tools"),
|
2012-10-10 16:01:54 +02:00
|
|
|
'actions' : {}
|
2012-10-14 21:12:21 +02:00
|
|
|
},
|
2012-10-08 18:16:43 +02:00
|
|
|
}
|
2012-10-06 16:22:15 +02:00
|
|
|
|
2012-10-14 17:01:54 +02:00
|
|
|
def parse_dict(action_map):
|
2012-10-08 20:46:52 +02:00
|
|
|
"""
|
2012-10-10 11:10:57 +02:00
|
|
|
Turn action dictionnary to parser, subparsers and arguments
|
2012-10-08 20:46:52 +02:00
|
|
|
|
|
|
|
Keyword arguments:
|
2012-10-14 17:01:54 +02:00
|
|
|
action_map -- Multi-level dictionnary of categories/actions/arguments list
|
2012-10-08 20:46:52 +02:00
|
|
|
|
|
|
|
Returns:
|
2012-10-10 11:10:57 +02:00
|
|
|
Namespace of args
|
2012-10-08 20:46:52 +02:00
|
|
|
|
|
|
|
"""
|
|
|
|
# Intialize parsers
|
|
|
|
parsers = subparsers_category = subparsers_action = {}
|
|
|
|
parsers['general'] = argparse.ArgumentParser()
|
|
|
|
subparsers = parsers['general'].add_subparsers()
|
|
|
|
|
2012-10-10 12:20:28 +02:00
|
|
|
# Add general arguments
|
2012-10-14 17:01:54 +02:00
|
|
|
for arg_name, arg_params in action_map['general_arguments'].items():
|
2012-10-10 12:20:28 +02:00
|
|
|
if arg_params['full']:
|
|
|
|
arg_fullname = arg_params['full']
|
|
|
|
del arg_params['full']
|
|
|
|
parsers['general'].add_argument(arg_name, arg_fullname, **arg_params)
|
|
|
|
else:
|
|
|
|
parsers['general'].add_argument(arg_name, **arg_params)
|
|
|
|
|
2012-10-14 17:01:54 +02:00
|
|
|
del action_map['general_arguments']
|
2012-10-10 12:20:28 +02:00
|
|
|
|
|
|
|
# Split categories into subparsers
|
2012-10-14 17:01:54 +02:00
|
|
|
for category, category_params in action_map.items():
|
2012-10-10 16:01:54 +02:00
|
|
|
if 'category_help' not in category_params: category_params['category_help'] = ''
|
|
|
|
subparsers_category[category] = subparsers.add_parser(category, help=category_params['category_help'])
|
2012-10-08 20:46:52 +02:00
|
|
|
subparsers_action[category] = subparsers_category[category].add_subparsers()
|
2012-10-10 12:20:28 +02:00
|
|
|
# Split actions
|
2012-10-10 16:01:54 +02:00
|
|
|
if 'actions' in category_params:
|
|
|
|
for action, action_params in category_params['actions'].items():
|
|
|
|
if 'action_help' not in action_params: action_params['action_help'] = ''
|
|
|
|
parsers[category + '_' + action] = subparsers_action[category].add_parser(action, help=action_params['action_help'])
|
2012-10-10 16:13:46 +02:00
|
|
|
# Set the action s related function
|
2012-10-10 16:01:54 +02:00
|
|
|
parsers[category + '_' + action].set_defaults(
|
|
|
|
func=str_to_func('yunohost_' + category + '.'
|
|
|
|
+ category + '_' + action))
|
|
|
|
# Add arguments
|
|
|
|
if 'arguments' in action_params:
|
|
|
|
for arg_name, arg_params in action_params['arguments'].items():
|
2012-10-10 19:13:38 +02:00
|
|
|
if 'full' in arg_params:
|
2012-10-10 16:01:54 +02:00
|
|
|
arg_fullname = arg_params['full']
|
|
|
|
del arg_params['full']
|
|
|
|
parsers[category + '_' + action].add_argument(arg_name, arg_fullname, **arg_params)
|
|
|
|
else:
|
|
|
|
parsers[category + '_' + action].add_argument(arg_name, **arg_params)
|
2012-10-08 20:46:52 +02:00
|
|
|
|
2012-10-14 17:40:13 +02:00
|
|
|
return parsers['general'].parse_args()
|
2012-10-08 20:46:52 +02:00
|
|
|
|
2012-10-10 20:53:42 +02:00
|
|
|
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 }))
|
|
|
|
|
2012-10-14 17:40:13 +02:00
|
|
|
def connect_services(action_map):
|
|
|
|
"""
|
|
|
|
Connect to different services needed by the action
|
|
|
|
|
|
|
|
Keyword arguments:
|
|
|
|
action_map -- Map of actions
|
2012-10-10 12:20:28 +02:00
|
|
|
|
|
|
|
Returns:
|
2012-10-14 17:44:16 +02:00
|
|
|
Dict -- openned connections or error code
|
2012-10-14 17:40:13 +02:00
|
|
|
|
|
|
|
"""
|
2012-10-14 17:41:03 +02:00
|
|
|
action_dict = action_map[sys.argv[1]]['actions'][sys.argv[2]]
|
2012-10-12 19:00:41 +02:00
|
|
|
connections = {}
|
2012-10-14 17:01:54 +02:00
|
|
|
required_connections = []
|
|
|
|
|
|
|
|
if 'connections' in action_dict:
|
|
|
|
required_connections = action_dict['connections']
|
2012-10-14 17:40:13 +02:00
|
|
|
|
2012-10-10 20:53:42 +02:00
|
|
|
try:
|
2012-10-12 19:00:41 +02:00
|
|
|
# Connect to different services if the action is requiring it
|
2012-10-14 17:01:54 +02:00
|
|
|
if 'ldap' in required_connections:
|
2012-10-12 19:00:41 +02:00
|
|
|
connections['ldap'] = YunoHostLDAP()
|
2012-10-14 17:01:54 +02:00
|
|
|
if 'firewall' in required_connections:
|
2012-10-12 19:00:41 +02:00
|
|
|
connections['firewall'] = open('/etc/init.d/iptables', 'w')
|
|
|
|
# TODO: Add other services connections
|
2012-10-10 20:53:42 +02:00
|
|
|
except YunoHostError, error:
|
|
|
|
display_error(error)
|
2012-10-14 17:44:16 +02:00
|
|
|
sys.exit(error.code)
|
2012-10-14 17:40:13 +02:00
|
|
|
else:
|
|
|
|
return connections
|
|
|
|
|
|
|
|
def disconnect_services(connections):
|
|
|
|
"""
|
|
|
|
Disconnect openned connections
|
|
|
|
|
|
|
|
Keyword arguments:
|
|
|
|
connections -- Dictionnary of openned connections
|
|
|
|
|
|
|
|
Returns:
|
2012-10-14 17:44:16 +02:00
|
|
|
Boolean
|
2012-10-14 17:40:13 +02:00
|
|
|
|
|
|
|
"""
|
|
|
|
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)
|
2012-10-14 17:44:16 +02:00
|
|
|
sys.exit(error.code)
|
2012-10-14 17:40:13 +02:00
|
|
|
else:
|
|
|
|
return True
|
|
|
|
|
|
|
|
def main(action_map):
|
|
|
|
"""
|
|
|
|
Main instructions
|
|
|
|
|
|
|
|
Parse the action_dict and execute the action-specific function,
|
|
|
|
then print json or pretty result if executed in a tty :)
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
int -- 0 or error code
|
|
|
|
|
|
|
|
"""
|
|
|
|
args = parse_dict(action_map)
|
|
|
|
connections = connect_services(action_map)
|
2012-10-10 16:01:54 +02:00
|
|
|
|
2012-10-08 18:16:43 +02:00
|
|
|
try:
|
2012-10-12 19:00:41 +02:00
|
|
|
if connections:
|
2012-10-14 17:40:13 +02:00
|
|
|
result = args.func(vars(args), connections)
|
2012-10-10 16:01:54 +02:00
|
|
|
else:
|
2012-10-14 17:40:13 +02:00
|
|
|
result = args.func(vars(args))
|
2012-10-10 20:53:42 +02:00
|
|
|
except TypeError:
|
|
|
|
print(_("Not (yet) implemented function"))
|
|
|
|
return 1
|
2012-10-08 18:16:43 +02:00
|
|
|
except YunoHostError, error:
|
2012-10-10 20:53:42 +02:00
|
|
|
display_error(error)
|
2012-10-08 18:16:43 +02:00
|
|
|
return error.code
|
|
|
|
else:
|
|
|
|
if os.isatty(1):
|
2012-10-10 14:19:17 +02:00
|
|
|
pretty_print_dict(result)
|
2012-10-08 18:16:43 +02:00
|
|
|
else:
|
|
|
|
print(json.dumps(result))
|
2012-10-10 16:01:54 +02:00
|
|
|
finally:
|
2012-10-14 17:40:13 +02:00
|
|
|
disconnect_services(connections)
|
|
|
|
|
2012-10-10 16:01:54 +02:00
|
|
|
return 0
|
2012-10-07 13:16:19 +02:00
|
|
|
|
2012-10-06 16:22:15 +02:00
|
|
|
if __name__ == '__main__':
|
2012-10-14 17:40:13 +02:00
|
|
|
sys.exit(main(action_map))
|