-
Notifications
You must be signed in to change notification settings - Fork 0
/
nfs.py
84 lines (68 loc) · 2.76 KB
/
nfs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import logging
import re
import subprocess
import kubernetes
import config
from config import Constants
import util
logger = logging.getLogger(__name__)
def get_exported_filesystems():
cur_node = config.get().current_node_ip
logger.debug(f"Listing mounts on {cur_node}")
mounts = util.run_process("showmount", "--no-headers", "-e", cur_node)
result = []
for line in mounts:
found = re.findall(r"(.*?)[^\S]+(\d.*)", line)
if len(found) == 1:
mount, clients = found[0]
split_clients = clients.split(",")
result.extend([(mount, client) for client in split_clients])
else:
raise RuntimeError(f"failed to parse mount: {line}")
logger.debug(f"Got mounts: {result}")
return result
def export_share(mount, client):
if (mount, client) in get_exported_filesystems():
return # share already mounted
logger.info(f"Exporting fs '{mount}'")
try:
util.run_process("exportfs", "-o", Constants.NFS_MOUNT_FLAGS, f"{client}:{mount}")
except subprocess.CalledProcessError as ex:
# exportfs doesn't like us running from inside a container
# check after we exported it to see if to happened or not and then error out then
if ex.returncode == 1 and (mount, client) not in get_exported_filesystems():
raise ex
def un_export_share(mount, client):
if (mount, client) not in get_exported_filesystems():
return # share already unmounted
logger.info(f"Unmounting fs '{mount}'")
try:
util.run_process("exportfs", "-u", f"{client}:{mount}")
except subprocess.CalledProcessError as ex:
# exportfs doesn't like us running from inside a container
# check after we exported it to see if to happened or not and then error out then
if ex.returncode == 1 and (mount, client) in get_exported_filesystems():
raise ex
def create_persistent_volume(pv_name, node_name, access_modes, desired_capacity, nfs_server, volume_path, sc_name, volume_mode):
pv = {
"accessModes": access_modes,
"capacity": {"storage": desired_capacity},
# "mount_options": None,
"nfs": {
"path": volume_path,
"readOnly": False,
"server": nfs_server
},
"storageClassName": sc_name,
"volumeMode": volume_mode,
}
body = kubernetes.client.V1PersistentVolume(api_version='v1', spec=pv,
metadata=kubernetes.client.V1ObjectMeta(
name=pv_name,
labels={"app": "storage", "component": "lab-disk"},
annotations={Constants.PV_ASSIGNED_NODE_ANNOTATION_KEY: node_name}
),
kind="PersistentVolume"
)
core_api = kubernetes.client.CoreV1Api()
core_api.create_persistent_volume(body)