Merge branch 'without-ceph' into 'master'

allow ucloud-api to also be able work without ceph i.e use filesystem

See merge request ungleich-public/ucloud-api!2
This commit is contained in:
Ahmed Bilal 2019-09-07 12:38:58 +02:00
commit 3f406799db
4 changed files with 31 additions and 5 deletions

1
.gitignore vendored
View file

@ -5,3 +5,4 @@ venv/
settings.json settings.json
ucloud_common ucloud_common
etcd3_wrapper etcd3_wrapper
log.txt

View file

@ -1,4 +1,17 @@
import logging
from etcd3_wrapper import Etcd3Wrapper from etcd3_wrapper import Etcd3Wrapper
from decouple import config 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")) etcd_client = Etcd3Wrapper(host=config("ETCD_URL"))

18
main.py
View file

@ -4,12 +4,14 @@
import json import json
import subprocess import subprocess
import os
from flask import Flask, request from flask import Flask, request
from flask_restful import Resource, Api from flask_restful import Resource, Api
from uuid import uuid4 from uuid import uuid4
from os.path import join from os.path import join
from config import etcd_client as client from config import etcd_client as client
from config import WITHOUT_CEPH, logging
from ucloud_common.vm import VmPool, VMStatus from ucloud_common.vm import VmPool, VMStatus
from ucloud_common.host import HostPool from ucloud_common.host import HostPool
@ -123,15 +125,23 @@ class VMAction(Resource):
if action == "delete" and vm_entry.hostname == "": if action == "delete" and vm_entry.hostname == "":
try: try:
path_without_protocol = vm_entry.path[vm_entry.path.find(":")+1:] path_without_protocol = vm_entry.path[vm_entry.path.find(":")+1:]
rc = subprocess.call(f"rbd rm {path_without_protocol}".split(" "))
except FileNotFoundError: if WITHOUT_CEPH:
return {"message": "VM image does not exists"} command_to_delete = ["rm", os.path.join("/var/vm", vm_entry.uuid)]
else: else:
if rc == 0: command_to_delete = ["rbd", "rm", path_without_protocol]
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) client.client.delete(vm_entry.key)
return {"message": "VM successfully deleted"} return {"message": "VM successfully deleted"}
else: else:
logging.exception(e)
return {"message": "Some error occurred while deleting VM"} 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", r = RequestEntry.from_scratch(type=f"{action.title()}VM",
uuid=data['uuid'], uuid=data['uuid'],

View file

@ -99,8 +99,10 @@ class CreateImageSchema(BaseSchema):
self.uuid: Field = Field("uuid", str, data.get("uuid", KeyError)) self.uuid: Field = Field("uuid", str, data.get("uuid", KeyError))
self.name = Field("name", str, data.get("name", KeyError)) self.name = Field("name", str, data.get("name", KeyError))
self.image_store = Field("image_store", str, data.get("image_store", KeyError)) self.image_store = Field("image_store", str, data.get("image_store", KeyError))
# Validations # Validations
self.uuid.validation = self.file_uuid_validation self.uuid.validation = self.file_uuid_validation
self.image_store.validation = self.image_store_name_validation
# All Fields # All Fields
fields = [self.uuid, self.name, self.image_store] fields = [self.uuid, self.name, self.image_store]