/
zettaknight_ldap.py
335 lines (262 loc) · 11.2 KB
/
zettaknight_ldap.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Copyright (c) 2015-2016 Matthew Carter, Ralph M Goodberlet.
#
# This file is part of Zettaknight.
#
# Zettaknight 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 3 of the License, or
# (at your option) any later version.
#
# Zettaknight 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 Zettaknight. If not, see <http://www.gnu.org/licenses/>.
#
# Import python libs
import inspect
import re
import sys
#from zettaknight import *
import zettaknight_utils
import zettaknight_globs
import paramiko
def create_cifs_share(**kwargs):
'''
Creates a zfs dataset for a given user or group.
'''
ret = {}
if zettaknight_globs.help_flag:
ret = """Create CIFS Share:
Function to create a new directory with proper permissions for user/group access over CIFS on primary and secondary ZFS servers. Arguments should be passed as key/value pairs.
Required Arguments:
user or group
Specifies the username or group name to create the share for.
Optional Arguments:
dataset
Specifies the parent dataset to create the share under. If not specified, the default will be pulled from Zettaknight conf files"""
return ret
user = False
group = False
if kwargs:
try:
if kwargs['dataset']:
dataset = kwargs['dataset']
except:
dataset = "{0}/{1}".format(zettaknight_globs.pool_name, zettaknight_globs.cifs_dset_suffix)
pass
if kwargs and "user" in kwargs.iterkeys():
user = kwargs['user']
if kwargs and "group" in kwargs.iterkeys():
group = kwargs['group']
ret[dataset] = {}
if user and group:
try:
raise Exception("Arguments user and group must be specified separately. Arguments passed: {0}".format(kwargs))
except Exception as e:
ret[dataset]['Create CIFS Share'] = {1: e}
zettaknight_utils.parse_output(ret)
return ret
if user:
cifs_cmd = "{0} -u {1} -d {2}".format(zettaknight_globs.cifs_share_script, user, dataset)
if group:
cifs_cmd = "{0} -g {1} -d {2}".format(zettaknight_globs.cifs_share_script, group, dataset)
if user:
obj = user
elif group:
obj = group
try:
if str(zettaknight_globs.zfs_conf[dataset]['primary']) == str(zettaknight_globs.fqdn):
if zettaknight_globs.zfs_conf[dataset]['snap']['remote_server']:
for remote_server in zettaknight_globs.zfs_conf[dataset]['snap']['remote_server']:
remote_user = zettaknight_globs.zfs_conf[dataset]['user']
remote_ssh = "{0}@{1}".format(remote_user, remote_server)
try:
ssh2 = paramiko.SSHClient()
ssh2.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh2.connect(remote_server, username=remote_user, key_filename=zettaknight_globs.identity_file)
remote_cmd = "zettaknight create_cifs_share user={0} dataset={1}".format(obj, dataset)
ssh2_stdin, ssh2_stdout, ssh2_stderr = ssh2.exec_command(remote_cmd)
ssh2.close()
except Exception as e:
pass
except:
pass
try:
ret[dataset]['Create CIFS Share'] = zettaknight_utils.spawn_job(cifs_cmd)
except Exception as e:
ret[dataset]['Create CIFS Share'] = {1: e}
#zettaknight_utils.parse_output(ret)
return ret
def set_cifs_quota(**kwargs):
'''
Set quota for user or group cifs share.
'''
ret = {}
if zettaknight_globs.help_flag:
ret = """Set CIFS Quota:
Function to set a quota on a user or group CIFS share. Sets the quota on primary and secondary ZFS servers. Arguments should be passed as key/value pairs.
Required Arguments:
user or group
Specifies the username or group name tied to the relevant share to set the quota on.
quota
Quota to be set. Should be specified with a number and unit, ie. 10T, 100G, 10M. 'None' should be specified to remove the quota.
Optional Arguments:
dataset
Specifies the parent dataset to search for the share under. If not specified, the default will be pulled from Zettaknight conf files"""
return ret
user = False
group = False
quota = False
try:
if kwargs and kwargs['dataset']:
dataset = kwargs['dataset']
except:
dataset = "{0}/{1}".format(zettaknight_globs.pool_name, zettaknight_globs.cifs_dset_suffix)
pass
if kwargs and "user" in kwargs.iterkeys():
user = kwargs['user']
if kwargs and "group" in kwargs.iterkeys():
group = kwargs['group']
if kwargs and "quota" in kwargs.iterkeys():
quota = kwargs['quota']
if user and group:
ret[dataset] = {}
try:
raise Exception("Arguments user and group must be specified separately. Arguments passed: {0}".format(kwargs))
except Exception as e:
ret[dataset]['Set User Quota'] = {1: e}
zettaknight_utils.parse_output(ret)
return ret
if not quota:
ret[dataset] = {}
try:
raise Exception("Quota argument must be supplied. Arguments passed: {0}".format(kwargs))
except Exception as e:
ret[dataset]['Set User Quota'] = {1: e}
zettaknight_utils.parse_output(ret)
return ret
if user:
obj = user
dset = "{0}/{1}".format(dataset, user)
elif group:
obj = group
dset = "{0}/{1}".format(dataset, group)
try:
if str(zettaknight_globs.zfs_conf[dataset]['primary']) == str(zettaknight_globs.fqdn):
if zettaknight_globs.zfs_conf[dataset]['snap']['remote_server']:
for remote_server in zettaknight_globs.zfs_conf[dataset]['snap']['remote_server']:
remote_user = zettaknight_globs.zfs_conf[dataset]['user']
remote_ssh = "{0}@{1}".format(remote_user, remote_server)
try:
ssh2 = paramiko.SSHClient()
ssh2.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh2.connect(remote_server, username=remote_user, key_filename=zettaknight_globs.identity_file)
remote_cmd = "zettaknight set_cifs_quota user={0} dataset={1} quota={2}".format(obj, dataset, quota)
ssh2_stdin, ssh2_stdout, ssh2_stderr = ssh2.exec_command(remote_cmd)
ssh2.close()
except Exception as e:
pass
except:
pass
ret[dset] = {}
quota_cmd = "/sbin/zfs set refquota={0} {1}".format(quota, dset)
ret[dset]['Set User Quota'] = zettaknight_utils.spawn_job(quota_cmd)
return ret
def get_cifs_quota(**kwargs):
'''
get quota for user or group cifs share.
'''
if zettaknight_globs.help_flag:
ret = """Get CIFS Quota:
Function to get and display quota on a user or group CIFS share. Arguments should be passed as key/value pairs.
Required Arguments:
user or group
Specifies the username or group name tied to the relevant share to set the quota on.
Optional Arguments:
dataset
Specifies the parent dataset to search for the share under. If not specified, the default will be pulled from Zettaknight conf files"""
return ret
ret = {}
user = False
group = False
try:
if kwargs and kwargs['dataset']:
dataset = kwargs['dataset']
except:
dataset = "{0}/{1}".format(zettaknight_globs.pool_name, zettaknight_globs.cifs_dset_suffix)
pass
if kwargs and "user" in kwargs.iterkeys():
user = kwargs['user']
if kwargs and "group" in kwargs.iterkeys():
group = kwargs['group']
if user and group:
ret[dataset] = {}
try:
raise Exception("Arguments user and group must be specified separately. Arguments passed: {0}".format(kwargs))
except Exception as e:
ret[dataset]['Get User Quota'] = {1: e}
zettaknight_utils.parse_output(ret)
return ret
if user:
dset = "{0}/{1}".format(dataset, user)
elif group:
dset = "{0}/{1}".format(dataset, group)
ret[dset] = {}
quota_cmd = "/sbin/zfs get refquota -H {0} -o value".format(dset)
ret[dset]['Get User Quota'] = zettaknight_utils.spawn_job(quota_cmd)
return ret
def get_cifs_share(**kwargs):
'''
get quota for user or group cifs share.
'''
if zettaknight_globs.help_flag:
ret = """Get CIFS Share:
Returns location UNC path of CIFS share for provided user or group. Arguments should be passed as key/value pairs.
Required Arguments:
user or group
Specifies the username or group name to query.
Optional Arguments:
dataset
Specifies the parent dataset to query for the share under. If not specified, the default will be pulled from Zettaknight conf files"""
return ret
ret = {}
user = False
group = False
try:
if kwargs and kwargs['dataset']:
dataset = kwargs['dataset']
except:
dataset = "{0}/{1}".format(zettaknight_globs.pool_name, zettaknight_globs.cifs_dset_suffix)
pass
if kwargs and "user" in kwargs.iterkeys():
user = kwargs['user']
if kwargs and "group" in kwargs.iterkeys():
group = kwargs['group']
if user and group:
ret[dataset] = {}
try:
raise Exception("Arguments user and group must be specified separately. Arguments passed: {0}".format(kwargs))
except Exception as e:
ret[dataset]['Get User Quota'] = {1: e}
zettaknight_utils.parse_output(ret)
return ret
if user:
dset = "{0}/{1}".format(dataset, user)
elif group:
dset = "{0}/{1}".format(dataset, group)
ret[dset] = {}
quota_cmd = "/sbin/zfs list -H {0} -o name".format(dset)
#ret[dset]['Get User Share'] = zettaknight_utils.spawn_job(quota_cmd)
job_out = zettaknight_utils.spawn_job(quota_cmd)
print("JOB OUT: {0}".format(job_out))
if 0 in job_out.iterkeys():
job_out[0] = "//{0}/{1}/{2}".format(str(zettaknight_globs.samba_service_name), str(zettaknight_globs.samba_share_suffix), str(user))
print("JOB OUT: {0}".format(job_out))
ret[dset]['Get User Share'] = job_out
return ret