Skip to content

Commit

Permalink
fixes, building nginx app server works
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanmark committed Feb 24, 2014
1 parent 1e6905f commit fcb6d14
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 178 deletions.
45 changes: 45 additions & 0 deletions README.md
@@ -0,0 +1,45 @@
# Trib Apps server builder

Here we have a simple python commandline script and a bunch of shell script templates. You can use `runserver.py` to start a new Amazon instance and feed it a shell script to automatically build your server.

usage: runserver.py [-h] [-k ACCESS_KEY] [-s SECRET_KEY] [-r REGION] [-z ZONE]
-p KEY_PAIR -g SECURITY_GROUP -t
{m3.xlarge,m3.2xlarge,m1.small,m1.medium,m1.large,m1.xlarge,c3.large,c3.xlarge,c3.2xlarge,c3.4xlarge,c3.8xlarge,c1.medium,c1.xlarge,cc2.8xlarge,g2.2xlarge,cg1.4xlarge,m2.xlarge,m2.2xlarge,m2.4xlarge,cr1.8xlarge,hi1.4xlarge,hs1.8xlarge,t1.micro}
-a AMI [-m CONFIG] -b BUILD_SCRIPT [-n SERVER_NAME]
[-c CLUSTER] [--hosts HOSTS] [--pretend]

Build some Amazon EC2 servers.

optional arguments:
-h, --help show this help message and exit
-k ACCESS_KEY, --access-key ACCESS_KEY
Your AWS access key
-s SECRET_KEY, --secret-key SECRET_KEY
Your AWS secret key
-r REGION, --region REGION
EC2 region
-z ZONE, --zone ZONE EC2 zone
-p KEY_PAIR, --key-pair KEY_PAIR
EC2 key pair
-g SECURITY_GROUP, --security-group SECURITY_GROUP
EC2 security group
-t {m3.xlarge,m3.2xlarge,m1.small,m1.medium,m1.large,m1.xlarge,c3.large,c3.xlarge,c3.2xlarge,c3.4xlarge,c3.8xlarge,c1.medium,c1.xlarge,cc2.8xlarge,g2.2xlarge,cg1.4xlarge,m2.xlarge,m2.2xlarge,m2.4xlarge,cr1.8xlarge,hi1.4xlarge,hs1.8xlarge,t1.micro}, --instance {m3.xlarge,m3.2xlarge,m1.small,m1.medium,m1.large,m1.xlarge,c3.large,c3.xlarge,c3.2xlarge,c3.4xlarge,c3.8xlarge,c1.medium,c1.xlarge,cc2.8xlarge,g2.2xlarge,cg1.4xlarge,m2.xlarge,m2.2xlarge,m2.4xlarge,cr1.8xlarge,hi1.4xlarge,hs1.8xlarge,t1.micro}
EC2 instance type
-a AMI, --ami AMI EC2 AMI id
-m CONFIG, --config-module CONFIG
Config module
-b BUILD_SCRIPT, --build-script BUILD_SCRIPT
Build script template to use
-n SERVER_NAME, --server-name SERVER_NAME
Name this server
-c CLUSTER, --cluster CLUSTER
Put server in a cluster
--hosts HOSTS Comma-delimited list of hosts to assign to this server
--pretend Output the build script and don't actually create the
server.

## Build scripts

All the build scripts are designed to use with Ubuntu AMIs. This rig would theoretically work with any image that loads and runs a shell script from the EC2 user-data.

