-
Notifications
You must be signed in to change notification settings - Fork 832
/
views.py
129 lines (105 loc) · 3.57 KB
/
views.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
"""
Module to handle the fstab file with the help of the reconfigure module.
"""
from jadi import component
import subprocess
import os
from aj.auth import authorize
from aj.api.http import get, post, HttpPlugin
from aj.api.endpoint import endpoint, EndpointError
from reconfigure.configs import FSTabConfig
from reconfigure.items.fstab import FilesystemData
@component(HttpPlugin)
class Handler(HttpPlugin):
def __init__(self, context):
self.context = context
@get(r'/api/fstab/mounts')
@authorize('fstab:mounts:read')
@endpoint(api=True)
def handle_api_mounted(self, http_context):
"""
Parse the output of the df command to generate a dict of mount devices.
:param http_context: HttpContext
:type http_context: HttpContext
:return: Details of mounted devices
:rtype: dict
"""
filesystems = []
mounted = subprocess.check_output(['df', '-PT']).decode('utf-8')
for entry in mounted.splitlines()[1:]:
entry = entry.split()
device = entry[0]
fstype = entry[1]
mountpoint = entry[6] # Problem with mac os ?
used = int(entry[3]) * 1024
size = int(entry[2]) * 1024
usage = used / size
filesystems.append({
'device':device,
'mountpoint': mountpoint,
'used': used,
'size': size,
'usage': usage,
'fstype': fstype,
})
return filesystems
@post(r'/api/fstab/command_umount')
@authorize('fstab:mounts:umount')
@endpoint(api=True)
def handle_api_umount(self, http_context):
"""
Umount some device.
:param http_context: HttpContext
:type http_context: HttpContext
:return: Success or not
:rtype: bool or throw error
"""
mountpoint = http_context.json_body()['mountpoint']
try:
subprocess.check_output(['umount', mountpoint])
return True
except Exception as e:
raise EndpointError(e)
@get(r'/api/fstab')
@authorize('fstab:read')
@endpoint(api=True)
def handle_api_get_fstab(self, http_context):
"""
Load the fstab file.
:param http_context: HttpContext
:type http_context: HttpContext
:return: Fstab as dict
:rtype: dict in load mode
"""
self.fstab_config = FSTabConfig(path='/etc/fstab')
self.fstab_config.load()
return self.fstab_config.tree.to_dict()
@post(r'/api/fstab')
@authorize('fstab:mounts:write')
@endpoint(api=True)
def handle_api_set_fstab(self, http_context):
"""
Write the fstab file.
Make a backup when save a new fstab file.
:param http_context: HttpContext
:type http_context: HttpContext
:return: Success or not
:rtype: bool or throw error in save mode
"""
config = http_context.json_body()['config']
new_fstab = FSTabConfig(content='')
new_fstab.load()
for filesystem in config:
device = FilesystemData()
for prop, value in filesystem.items():
setattr(device, prop, value)
new_fstab.tree.filesystems.append(device)
data = new_fstab.save()[None]
# Always make a backup
os.rename('/etc/fstab', '/etc/fstab.bak')
try:
with open('/etc/fstab', 'w') as f:
f.write(data)
return True
except Exception as e:
raise EndpointError(e)