#!/usr/bin/python

import subprocess32 as subprocess
import sys
import threading
import prlsdkapi

def log(mess):
    if not mess:
        return
    with open("/dev/kmsg", "w") as f:
        f.write("prl_disp_autosuspend: " + mess)
        f.close()

def error(mess):
    log(mess)
    sys.exit(1)

def prepare_to_reboot(timeout = 60 * 1000):
    threads = []
    # Connect to dispatcher
    prlsdkapi.init_server_sdk()
    server = prlsdkapi.Server()
    server.login_local().wait(msecs = timeout)

    # Create file with list of auto autostart
    noautostart = "units_skip_start = ("

    # Get VMs list
    vms = server.get_vm_list_ex(prlsdkapi.consts.PVTF_VM + prlsdkapi.consts.PVTF_CT)
    for res in [ r for r in vms.wait(msecs = timeout) ]:
        # Place to list only if it was not in running state
        if (res.get_config().get_auto_start() == prlsdkapi.consts.PAO_VM_START_ON_RELOAD and
                res.get_vm_info().get_state() != prlsdkapi.consts.VMS_RUNNING):
            noautostart += "\"%s\", " % res.get_config().get_uuid()

        # Suspend VMs
        if (res.get_config().get_vm_type() != prlsdkapi.consts.PVT_VM or
                res.get_config().get_auto_stop() != prlsdkapi.consts.PAO_VM_SUSPEND or
                res.get_vm_info().get_state() in
                (prlsdkapi.consts.VMS_STOPPED, prlsdkapi.consts.VMS_STOPPING,
                prlsdkapi.consts.VMS_SUSPENDING, prlsdkapi.consts.VMS_SUSPENDED)):
            continue
        thread = threading.Thread(target=res.suspend().wait)
        thread.start()
        threads.append(thread)

    with open("/var/spool/prl-disp/prl_disp_noautostart.py", "w") as f:
        f.write(noautostart + ")\n")
        f.close()

    # Wait for VMs suspended
    for thread in threads:
        thread.join()

    # Disconnect
    server.logoff().wait(msecs = timeout)
    prlsdkapi.deinit_sdk()

# Detect that we're going to reboot/shutdown
proc = subprocess.Popen("/usr/bin/systemctl -q --no-legend list-jobs",
        stderr=None, stdout=subprocess.PIPE,
        stdin=subprocess.PIPE, shell = True)
(output, err) = proc.communicate()

for line in output.splitlines():
    try:
        if not line.split(" ")[1] in ("reboot.target", "shutdown.target"):
            continue
        try:
            prepare_to_reboot()
            break
        except Exception, err:
            error(repr(err))
    except IndexError:
        pass
