Commit 171e0102 authored by Antoine Millet's avatar Antoine Millet
Browse files

[bug#3920] Implemented shutdown handler method.

parent 709f03d2
Loading
Loading
Loading
Loading
+37 −0
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@
import inspect
import logging
from sjrpc.utils import RpcHandler, pure
from sjrpc.core import RpcError
from conf import CCConf
from exceptions import (AlreadyRegistered, AuthenticationError, RightError,
                        ReservedTagError, BadObjectError, BadRoleError,
@@ -463,6 +464,42 @@ class CliHandler(OnlineCCHandler):
                
        return errs.get_dict()

    @listed
    def shutdown(self, conn, query, reboot=True, gracefull=True):
        '''
        Execute a shutdown on selected objects (must be roles hv or host).

        :param query: the tql query to select objects.
        :param reboot: reboot the host instead of just shut it off
        :param gracefull: properly shutdown the host
        :return: a dict where key is the id of a selected object, and the value
            a tuple (errcode, message) where errcode is (success|error|warn) and
            message an error message.
        '''

        self._check(conn, 'execute', query)
        objects = self._server.list(query, show=set(('r',)))
        errs = Reporter()
        for obj in objects:
            if obj['r'] not in ('hv', 'host'):
                errs.error(obj['id'], 'bad role')
                continue
            try:
                objcon = self._server.get_connection(obj['id'])
            except KeyError:
                errs.error(obj['id'], 'node not connected')
            else:
                try:
                    objcon.connection.call('node_shutdown',
                                                      reboot, gracefull)
                except RpcError as err:
                    errs.error(obj['id'], '%s (exc: %s)' % (err.message,
                                                            err.exception))
                else:
                    errs.success(obj['id'], 'ok')
                
        return errs.get_dict()

    @listed
    def dbstats(self, conn):
        '''