Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
165 lines (138 sloc) 5.58 KB
from __future__ import with_statement
import os
import os.path
import string
from time import sleep
from tempfile import mkstemp
import boto
from fabric.api import (run
#, cd
, settings
#, local
, put
#, hosts
#, hide
)
# from fabric.context_managers import prefix
# from fabric.contrib.console import confirm
def InstanceContext(instance, username='root'):
return settings(host=instance.ip_address,
user=username,
host_string='%s@%s'%(username, instance.ip_address))
def gen_dev_names():
for l in string.letters[5:]:
yield '/dev/sd%s'%l
def create_udev_rule_for_ebs_vol(device, mountpoint):
if device.startswith('/dev/'):
device = device[len('/dev/'):]
assert len(device)==3
assert device.startswith('sd')
tmp_fd, tmp_path = mkstemp()
tmp_file = os.fdopen(tmp_fd, 'w')
tmp_file.write(
'''\
SUBSYSTEM!="block", GOTO="%(dev)s_end"
KERNEL!="%(dev)s", GOTO="%(dev)s_end"
ACTION=="add", RUN+="/bin/mkdir -p %(mountpoint)s"
ACTION=="add", RUN+="/bin/mount /dev/sdf %(mountpoint)s"
ACTION=="remove", RUN+="/bin/umount %(mountpoint)s"
ACTION=="remove", RUN+="/bin/rmdir %(mountpoint)s"
LABEL="%(dev)s_end"
'''%{'dev':device, 'mountpoint':mountpoint})
tmp_file.close()
put(tmp_path, '/etc/udev/rules.d/86-%s.rules'%device)
os.unlink(tmp_path)
def attach_and_mount_ebs_volume(instance, volume_id, device, mount_point):
create_udev_rule_for_ebs_vol(device, mount_point)
ec2_conn.attach_volume(volume_id=volume_id,
instance_id=instance.id,
device=device)
################################################################################
ec2_conn = boto.connect_ec2()
local_working_dir = '~/nosql_summer_hackathon/'
wikistats_snapshot_id = "snap-753dfc1c"
zone = 'us-east-1a'
instances = []
base_image_id = "ami-5a43a933"
base_image = ec2_conn.get_image(base_image_id)
key_name = 'nosql-riak-keypair'
def setup_keypair(key_name=key_name):
keyfile_path = os.path.join(local_working_dir, '%s.pem'%key_name)
if not os.path.exists(keyfile_path):
kp = ec2_conn.create_key_pair(key_name)
open(keyfile_path,'w').write(kp.material)
## @@TR: finish ...
def get_running_instances():
return ec2_conn.get_all_instances()[0].instances
def create_instance(
instance_type='m1.large',
key_name='nosql-riak-keypair'):
instance = base_image.run(
instance_type=instance_type,
key_name='nosql-riak-keypair',
security_groups=['default'],
placement=zone).instances[0]
instances.append(instance)
# @@TR: should add a timeout
while instance.state == 'pending':
sleep(2)
instance.update()
return instance
def bootstrap_instance(instance):
bootstrap_gentoo(instance)
attach_nosql_summer_volumes(instance)
install_erlang_and_riak(instance)
return instance
def bootstrap_gentoo(instance):
with InstanceContext(instance):
run("""
( grep 'alias l=' /etc/bash/bashrc > /dev/null ) ||\
echo 'alias l=\"ls -alh --color\"' >> /etc/bash/bashrc
""")
run("emerge -u eix")
run("eix-sync")
run("emerge -u bash")
run("emerge -u erlang mercurial git htop screen sysstat lsof")
def attach_nosql_summer_volumes(instance,
attach_downloads=False,
attach_wikistats=False):
disk_devs = gen_dev_names()
with InstanceContext(instance):
attach_and_mount_ebs_volume(instance=instance,
volume_id="vol-ce8b94a7",
device=disk_devs.next(),
mount_point='/mnt/working')
if attach_downloads:
if False:
downloads_vol = ec2_conn.create_volume(size=5, zone=zone)
wiki_downloads_vol_id = downloads_vol.id
else:
wiki_downloads_vol_id = 'vol-6e021807'
wiki_downloads_vol_dev_name = disk_devs.next()
create_udev_rule_for_ebs_vol(
wiki_downloads_vol_dev_name, '/mnt/downloads')
ec2_conn.attach_volume(volume_id=wiki_downloads_vol_id,
instance_id=instance.id,
device=wiki_downloads_vol_dev_name)
if attach_wikistats:
wikistats_vol = ec2_conn.create_volume(
size=400, snapshot=wikistats_snapshot_id, zone=zone)
wikistats_vol_dev_name = disk_devs.next()
create_udev_rule_for_ebs_vol(
wikistats_vol_dev_name, "/mnt/wikistats")
ec2_conn.attach_volume(volume_id=wikistats_vol.id,
instance_id=instance.id,
device=wikistats_vol_dev_name)
#riak_install_dir = '/mnt/riak-0.13.0'
def install_erlang_and_riak(instance):
with InstanceContext(instance):
#run("hg clone http://hg.basho.com/riak")
run("""[[ -e riak-0.13.0.tar.gz ]] || \
curl -O http://downloads.basho.com/riak/riak-0.13/riak-0.13.0.tar.gz""")
run("[[ -e riak-0.13.0 ]] || tar zxvf riak-0.13.0.tar.gz")
run("cd riak-0.13.0/ && make all")
def create_local_riak_cluster(instance):
with InstanceContext(instance):
run("cd riak-0.13.0/ && ( [[ -e dev ]] || make devrel )")
################################################################################
# base_instance = get_running_instances()[0]
Something went wrong with that request. Please try again.