-
Notifications
You must be signed in to change notification settings - Fork 38
/
fslimit.py
206 lines (175 loc) · 6.66 KB
/
fslimit.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# KuberDock - is a platform that allows users to run applications using Docker
# container images and create SaaS / PaaS based on these applications.
# Copyright (C) 2017 Cloud Linux INC
#
# This file is part of KuberDock.
#
# KuberDock is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# KuberDock is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with KuberDock; if not, see <http://www.gnu.org/licenses/>.
"""
Usage: fslimit.py containers|storage [name=limit ...]
Examples:
fslimit.py containers a00750a46fbb3c5bd512c790031dca02ef87359ae3cb54f70bdb2a2f6e0f66a9=1g
fslimit.py storage /var/lib/kuberdock/storage/23/mydrive1=5g
fslimit.py storage mydrive1_23=3g
"""
import glob
import os
import re
import subprocess
import sys
OVERLAY = '/var/lib/docker/overlay'
PROJECTS = '/etc/projects'
PROJID = '/etc/projid'
PROJECT_PATTERN = re.compile(r'^(?P<id>\d+):(?P<path>.+)$')
PROJID_PATTERN = re.compile(r'^(?P<name>.+):(?P<id>\d+)$')
STORAGE = '/var/lib/kuberdock/storage'
def _containers():
containers = {}
target_path = os.path.join(OVERLAY, '*', 'upper')
for target in glob.glob(target_path):
container_path = os.path.dirname(target)
if not container_path.endswith('-init'):
container_name = os.path.basename(container_path)
containers[container_name] = container_path
return containers
def _exit(message, code, usage=False):
print message
if usage:
print __doc__
sys.exit(code)
def _fs():
mounts = {}
with open('/proc/mounts') as mounts_file:
for mount in mounts_file.readlines():
device, mount_point, file_system, _options = mount.split()[:4]
mounts[mount_point] = {
'device': device,
'mount_point': mount_point,
'file_system': file_system,
'options': _options.split(','),
}
return mounts
def _limits(parent):
limits = {}
for limit in sys.argv[2:]:
name, _, value = limit.partition('=')
# Use given names as paths if it represent an absolute path. Otherwise
# use it as subdir for the parent dir.
if name.startswith('/'):
path = name
else:
path = os.path.join(parent, name)
limits[name] = {'limit': value, 'path': path}
return limits
def _mount(path):
while not os.path.ismount(path):
path = os.path.dirname(path)
return path
def _storage():
storage = {}
target_path = os.path.join(STORAGE, '*')
for storage_path in glob.glob(target_path):
# we expect here directories like <STORAGE>/<user id>/<user storage>
if not os.path.isdir(storage_path):
continue
subdir = os.path.basename(storage_path)
try:
int(subdir)
except (TypeError, ValueError):
continue
user_storage_parent = os.path.join(storage_path, '*')
for lstorage in glob.glob(user_storage_parent):
storage[lstorage] = lstorage
return storage
FSLIMIT_TARGETS = {
'containers': (OVERLAY, _containers),
'storage': (STORAGE, _storage,),
}
def _target():
"""Returns tuple of Parent directory, method for getting subdirs and
flag which determine the way of working with paths - use absolute
"""
if len(sys.argv) < 2:
_exit('No target specified', 3, usage=True)
target = sys.argv[1]
if target not in FSLIMIT_TARGETS:
_exit('Unknown target', 4, usage=True)
return target
def check_xfs(fs):
fs_type = fs['file_system']
if fs_type != 'xfs':
_exit('Only XFS supported as backing filesystem', 1)
def check_prjquota(fs):
if 'prjquota' not in fs['options']:
_exit('Enable project quota for {0}'.format(fs['device']), 2)
def fslimit(fs, parent, dirs, abspath=False):
delete = set()
max_id = 0
projects = set()
projects_lines = []
projid_lines = []
if os.path.exists(PROJECTS) and os.path.exists(PROJID):
with open(PROJECTS) as projects_file:
for project in projects_file.read().splitlines():
project_match = PROJECT_PATTERN.match(project)
if project_match:
project_dict = project_match.groupdict()
id_ = int(project_dict['id'])
path = project_dict['path']
if path.startswith(parent):
if abspath:
name = path
else:
name = os.path.basename(path)
if name not in dirs:
delete.add(id_)
continue
projects.add(name)
max_id = max([max_id, id_])
projects_lines.append(project)
with open(PROJID) as projid_file:
for projid in projid_file.read().splitlines():
projid_match = PROJID_PATTERN.match(projid)
if projid_match:
projid_dict = projid_match.groupdict()
id_ = int(projid_dict['id'])
if id_ in delete:
continue
max_id = max([max_id, id_])
projid_lines.append(projid)
new = _limits(parent)
for name, data in new.iteritems():
if name not in projects:
max_id += 1
projects_lines.append('{0}:{1}'.format(max_id, data['path']))
projid_lines.append('{0}:{1}'.format(name, max_id))
with open(PROJECTS, 'w') as projects_file:
projects_file.writelines(l + os.linesep for l in projects_lines)
with open(PROJID, 'w') as projid_file:
projid_file.writelines(l + os.linesep for l in projid_lines)
for name, data in new.items():
project = 'project -s {0}'.format(name)
limit = 'limit -p bsoft={0} bhard={0} {1}'.format(data['limit'], name)
for c in project, limit:
subprocess.call(['xfs_quota', '-x', '-c', c, fs['mount_point']])
if __name__ == '__main__':
target_ = _target()
parent_, get_dirs = FSLIMIT_TARGETS[target_]
use_abspath = False
if target_ == 'storage':
use_abspath = True
fs_ = _fs()[_mount(parent_)]
check_xfs(fs_)
check_prjquota(fs_)
fslimit(fs_, parent_, get_dirs(), abspath=use_abspath)