Skip to content

Instantly share code, notes, and snippets.

@sofa420
Forked from kmpm/fabfile.py
Created July 30, 2012 17:55
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sofa420/3208700 to your computer and use it in GitHub Desktop.
Save sofa420/3208700 to your computer and use it in GitHub Desktop.
Fabric deployment of Node.js on Ubuntu
from __future__ import with_statement
from fabric.api import env
from fabric.api import *
from fabric.contrib.console import confirm
from fabric.contrib.project import upload_project
import ubuntu
import protobuf
def staging():
env.hosts = ['10.100.0.70',]
env.remote_admin = 'sysadmin'
def ssh():
for host in env.hosts:
local("ssh-copy-id %s@%s" % (env.remote_admin, host))
def hostconfig():
ubuntu.apt_upgrade()
ubuntu.install_git()
ubuntu.install_mongodb()
ubuntu.install_nodejs()
def install_protobuf():
protobuf.install_protobuf()
protobuf.install_protobuf_node()
def testapp():
install_protobuf()
with cd('~/projects'):
upload_project('./receiver', '/home/sysadmin/projects')
ubuntu.upstart("receiver", "app.js",
user=env.remote_admin
, home="/home/%s" % env.remote_admin
,location_dir='/home/sysadmin/projects/receiver/')
sudo("stop receiver")
sudo("start receiver")
from fabric.api import *
from fabric.contrib import files
import ubuntu
def install_protobuf():
if not ubuntu.cmd_exists('protoc'):
version="2.4.1"
folder="protobuf-%s" % version
filename="%s.tar.bz2" % folder
source="http://protobuf.googlecode.com/files/%s" & filename
with cd('/tmp'):
run("wget %s" % source)
run("tar -jxf %s" % filename)
with cd('/tmp/%s' % folder):
run("./configure --prefix=/usr")
run("make")
sudo("make install")
with cd('/tmp/'):
run("rm -Rf %s*" % folder)
def install_protobuf_node():
if not files.exists(".node_libraries/protobuf_for_node.node"):
if not ubuntu.cmd_exists('hg'):
ubuntu.apt_install('mercurial')
source="hg clone https://code.google.com/p/protobuf-for-node/"
with cd('/tmp'):
run(source)
with cd('/tmp/protobuf-for-node'):
run("PROTOBUF=/usr node-waf configure clean build")
run("node-waf install")
with cd('/tmp'):
run("rm -Rf protobuf-for-node")
from fabric.api import *
from fabric.contrib import files
SOURCES_D="/etc/apt/sources.list.d"
__all__= ['cmd_exists', 'apt', 'apt_upgrade']
def cmd_exists(cmd):
#TODO: some sort of run test of the command perhaps
return files.exists("/usr/bin/%s" % cmd)
def apt(cmdline):
if not cmd_exists('aptitude'):
sudo("apt-get -y install aptitude")
sudo("aptitude %s" % cmdline)
def apt_install(packages):
apt("-y install %s" % packages)
def apt_update():
apt("update")
def apt_upgrade():
apt_update()
apt("-y safe-upgrade")
def install_git():
if not cmd_exists('git'):
apt_install("build-essential git git-core")
run("git config --global color.ui true")
def install_mongodb():
with cd(SOURCES_D):
if not files.exists('mongodb.list'):
sudo('echo "deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen">mongodb.list')
sudo("sudo apt-key adv --keyserver keyserver.ubuntu.com --recv 7F0CEB10")
apt_update()
apt_install("mongodb-10gen")
def install_nodejs():
listfile='chris-lea-node_js-natty.list'
with cd(SOURCES_D):
if not files.exists(listfile):
files.append(listfile, "deb http://ppa.launchpad.net/chris-lea/node.js/ubuntu natty main", use_sudo=True)
sudo("sudo apt-key adv --keyserver keyserver.ubuntu.com --recv C7917B12")
apt_update()
if not cmd_exists('node'):
apt_install("nodejs nodejs-dev")
#npm
if not cmd_exists('curl'):
apt_install("curl")
if not cmd_exists('npm'):
sudo("curl http://npmjs.org/install.sh | sh")
def upstart(appname, script, force=False, location_dir="/tmp", home="/root", user="root", description="Node.js server App"):
dst="/etc/init/%s.conf" % appname
logfile="/var/log/%s.log" % appname
context = {'appname':appname
, 'script':script
, 'logfile':logfile
, 'location_dir':location_dir
, 'home':home
, 'user':user
, 'description':description}
if not files.exists(dst) or force:
files.upload_template('upstart.template'
, dst
, use_sudo=True
, context=context)
sudo("chown root:root %s" % dst)
sudo("touch %s" % logfile)
sudo("chown %(user)s:%(user)s %(logfile)s" % context)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment