# Copyright (c) 2018 DDN. All rights reserved.
# Use of this source code is governed by a MIT-style
# license that can be found in the LICENSE file.


import os

from chroma_agent.lib.shell import AgentShell
from chroma_agent.log import console_log
from chroma_agent.device_plugins.action_runner import CallbackAfterResponse
from chroma_agent.lib.pacemaker import PacemakerConfig


def ssi(runlevel):
    # force a manual failover by failing a node
    AgentShell.try_run(["sync"])
    AgentShell.try_run(["sync"])
    AgentShell.try_run(["init", runlevel])


def fail_node():
    ssi("0")


def stonith(node):
    p_cfg = PacemakerConfig()

    # TODO: signal that manager that a STONITH has been done so that it
    #       doesn't treat it as an AWOL
    console_log.info("Rebooting %s per a STONITH request" % node)

    p_cfg.get_node(node).fence_reboot()


def shutdown_server(halt=True, at_time="now"):
    def _shutdown():
        console_log.info("Initiating server shutdown per manager request")
        # This will initiate a "nice" shutdown with a wall from root, etc.
        AgentShell.try_run(["shutdown", "-H" if halt else "-h", at_time])

        console_log.info("Terminating")
        os._exit(0)

    raise CallbackAfterResponse(None, _shutdown)


def reboot_server(at_time="now"):
    def _reboot():
        console_log.info("Initiating server reboot per manager request")
        # reboot(8) just calls shutdown anyhow.
        AgentShell.try_run(["shutdown", "-r", at_time])

        console_log.info("Terminating")
        os._exit(0)

    raise CallbackAfterResponse(None, _reboot)


ACTIONS = [reboot_server, shutdown_server, fail_node, stonith]

[((16, 4, 16, 32), 'chroma_agent.lib.shell.AgentShell.try_run', 'AgentShell.try_run', ({(16, 23, 16, 31): "['sync']"}, {}), "(['sync'])", False, 'from chroma_agent.lib.shell import AgentShell\n'), ((17, 4, 17, 32), 'chroma_agent.lib.shell.AgentShell.try_run', 'AgentShell.try_run', ({(17, 23, 17, 31): "['sync']"}, {}), "(['sync'])", False, 'from chroma_agent.lib.shell import AgentShell\n'), ((18, 4, 18, 42), 'chroma_agent.lib.shell.AgentShell.try_run', 'AgentShell.try_run', ({(18, 23, 18, 41): "['init', runlevel]"}, {}), "(['init', runlevel])", False, 'from chroma_agent.lib.shell import AgentShell\n'), ((26, 12, 26, 29), 'chroma_agent.lib.pacemaker.PacemakerConfig', 'PacemakerConfig', ({}, {}), '()', False, 'from chroma_agent.lib.pacemaker import PacemakerConfig\n'), ((30, 4, 30, 65), 'chroma_agent.log.console_log.info', 'console_log.info', ({(30, 21, 30, 64): "('Rebooting %s per a STONITH request' % node)"}, {}), "('Rebooting %s per a STONITH request' % node)", False, 'from chroma_agent.log import console_log\n'), ((44, 10, 44, 48), 'chroma_agent.device_plugins.action_runner.CallbackAfterResponse', 'CallbackAfterResponse', ({(44, 32, 44, 36): 'None', (44, 38, 44, 47): '_shutdown'}, {}), '(None, _shutdown)', False, 'from chroma_agent.device_plugins.action_runner import CallbackAfterResponse\n'), ((56, 10, 56, 46), 'chroma_agent.device_plugins.action_runner.CallbackAfterResponse', 'CallbackAfterResponse', ({(56, 32, 56, 36): 'None', (56, 38, 56, 45): '_reboot'}, {}), '(None, _reboot)', False, 'from chroma_agent.device_plugins.action_runner import CallbackAfterResponse\n'), ((37, 8, 37, 74), 'chroma_agent.log.console_log.info', 'console_log.info', ({(37, 25, 37, 73): '"""Initiating server shutdown per manager request"""'}, {}), "('Initiating server shutdown per manager request')", False, 'from chroma_agent.log import console_log\n'), ((39, 8, 39, 73), 'chroma_agent.lib.shell.AgentShell.try_run', 'AgentShell.try_run', ({(39, 27, 39, 72): "['shutdown', '-H' if halt else '-h', at_time]"}, {}), "(['shutdown', '-H' if halt else '-h', at_time])", False, 'from chroma_agent.lib.shell import AgentShell\n'), ((41, 8, 41, 39), 'chroma_agent.log.console_log.info', 'console_log.info', ({(41, 25, 41, 38): '"""Terminating"""'}, {}), "('Terminating')", False, 'from chroma_agent.log import console_log\n'), ((42, 8, 42, 19), 'os._exit', 'os._exit', ({(42, 17, 42, 18): '(0)'}, {}), '(0)', False, 'import os\n'), ((49, 8, 49, 72), 'chroma_agent.log.console_log.info', 'console_log.info', ({(49, 25, 49, 71): '"""Initiating server reboot per manager request"""'}, {}), "('Initiating server reboot per manager request')", False, 'from chroma_agent.log import console_log\n'), ((51, 8, 51, 55), 'chroma_agent.lib.shell.AgentShell.try_run', 'AgentShell.try_run', ({(51, 27, 51, 54): "['shutdown', '-r', at_time]"}, {}), "(['shutdown', '-r', at_time])", False, 'from chroma_agent.lib.shell import AgentShell\n'), ((53, 8, 53, 39), 'chroma_agent.log.console_log.info', 'console_log.info', ({(53, 25, 53, 38): '"""Terminating"""'}, {}), "('Terminating')", False, 'from chroma_agent.log import console_log\n'), ((54, 8, 54, 19), 'os._exit', 'os._exit', ({(54, 17, 54, 18): '(0)'}, {}), '(0)', False, 'import os\n')]