Commits

Vladimir Mihailenco  committed 712c654

Initial commit

  • Participants

Comments (0)

Files changed (7)

+syntax: glob
+*.pyc

File fab_deploy_ext/__init__.py

+from fab_deploy_ext.file import *
+from fab_deploy_ext.ssh import *
+from fab_deploy_ext.maintenance import *
+from fab_deploy_ext.rabbitmq import *

File fab_deploy_ext/file.py

+from fabric.api import run
+from fabric.api import local
+from fabric.api import sudo
+from fabric.api import output
+from fabric.api import settings
+from fabric.api import hide
+
+
+from fab_deploy_ext import utils
+
+
+__all__ = ['file_list', 'file_read']
+
+
+def file_list(dir):
+    with settings(hide('running', 'stdout')):
+        # -1 - list one file per line
+        out = run('ls -1 %s' % dir)
+    for file in utils.split_lines(out):
+        yield dir + '/' + file
+
+
+def file_read(path, use_sudo=False):
+    cmd = sudo if use_sudo else run
+    with settings(hide('running', 'stdout')):
+        out = cmd('cat %s' % path)
+    return out

File fab_deploy_ext/maintenance.py

+import os
+
+import functools
+import contextlib
+
+from fabric.api import env, run
+from fab_deploy import apache
+
+
+__all__ = ['maintenance_start', 'maintenance_stop']
+
+
+def maintenance_start():
+    dirname = os.path.dirname(env.conf.MAINTENANCE_FILE)
+
+    run('mkdir -p %s' % dirname)
+    run('touch %s' % env.conf.MAINTENANCE_FILE)
+    apache.touch()
+
+
+def maintenance_stop():
+    run('rm %s' % env.conf.MAINTENANCE_FILE)
+    apache.touch()
+
+
+@contextlib.contextmanager
+def in_maintenance():
+    maintenance_start()
+    yield
+    maintenance_stop()
+
+
+def maintenance(func):
+    @functools.wraps(func)
+    def inner(*args, **kwargs):
+        with in_maintenance():
+            return func(*args, **kwargs)
+    return inner

File fab_deploy_ext/rabbitmq.py

+from fabric.api import sudo
+from fab_deploy import system
+from fab_deploy import utils
+
+
+__all__ = ['rabbitmq_setup', 'rabbitmq_add_user', 'rabbitmq_add_vhost',
+           'rabbitmq_set_permissions']
+
+
+def rabbitmq_setup():
+    system.aptitude_install('rabbitmq-server')
+
+
+@utils.run_as_sudo
+def rabbitmq_add_user(username, password):
+    sudo('rabbitmqctl add_user %s %s' % (username, password), pty=True)
+
+
+@utils.run_as_sudo
+def rabbitmq_add_vhost(vhost):
+    sudo('rabbitmqctl add_vhost %s' % (vhost), pty=True)
+
+
+@utils.run_as_sudo
+def rabbitmq_set_permissions(username, vhost, configure='.*', write='.*', read='.*'):
+    cmd = 'rabbitmqctl set_permissions -p %s %s "%s" "%s" "%s"' % (vhost,
+        username, configure, write, read)
+    sudo(cmd, pty=True)

File fab_deploy_ext/ssh.py

+import re
+
+from fabric.api import get
+from fabric.contrib.files import comment
+from fabric.contrib.files import uncomment
+from fabric.contrib.files import exists
+
+from fab_deploy_ext import file
+from fab_deploy_ext import utils
+
+
+__all__ = ['ssh_list_files', 'ssh_list_keys', 'ssh_disable_key',
+           'ssh_enable_key']
+
+
+def _get_home_dirs(users):
+    if users is None:
+        dirs = file.file_list('/home')
+    else:
+        dirs = []
+        for user in users:
+            dirs.append('/home/%s' % user)
+    return dirs
+
+
+def ssh_list_files(users):
+    dirs = _get_home_dirs(users)
+    for dir in dirs:
+        authorized_keys = '%s/.ssh/authorized_keys' % dir
+        if exists(authorized_keys):
+            yield authorized_keys
+
+
+def ssh_list_keys(users=None, include_comments=False, use_sudo=False):
+    for authorized_keys in ssh_list_files(users):
+        content = file.file_read(authorized_keys, use_sudo=True)
+        for ssh_key in utils.split_lines(content):
+            if not include_comments and ssh_key.startswith('#'):
+                continue
+            yield ssh_key
+
+
+def ssh_disable_key(key, users=None, use_sudo=False):
+    regex = '%s' % re.escape(key)
+    regex = regex.replace('\/', '/')
+
+    for authorized_keys in ssh_list_files(users):
+        comment(authorized_keys, regex, use_sudo=use_sudo)
+
+
+def ssh_enable_key(key, users=None, use_sudo=False):
+    regex = '%s' % re.escape(key)
+
+    for authorized_keys in ssh_list_files(users):
+        uncomment(authorized_keys, regex, use_sudo=use_sudo)

File fab_deploy_ext/utils.py

+def split_lines(str):
+    lines = str.split('\n')
+    for line in lines:
+        yield line.rstrip('\r')