--vm added to detect if it is running on vm

This commit is contained in:
ahmadbilalkhalid 2019-07-04 13:09:29 +05:00
parent b9c994bb4c
commit 5518164c15
1 changed files with 7 additions and 4 deletions

11
main.py
View File

@ -18,12 +18,14 @@ from etcd3_wrapper import Etcd3Wrapper
from decouple import config
def get_vm_start_cmd(owner_dir, vm_uuid):
def get_vm_start_cmd(owner_dir, vm_uuid, vm=False):
vm_sock_flags = f"-qmp unix:{owner_dir}/.vm/{vm_uuid}-sock,server,nowait"
vm_start_command_flags = (
f"-boot c -net nic -net user -m 256 {vm_sock_flags} -daemonize"
)
vm_start_command = f"""qemu-system-x86_64 {owner_dir}/.vm/{vm_uuid}.raw {vm_start_command_flags}"""
if vm:
vm_start_command += " -display none"
return vm_start_command
@ -36,7 +38,7 @@ def get_qemu_mon(sock_file):
return m
def main(hostname):
def main(hostname, is_vm):
client = Etcd3Wrapper()
events = client.watch_prefix("/v1/vm/")
@ -109,7 +111,7 @@ def main(hostname):
else:
print("Starting VM")
subprocess.run(
get_vm_start_cmd(owner_dir, vm_uuid).split(" ")
get_vm_start_cmd(owner_dir, vm_uuid, is_vm).split(" ")
)
e.value["status"] = "RUNNING"
client.put(e.key, e.value, value_in_json=True)
@ -119,6 +121,7 @@ def main(hostname):
argparser = argparse.ArgumentParser()
argparser.add_argument("hostname", help="Name of this host. e.g /v1/host/1")
argparser.add_argument("--vm", type=bool, default=False)
args = argparser.parse_args()
main(args.hostname)
main(args.hostname, args.vm)