Ejemplo n.º 1
0
def reboot_and_wait(wait=600, command='reboot'):
    """Reboot the remote system.

    Args:
        wait: Time to wait remote system after reboot in seconds.
        command: Command for rebooting remote system.

    Returns:
        None
    """
    # Shorter timeout for a more granular cycle than the default.
    timeout = 10
    # Use 'wait' as max total wait time
    attempts = int(round(float(wait) / float(timeout)))
    # Don't bleed settings, since this is supposed to be self-contained.
    # User adaptations will probably want to drop the "with settings()" and
    # just have globally set timeout/attempts values.
    with settings(timeout=timeout,
                  connection_attempts=attempts,
                  warn_only=True):
        run(command)
        # Try to make sure we don't slip in before pre-reboot lockdown
        time.sleep(10)
        # This is actually an internal-ish API call, but users can simply drop
        # it in real fabfile use -- the next run/sudo/put/get/etc call will
        # automatically trigger a reconnect.
        # We use it here to force the reconnect while this function is still in
        # control and has the above timeout settings enabled.
        connections.connect(env.host_string)
Ejemplo n.º 2
0
def disable_selinux():
    """Disable SELinux.

    Edit ``/etc/selinux/config`` and write ``SELINUX=disabled`` to it.
    Also call ``setenforce 0`` to switch SELinux into Permissive mode.

    Returns:
        True if ``/etc/selinux/config`` changed or if SELinux ``Enforcing`` mode switched into ``Permissive`` mode, False otherwise.

    """
    if run('if [ -e /etc/selinux/config ] ; then echo exists ; fi'
           ) == 'exists':
        changed1 = edit_file(
            '/etc/selinux/config',
            replace_line(r'\s*SELINUX\s*=\s*.*', 'SELINUX=disabled'))
    else:
        changed1 = False
    if run('if [ -e /usr/sbin/setenforce ] && [ -e /usr/sbin/getenforce ] ; then echo exists ; fi'
           ) == 'exists':
        changed2 = run(
            'STATUS=$(getenforce) ; if [ "$STATUS" == "Enforcing" ] ; then setenforce 0 ; echo perm ; fi'
        ) == 'perm'
    else:
        changed2 = False
    return changed1 or changed2
Ejemplo n.º 3
0
def localectl_set_locale(locale):
    """localectl set-locale ``name``.

    For example, ``localectl_set_locale('LANG=en_US.UTF-8')``.

    """
    return run('localectl set-locale ' + locale)
Ejemplo n.º 4
0
def timedatectl_set_timezone(timezone):
    """timedatectl set-timezone ``timezone``.

    For example, ``timedatectl_set_timezone('Europe/Kiev')``.

    """
    return run('timedatectl set-timezone ' + timezone)
Ejemplo n.º 5
0
def systemctl_set_default(name):
    """systemctl set-default ``name``.

    For example, ``systemctl_set_default('multi-user.target')``

    """
    return run('systemctl daemon-reload ; systemctl set-default ' + name +
               ' ; systemctl daemon-reload')
Ejemplo n.º 6
0
def systemctl_get_default():
    """systemctl get-default.

    Returns:
        Output of command ``systemctl get-default``.
    """
    return run(
        'systemctl daemon-reload ; systemctl get-default ; systemctl daemon-reload'
    )
Ejemplo n.º 7
0
def test_run(monkeypatch):
    run_state = {
        r'command': {
            'stdout': 'stdout',
            'failed': False
        },
    }
    mock_run = mock_run_factory(run_state)
    monkeypatch.setattr(fabric.api, 'run', mock_run)
    assert run('command') == 'stdout'
Ejemplo n.º 8
0
def is_user_in_group(username, groupname):
    """is user in group?
    """
    group = run("getent group")
    for line in group.split("\n"):
        line = line.strip()
        if not line:
            continue
        if line.split(":")[0] == groupname:
            members = line.split(":")[3].split(",")
            return username in members
    return False
Ejemplo n.º 9
0
def is_reboot_required():
    """Is reboot required?

    .. note::
        This function uses internally ``/usr/bin/needs-restarting`` from ``yum-utils`` package.

    Returns:
        True if server reboot is required after ``yum_update()``, False otherwise.
    """
    if run('if [ ! -e /usr/bin/needs-restarting ] ; then echo notexists ; fi'
           ) == 'notexists':
        run('yum -y install yum-utils')
    with settings(warn_only=True):
        stdout = run('/usr/bin/needs-restarting -r')
    reboot_required_regexp = re.compile(
        r'^Reboot is required to ensure that your system benefits from these updates\.$'
    )
    reboot_required = False
    for line in stdout.split('\n'):
        line = line.strip()
        if reboot_required_regexp.match(line):
            reboot_required = True
            break
    return reboot_required
