From ee114bcf7b13bc9abc438bec9299588dc2eff3b7 Mon Sep 17 00:00:00 2001 From: Ahmad Bilal Khalid Date: Thu, 4 Jul 2019 10:19:40 +0500 Subject: [PATCH] take hostname from command line --- main.py | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/main.py b/main.py index 1aaa914..05e4bc3 100644 --- a/main.py +++ b/main.py @@ -1,15 +1,17 @@ # TODO -# Implement Monitoring of VM +# 1. Verify that commands successfully did what they are +# supposed to do by querying state of VM using QMP. +# 2. Implement Monitoring of VM. + + +# For QEMU Monitor Protocol Commands Information, See +# https://qemu.weilnetz.de/doc/qemu-doc.html#pcsys_005fmonitor import json import shutil import os import subprocess - -# For Commands Information -# https://qemu.weilnetz.de/doc/qemu-doc.html#pcsys_005fmonitor - - +import argparse import qmp from etcd3_wrapper import Etcd3Wrapper @@ -18,7 +20,9 @@ from decouple import config def get_vm_start_cmd(owner_dir, vm_uuid): 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_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}""" return vm_start_command @@ -32,9 +36,8 @@ def get_qemu_mon(sock_file): return m -def main(): +def main(hostname): client = Etcd3Wrapper() - hostname = config("HOST_NAME") events = client.watch_prefix("/v1/vm/") @@ -105,11 +108,17 @@ def main(): client.put(e.key, e.value, value_in_json=True) else: print("Starting VM") - subprocess.run(get_vm_start_cmd(owner_dir, vm_uuid).split(" ")) + subprocess.run( + get_vm_start_cmd(owner_dir, vm_uuid).split(" ") + ) e.value["status"] = "RUNNING" client.put(e.key, e.value, value_in_json=True) else: continue -main() +argparser = argparse.ArgumentParser() +argparser.add_argument("hostname", help="Name of this host. e.g /v1/host/1") +args = argparser.parse_args() + +main(args.hostname)