take hostname from command line

This commit is contained in:
ahmadbilalkhalid 2019-07-04 10:19:40 +05:00
parent f479b961b0
commit ee114bcf7b
1 changed files with 20 additions and 11 deletions

31
main.py
View File

@ -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)