# 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]

[((453, 481), 'chroma_agent.lib.shell.AgentShell.try_run', 'AgentShell.try_run', (["['sync']"], {}), "(['sync'])\n", (471, 481), False, 'from chroma_agent.lib.shell import AgentShell\n'), ((486, 514), 'chroma_agent.lib.shell.AgentShell.try_run', 'AgentShell.try_run', (["['sync']"], {}), "(['sync'])\n", (504, 514), False, 'from chroma_agent.lib.shell import AgentShell\n'), ((519, 557), 'chroma_agent.lib.shell.AgentShell.try_run', 'AgentShell.try_run', (["['init', runlevel]"], {}), "(['init', runlevel])\n", (537, 557), False, 'from chroma_agent.lib.shell import AgentShell\n'), ((623, 640), 'chroma_agent.lib.pacemaker.PacemakerConfig', 'PacemakerConfig', ([], {}), '()\n', (638, 640), False, 'from chroma_agent.lib.pacemaker import PacemakerConfig\n'), ((758, 819), 'chroma_agent.log.console_log.info', 'console_log.info', (["('Rebooting %s per a STONITH request' % node)"], {}), "('Rebooting %s per a STONITH request' % node)\n", (774, 819), False, 'from chroma_agent.log import console_log\n'), ((1227, 1265), 'chroma_agent.device_plugins.action_runner.CallbackAfterResponse', 'CallbackAfterResponse', (['None', '_shutdown'], {}), '(None, _shutdown)\n', (1248, 1265), False, 'from chroma_agent.device_plugins.action_runner import CallbackAfterResponse\n'), ((1570, 1606), 'chroma_agent.device_plugins.action_runner.CallbackAfterResponse', 'CallbackAfterResponse', (['None', '_reboot'], {}), '(None, _reboot)\n', (1591, 1606), False, 'from chroma_agent.device_plugins.action_runner import CallbackAfterResponse\n'), ((939, 1005), 'chroma_agent.log.console_log.info', 'console_log.info', (['"""Initiating server shutdown per manager request"""'], {}), "('Initiating server shutdown per manager request')\n", (955, 1005), False, 'from chroma_agent.log import console_log\n'), ((1089, 1154), 'chroma_agent.lib.shell.AgentShell.try_run', 'AgentShell.try_run', (["['shutdown', '-H' if halt else '-h', at_time]"], {}), "(['shutdown', '-H' if halt else '-h', at_time])\n", (1107, 1154), False, 'from chroma_agent.lib.shell import AgentShell\n'), ((1164, 1195), 'chroma_agent.log.console_log.info', 'console_log.info', (['"""Terminating"""'], {}), "('Terminating')\n", (1180, 1195), False, 'from chroma_agent.log import console_log\n'), ((1204, 1215), 'os._exit', 'os._exit', (['(0)'], {}), '(0)\n', (1212, 1215), False, 'import os\n'), ((1329, 1393), 'chroma_agent.log.console_log.info', 'console_log.info', (['"""Initiating server reboot per manager request"""'], {}), "('Initiating server reboot per manager request')\n", (1345, 1393), False, 'from chroma_agent.log import console_log\n'), ((1450, 1497), 'chroma_agent.lib.shell.AgentShell.try_run', 'AgentShell.try_run', (["['shutdown', '-r', at_time]"], {}), "(['shutdown', '-r', at_time])\n", (1468, 1497), False, 'from chroma_agent.lib.shell import AgentShell\n'), ((1507, 1538), 'chroma_agent.log.console_log.info', 'console_log.info', (['"""Terminating"""'], {}), "('Terminating')\n", (1523, 1538), False, 'from chroma_agent.log import console_log\n'), ((1547, 1558), 'os._exit', 'os._exit', (['(0)'], {}), '(0)\n', (1555, 1558), False, 'import os\n')]