Commits

Vladimir Mihailenco committed 2652bab

Add celery, supervisor and setup.py

Comments (0)

Files changed (6)

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 *
+from fab_deploy_ext.rabbitmq import *
+from fab_deploy_ext.celery import *
+from fab_deploy_ext.supervisor import *
+
+
+VERSION = (0, 1, 0, 'alpha', 0)
+
+def get_version():
+    version = '%s.%s' % (VERSION[0], VERSION[1])
+    if VERSION[2]:
+        version = '%s.%s' % (version, VERSION[2])
+    if VERSION[3:] == ('alpha', 0):
+        version = '%s pre-alpha' % version
+    else:
+        if VERSION[3] != 'final':
+            version = '%s %s %s' % (version, VERSION[3], VERSION[4])
+    return version

fab_deploy_ext/celery.py

+from fabric.api import run
+from fabric.api import sudo
+
+from fab_deploy import utils
+
+
+__all__ = ['celery_create_log_dir', 'celeryctl']
+
+
+@utils.run_as_sudo
+def celery_create_log_dir():
+    sudo('mkdir --parents /var/log/supervisord')
+    sudo('mkdir --parents /var/log/celery')
+
+@utils.inside_project
+def celeryctl(command=''):
+    run('./manage.py celeryctl %s' % command)

fab_deploy_ext/file.py

 from fabric.api import settings
 from fabric.api import hide
 
-
 from fab_deploy_ext import utils
 
 

fab_deploy_ext/ssh.py

+import os
 import re
 import datetime
 
            'ssh_enable_key']
 
 
-def _get_home_dirs(users):
+def _get_users(users, exclude_users=None):
+    if exclude_users is None:
+        exclude_users = []
     if users is None:
-        dirs = file.file_list('/home')
-        dirs.append('/root')
+        if 'root' not in exclude_users:
+            yield 'root'
+        for dir in file.file_list('/home'):
+            user = os.path.basename(dir)
+            if user not in exclude_users:
+                yield user
     else:
-        dirs = []
+        if exclude_users:
+            users = set(users) - set(exclude_users)
         for user in users:
-            if user == 'root':
-                dirs.append('/root')
-            else:
-                dirs.append('/home/%s' % user)
-    return dirs
+            yield user
 
 
-def ssh_list_files(users):
-    dirs = _get_home_dirs(users)
-    for dir in dirs:
+def _get_user_dir(user):
+    if user == 'root':
+        return '/root'
+    else:
+        return '/home/%s' % user
+
+
+def ssh_list_files(users, exclude_users=None):
+    users = _get_users(users, exclude_users=exclude_users)
+    for user in users:
+        dir = _get_user_dir(user)
         authorized_keys = '%s/.ssh/authorized_keys' % dir
-        if exists(authorized_keys):
-            yield authorized_keys
+        if exists(authorized_keys, use_sudo=True):
+            yield user, authorized_keys
 
 
-def ssh_list_keys(users=None, include_comments=False, use_sudo=False):
-    for authorized_keys in ssh_list_files(users):
+def ssh_list_keys(users=None, exclude_users=None,  include_comments=False,
+                  use_sudo=False):
+    for user, authorized_keys in ssh_list_files(users, exclude_users=exclude_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
+            yield user, ssh_key
 
 
-def ssh_disable_key(key, users=None, use_sudo=False):
+def ssh_disable_key(key, users=None, exclude_users=None, use_sudo=False):
     regex = '%s' % re.escape(key)
     regex = regex.replace('\/', '/')
 
     now = datetime.datetime.now().strftime('%Y.%m.%d-%H.%M')
     backup = '.%s.bak' % now
-    for authorized_keys in ssh_list_files(users):
+    for user, authorized_keys in ssh_list_files(users, exclude_users=exclude_users):
         comment(authorized_keys, regex, use_sudo=use_sudo, backup=backup)
 
 
-def ssh_enable_key(key, users=None, use_sudo=False):
+def ssh_enable_key(key, users=None, exclude_users=None, use_sudo=False):
     regex = '%s' % re.escape(key)
 
     now = datetime.datetime.now().strftime('%Y.%m.%d-%H.%M')
     backup = '.%s.bak' % now
-    for authorized_keys in ssh_list_files(users):
+    for user, authorized_keys in ssh_list_files(users, exclude_users=exclude_users):
         uncomment(authorized_keys, regex, use_sudo=use_sudo, backup=backup)

fab_deploy_ext/supervisor.py

+from fabric.api import sudo
+from fabric.api import cd
+
+from fab_deploy import utils
+
+
+__all__ = ['supervisor_install', 'supervisord', 'supervisorctl',
+           'supervisor_create_log_dir']
+
+
+@utils.run_as_sudo
+def supervisor_install():
+    sudo('pip install --upgrade supervisor')
+
+
+@utils.run_as_sudo
+def supervisord():
+    with cd(env.conf.SRC_DIR):
+        sudo('supervisord --configuration=/etc/supervisord.conf')
+
+
+@utils.run_as_sudo
+def supervisorctl(command=''):
+    with cd(env.conf.SRC_DIR):
+        sudo('supervisorctl --configuration=/etc/supervisord.conf %s' % command)
+
+
+@utils.run_as_sudo
+def supervisor_create_log_dir():
+    sudo('mkdir --parents /var/log/supervisord')
+from distutils.core import setup
+
+
+version = __import__('fab_deploy_ext').get_version()
+
+
+setup(
+    name='django-fab-deploy-ext',
+    version=version.replace(' ', '-'),
+    description='Django fab deploy extensions',
+    author='Vladimir Mihailenco',
+    author_email='vladimir.webdev@gmail.com',
+    url='',
+    install_requires=['fabric', 'django-fab-deploy', 'jinja2'],
+    packages=['fab_deploy_ext'],
+    classifiers=[
+        'Development Status :: 2 - Pre-Alpha',
+        'Environment :: Web Environment',
+        'Framework :: Django',
+        'Intended Audience :: Developers',
+        'License :: OSI Approved',
+        'Operating System :: OS Independent',
+        'Programming Language :: Python',
+        'Topic :: Software Development :: Libraries :: Python Modules',
+    ],
+)