rougail/bin/CreoleService

72 lines
2.2 KiB
Python
Executable File

#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import argparse
from pyeole import scriptargs
from pyeole.log import init_logging
from pyeole.service import manage_services
from creole.reconfigure import services
from pyeole.ihm import only_root
only_root()
def parse_cmdline():
service_actions=['apply', 'configure', 'enable', 'disable', 'status',
'start', 'stop', 'restart', 'reload']
parser = argparse.ArgumentParser(description="Action sur les services",
parents=[scriptargs.container(),
scriptargs.logging('info')])
parser.add_argument('service', help="Nom du service")
parser.add_argument('action', choices=service_actions,
help="Action à effectuer")
parser.add_argument("-f", "--force", action="store_true", default=False,
help="Ne pas valider l'état de service")
parser.add_argument("-s", "--silent", action="store_true", default=False,
help="Ne pas affichier sur la console")
opts = parser.parse_args()
if opts.verbose:
opts.log_level = 'info'
if opts.debug:
opts.log_level = 'debug'
if opts.silent:
opts.log_level = 'error'
return opts
def main():
options = parse_cmdline()
log = init_logging(level=options.log_level)
try:
display = 'console'
if options.silent:
display = 'log'
if options.service == 'all':
if options.action == 'restart':
services('stop', display_title=False, try_restart_lxc=False)
services('start', display_title=False, try_restart_lxc=False)
else:
services(options.action, display_title=False, try_restart_lxc=False)
ret = True
else:
ret = manage_services(options.action, options.service,
container=options.container, force=options.force,
display=display)
except Exception, err:
if options.debug:
log.debug(err, exc_info=True)
else:
log.error(err)
sys.exit(1)
sys.exit(ret)
if __name__ == '__main__':
main()