From fefee23a5a21db39bc4270bc9fbb3a5c5b52d4a7 Mon Sep 17 00:00:00 2001 From: Ahmed Bilal Khalid Date: Wed, 4 Sep 2019 18:01:55 +0500 Subject: [PATCH] a --- config.py | 2 ++ main.py | 21 +++++++++++++-------- schemas.py | 2 ++ 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/config.py b/config.py index 833dc0d..a292eb5 100644 --- a/config.py +++ b/config.py @@ -1,4 +1,6 @@ from etcd3_wrapper import Etcd3Wrapper from decouple import config +WITHOUT_CEPH = config("WITHOUT_CEPH", False) + etcd_client = Etcd3Wrapper(host=config("ETCD_URL")) diff --git a/main.py b/main.py index 4cc2e9f..8d97b40 100755 --- a/main.py +++ b/main.py @@ -4,12 +4,14 @@ import json import subprocess +import os from flask import Flask, request 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 ucloud_common.vm import VmPool, VMStatus from ucloud_common.host import HostPool @@ -123,15 +125,18 @@ class VMAction(Resource): if action == "delete" and vm_entry.hostname == "": try: path_without_protocol = vm_entry.path[vm_entry.path.find(":")+1:] - rc = subprocess.call(f"rbd rm {path_without_protocol}".split(" ")) - except FileNotFoundError: - return {"message": "VM image does not exists"} - else: - if rc == 0: - client.client.delete(vm_entry.key) - return {"message": "VM successfully deleted"} + + if WITHOUT_CEPH: + command_to_delete = ["rm", os.path.join("/var/vm", vm_entry.uuid)] else: - return {"message": "Some error occurred while deleting VM"} + 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"} + else: + client.client.delete(vm_entry.key) + return {"message": "VM successfully deleted"} r = RequestEntry.from_scratch(type=f"{action.title()}VM", uuid=data['uuid'], diff --git a/schemas.py b/schemas.py index 2e46b11..90acd50 100755 --- a/schemas.py +++ b/schemas.py @@ -99,8 +99,10 @@ class CreateImageSchema(BaseSchema): self.uuid: Field = Field("uuid", str, data.get("uuid", KeyError)) self.name = Field("name", str, data.get("name", KeyError)) self.image_store = Field("image_store", str, data.get("image_store", KeyError)) + # Validations self.uuid.validation = self.file_uuid_validation + self.image_store.validation = self.image_store_name_validation # All Fields fields = [self.uuid, self.name, self.image_store]