Menu

[r5782]: / trunk / gui / middleware / notifier.py  Maximize  Restore  History

Download this file

650 lines (569 with data), 27.5 kB

  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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
#!/usr/bin/env python
#-
# Copyright (c) 2010 iXsystems, Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
""" Helper for FreeNAS to execute command line tools
This helper class abstracts operating system operations like starting,
stopping, restarting services out from the normal Django stuff and makes
future extensions/changes to the command system easier. When used as a
command line utility, this helper class can also be used to do these
actions.
"""
import ctypes
import types
import syslog
from shlex import split as shlex_split
from subprocess import Popen, PIPE
class notifier:
from os import system as ___system
from pwd import getpwnam as ___getpwnam
def __system(self, command):
syslog.openlog("freenas", syslog.LOG_CONS | syslog.LOG_PID)
syslog.syslog(syslog.LOG_NOTICE, "Executing: " + command)
# TODO: python's signal class should be taught about sigprocmask(2)
# This is hacky hack to work around this issue.
libc = ctypes.cdll.LoadLibrary("libc.so.7")
omask = (ctypes.c_uint32 * 4)(0, 0, 0, 0)
mask = (ctypes.c_uint32 * 4)(0, 0, 0, 0)
pmask = ctypes.pointer(mask)
pomask = ctypes.pointer(omask)
libc.sigprocmask(3, pmask, pomask)
self.___system("(" + command + ") 2>&1 | logger -p daemon.notice -t freenas")
libc.sigprocmask(3, pomask, None)
syslog.syslog(syslog.LOG_INFO, "Executed: " + command)
def __system_nolog(self, command):
retval = 0
syslog.openlog("freenas", syslog.LOG_CONS | syslog.LOG_PID)
syslog.syslog(syslog.LOG_NOTICE, "Executing: " + command)
# TODO: python's signal class should be taught about sigprocmask(2)
# This is hacky hack to work around this issue.
libc = ctypes.cdll.LoadLibrary("libc.so.7")
omask = (ctypes.c_uint32 * 4)(0, 0, 0, 0)
mask = (ctypes.c_uint32 * 4)(0, 0, 0, 0)
pmask = ctypes.pointer(mask)
pomask = ctypes.pointer(omask)
libc.sigprocmask(3, pmask, pomask)
retval = self.___system("(" + command + ") 2>&1 > /dev/null")
libc.sigprocmask(3, pomask, None)
syslog.syslog(syslog.LOG_INFO, "Executed: " + command)
return retval
def __pipeopen(self, command):
syslog.openlog("freenas", syslog.LOG_CONS | syslog.LOG_PID)
syslog.syslog(syslog.LOG_NOTICE, "Popen()ing: " + command)
args = shlex_split(command)
return Popen(args, stdin = PIPE, stdout = PIPE, stderr = PIPE, close_fds = True)
def _do_nada(self):
pass
def _simplecmd(self, action, what):
syslog.openlog("freenas", syslog.LOG_CONS | syslog.LOG_PID)
syslog.syslog(syslog.LOG_DEBUG, "Calling: %s(%s) " % (action, what))
try:
f = getattr(self, '_' + action + '_' + what)
except AttributeError:
""" Provide generic start/stop/restart verbs for rc.d scripts """
if action in ("start", "stop", "restart", "reload"):
self.__system("/usr/sbin/service " + what + " " + action)
f = self._do_nada
else:
raise "Internal error: Unknown command"
try:
f()
except:
raise
def init(self, what, objectid = None):
""" Dedicated command to create "what" designated by an optional objectid.
The helper will use method self._init_[what]() to create the object"""
if objectid == None:
self._simplecmd("init", what)
else:
try:
f = getattr(self, '_init_' + what)
f(objectid)
except:
raise
def destroy(self, what, objectid = None):
if objectid == None:
raise ValueError("Calling destroy without id")
else:
try:
f = getattr(self, '_destroy_' + what)
f(objectid)
except:
raise
def start(self, what):
""" Start the service specified by "what".
The helper will use method self._start_[what]() to start the service.
If the method does not exist, it would fallback using service(8)."""
self._simplecmd("start", what)
def stop(self, what):
""" Stop the service specified by "what".
The helper will use method self._stop_[what]() to stop the service.
If the method does not exist, it would fallback using service(8)."""
self._simplecmd("stop", what)
def restart(self, what):
""" Restart the service specified by "what".
The helper will use method self._restart_[what]() to restart the service.
If the method does not exist, it would fallback using service(8)."""
self._simplecmd("restart", what)
def reload(self, what):
""" Reload the service specified by "what".
The helper will use method self._reload_[what]() to reload the service.
If the method does not exist, the helper will try self.restart of the
service instead."""
try:
self._simplecmd("reload", what)
except:
self.restart(what)
def change(self, what):
""" Notify the service specified by "what" about a change.
The helper will use method self.reload(what) to reload the service.
If the method does not exist, the helper will try self.start the
service instead."""
try:
self.reload(what)
except:
self.start(what)
def _restart_iscsitarget(self):
self.__system("/usr/sbin/service ix-istgt quietstart")
self.__system("/usr/sbin/service istgt restart")
def _reload_iscsitarget(self):
self.__system("/usr/sbin/service ix-istgt quietstart")
self.__system("/usr/sbin/service istgt restart")
def _start_network(self):
# TODO: Skip this step when IPv6 is already enabled
self.__system("/sbin/sysctl net.inet6.ip6.auto_linklocal=1")
self.__system("/usr/sbin/service autolink auto_linklocal quietsatrt")
self.__system("/usr/sbin/service netif stop")
self.__system("/etc/netstart")
def _reload_named(self):
self.__system("/usr/sbin/service named reload")
def _reload_networkgeneral(self):
self.__system('/bin/hostname ""')
self.__system("/usr/sbin/service hostname quietstart")
self.__system("/usr/sbin/service routing restart")
def _reload_timeservices(self):
self.__system("/usr/sbin/service ix-localtime quietstart")
self.__system("/usr/sbin/service ix-ntpd quietstart")
self.__system("/usr/sbin/service ntpd restart")
def _reload_ssh(self):
self.__system("/usr/sbin/service ix-sshd quietstart")
self.__system("/usr/sbin/service sshd restart")
def _restart_ssh(self):
self.__system("/usr/sbin/service ix-sshd quietstart")
self.__system("/usr/sbin/service sshd restart")
def _restart_ldap(self):
self.__system("/usr/sbin/service ix-ldap quietstart")
self.__system("/usr/sbin/service ix-nsswitch quietstart")
self.__system("/usr/sbin/service ix-pam quietstart")
self.__system("/usr/sbin/service ix-samba quietstart")
self.__system("/usr/sbin/service samba forcestop")
self.__system("/usr/bin/killall nmbd")
self.__system("/usr/bin/killall smbd")
self.__system("/usr/bin/killall winbindd")
self.__system("/bin/sleep 5")
self.__system("/usr/sbin/service samba quietstart")
def _restart_activedirectory(self):
self.__system("/usr/sbin/service ix-kerberos quietstart")
self.__system("/usr/sbin/service ix-nsswitch quietstart")
self.__system("/usr/sbin/service ix-pam quietstart")
self.__system("/usr/sbin/service ix-samba quietstart")
self.__system("/usr/sbin/service ix-kinit quietstart")
self.__system("/bin/sleep 5")
self.__system("/usr/sbin/service ix-activedirectory quietstart")
self.__system("/usr/sbin/service samba forcestop")
self.__system("/usr/bin/killall nmbd")
self.__system("/usr/bin/killall smbd")
self.__system("/usr/bin/killall winbindd")
self.__system("/bin/sleep 5")
self.__system("/usr/sbin/service samba quietstart")
def _reload_tftp(self):
self.__system("/usr/sbin/service ix-inetd quietstart")
self.__system("/usr/sbin/service inetd restart")
def _restart_tftp(self):
self.__system("/usr/sbin/service ix-inetd quietstart")
self.__system("/usr/sbin/service inetd restart")
def _reload_ftp(self):
self.__system("/usr/sbin/service ix-proftpd quietstart")
self.__system("/usr/sbin/service proftpd restart")
def _restart_ftp(self):
self.__system("/usr/sbin/service ix-proftpd quietstart")
self.__system("/usr/sbin/service proftpd restart")
def _start_ftp(self):
self.__system("/usr/sbin/service ix-proftpd quietstart")
self.__system("/usr/sbin/service proftpd start")
def _load_afp(self):
self.__system("/usr/sbin/service ix-afpd quietstart")
self.__system("/usr/sbin/service netatalk quietstart")
def _restart_afp(self):
self.__system("/usr/sbin/service ix-afpd quietstart")
self.__system("/usr/sbin/service netatalk restart")
def _reload_nfs(self):
self.__system("/usr/sbin/service ix-nfsd quietstart")
self.__system("/usr/sbin/service mountd forcerestart")
def _restart_nfs(self):
self.__system("/usr/sbin/service mountd forcestop")
self.__system("/usr/sbin/service nfsd forcestop")
self.__system("/usr/sbin/service ix-nfsd quietstart")
self.__system("/usr/sbin/service nfsd quietstart")
def _restart_dynamicdns(self):
self.__system("/usr/sbin/service ix-inadyn quietstart")
self.__system("/usr/sbin/service inadyn restart")
def _restart_system(self):
self.__system("/bin/sleep 3 && /sbin/shutdown -r now &")
def _stop_system(self):
self.__system("/sbin/shutdown -p now")
def _reload_cifs(self):
self.__system("/usr/sbin/service ix-samba quietstart")
self.__system("/usr/sbin/service samba restart")
def _restart_cifs(self):
# TODO: bug in samba rc.d script
# self.__system("/usr/sbin/service samba forcestop")
self.__system("/usr/bin/killall nmbd")
self.__system("/usr/bin/killall smbd")
self.__system("/usr/sbin/service samba quietstart")
def _restart_snmp(self):
self.__system("/usr/sbin/service bsnmpd forcestop")
self.__system("/usr/sbin/service bsnmpd quietstart")
def __open_db(self):
"""Open and return a cursor object for database access."""
dbname = ""
try:
from freenasUI.settings import DATABASE_NAME as dbname
except:
dbname = '/data/freenas-v1.db'
import sqlite3
conn = sqlite3.connect(dbname)
c = conn.cursor()
return c
def __gpt_labeldisk(self, type, devname, label = "", swapsize=2):
"""Label the whole disk with GPT under the desired label and type"""
# Taste the disk to know whether it's 4K formatted.
# requires > 8.1-STABLE after r213467
ret_4kstripe = self.__system_nolog("geom disk list %s "
"| grep 'Stripesize: 4096'" % (devname))
ret_512bsector = self.__system_nolog("geom disk list %s "
"| grep 'Sectorsize: 512'" % (devname))
# Make sure that the partition is 4k-aligned, if the disk reports 512byte sector
# while using 4k stripe, use an offset of 64.
need4khack = (ret_4kstripe == 0) and (ret_512bsector == 0)
# Caculate swap size.
swapsize = swapsize * 1024 * 1024 * 2
# Round up to nearest whole integral multiple of 64 if we need 4k hack
if need4khack:
swapsize = ((swapsize+34+63)/64)*64
# To be safe, wipe out the disk, both ends... before we start
self.__system("dd if=/dev/zero of=/dev/%s bs=1m count=1" % (devname))
self.__system("dd if=/dev/zero of=/dev/%s bs=1m oseek=`diskinfo %s "
"| awk '{print ($3 / (1024*1024)) - 4;}'`" % (devname, devname))
if label != "":
self.__system("gpart create -s gpt /dev/%s && gpart add -t freebsd-swap -l swap-%s -s %d %s && gpart add -t %s -l %s %s" %
(devname, label, swapsize, devname, type, label, devname))
else:
self.__system("gpart create -s gpt /dev/%s && gpart add -t freebsd-swap -l swap-%s -s %d %s && gpart add -t %s %s" %
(devname, devname, swapsize, devname, type, devname))
return need4khack
def __gpt_unlabeldisk(self, devname):
"""Unlabel the disk"""
self.__system("swapoff -a && gpart delete -i 1 /dev/%s && gpart destroy /dev/%s" %
(devname, devname))
self.__system("gpart delete -i 2 /dev/%s && gpart destroy /dev/%s" %
(devname, devname))
# To be safe, wipe out the disk, both ends...
self.__system("dd if=/dev/zero of=/dev/%s bs=1m count=10" % (devname))
self.__system("dd if=/dev/zero of=/dev/%s bs=1m oseek=`diskinfo %s "
"| awk '{print ($3 / (1024*1024)) - 3;}'`" % (devname, devname))
def __create_zfs_volume(self, c, z_id, z_name, swapsize):
"""Internal procedure to create a ZFS volume identified by volume id"""
z_vdev = ""
need4khack = False
# Grab all disk groups' id matching the volume ID
c.execute("SELECT id, group_type FROM storage_diskgroup WHERE "
"group_volume_id = ?", (z_id,))
vgroup_list = c.fetchall()
for vgrp_row in vgroup_list:
hack_vdevs = []
vgrp = (vgrp_row[0],)
vgrp_type = vgrp_row[1]
if vgrp_type != 'stripe':
z_vdev += " " + vgrp_type
# Grab all member disks from the current vdev group
c.execute("SELECT disk_disks, disk_name FROM storage_disk WHERE "
"disk_group_id = ?", vgrp)
vdev_member_list = c.fetchall()
for disk in vdev_member_list:
need4khack = self.__gpt_labeldisk(type = "freebsd-zfs",
devname = disk[0],
label = disk[1],
swapsize=swapsize)
if need4khack:
hack_vdevs.append(disk[1])
self.__system("gnop create -S 4096 /dev/gpt/" + disk[1])
z_vdev += " /dev/gpt/" + disk[1] + ".nop"
else:
z_vdev += " /dev/gpt/" + disk[1]
# Finally, create the zpool.
# TODO: disallowing cachefile may cause problem if there is
# preexisting zpool having the exact same name.
self.__system("zpool create -o cachefile=none -fm /mnt/%s %s %s" %
(z_name, z_name, z_vdev))
# If we have 4k hack then restore system to whatever it should be
if need4khack:
self.__system("zpool export %s" % (z_name))
for disk in hack_vdevs:
self.__system("gnop destroy /dev/gpt/" + disk + ".nop")
self.__system("zpool import %s" % (z_name))
def create_zfs_dataset(self, path, props=None):
"""Internal procedure to create ZFS volume"""
options = " "
if props:
assert type(props) is types.DictType
for k in props.keys():
options += "-o %s=%s " % (k, props[k])
zfsproc = self.__pipeopen("/sbin/zfs create %s %s" % (options, path))
zfs_output, zfs_err = zfsproc.communicate()
zfs_error = zfsproc.wait()
return zfs_error, zfs_err
def list_zfs_datasets(self, path="", recursive=False):
"""Return a dictionary that contains all ZFS dataset list and their mountpoints"""
if recursive:
zfsproc = self.__pipeopen("/sbin/zfs list -Hr %s" % (path))
else:
zfsproc = self.__pipeopen("/sbin/zfs list -H %s" % (path))
zfs_output, zfs_err = zfsproc.communicate()
zfs_output = zfs_output.split('\n')
retval = {}
for line in zfs_output:
if line != "":
data = line.split('\t')
retval[data[0]] = data[4]
return retval
def get_zfs_attributes(self, zfsname):
"""Return a dictionary that contains all ZFS attributes"""
zfsproc = self.__pipeopen("/sbin/zfs get all %s" % (zfsname))
zfs_output, zfs_err = zfsproc.communicate()
zfs_output = zfs_output.split('\n')
retval = {}
for line in zfs_output:
if line != "":
data = line.split('\t')
retval[data[1]] = data[2]
return retval
def set_zfs_attribute(self, name, attr, value):
self.__system("zfs set %s=%s %s" % (attr, value, name))
def destroy_zfs_dataset(self, path):
self.__system("zfs destroy %s" % (path))
def __destroy_zfs_volume(self, c, z_id, z_name):
"""Internal procedure to destroy a ZFS volume identified by volume id"""
# First, destroy the zpool.
self.__system("zpool destroy -f %s" % (z_name))
# Clear out disks associated with the volume
c.execute("SELECT id FROM storage_diskgroup WHERE group_volume_id = ?", (z_id,))
vgroup_list = c.fetchall()
for vgrp in vgroup_list:
c.execute("SELECT disk_disks, disk_name FROM storage_disk WHERE "
"disk_group_id = ?", vgrp)
vdev_member_list = c.fetchall()
for disk in vdev_member_list:
self.__gpt_unlabeldisk(devname = disk[0])
def __create_ufs_volume(self, c, u_id, u_name, swapsize):
geom_vdev = ""
ufs_device = ""
c.execute("SELECT id, group_type, group_name FROM storage_diskgroup "
"WHERE group_volume_id = ?", (u_id,))
# TODO: We do not support multiple GEOM levels for now.
vgrp_row = c.fetchone()
ufs_volume_id = (vgrp_row[0],)
geom_type = vgrp_row[1]
geom_name = vgrp_row[2]
# Grab all disks from the group
c.execute("SELECT disk_disks, disk_name FROM storage_disk WHERE "
"disk_group_id = ?", ufs_volume_id)
if geom_type == '':
disk = c.fetchone()
self.__gpt_labeldisk(type = "freebsd-ufs", devname = disk[0], swapsize=swapsize)
ufs_device = "/dev/ufs/" + disk[1]
# TODO: Need to investigate why /dev/gpt/foo can't have label /dev/ufs/bar
# generated automatically
self.__system("newfs -U -L %s /dev/%sp2" % (u_name, disk[0]))
else:
vdev_member_list = c.fetchall()
for disk in vdev_member_list:
geom_vdev += " /dev/" + disk[0]
self.__system("geom %s load" % (geom_type))
self.__system("geom %s label %s %s" % (geom_type, geom_name, geom_vdev))
ufs_device = "/dev/%s/%s" % (geom_type, geom_name)
self.__system("newfs -U -L %s %s" % (u_name, ufs_device))
def __destroy_ufs_volume(self, c, u_id, u_name):
"""Internal procedure to destroy a UFS volume identified by volume id"""
c.execute("SELECT id, group_type, group_name FROM storage_diskgroup WHERE "
"group_volume_id = ?", (u_id,))
vgrp_row = c.fetchone()
ufs_volume_id = (vgrp_row[0],)
geom_type = vgrp_row[1]
geom_name = vgrp_row[2]
# Grab all disks from the group
c.execute("SELECT disk_disks, disk_name FROM storage_disk WHERE "
"disk_group_id = ?", ufs_volume_id)
if geom_type == '':
disk = c.fetchone()
self.__system("umount -f /dev/ufs/" + u_name)
self.__gpt_unlabeldisk(devname = disk[0])
else:
self.__system("umount -f /dev/ufs/" + u_name)
self.__system("geom %s stop %s" % (geom_type, geom_name))
vdev_member_list = c.fetchall()
for disk in vdev_member_list:
disk_name = " /dev/" + disk[0]
self.__system("geom %s clear %s" % (geom_type, disk_name))
def _init_volume(self, volume_id):
"""Initialize a volume designated by volume_id"""
c = self.__open_db()
c.execute("SELECT adv_swapondrive FROM system_advanced ORDER BY -id LIMIT 1")
swapsize=c.fetchone()[0]
c.execute("SELECT vol_fstype, vol_name FROM storage_volume WHERE id = ?",
(volume_id,))
volume = c.fetchone()
assert volume[0] == 'ZFS' or volume[0] == 'UFS'
if volume[0] == 'ZFS':
self.__create_zfs_volume(c, volume_id, volume[1], swapsize)
elif volume[0] == 'UFS':
self.__create_ufs_volume(c, volume_id, volume[1], swapsize)
self._reload_disk()
def _destroy_volume(self, volume_id):
"""Destroy a volume designated by volume_id"""
c = self.__open_db()
c.execute("SELECT vol_fstype, vol_name FROM storage_volume WHERE id = ?",
(volume_id,))
volume = c.fetchone()
assert volume[0] == 'ZFS' or volume[0] == 'UFS' or volume[0] == 'iscsi'
if volume[0] == 'ZFS':
self.__destroy_zfs_volume(c = c, z_id = volume_id, z_name = volume[1])
elif volume[0] == 'UFS':
self.__destroy_ufs_volume(c = c, u_id = volume_id, u_name = volume[1])
def _reload_disk(self):
self.__system("/usr/sbin/service ix-fstab quietstart")
self.__system("/usr/sbin/service swap1 quietstart")
self.__system("/usr/sbin/service mountlate quietstart")
# Create a user in system then samba
def __pw_with_password(self, command, password):
pw = self.__pipeopen(command)
msg = pw.communicate("%s\n" % password)[1]
if msg != "":
syslog.syslog(syslog.LOG_NOTICE, "Command reports " + msg)
def __smbpasswd(self, username, password):
command = '/usr/local/bin/smbpasswd -s -a "%s"' % (username)
smbpasswd = self.__pipeopen(command)
smbpasswd.communicate("%s\n%s\n" % (password, password))
def __issue_pwdchange(self, username, command, password):
self.__pw_with_password(command, password)
self.__smbpasswd(username, password)
def user_create(self, username, fullname, password, uid = -1, gid = -1,
shell = "/sbin/nologin", homedir = "/mnt"):
"""Creates a user with the given parameters.
uid and gid can be omitted or specified as -1 which means the system should
choose automatically.
The default shell is /sbin/nologin.
Returns user uid and gid"""
command = '/usr/sbin/pw useradd "%s" -h 0 -c "%s"' % (username, fullname)
if uid >= 0:
command += " -u %d" % (uid)
if gid >= 0:
command += " -g %d" % (gid)
if homedir != '/nonexistent':
command += ' -s "%s" -d "%s" -m' % (shell, homedir)
else:
command += ' -s "%s" -d "%s"' % (shell, homedir)
self.__issue_pwdchange(username, command, password)
smb_command = "/usr/local/bin/pdbedit -w %s" % username
smb_cmd = self.__pipeopen(smb_command)
smb_hash = smb_cmd.communicate()
smb_hash = smb_hash[0]
user = self.___getpwnam(username)
return (user.pw_uid, user.pw_gid, user.pw_passwd, smb_hash)
def user_changepassword(self, username, password):
"""Changes user password"""
command = '/usr/sbin/pw usermod "%s" -h 0' % (username)
self.__issue_pwdchange(username, command, password)
smb_command = "/usr/local/bin/pdbedit -w %s" % username
smb_cmd = self.__pipeopen(smb_command)
smb_hash = smb_cmd.communicate()
smb_hash = smb_hash[0]
user = self.___getpwnam(username)
return (user.pw_passwd, smb_hash)
def user_deleteuser(self, username):
self.__system('/usr/sbin/pw userdel "%s"' % (username))
def user_deletegroup(self, groupname):
self.__system('/usr/sbin/pw groupdel "%s"' % (groupname))
def user_getnextuid(self):
command = "/usr/sbin/pw usernext"
pw = self.__pipeopen(command)
uidgid = pw.communicate()
uid = uidgid[0].split(':')[0]
return uid
def user_getnextgid(self):
command = "/usr/sbin/pw groupnext"
pw = self.__pipeopen(command)
uidgid = pw.communicate()
gid = uidgid[0]
return gid
def _reload_user(self):
self.__system("/usr/sbin/service ix-passwd quietstart")
self.reload("cifs")
def mp_change_permission(self, path='/mnt', user='root', group='wheel',
mode='0755', recursive=False):
if recursive:
flags='-R '
else:
flags=''
self.__system("/usr/sbin/chown %s%s:%s %s" % (flags, user, group, path))
self.__system("/bin/chmod %s%s %s" % (flags, mode, path))
def validate_xz(self, path):
ret = self.__system_nolog("/usr/bin/xz -t %s" % (path))
if ret == 0:
return True
return False
def usage():
print ("Usage: %s action command" % argv[0])
print """\
Action is one of:
start: start a command
stop: stop a command
restart: restart a command
reload: reload a command (try reload, if unsuccessful do restart)
change: notify change for a command (try self.reload, if unsuccessful do start)"""
exit
# When running as standard-alone script
if __name__ == '__main__':
from sys import argv
if len(argv) != 3:
usage()
else:
n = notifier()
try:
f = getattr(n, argv[1])
except:
print ("Unknown action: %s" % argv[1])
usage()
f(argv[2])
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.