-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfabfile.py
65 lines (53 loc) · 2.43 KB
/
fabfile.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import os
import sys
import glob
import datetime
import boto
from fabric.api import env, run, local, put, cd, sudo
from fabric.contrib import files
#local code:
sys.path.insert(0, ".")
import config
import ec2provision
env.user = config.ENV_USER
env.key_filename = config.KEY_FILENAME
def _provision_instance():
access_key, secret_key = config.AWS_ACCESS_KEY, config.AWS_SECRET_ACCESS_KEY
ami_id = config.AMI_ID
key_name = config.KEY_NAME
instance_type = config.INSTANCE_TYPE
placement = config.EC2_ZONE
prov_host = ec2provision.run_instance(access_key, secret_key, ami_id, key_name, instance_type=instance_type, placement=placement)
return prov_host.dns_name
def provision():
"""Start an EC2 instance, to be used in 'bootstrap' step."""
provisioned_host = _provision_instance()
print "Success. Hostname: %s " % (provisioned_host,)
def bootstrap():
"""Install Chef, then install and run cookbooks."""
sudo("apt-get -q update")
sudo("adduser --gecos GECOS --disabled-password %s" % config.APP_USER)
install_chef()
setup_chef_env()
sudo("chef-solo -l debug -c %(root_dir)s/chef_config.rb -j %(root_dir)s/chef_attrs.rb" % {"root_dir":config.CHEF_RESOURCES_ROOT})
def install_chef():
"""Install a recent version of Chef on remote instance."""
sudo('echo "deb http://apt.opscode.com/ `lsb_release -cs`-0.10 main" | sudo tee /etc/apt/sources.list.d/opscode.list')
sudo('sudo mkdir -p /etc/apt/trusted.gpg.d')
sudo('gpg --keyserver keys.gnupg.net --recv-keys 83EF826A')
sudo('gpg --export [email protected] | sudo tee /etc/apt/trusted.gpg.d/opscode-keyring.gpg > /dev/null')
sudo("apt-get update")
sudo("apt-get -y install chef")
def setup_chef_env():
"""Put Chef cookbooks and config files on remote instance."""
root_dir = config.CHEF_RESOURCES_ROOT
sudo("mkdir -p %(root_dir)s/cookbooks" % {"root_dir":root_dir})
put("chef_config.rb", "/tmp/chef_config.rb")
sudo("mv /tmp/chef_config.rb %(root_dir)s/" % {"root_dir":root_dir})
put("chef_attrs.rb", "/tmp/chef_attrs.rb")
sudo("mv /tmp/chef_attrs.rb %(root_dir)s/" % {"root_dir":root_dir})
local("tar -cf cookbooks.tar cookbooks && gzip cookbooks.tar")
put("cookbooks.tar.gz", "/tmp/cookbooks.tar.gz")
local("rm cookbooks.tar.gz")
sudo("mv /tmp/cookbooks.tar.gz %(root_dir)s/" % {"root_dir":root_dir})
sudo("cd %(root_dir)s/ && tar -zxf cookbooks.tar.gz" % {"root_dir":root_dir})