Skip to content

Instantly share code, notes, and snippets.

@ingenieroariel
Created November 4, 2010 02:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ingenieroariel/662072 to your computer and use it in GitHub Desktop.
Save ingenieroariel/662072 to your computer and use it in GitHub Desktop.
Geonode Amazon EC2 fabfile
# easy_install boto fabric
# The only pre-req is having created a keypair via the amazon web interface
#
# Usage:
# fab geonode
from fabric.api import env, sudo, run, cd
import os, time, boto
import ConfigParser
CONFIG_FILE=".gnec2.cfg"
MAVERIK_64="ami-688c7801"
MAVERIK_32="ami-1a837773"
LUCID_64="ami-da0cf8b3"
LUCID_32="ami-a403f7cd"
config = ConfigParser.RawConfigParser()
# If there is no config file, let's write one.
if not os.path.exists(CONFIG_FILE):
config.add_section('ec2')
config.set('ec2', 'AMI', LUCID_32)
config.set('ec2', 'INSTANCE_TYPE', 'm1.small')
config.set('ec2', 'SECURITY_GROUP', 'geonode')
config.set('ec2', 'KEY_PATH', 'geonode.pem')
config.set('ec2', 'AWS_ACCESS_KEY_ID', '')
config.set('ec2', 'AWS_SECRET_ACCESS_KEY', '')
config.set('ec2', 'USER', 'ubuntu')
# Writing our configuration file to CONFIG_FILE
with open(CONFIG_FILE, 'wb') as configfile:
config.write(configfile)
else:
config.read(CONFIG_FILE)
MY_AMI=config.get('ec2', 'AMI')
SECURITY_GROUP=config.get('ec2', 'SECURITY_GROUP')
KEY_PATH = config.get('ec2', 'KEY_PATH')
INSTANCE_TYPE = config.get('ec2', 'INSTANCE_TYPE')
os.environ["AWS_ACCESS_KEY_ID"]=config.get('ec2', 'AWS_ACCESS_KEY_ID')
os.environ["AWS_SECRET_ACCESS_KEY"]=config.get('ec2', 'AWS_SECRET_ACCESS_KEY')
host = None
try:
host = config.get('ec2', 'HOST')
except:
pass
if host is not None and host != '':
env.hosts = [host,]
env.user = config.get('ec2', 'USER')
env.key_filename = KEY_PATH
print "Instance already created, using it: %s" % host
else:
conn = boto.connect_ec2()
image = conn.get_image(MY_AMI)
security_groups = conn.get_all_security_groups()
try:
[geonode_group] = [x for x in security_groups if x.name == SECURITY_GROUP]
except ValueError:
# this probably means the security group is not defined
# create the rules programatically to add access to ports 22, 80, 8000 and 8001
geonode_group = conn.create_security_group(SECURITY_GROUP, 'Cool GeoNode rules')
geonode_group.authorize('tcp', 22, 22, '0.0.0.0/0')
geonode_group.authorize('tcp', 80, 80, '0.0.0.0/0')
geonode_group.authorize('tcp', 8000, 8001, '0.0.0.0/0')
geonode_group.authorize('tcp', 8080, 8080, '0.0.0.0/0')
try:
[geonode_key] = [x for x in conn.get_all_key_pairs() if x.name == 'geonode']
except ValueError:
# this probably means the key is not defined
# get the first one in the belt for now:
print "GeoNode file not found in the server"
geonode_key = conn.get_all_key_pairs()[0]
reservation = image.run(security_groups=[geonode_group,], key_name=geonode_key.name, instance_type=INSTANCE_TYPE)
instance = reservation.instances[0]
print "Firing up instance"
# Give it 10 minutes to appear online
for i in range(120):
time.sleep(5)
instance.update()
print instance.state
if instance.state == "running":
break
if instance.state == "running":
dns = instance.dns_name
print "Instance up and running at %s" % dns
config.set('ec2', 'HOST', dns)
config.set('ec2', 'INSTANCE', instance.id)
env.hosts = [dns,]
env.user = config.get('ec2', 'USER')
env.key_filename = KEY_PATH
with open(CONFIG_FILE, 'wb') as configfile:
config.write(configfile)
print "ssh -i %s ubuntu@%s" % (KEY_PATH, dns)
print "Terminate the instance via the web interface %s" % instance
time.sleep(20)
def terminate():
instance_id = config.get('ec2', 'INSTANCE')
conn = boto.connect_ec2()
conn.get_all_instances()
instance = None
for reservation in conn.get_all_instances():
for ins in reservation.instances:
if ins.id == instance_id:
instance = ins
print 'Terminating instance'
instance.terminate()
# Give it 10 minutes to terminate
for i in range(120):
time.sleep(5)
instance.update()
print instance.state
if instance.state == "terminated":
config.set('ec2', 'HOST', '')
config.set('ec2', 'INSTANCE', '')
with open(CONFIG_FILE, 'wb') as configfile:
config.write(configfile)
break
def setup():
sudo('apt-get -y update')
sudo('apt-get -y dist-upgrade')
sudo('add-apt-repository "deb http://archive.canonical.com/ lucid partner"')
sudo('apt-get -y update')
# not working yet, how can we programmatically accept the EULA?
# run("DEBIAN_FRONTEND=noninteractive sudo apt-get install -y sun-java6-jdk || debconf 'echo SET shared/accepted-sun-dlj-v1-1 true; echo $(read) >&2' apt-get install -y sun-java6-jdk")
sudo('apt-get install -y openjdk-6-jdk')
sudo('apt-get install -y subversion git-core binutils build-essential python-dev python-setuptools python-imaging python-reportlab gdal-bin libproj-dev libgeos-dev unzip maven2 python-urlgrabber')
def build():
run('git clone git://github.com/GeoNode/geonode.git')
with cd('geonode'):
run('git submodule update --init')
# WORKAROUND: Avoid compiling reportlab because it is already installed via apt-get and it hangs with fabric (too much data)
run("sed '/reportlab/d' shared/core-libs.txt > core-libs.txt;mv core-libs.txt shared/core-libs.txt")
run('python bootstrap.py')
run('source bin/activate; paver build')
run('source bin/activate; paver make_release')
def deploy():
dns = config.get('ec2', 'HOST')
with cd('geonode'):
run("perl -pi -e 's/127.0.0.1/0.0.0.0/g' shared/dev-paste.ini")
run("perl -pi -e 's/localhost/0.0.0.0/g' src/geoserver-geonode-ext/jetty.xml")
run('echo "SITEURL = \'http://%s:8000/\'" >> src/GeoNodePy/geonode/local_settings.py' % dns )
run('echo "GEOSERVER_BASE_URL = \'http://%s:8001/geoserver/\'" >> src/GeoNodePy/geonode/local_settings.py' % dns )
run('echo "GEONETWORK_BASE_URL = \'http://%s:8001/geonetwork/\'" >> src/GeoNodePy/geonode/local_settings.py' % dns )
# set the django settings module in the activate script to avoid having to type in some cases
run('echo "export DJANGO_SETTINGS_MODULE=\'geonode.settings\'" >> bin/activate')
# create a passwordless superuser, you can use 'django-admin.py changepassword admin' afterwards
run('source bin/activate;django-admin.py createsuperuser --noinput --username=admin --email=admin@admin.admin')
print "In order to login you have to run first 'django-admin.py changepassword admin'"
def host():
dns = config.get('ec2', 'HOST')
print "Access your new geonode instance via the following url:"
print "http://%s:8000" % dns
with cd('geonode'):
run('source bin/activate;paver host')
def geonode():
setup()
build()
deploy()
host()
def dry():
geonode()
terminate()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment