Skip to content

Instantly share code, notes, and snippets.

@richleland
Created October 29, 2012 20:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save richleland/3976421 to your computer and use it in GitHub Desktop.
Save richleland/3976421 to your computer and use it in GitHub Desktop.
#!/bin/bash
# set the hostname
echo "replace-me" > /etc/hostname
grep -q "127.0.0.1 replace-me" /etc/hosts || sed -i '1i127.0.0.1 replace-me' /etc/hosts
service hostname restart
# install saltstack-minion
add-apt-repository ppa:saltstack/salt-daily -y
apt-get update -y
apt-get install python-software-properties -y
apt-get install salt-minion -y
# set salt master location, id for minion and start minion
sed -i 's/#master: salt/master: [salt-master-fqdn-or-ip]/' /etc/salt/minion
service salt-minion restart
import boto
import os
from fabric.api import prompt, task
from fabric.colors import cyan, yellow
#from fabric.context_managers import cd
#from fabric.contrib.console import confirm
from fabric.utils import puts
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
INSTANCE_TYPES = ['t1.micro', 'm1.small', 'm1.medium', 'm1.large', 'm1.xlarge',
'c1.medium', 'c1.xlarge', 'm2.xlarge', 'm2.2xlarge',
'm2.4xlarge', 'cc1.4xlarge', 'cg1.4xlarge', 'cc2.8xlarge']
def validate_instance_type(user_input):
"Ensure that the user supplied instance type is valid."
if user_input in INSTANCE_TYPES:
return user_input
else:
raise ValueError("The instance type {0} is not valid.".format(user_input))
@task
def create_instance():
"Step-by-step creation of EC2 instance(s)."
conn = boto.connect_ec2()
key_pairs = conn.get_all_key_pairs()
security_groups = conn.get_all_security_groups()
zones = conn.get_all_zones()
ami = prompt(yellow("Which AMI would you like to use?"), default='ami-a29943cb')
name = prompt(yellow("What would you like to name the instance?"))
puts("\nAvailable instance types")
puts("-------------------------------------------------------------------")
puts("\n".join(INSTANCE_TYPES))
instance_type = prompt(yellow("\nWhich instance type would you like to use?"), validate=validate_instance_type, default="m1.small")
puts("\nAvailable security groups")
puts("-------------------------------------------------------------------")
puts("\n".join([group.name for group in security_groups]))
security_group = [prompt(yellow("\nWhich security group would you like to use?"), default="default")]
puts("\nAvailable key pairs")
puts("-------------------------------------------------------------------")
puts("\n".join([key.name for key in key_pairs]))
key_pair = prompt(yellow("\nWhich key would you like to use?"), default='some-key')
puts("\nAvailability zones")
puts("-------------------------------------------------------------------")
puts("\n".join([zone.name for zone in zones]))
placement = prompt(yellow("\nWhich availability zone should the instance live in?"))
num_instances = prompt(yellow("How many instances do you want to create?"), validate=int, default=1)
bootstrap_script = os.path.join(PROJECT_ROOT, 'scripts', 'bootstrap-minion.sh')
with open(bootstrap_script) as f:
user_data = f.read()
user_data = user_data.replace('replace-me', name)
reservation = conn.run_instances(
image_id=ami,
instance_type=instance_type,
security_groups=security_group,
placement=placement,
key_name=key_pair,
user_data=user_data,
min_count=num_instances,
max_count=num_instances
)
instance = reservation.instances[0]
instance.add_tag('Name', name)
puts(cyan(reservation))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment