# # # EC2 Setup # # namespace :setup do desc "Setup a new app server on EC2 without the app. Useful for creating an EBS image. Usage: HOSTS= PROMPTNAME= [DNSNAME=]." remote_task :ec2_core do ENV["DNSNAME"] ||= ENV["HOSTS"] if ENV["HOSTS"].nil? or ENV["HOSTS"]=="" or ENV["PROMPTNAME"].nil? or ENV["PROMPTNAME"]=="" then puts "Usage:" puts "rake setup:ec2_core HOSTS= PROMPTNAME= [DNSNAME=]" puts "Where:" puts " HOSTS - hostname of the EC2 instance to setup." puts " DNSNAME - The ultimate hostname that the EC2 instance will use." exit end break unless target_host == ENV["HOSTS"] puts "Setup EC2 instance #{target_host} for application #{application}..." # # Yum Packages # packages=[ "gcc", "gcc-c++", "make", "openssl-devel","libcurl-devel", "libxml2","libxml2-devel", "libxslt","libxslt-devel", "mysql","mysql-devel", "openssl","zlib", "pcre","pcre-devel", "git", "rubygems","ruby-devel" ] # # Core Gems # core_gems=[ "bundler", "bluepill", "unicorn" ] # # Setup ~ec2-user and ~root # rsync "#{ENV['HOME']}/.ssh/id_rsa","~/.ssh/id_rsa" rsync "config/templates/known_hosts","~/.ssh/known_hosts" put "~/.bashrc" do File.read("config/templates/bashrc.tmpl").gsub("%%%PROMPTNAME%%%",ENV["PROMPTNAME"]) end run "chmod 600 ~/.bashrc ~/.ssh/known_hosts" run "chmod 400 ~/.ssh/id_rsa" sudo "cp ~ec2-user/.bashrc ~root/.bashrc" sudo "cp ~ec2-user/.ssh/known_hosts ~root/.ssh/known_hosts" sudo "cp ~ec2-user/.ssh/id_rsa ~root/.ssh/id_rsa" sudo "chmod 600 ~root/.bashrc ~root/.ssh/known_hosts" sudo "chmod 400 ~root/.ssh/id_rsa" sudo "hostname #{ENV["DNSNAME"]}" if ENV["DNSNAME"]!=ENV["HOSTS"] # # Yum Packages # sudo "yum -y remove ruby" sudo "yum -y install "+packages.join(' ') sudo "yum -y update" # # Setup Users # sudo "groupadd -f apps" sudo "useradd -g apps -c \"Nginx Webserver\" nginx" sudo "useradd -g apps -c \"Unicorn Appserver\" unicorn" sudo "useradd -g apps -c \"Resque Background Tasks\" resque" sudo "useradd -g apps -c \"Bluepill Process Monitoring\" bluepill" sudo "usermod -a -G apps ec2-user" # # Log directory setup # sudo "chgrp apps /var/log" sudo "chmod 775 /var/log" # # Ruby & Core Gems # ruby_version="ruby-1.9.2-p290" run "rm -f /tmp/#{ruby_version}.tz" run "rm -rf /tmp/#{ruby_version}" run "wget -O /tmp/#{ruby_version}.tz http://ftp.ruby-lang.org/pub/ruby/1.9/#{ruby_version}.tar.gz" run "(cd /tmp;tar xzf #{ruby_version}.tz)" run "(cd /tmp/#{ruby_version};./configure --prefix=/usr)" run "(cd /tmp/#{ruby_version};make)" run "(cd /tmp/#{ruby_version};sudo make install)" sudo "gem install "+core_gems.join(' ') # # Nginx # nginx_version="nginx-1.0.8" run "rm -f /tmp/#{nginx_version}.tz" run "rm -rf /tmp/#{nginx_version}" run "wget -O /tmp/#{nginx_version}.tz http://nginx.org/download/#{nginx_version}.tar.gz" run "(cd /tmp;tar xzf #{nginx_version}.tz)" run "(cd /tmp/#{nginx_version};./configure --prefix=/opt/nginx --with-http_ssl_module --with-http_realip_module)" run "(cd /tmp/#{nginx_version};make)" run "(cd /tmp/#{nginx_version};sudo make install)" sudo "chown nginx /opt/nginx/conf/" sudo "chgrp apps /opt/nginx/conf/" sudo "chmod 771 /opt/nginx/conf/" sudo "mkdir -p /var/log/nginx" sudo "chown nginx /var/log/nginx" sudo "chgrp apps /var/log/nginx" sudo "chmod 771 /var/log/nginx" sudo "rm -f /opt/nginx/conf/nginx.conf" # Don't leave the default one lying around # # Unicorn: Setup Application Base # sudo "mkdir -p /var/server" sudo "chown unicorn /var/server" sudo "chgrp apps /var/server" sudo "chmod 771 /var/server" # # Setup Bluepill # sudo "mkdir -p /etc/bluepill" sudo "chown bluepill /etc/bluepill" sudo "chgrp apps /etc/bluepill" sudo "chmod 771 /etc/bluepill" puts "Setup EC2 instance #{target_host}...done" end end