Ejemplo n.º 10
0
def get_virtualization_type():
    """Get virtualization type.

    Returns:
        None if no vitrualization detected, or vitrualization type as string, for example, "openvz" or "kvm" or something else.
    """
    stdout = run('hostnamectl status')
    virtualization_type = None
    virtualization_line_regexp = re.compile(
        r'^\s*Virtualization:\s(?P<virtualization_type>\w+)\s*$')
    for line in stdout.split('\n'):
        match = virtualization_line_regexp.match(line)
        if match:
            virtualization_type = match.group('virtualization_type')
            break
    return virtualization_type
Ejemplo n.º 11
0
def is_group_exists(name):
    """is group exists?

    Args:
        name: group name

    Returns:
        True if group exists, False if group not exists.
    """
    group = run("getent group")
    for line in group.split("\n"):
        line = line.strip()
        if not line:
            continue
        if line.split(":")[0] == name:
            return True
    return False
Ejemplo n.º 12
0
def get_user_home_directory(name):
    """get user home directory

    Args:
        name: user name

    Returns:
        Home directory if user exists or None if user not exists.
    """
    passwd = run("getent passwd")
    for line in passwd.split("\n"):
        line = line.strip()
        if not line:
            continue
        if line.split(":")[0] == name:
            return line.split(":")[5]
    return None
Ejemplo n.º 13
0
def is_user_exists(name):
    """is user exists?

    Args:
        name: user name

    Returns:
        True if user exists, False if user not exists.
    """
    passwd = run("getent passwd")
    for line in passwd.split("\n"):
        line = line.strip()
        if not line:
            continue
        if line.split(":")[0] == name:
            return True
    return False
Ejemplo n.º 14
0
def yum_remove(*args):
    """yum remove package/packages.

    .. note::
        At least one package name must be specified.

    Args:
        args: String with package names with spaces or newlines as separators.
            Or list of such strings. Or any recursive combination of such lists.

    Returns:
        True if some packages removed, False otherwise.
    """
    packages = _parse_packages(0, False, *args)
    command = "yum -y remove " + " ".join(packages)
    stdout = run(command)
    return _is_changed(stdout, r'^No Packages marked for removal$')
Ejemplo n.º 15
0
def yum_update(*args):
    """yum update package/packages.

    .. note::
        List of packages can be empty. In this case all packages will be updated.

    Args:
        args: String with package names with spaces or newlines as separators.
            Or list of such strings. Or any recursive combination of such lists.

    Returns:
        True if some packages updated, False otherwise.
    """
    if not args:
        packages = list()
    else:
        packages = _parse_packages(0, True, *args)
    command = "yum -y update " + " ".join(packages)
    stdout = run(command)
    return _is_changed(stdout, r'^No packages marked for update$')
Ejemplo n.º 16
0
def systemctl_start(name):
    """systemctl start ``name``.
    """
    run('systemctl daemon-reload ; systemctl start ' + name +
        ' ; systemctl daemon-reload')
Ejemplo n.º 17
0
def create_group(name):
    """create group if it not exists
    """
    if is_group_not_exists(name):
        run("groupadd %s" % name)
Ejemplo n.º 18
0
def remove_group(name):
    """remove group if it exists
    """
    if is_group_exists(name):
        run("groupdel %s" % name)
Ejemplo n.º 19
0
def add_user_to_group(username, groupname):
    """add user to group
    """
    if is_user_not_in_group(username, groupname):
        run("gpasswd --add %s %s" % (username, groupname))
Ejemplo n.º 20
0
def delete_user_from_group(username, groupname):
    """delete user_from group
    """
    if is_user_in_group(username, groupname):
        run("gpasswd --delete %s %s" % (username, groupname))
Ejemplo n.º 21
0
def create_user(name):
    """create user if it not exists
    """
    if is_user_not_exists(name):
        run("useradd %s --comment %s" % (name, name))
Ejemplo n.º 22
0
def remove_user(name):
    """remove user if it exists
    """
    if is_user_exists(name):
        run("userdel %s" % name)
Ejemplo n.º 23
0
def systemctl_preset(name):
    """systemctl preset ``name``.
    """
    run('systemctl daemon-reload ; systemctl preset ' + name +
        ' ; systemctl daemon-reload')
Ejemplo n.º 24
0
def systemctl_unmask(name):
    """systemctl unmask ``name``.
    """
    run('systemctl daemon-reload ; systemctl unmask ' + name +
        ' ; systemctl daemon-reload')
Ejemplo n.º 25
0
def systemctl_disable(name):
    """systemctl disable ``name``.
    """
    run('systemctl daemon-reload ; systemctl disable ' + name +
        ' ; systemctl daemon-reload')
Ejemplo n.º 26
0
def systemctl_reload(name):
    """systemctl reload ``name``.
    """
    run('systemctl daemon-reload ; systemctl reload ' + name +
        ' ; systemctl daemon-reload')
Ejemplo n.º 27
0
def systemctl_stop(name):
    """systemctl stop ``name``.
    """
    run('systemctl daemon-reload ; systemctl stop ' + name +
        ' ; systemctl daemon-reload')