def tune2fs(device, maxcounts='0', interval='0'): """ Adjust filesystem parameters. """ if not ismounted('%(device)s' % locals()): with settings(hide('stdout', 'warnings'), warn_only=True): run_as_root('tune2fs -c %(maxcounts)s -i %(interval)s %(device)s' % locals()) else: abort("Partition is mounted")
def upload_sudo_lcmd(): """ Delivers /usr/local/bin/sudo_lcmd """ src = os.path.join(TEMPLATE_DIR, 'sudoers', 'sudo_lcmd') dest = '/usr/local/bin/sudo_lcmd' put(src, dest, use_sudo=True, mode=0755) with settings(hide('stdout')): run_as_root('chown root:root %s' % dest)
def create(name, gid=None): """ Create a new group. """ args = [] if gid: args.append('-g %s' % quote(gid)) args.append(name) args = ' '.join(args) run_as_root('groupadd %s' % args)
def lvcreate(lvname, size, vgname): """ Create a logical volum. """ if not vg_exists(vgname): abort('VG %(vgname)s does not exist.' % locals()) if not lv_exists(lvname, vgname): with settings(hide('stdout', 'warnings'), warn_only=True): run_as_root('lvcreate -L %(size)s -n %(lvname)s %(vgname)s' % locals()) else: abort('LV %(lvname)s already exists.' % locals())
def mkfs(device, ftype): """ Format filesystem Example:: from lib.disk import mkfs mkfs('/dev/sda2', 'ext4') """ if not ismounted('%(device)s' % locals()): with settings(hide('stdout', 'warnings'), warn_only=True): run_as_root('mkfs.%(ftype)s %(device)s' % locals()) else: abort("Partition is mounted")
def lv_exists(lvname, vgname): """ Check if logical volum exists. """ with settings(hide('running', 'stdout', 'warnings'), warn_only=True): res = run_as_root('lvs --noheadings -o lv_name %(vgname)s' % locals()) return lvname in res.split()
def mount(device, mountpoint): """ Mount a partition Warning : provide LVM partition w/ the following format: /dev/mapper/vgname-lvname Example:: from lib.disk import mount mount('/dev/sdb1', '/mnt/usb_drive') mount('/dev/mapper/vg00-lv01', '/mnt/example') """ if not ismounted(device): run_as_root('mount %(device)s %(mountpoint)s' % locals())
def is_installed(rpm): """Check if an RPM package is installed.""" with settings(hide('running', 'stdout', 'stderr', 'warnings'), warn_only=True): res = run_as_root('yum list installed %s' % quote(rpm)) if res.succeeded: return True return False
def modify(name, comment=None, home=None, move_current_home=False, group=None, extra_groups=None, login_name=None, password=None, shell=None, uid=None, non_unique=False): """ Modify an existing user. """ args = [] if comment: args.append('-c %s' % quote(comment)) if home: args.append('-d %s' % quote(home)) if move_current_home: args.append('-m') if group: args.append('-g %s' % quote(group)) if extra_groups: groups = ','.join(quote(group) for group in extra_groups) args.append('-G %s' % groups) if login_name: args.append('-l %s' % quote(login_name)) if password: args.append('-p 0') if shell: args.append('-s %s' % quote(shell)) if uid: args.append('-u %s' % quote(uid)) if non_unique: args.append('-o') if args: args.append(name) args = ' '.join(args) run_as_root('usermod %s' % args)
def create(mountpoint, size, vgname="vgappli", user='******', group='root', permissions='0755', type='ext4'): """ Create a filesystem """ # As a rule, we change `/' to `_' in the lvname and omit the first one. # We also replace `-' by `_' or else device mapper change it to `--' lvname = '_'.join(mountpoint.split('/')[1:]).replace('-', '_') device = os.path.join('/dev', vgname, lvname) dm = os.path.join('/dev/mapper/', '-'.join([vgname, lvname])) lvcreate(lvname, size, vgname) # Pass dm as arg because mkfs() check if the device is already mount # parsing the output of `mount` command and LVM partition are listed # with their device mapper. mkfs(dm, type) tune2fs(dm) if not is_dir(mountpoint): run_as_root('umask 0022 && mkdir -p %(mountpoint)s' % locals()) mount(device, mountpoint) add_fstab(device, mountpoint, type) # setup permissions run_as_root('chown %(user)s:%(group)s %(mountpoint)s' % locals()) run_as_root('chmod %(permissions)s %(mountpoint)s' % locals())
def ismounted(device): """ Check if partition is mounted Example:: from lib.disk import ismounted if ismounted('/dev/sda1'): print ("disk sda1 is mounted") """ # Check filesystem with settings(hide('running', 'stdout')): res = run_as_root('mount') for line in res.splitlines(): fields = line.split() if fields[0] == device: return True return False
def vg_exists(vgname): """ Check if volum group exists. """ with settings(hide('running', 'stdout', 'warnings'), warn_only=True): return run_as_root('vgs %(vgname)s' % locals()).succeeded
def create(name, comment=None, home=None, create_home=None, skeleton_dir=None, group=None, create_group=True, extra_groups=None, password=None, system=False, shell=None, uid=None, non_unique=False): """ Create a new user and its home directory. If *create_home* is ``None`` (the default), a home directory will be created for normal users, but not for system users. You can override the default behaviour by setting *create_home* to ``True`` or ``False``. If *system* is ``True``, the user will be a system account. Its UID will be chosen in a specific range, and it will not have a home directory, unless you explicitely set *create_home* to ``True``. If *shell* is ``None``, the user's login shell will be the system's default login shell (usually ``/bin/bash``). If *password* is ``True``, the password will be set to 0 (to allow SSH connection) """ # Note that we use useradd (and not adduser), as it is the most # portable command to create users across various distributions: # http://refspecs.linuxbase.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/useradd.html args = [] if comment: args.append('-c %s' % quote(comment)) if home: args.append('-d %s' % quote(home)) if group: args.append('-g %s' % quote(group)) if create_group: if not _group_exists(group): _group_create(group) if extra_groups: groups = ','.join(quote(group) for group in extra_groups) args.append('-G %s' % groups) if create_home is None: create_home = not system if create_home is True: args.append('-m') elif create_home is False: args.append('-M') if skeleton_dir: args.append('-k %s' % quote(skeleton_dir)) if password: args.append('-p 0') if system: args.append('-r') if shell: args.append('-s %s' % quote(shell)) if uid: args.append('-u %s' % quote(uid)) if non_unique: args.append('-o') args.append(name) args = ' '.join(args) run_as_root('useradd %s' % args)
def delete(name): """ Delete an existing user. """ if exists(name): run_as_root('userdel %s' % quote(name))