diff --git a/patchwork/git.py b/patchwork/git.py new file mode 100644 index 0000000..90ec545 --- /dev/null +++ b/patchwork/git.py @@ -0,0 +1,10 @@ +from fabric.api import cd, run +from fabric.contrib.files import exists + + +def clone(repo, path, pull_cmd="git pull"): + if not exists(path): + run("git clone %s %s" % (repo, path)) + else: + with cd(path): + run(pull_cmd) diff --git a/patchwork/info.py b/patchwork/info.py index 5394f74..d4dd122 100644 --- a/patchwork/info.py +++ b/patchwork/info.py @@ -1,7 +1,8 @@ +from fabric.api import run, settings, hide from fabric.contrib.files import exists -def distro_name(): +def distro_name(runner=run): """ Return simple Linux distribution name identifier, e.g. ``"fedora"``. @@ -23,6 +24,12 @@ def distro_name(): for sentinel in sentinels: if exists('/etc/%s' % sentinel): return name + + with settings(hide('everything'), warn_only=True): + distro = runner('lsb_release --short --id') + if distro.succeeded: + return distro.lower() + return "other" diff --git a/patchwork/packages/__init__.py b/patchwork/packages/__init__.py index 13dce4e..9cbba89 100644 --- a/patchwork/packages/__init__.py +++ b/patchwork/packages/__init__.py @@ -1,3 +1,5 @@ +from functools import wraps + from fabric.api import sudo from patchwork.info import distro_family @@ -14,8 +16,8 @@ def package(*packages): # Run from cache vs updating package lists every time; assume 'yes'. yum = "yum install -y %s" manager = apt if distro_family() == "debian" else yum - for package in packages: - sudo(manager % package) + + sudo(manager % " ".join(packages)) def rubygem(gem): @@ -23,3 +25,21 @@ def rubygem(gem): Install a Rubygem """ return sudo("gem install -b --no-rdoc --no-ri %s" % gem) + + +class requires_packages(object): + """ + A decorator that ensures the listed packages are installed. Example: + + @task + @requires_packages('python-dev', 'redis-server', 'nginx') + def my_task(): ... + """ + def __init__(self, *args): + self.packages = args + + def __call__(self, fn, *args, **kwargs): + def wrapper(): + package(*self.packages) + fn(*args, **kwargs) + return wraps(fn)(wrapper) diff --git a/patchwork/supervisor.py b/patchwork/supervisor.py new file mode 100644 index 0000000..509c17b --- /dev/null +++ b/patchwork/supervisor.py @@ -0,0 +1,19 @@ +import time + +from fabric.api import sudo +from fabric.contrib.files import upload_template + + +def supervise(conf, destination='/etc/supervisor/conf.d', **kwargs): + """ + Installs a service into supervisor. ``kwargs`` are passed onto the upload_template call. + """ + if 'use_sudo' not in kwargs: + kwargs['use_sudo'] = True + + upload_template(conf, destination, **kwargs) + + sudo("service supervisor stop") + time.sleep(2) + sudo("service supervisor start") + sudo("supervisorctl status")