The build script is generated using Jinja2. `runserver.py` will load whatever variables are present in `config.py` and pass them in as context to whatever template is specified by the `-b` or `--build-script` flags.
Empty file added assets/PUT_PRIVATE_KEYS_HERE
Empty file.
1 change: 1 addition & 0 deletions assets/authorized_keys.sample
@@ -0,0 +1 @@
ssh-rsa XXXX myprivatekey
23 changes: 8 additions & 15 deletions assets/newsapps/etc/logrotate.d/newsapps.conf
@@ -1,16 +1,9 @@
/var/log/apps/apache.error.log
/var/log/apps/apache.access.log
/var/log/apps/app.error.log
/var/log/apps/varnish.access.log
/var/log/apps/db.access.log
{
rotate 7
daily
missingok
notifempty
delaycompress
compress
postrotate
reload rsyslog >/dev/null 2>&1 || true
endscript
copytruncate
dateext
compress

/home/newsapps/logs/* {
minsize 100k
weekly
rotate 3
}
2 changes: 2 additions & 0 deletions assets/ssh_config.sample
@@ -0,0 +1,2 @@
IdentityFile ~/.ssh/my-github-private-key.pem
IdentityFile ~/.ssh/some-other-private-key.pem
12 changes: 10 additions & 2 deletions boot-scripts/base.sh
Expand Up @@ -76,12 +76,12 @@ export SECURITY_GROUP={{SECURITY_GROUP}}
export PRIVATE_KEY=/home/$USERNAME/.ssh/{{KEY_PAIR}}.pem
export AWS_ACCESS_KEY_ID={{ACCESS_KEY}}
export AWS_SECRET_ACCESS_KEY={{SECRET_KEY}}
" > /etc/profile.d/cloud-commander.sh
" > /etc/profile.d/aws-creds.sh
source /etc/profile

# Pull down assets
echo "Downloading assets..."
ASSET_DIR="/home/$USERNAME/cloud-commander"
ASSET_DIR="/home/$USERNAME/assets"
s3cmd get --config=/home/$USERNAME/.s3cfg --no-progress s3://{{ASSET_BUCKET}}/{{ASSET_KEY}} /home/$USERNAME/assets.tgz

cd /home/$USERNAME
Expand Down Expand Up @@ -125,6 +125,14 @@ chmod -Rf go-rwx /home/$USERNAME/.ssh
# setup our local hosts file
/usr/local/bin/hosts-for-cluster

# setup logs
mkdir /home/$USERNAME/logs
chmod o+w /home/$USERNAME/logs

install_file newsapps /etc/logrotate.d/newsapps.conf

mkdir /home/$USERNAME/sites

{% if CLOUDKICK_OAUTH_KEY -%}
{% include "_cloudkick.sh" %}
{% endif -%}
Expand Down
2 changes: 1 addition & 1 deletion boot-scripts/lib.sh
Expand Up @@ -6,7 +6,7 @@
#
function install_file {
echo "Installing '$2' from assets"
cp /home/$USERNAME/cloud-commander/$1$2 $2
cp /home/$USERNAME/assets/$1$2 $2
chown root:root $2
chmod 644 $2
}
Expand Down
4 changes: 4 additions & 0 deletions boot-scripts/newsapps/app-nginx.sh
Expand Up @@ -5,6 +5,10 @@
install_pkg nginx-full
install_file newsapps /etc/nginx/nginx.conf

mkdir /home/$USERNAME/nginx
mkdir /mnt/nginx-cache
chmod ugo+rw /mnt/nginx-cache

service nginx restart

{% endblock %}
157 changes: 0 additions & 157 deletions build.sh

This file was deleted.

11 changes: 8 additions & 3 deletions runserver.py
Expand Up @@ -61,6 +61,8 @@ def render(template_name, template_dict):
help="Name this server")
parser.add_argument("-c", "--cluster", dest="cluster",
help="Put server in a cluster")
parser.add_argument("--hosts", dest="hosts", default='',
help="Comma-delimited list of hosts to assign to this server")

parser.add_argument("--pretend", dest="pretend", action='store_true',
help="Output the build script and don't actually create the server.")
Expand Down Expand Up @@ -97,7 +99,7 @@ def render(template_name, template_dict):
string.ascii_uppercase + string.digits) for x in range(8))
with io.BytesIO() as data_stream:
with tarfile.open(fileobj=data_stream, mode='w:gz') as tarball:
tarball.add(PWD + '/assets')
tarball.add(PWD + '/assets', 'assets')
data_stream.seek(0)
s3_key.set_contents_from_file(data_stream)

Expand All @@ -108,15 +110,18 @@ def render(template_name, template_dict):

tags = {'Name': args.server_name}
if args.cluster:
hosts = map(lambda x: x.strip(), args.hosts.split(','))
if args.hosts:
hosts = map(lambda x: x.strip(), args.hosts.split(','))
else:
hosts = list()
if args.server_name not in hosts:
hosts.insert(0, args.server_name)
tags['Cluster'] = args.cluster
tags['Hosts'] = ', '.join(hosts)
tags['Type'] = config.SERVER_TYPES[args.build_script]

if args.pretend:
col1 = 18
col1 = 25
col2 = 30
build_filename = 'build.sh'
with open(build_filename, 'w') as fp:
Expand Down

0 comments on commit fcb6d14

Please sign in to comment.