Created
November 8, 2011 02:21
-
-
Save zmsmith/1346825 to your computer and use it in GitHub Desktop.
Django Chef Blog Post
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
#!/usr/bin/env python | |
import subprocess | |
import sys | |
from boto.ec2.connection import EC2Connection | |
from chef import ChefAPI, Node | |
base_command = "knife ec2 server create" | |
AWS_ACCESS_KEY = "" | |
AWS_SECRET_KEY = "" | |
# The name of the key pair you want to use to launch the new machine | |
# If you don't know what that is, checkout this link: | |
# http://chris-richardson.blog-city.com/amazon_ec2_keypairs_and_other_stumbling_blocks_1.htm | |
AWS_SSH_KEY_NAME = "" | |
# The name of the private key file that corresponds with that key on AWS. | |
# This script assumes that this file is located in ~/.ssh/ | |
AWS_SSH_KEY_FILE = "" | |
def main(): | |
api = ChefAPI.from_config_file('.chef/knife.rb') | |
# This loop figures out a name for your new machine. This is useful | |
# when you have multiple machines of the same type. | |
base_name = "web%s" | |
index = 1 | |
while True: | |
name = base_name % index | |
node = Node(name) | |
if node.exists: | |
index += 1 | |
else: | |
break | |
print "Bootstrapping %s" % name | |
base_opts = '-A %(aws_key)s -K %(aws_secret)s -I "ami-fd589594" -f "t1.micro" -S "%(key)s" \ | |
-x "ubuntu" -G "default,linux,web" -i "~/.ssh/%(file)s" -r "role[web]"' \ | |
% {'key': AWS_SSH_KEY_NAME, 'file': AWS_SSH_KEY_FILE, 'aws_key': AWS_ACCESS_KEY, 'aws_secret': AWS_SECRET_KEY} | |
opts = "%s -N %s" % (base_opts, name) | |
command = "%s %s" % (base_command, opts) | |
print "Running %s" % command | |
subprocess.call(command, shell=True) | |
node = Node(name) | |
ec2_id = node['ec2']['instance_id'] | |
hostname = node['ec2']['public_hostname'] | |
# Tag the instance on ec2 with the Node name | |
conn = EC2Connection(AWS_ACCESS_KEY, AWS_SECRET_KEY) | |
created_tags = conn.create_tags([ec2_id], {"Name": name}) | |
# Reboot to make sure we can survive one and to restart all services | |
print "Rebooting new machine..." | |
conn.reboot_instances([ec2_id]) | |
print "In a few minutes, visit your new machine:" | |
print node['ec2']['public_hostname'] | |
if __name__ == '__main__': | |
main() |
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
class Chef::Knife::Ec2ServerCreate < Chef::Knife | |
alias :original_run :run | |
def run | |
@initial_sleep_delay = 30 | |
original_run | |
end | |
end |
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
name "web" | |
# `env_root` is the location to create a virtualenv | |
# the virtual env will have the name `app_name`-env | |
# `repo` is the github repo, assumed to be public, cloned into `env_root`/`app_name`-env/`app_name` | |
# `settings` is the settings file, assumed to be in settings/ at your repo's root | |
default_attributes("site_domain" => "yourawsmdomain.ly", | |
"env_root" => "/var/www", | |
"app_name" =>"djangohelloworld", | |
"repo" => "zmsmith/djangohelloworld", | |
"settings" => "__init__.py") | |
run_list "recipe[djangoquickstart]" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment