Store metadata file

This commit is contained in:
Mehran Kholdi 2020-04-24 20:08:18 +04:30
parent 373c43fa0e
commit 4ec6acf731
3 changed files with 51 additions and 14 deletions

View File

@ -1,13 +1,11 @@
import grpc
from pathlib import Path
from google.protobuf.wrappers_pb2 import BoolValue
from consts import DATA_DIR
import rawfile_util
from csi import csi_pb2, csi_pb2_grpc
from orchestrator.k8s import volume_to_node, run_on_node
from util import log_grpc_request, run
from remote import init_rawfile, scrub
from util import log_grpc_request, run
NODE_NAME_TOPOLOGY_KEY = "hostname"
@ -49,9 +47,7 @@ class RawFileNodeServicer(csi_pb2_grpc.NodeServicer):
@log_grpc_request
def NodePublishVolume(self, request, context):
mount_path = request.target_path
img_dir = Path(f"{DATA_DIR}/{request.volume_id}")
img_file = Path(f"{img_dir}/raw.img")
img_file = rawfile_util.img_file(request.volume_id)
run(f"mount {img_file} {mount_path}")
return csi_pb2.NodePublishVolumeResponse()
@ -112,7 +108,7 @@ class RawFileControllerServicer(csi_pb2_grpc.ControllerServicer):
)
size = request.capacity_range.required_bytes
size = min(size, 10 * 1024 * 1024) # At least 10MB
size = max(size, 10 * 1024 * 1024) # At least 10MB
try:
node_name = request.accessibility_requirements.preferred[0].segments[

30
rawfile_util.py Normal file
View File

@ -0,0 +1,30 @@
import json
from pathlib import Path
from consts import DATA_DIR
def img_dir(volume_id):
return Path(f"{DATA_DIR}/{volume_id}")
def meta_file(volume_id):
return Path(f"{img_dir(volume_id)}/disk.meta")
def metadata(volume_id):
try:
return json.loads(meta_file(volume_id).read_text())
except FileNotFoundError:
return {}
def img_file(volume_id):
return Path(metadata(volume_id)["img_file"])
def patch_metadata(volume_id, obj):
old_data = metadata(volume_id)
new_data = {**old_data, **obj}
meta_file(volume_id).write_text(json.dumps(new_data))
return new_data

View File

@ -3,18 +3,29 @@ from util import remote_fn
@remote_fn
def scrub(volume_id):
# TODO: stub
pass
import time
import rawfile_util
rawfile_util.patch_metadata(volume_id, {"deleted_at": time.time()})
@remote_fn
def init_rawfile(volume_id, size):
import time
import rawfile_util
from util import run
from consts import DATA_DIR
from pathlib import Path
img_dir = Path(f"{DATA_DIR}/{volume_id}")
img_dir = rawfile_util.img_dir(volume_id)
img_dir.mkdir(parents=False, exist_ok=False)
img_file = Path(f"{img_dir}/raw.img")
img_file = f"{img_dir}/disk.img"
rawfile_util.patch_metadata(
volume_id,
{
"volume_id": volume_id,
"created_at": time.time(),
"img_file": img_file,
"size": size,
},
)
run(f"truncate -s {size} {img_file}")
run(f"mkfs.ext4 {img_file}")