New Features + Refactoring
1. User can now use image name instead of image uuid when creation vm.
For Example, now user can create an alpine vm using the following
command
```shell
ucloud-cli vm create --vm-name myvm --cpu 2 --ram '2GB' \
--os-ssd '10GB' --image images:alpine
```
2. Instead of directly running code, code is now placed under a function
main and is called using the following code
```python
if __name__ == "__main__":
main()
```
3. Multiprocess (Process) is used instead of threading (Thread) to update
heart beat of host.
4. IP Address of vm is included in vm's status which is retrieved by the
following command
```shell
ucloud-cli vm status --vm-name myvm
```
This commit is contained in:
parent
da77ac65eb
commit
93dee1c9fc
13 changed files with 354 additions and 233 deletions
|
|
@ -16,7 +16,10 @@ logging.basicConfig(
|
|||
datefmt="%d-%b-%y %H:%M:%S",
|
||||
)
|
||||
|
||||
etcd_client = Etcd3Wrapper(host=config("ETCD_URL"))
|
||||
etcd_wrapper_args = ()
|
||||
etcd_wrapper_kwargs = {"host": config("ETCD_URL")}
|
||||
|
||||
etcd_client = Etcd3Wrapper(*etcd_wrapper_args, **etcd_wrapper_kwargs)
|
||||
|
||||
HOST_PREFIX = config("HOST_PREFIX")
|
||||
VM_PREFIX = config("VM_PREFIX")
|
||||
|
|
|
|||
55
host/main.py
55
host/main.py
|
|
@ -1,27 +1,32 @@
|
|||
import argparse
|
||||
import threading
|
||||
# import threading
|
||||
import time
|
||||
import os
|
||||
import sys
|
||||
import virtualmachine
|
||||
import multiprocessing as mp
|
||||
|
||||
from ucloud_common.host import HostEntry
|
||||
from ucloud_common.request import RequestEntry, RequestType
|
||||
|
||||
from config import (vm_pool, host_pool, request_pool,
|
||||
etcd_client, logging, running_vms,
|
||||
REQUEST_PREFIX, WITHOUT_CEPH)
|
||||
etcd_wrapper_args, etcd_wrapper_kwargs,
|
||||
REQUEST_PREFIX, HOST_PREFIX,
|
||||
WITHOUT_CEPH, VM_DIR, HostPool)
|
||||
from etcd3_wrapper import Etcd3Wrapper
|
||||
import etcd3
|
||||
|
||||
def update_heartbeat(host):
|
||||
client = Etcd3Wrapper(*etcd_wrapper_args, **etcd_wrapper_kwargs)
|
||||
host_pool = HostPool(client, HOST_PREFIX)
|
||||
this_host = host_pool.get(host)
|
||||
|
||||
def update_heartbeat(host: HostEntry):
|
||||
while True:
|
||||
host.update_heartbeat()
|
||||
host_pool.put(host)
|
||||
this_host.update_heartbeat()
|
||||
host_pool.put(this_host)
|
||||
time.sleep(10)
|
||||
|
||||
logging.info("Updated last heartbeat time %s", host.last_heartbeat)
|
||||
|
||||
|
||||
def maintenance(host):
|
||||
# To capture vm running according to running_vms list
|
||||
|
||||
|
|
@ -66,16 +71,25 @@ def main():
|
|||
argparser.add_argument("hostname", help="Name of this host. e.g /v1/host/1")
|
||||
args = argparser.parse_args()
|
||||
|
||||
assert WITHOUT_CEPH and os.path.isdir(VM_DIR), (
|
||||
"You have set WITHOUT_CEPH to True. So, the vm directory mentioned"
|
||||
" in .env file must exists. But, it don't." )
|
||||
|
||||
mp.set_start_method('spawn')
|
||||
heartbeat_updating_process = mp.Process(target=update_heartbeat, args=(args.hostname,))
|
||||
|
||||
host_pool = HostPool(etcd_client, HOST_PREFIX)
|
||||
host = host_pool.get(args.hostname)
|
||||
if not host:
|
||||
print("No Such Host")
|
||||
exit(1)
|
||||
|
||||
if WITHOUT_CEPH and not os.path.isdir("/var/vm"):
|
||||
print("You have set WITHOUT_CEPH to True. So, the /var/vm must exists. But, it don't")
|
||||
sys.exit(1)
|
||||
assert host, "No such host"
|
||||
|
||||
try:
|
||||
heartbeat_updating_process.start()
|
||||
except Exception as e:
|
||||
logging.info("No Need To Go Further. Our heartbeat updating mechanism is not working")
|
||||
logging.exception(e)
|
||||
exit(-1)
|
||||
|
||||
|
||||
logging.info("%s Session Started %s", '*' * 5, '*' * 5)
|
||||
|
||||
# It is seen that under heavy load, timeout event doesn't come
|
||||
|
|
@ -86,13 +100,6 @@ def main():
|
|||
# update the heart beat in a predictive manner we start Heart
|
||||
# beat updating mechanism in separated thread
|
||||
|
||||
heartbeat_updating_thread = threading.Thread(target=update_heartbeat, args=(host,))
|
||||
try:
|
||||
heartbeat_updating_thread.start()
|
||||
except Exception as e:
|
||||
logging.info("No Need To Go Further. Our heartbeat updating mechanism is not working")
|
||||
logging.exception(e)
|
||||
exit(-1)
|
||||
|
||||
for events_iterator in [
|
||||
etcd_client.get_prefix(REQUEST_PREFIX, value_in_json=True),
|
||||
|
|
@ -134,4 +141,6 @@ def main():
|
|||
logging.info("Running VMs %s", running_vms)
|
||||
|
||||
|
||||
main()
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue