def info(self, name): """ Get pool info Args: name(str): Pool name:func=["pool", "list"] Returns: pool_info(dict): Pool info """ zpool_output = exec_command('zpool status %s' % name).stdout prop_output = exec_command('zpool get all %s' % name).stdout lines = zpool_output.splitlines() state = lines[1].split(':')[1].strip() disk_list = list() for line in lines[7:-1]: if len(line.strip()) != 0: disk_list.append(line.strip().split()[0]) info = OrderedDict() props = dict() for line in prop_output.splitlines()[1:]: name, prop, value, source = line.split() props[prop] = value info['size'] = props['size'] info['allocated'] = props['allocated'] info['capacity'] = props['capacity'] info['free'] = props['free'] info['failmode'] = props['failmode'] info['health'] = props['health'] info['disk_list'] = disk_list return info
def create(self, name, disk_list, raid_type=""): """Create pool Args: name(str): Pool name disk_list(list): A list of disk:func=["disk", "list"] raid_type(str): RAID type:enum=["raidz", "raidz2", "raidz3", "mirror"] Raises: NameError(str): Pool name exist or invalid ValueError(str): Disk name is invalid TypeError(str): RAID type invalid Examples: Take a pool name, and then select the disk you want, select the raid type. >> pool create name p1 disk_list disk0,disk1 raid_type mirror """ if name in self.list(): # check pool not exist raise NameError("pool exists", name) if name.isdigit(): raise NameError("Pool name can not be digit") if raid_type != "" and raid_type not in [ "raidz", "raidz2", "raidz3", "mirror" ]: raise TypeError("RAID type: %s is invalid" % raid_type) exec_command('zpool create -f %s %s %s' % (name, raid_type, " ".join(disk_list)))
def destroy(self, share_name): """ Create share folder Args: share_name(str): Share name:func=["share", "list"] Raises: NameError(str): Status is not exist """ exec_command('zfs destroy %s' % share_name)
def destroy(self, username): """ Destroy user Args: username(str): User name:func=["user", "list"] Raises: NameError(str): Can not delete admin """ if username == 'admin': raise NameError('Can not delete admin') exec_command('deluser %s' % username)
def enable(self, share_name): """ Enable file share Args: share_name(str): Share name:func=["share", "list"] Raises: NameError(str): Status is not exist """ exec_command('zfs set sharesmb=on %s' % share_name) exec_command('zfs set acltype=posixacl %s' % share_name)
def destroy(self, name): """Create pool Args: name(str): Pool name:func=["pool", "list"] Raises: NameError(str): Pool name exist or invalid """ if name not in self.list(): # check pool not exist raise NameError("pool not exists", name) exec_command('zpool destroy %s' % name)
def create(self, pool_name, share_name): """ Create share folder Args: pool_name(str): Pool name :func=["pool", "list"] share_name(str): Share name Raises: NameError(str): Status is not exist """ if pool_name not in self.modules.pool.list(): # check pool not exist raise NameError("pool not exists", pool_name) exec_command('zfs create %s/%s' % (pool_name, share_name)) self.enable(pool_name + '/' + share_name)
def chpasswd(self, username, password): """ Change password Args: username(str): User name password(str): Password Raises: NameError(str): username error """ try: exec_command('id %s' % username) except: raise NameError('User %s not exists' % username) encrypted_passwd = crypt.crypt(password, crypt.mksalt(crypt.METHOD_MD5)) exec_command("echo {}:{} | chpasswd".format(username, encrypted_passwd))
def list(self, pool_name, share_name): """ List all disks Args: status(str): Get all/free/used disks :enum=["ALL", "FREE", "ONLINE", "UNINITIATED", "INITIATING"] Returns: disk_list(dict): a list of disks Raises: NameError(str): Status is not exist """ lsblk_output = json.loads( exec_command('lsblk -JO').stdout)['blockdevices'] output = OrderedDict() for dev in lsblk_output: disk = OrderedDict() disk['size'] = dev['size'] disk['type'] = dev['type'] disk['mountpoint'] = dev['mountpoint'] disk['label'] = dev['label'] disk['model'] = dev['model'] disk['serial'] = dev['serial'] output[dev['name']] = disk return output
def disable(self, pool_name, share_name): """ Disable file share Args: pool_name(str): Pool name :func=["pool", "list"] share_name(str): Share name Raises: NameError(str): Status is not exist """ if pool_name not in self.modules.pool.list(): # check pool not exist raise NameError("pool not exists", pool_name) try: exec_command('zfs set sharesmb=off %s/%s' % (pool_name, share_name)) except: pass
def set_user_acl(self, folder, user, permission): """ Set user ACL of folder Args: folder(str): Folder to set:func=["share", "list"] user(str): User name:func=["user", "list"] permission(str): Access permission:enum=["RO", "RW", "DA"] Exmaples: set_user_acl("folder1", "admin", "RO") RO: Read only RW: Read Write DA: Deny access Raises: TypeError(str): No such persmisison type """ enum = {"RO": "r-", "RW": "rwX", "DA": "---"} if permission not in enum: raise TypeError("No such permisison type") acl = enum[permission] folder_info = self.modules.share.list()[folder] path = folder_info['mountpoint'] exec_command('setfacl -R -m u:{}:{} {}'.format(user, acl, path))
def create(self, username, password): """ Create user Args: username(str): User name password(str): Password Raises: NameError(str): Username error ParameterError(str): Password error """ try: exec_command('id %s' % username) raise NameError('User %s already exists' % username) except: pass encrypted_passwd = crypt.crypt(password, crypt.mksalt(crypt.METHOD_MD5)) exec_command("adduser -D -H -h {} -s {} -G users {} ".format('/', '/bin/false', username)) exec_command("echo {}:{} | chpasswd".format(username, encrypted_passwd)) exec_command("printf '{}\n{}\n' | smbpasswd -a -s {}".format(password, password, username))
def list(self): """ List all pools Returns: pool_list(dict): a list of pool """ zpool_output = exec_command( 'zpool list -o name,size,alloc,free,health').stdout output = OrderedDict() for line in zpool_output.splitlines()[1:]: name, size, alloc, free, health = line.split() pool = OrderedDict() pool['size'] = size pool['alloc'] = alloc pool['free'] = free pool['health'] = health output[name] = pool return output
def list(self, pool_name=None): """ List all share folders Args: pool_name(str): Pool name :func=["pool", "list"] Returns: share_list(list): A list of share folders """ res = OrderedDict() output = exec_command( 'zfs list -t filesystem -o name,avail,used,mountpoint').stdout for line in output.splitlines()[1:]: dataset_name, avail, used, mountpoint = line.split() if '/' not in dataset_name: continue if pool_name is None or pool_name + '/' == dataset_name[ 0:len(pool_name + '/')]: name = dataset_name res[name] = OrderedDict() res[name]['used'] = used res[name]['availible'] = avail res[name]['pool'] = dataset_name.split('/')[0] res[name]['mountpoint'] = mountpoint return res
def reboot(self): """ Reboot """ exec_command("reboot")
def __init__(self): exec_command('mkdir -p /var/lib/samba/usershares') exec_command('chmod 1700 /var/lib/samba/usershares')
def __init__(self): exec_command('/sbin/modprobe zfs') exec_command('zpool import -a')
auth.token_db[uid] = "admin" auth.users_db.save() auth.token_db.save() def get_module_list(): this_dir = os.path.dirname(os.path.abspath(__file__)) modules_dir = os.path.join(this_dir, 'modules') module_list = list() module_files = glob.glob(modules_dir + "/[a-z]*.py") for file in module_files: module_name = os.path.basename(file).split('.')[0] m = importlib.import_module("nas.modules.%s" % module_name) module_attrs = dir(m) for attr_name in module_attrs: attr = getattr(m, attr_name) if inspect.isclass(attr) and issubclass( attr, LarvaObject) and attr != LarvaObject: module_list.append(attr) return module_list modules = get_module_list() server = Larva(modules, host='0.0.0.0', port=8080, auth=auth) app = server.app exec_command("chmod 660 /var/db/larva.db") exec_command("chown root:admin /var/db/larva.db") app.wsgi_app = ProxyFix(app.wsgi_app)
def poweroff(self): """ Power OFF """ exec_command("poweroff")