forked from bn222/cluster-deployment-automation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nfs.py
58 lines (48 loc) · 1.72 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
import host
import os
import common
from logger import logger
from typing import Optional
import sys
import threading
import time
"""
NFS is needed in many cases to network mount the folder that contains
ISO files such that Red Fish Virtual Media managers can load the image.
"""
_lock = threading.Lock()
class NFS:
def __init__(self, host: host.Host, port: str):
self._host = host
self._port = port
pass
def host_file(self, file: str) -> str:
with _lock:
dir_name = os.path.dirname(file)
if not self._exists(dir_name):
self._add(dir_name)
self._export_fs()
ip = self._ip()
if ip is None:
logger.error(f"Failed to get ip when hosting file {file} on nfs")
sys.exit(-1)
ret = f"{self._ip()}:{file}"
return ret
def _exists(self, dir_name: str) -> bool:
exports = self._host.read_file("/etc/exports")
return any(dir_name in x.split(" ")[0] for x in exports.split("\n"))
def _add(self, dir_name: str) -> None:
contents = self._host.read_file("/etc/exports")
self._host.write("/etc/exports", f"{contents}\n{dir_name}")
def _export_fs(self) -> None:
self._host.run("systemctl enable nfs-server")
started_at = time.monotonic()
while True:
ret = self._host.run("systemctl restart nfs-server")
if ret.success():
break
if time.monotonic() > started_at + 60:
logger.error_and_exit(f"failed to `systemctl restart nfs-server`: {ret}")
time.sleep(1)
def _ip(self) -> Optional[str]:
return common.port_to_ip(self._host, self._port)