Created
October 21, 2012 12:24
-
-
Save hermansc/3926847 to your computer and use it in GitHub Desktop.
fabfile.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from fabric.api import * | |
import subprocess, shlex, atexit, time | |
@task | |
def deploy(): | |
env.user = prompt('Your username at login and deployment box: ') | |
prod = SSHTunnel(env.user, 'loginserver', 'productionserver') | |
env.hosts = [prod.entrance()] | |
env.host_string = prod.entrance() | |
print "[INFO] Running SSH commands to production server through tunnel at localhost:2022" | |
sudo('su deploymentuser -s /bin/bash) | |
with cd('/home/deploymentuser/productioncode/'): | |
run('git pull') | |
print('Last 5 tags: ') | |
run('git tag | tail -n 5') | |
tag = prompt('Give a new tag for this deployment: ') | |
run('git tag %s' % tag) | |
run('git push --tags') | |
run('git push') | |
run('invoke-rc.d production.fcgi restart') | |
class SSHTunnel: | |
""" | |
http://www.alleyinteractive.com/blog/ssh-tunnel-fabric/ | |
""" | |
def __init__(self, bridge_user, bridge_host, dest_host, bridge_port=22, dest_port=22,\ | |
local_port=2022, timeout=15): | |
self.local_port = local_port | |
cmd = 'ssh -vAN -L %d:%s:%d %s@%s' % (local_port, dest_host, dest_port, bridge_user, bridge_host) | |
self.p = subprocess.Popen(shlex.split(cmd), stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
start_time = time.time() | |
atexit.register(self.p.kill) | |
while not 'Entering interactive session' in self.p.stderr.readline(): | |
if time.time() > start_time + timeout: | |
raise Exception("SSH tunnel timed out") | |
def entrance(self): | |
return 'localhost:%d' % self.local_port |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment