#!{{ parameter_dict.get('python-path') }}
# BEWARE: This file is operated by slapos node
# BEWARE: It will be overwritten automatically

# Echo client program
import socket
import time
import os
from slapos.qemuqmpclient import QemuQMPWrapper, getInitialQemuResourceDict

# XXX: to be factored with slapos.toolbox qemu qmp wrapper.

socket_path = '{{ parameter_dict.get("socket-path") }}'
pid_file = '{{ parameter_dict.get("pid-file") }}'
vnc_password = '{{ parameter_dict.get("vnc-passwd") }}'
status_path = '{{ parameter_dict.get("kvm-status-path") }}'
cpu_amount = {{ parameter_dict.get("cpu-count") }}
cpu_model = '{{ parameter_dict.get("cpu-model", "qemu64") }}'
slot_hotplug_size = {{ parameter_dict.get("ram-hotplug-slot-size", 512) }}
ram_size = {{ parameter_dict.get("ram-size") }}
enable_device_hotplug = '{{ parameter_dict.get("enable-device-hotplug") }}'.lower()

def write(message):
  with open(status_path, 'w') as status_file:
    status_file.write(message)

def update():

  try:
    init_dict = getInitialQemuResourceDict(pid_file)
    if os.path.exists(status_path):
      os.unlink(status_path)

    if init_dict is None:
      # qemu process is not OK
      return
    init_ram_size = int(init_dict['ram'].split('M')[0])
    if cpu_amount < 1:
      raise ValueError("CPU should be at least equal to 1.")
    hotplug_ram = ram_size - init_ram_size
    if hotplug_ram < 0:
      raise ValueError("RAM size cannot be less than the initial value %s MB" % init_ram_size)

    qemu_wrapper = QemuQMPWrapper(socket_path)
    qemu_wrapper.setVNCPassword(vnc_password)
    if enable_device_hotplug == 'true':
      write("Qemu Controller is still running...")
      qemu_wrapper.updateDevice({
        'device': 'cpu',
        'amount': cpu_amount,
        'model': "%s-x86_64-cpu" % cpu_model
      })
      qemu_wrapper.updateDevice({
        'device': 'memory',
        'mem': hotplug_ram,
        'slot': slot_hotplug_size,
        'nslot': 128,
        'canreboot': 1
      })
  except Exception as e:
    write(str(e))
    raise

  write("")

if __name__ == "__main__":
  update()
