Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some small features I added #4

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions patchwork/git.py
Original file line number Diff line number Diff line change
@@ -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)
9 changes: 8 additions & 1 deletion patchwork/info.py
Original file line number Diff line number Diff line change
@@ -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"``.

Expand All @@ -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"


Expand Down
24 changes: 22 additions & 2 deletions patchwork/packages/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from functools import wraps

from fabric.api import sudo
from patchwork.info import distro_family

Expand All @@ -14,12 +16,30 @@ 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):
"""
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)
19 changes: 19 additions & 0 deletions patchwork/supervisor.py
Original file line number Diff line number Diff line change
@@ -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")