diff --git a/config.py b/config.py index 3308020..603501c 100644 --- a/config.py +++ b/config.py @@ -1,6 +1,17 @@ +import logging + from etcd3_wrapper import Etcd3Wrapper from decouple import config +logging.basicConfig( + level=logging.DEBUG, + filename="log.txt", + filemode="a", + format="%(asctime)s: %(levelname)s - %(message)s", + datefmt="%d-%b-%y %H:%M:%S", +) + + WITHOUT_CEPH = config("WITHOUT_CEPH", False, cast=bool) etcd_client = Etcd3Wrapper(host=config("ETCD_URL")) diff --git a/log.txt b/log.txt new file mode 100644 index 0000000..69a30f5 --- /dev/null +++ b/log.txt @@ -0,0 +1,22 @@ +05-Sep-19 19:13:22: WARNING - * Debugger is active! +05-Sep-19 19:13:22: INFO - * Debugger PIN: 189-665-873 +05-Sep-19 19:13:49: INFO - * Detected change in '/home/meow/Desktop/code/ucloud-api/main.py', reloading +05-Sep-19 19:13:50: WARNING - * Debugger is active! +05-Sep-19 19:13:50: INFO - * Debugger PIN: 189-665-873 +05-Sep-19 19:13:57: INFO - * Detected change in '/home/meow/Desktop/code/ucloud-api/main.py', reloading +05-Sep-19 19:13:58: WARNING - * Debugger is active! +05-Sep-19 19:13:58: INFO - * Debugger PIN: 189-665-873 +05-Sep-19 19:15:12: INFO - * Detected change in '/home/meow/Desktop/code/ucloud-api/main.py', reloading +05-Sep-19 19:19:39: INFO - * Running on http://[::]:5000/ (Press CTRL+C to quit) +05-Sep-19 19:19:39: INFO - * Restarting with stat +05-Sep-19 19:19:40: WARNING - * Debugger is active! +05-Sep-19 19:19:40: INFO - * Debugger PIN: 189-665-873 +05-Sep-19 19:19:43: DEBUG - Starting new HTTPS connection (1): otp.ungleich.ch:443 +05-Sep-19 19:19:48: DEBUG - https://otp.ungleich.ch:443 "POST /ungleichotp/verify/ HTTP/1.1" 200 15 +05-Sep-19 19:19:48: INFO - ::1 - - [05/Sep/2019 19:19:48] "POST /vm/action HTTP/1.1" 200 - +05-Sep-19 19:19:53: DEBUG - Starting new HTTPS connection (1): otp.ungleich.ch:443 +05-Sep-19 19:19:58: DEBUG - https://otp.ungleich.ch:443 "POST /ungleichotp/verify/ HTTP/1.1" 200 15 +05-Sep-19 19:19:58: INFO - ::1 - - [05/Sep/2019 19:19:58] "GET /user/vms HTTP/1.1" 200 - +05-Sep-19 19:20:26: DEBUG - Starting new HTTPS connection (1): otp.ungleich.ch:443 +05-Sep-19 19:20:32: DEBUG - https://otp.ungleich.ch:443 "POST /ungleichotp/verify/ HTTP/1.1" 200 15 +05-Sep-19 19:20:32: INFO - ::1 - - [05/Sep/2019 19:20:32] "POST /vm/action HTTP/1.1" 200 - diff --git a/main.py b/main.py index 8d97b40..7c2a61d 100755 --- a/main.py +++ b/main.py @@ -11,7 +11,7 @@ from flask_restful import Resource, Api from uuid import uuid4 from os.path import join from config import etcd_client as client -from config import WITHOUT_CEPH +from config import WITHOUT_CEPH, logging from ucloud_common.vm import VmPool, VMStatus from ucloud_common.host import HostPool @@ -131,9 +131,14 @@ class VMAction(Resource): else: command_to_delete = ["rbd", "rm", path_without_protocol] - subprocess.check_output(command_to_delete) - except Exception as e: - return {"message": "Some error occurred while deleting VM"} + subprocess.check_output(command_to_delete, stderr=subprocess.PIPE) + except subprocess.CalledProcessError as e: + if "No such file" in e.stderr.decode("utf-8"): + client.client.delete(vm_entry.key) + return {"message": "VM successfully deleted"} + else: + logging.exception(e) + return {"message": "Some error occurred while deleting VM"} else: client.client.delete(vm_entry.key) return {"message": "VM successfully deleted"}