mza / vehicle-assembly

Prepares apps for deployment on Amazon Web Services

This URL has Read+Write access

vehicle-assembly / recipes / vehicle_assembly.rb
100644 195 lines (159 sloc) 5.128 kb
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
prefix = "#{File.dirname(__FILE__)}/../"
rails_root = "#{prefix}/../../../"
require "#{prefix}/lib/vehicle_assembly"
 
before "bootstrap:cold", "bootstrap:update_cache"
after "deploy:cold", "webapp:init"
 
namespace :mission_control do
  
  task :launch do
    vab = VehicleAssembly::Configuration.start_countdown_for "config/cloud.rb"
    vab.vehicle.machines.each do |machine|
    unless machine.task.nil?
      self.desc "Task defined in #{machine.class.to_s}"
      self.send :task, machine.task_name, &machine.task
      # run "#{machine.task_name}"
    end
    end
  end
  
end
 
namespace :bootstrap do
 
  desc "Bootstraps a cold instance"
  task :cold do
    ubuntu
    apache
    gems
    capistrano
    git
    svn
    webapp
    passenger
    mysql
    sqlite
    rcov
    library
    rmagick
    library
    ssl
  end
 
  task :ubuntu do
    run "apt-get install ruby1.8-dev -y"
    run "apt-get install gcc -y"
    run "apt-get install build-essential -y"
    run "ldconfig /usr/local/lib"
  end
  
  desc "Bootstraps Apache 2"
  task :apache do
    run "apt-get install apache2 -y"
    run "apt-get install apache2-prefork-dev -y"
  end
  
  desc "Bootstraps Rubygems (including Passenger and Rails)"
  task :gems do
    run "apt-get install rubygems -y"
    run "gem update"
    run "gem update --system"
    put File.read("#{prefix}/lib/deploy/gem.rb"), "/usr/bin/gem"
    run "gem install passenger --no-ri --no-rdoc"
    run "gem install rails --no-ri --no-rdoc"
  end
 
  desc "Bootstraps Git"
  task :git do
    run "apt-get install git-core -y"
  end
 
  desc "Bootstraps SVN"
  task :svn do
    run "apt-get install subversion -y"
  end
    
  desc "Bootstraps the web app environment"
  task :webapp do
    run "mkdir -p /var/rails"
  end
  
  desc "Bootstraps Apache 2 with Passenger"
  task :passenger do
    run "cd #{passenger_root}; rake clean apache2"
    template = ERB.new File.read("#{prefix}/lib/deploy/templates/passenger.erb")
    prepared = template.result(binding)
    put prepared, "/etc/apache2/passenger.conf"
    run "cat /etc/apache2/passenger.conf >> /etc/apache2/apache2.conf"
    run "rm /etc/apache2/passenger.conf"
    run "/etc/init.d/apache2 restart"
    run "ldconfig /usr/local/lib"
  end
  
  desc "Bootstraps MySQL"
  task :mysql do
    run "export DEBIAN_FRONTEND=noninteractive; apt-get -y install mysql-server"
    run "apt-get install libmysqlclient15-dev -y"
    run "gem install mysql -- --with-mysql-dir=/usr/include/mysql"
  end
  
  task :library do
    run "ldconfig /usr/local/lib"
  end
  
  task :sqlite do
    run "gem install sqlite3-ruby"
  end
  
  task :update_cache do
    run "apt-get update"
  end
    
  task :rails do
    run "gem install rails"
  end
  
  task :rcov do
    run "gem install rcov"
  end
 
  task :capistrano do
    run "gem install capistrano"
  end
 
  task :rmagick do
    run "apt-get build-dep imagemagick -y"
    run "apt-get install gcc libjpeg62-dev libbz2-dev libtiff4-dev libwmf-dev libz-dev libpng12-dev libx11-dev libxt-dev libxext-dev libxml2-dev libfreetype6-dev liblcms1-dev libexif-dev perl libjasper-dev libltdl3-dev graphviz gs-gpl pkg-config -y"
    run "cd /tmp; wget ftp://ftp.imagemagick.org/pub/ImageMagick/ImageMagick-6.4.9-2.tar.gz"
    run "cd /tmp; tar -zxvf /tmp/ImageMagick-6.4.9-2.tar.gz"
    run "cd /tmp/ImageMagick-6.4.9-2; ./configure"
    run "cd /tmp/ImageMagick-6.4.9-2; make"
    run "cd /tmp/ImageMagick-6.4.9-2; make install"
    run "gem install rmagick"
  end
    
  task :ssl do
    run "a2enmod ssl"
    run "a2enmod headers"
    
    # Transfer keys (via SFTP)
    put File.read("#{rails_root}/config/ssl/server.crt"), "/etc/apache2/server.crt"
    put File.read("#{rails_root}/config/ssl/server.key"), "/etc/apache2/server.key"
    
    # Restart Apache
    run "/etc/init.d/apache2 restart"
  end
    
end
 
namespace :webapp do
 
  desc "Configures Apache and Passenger for a new web app"
  task :init do
    template = ERB.new File.read("#{prefix}/lib/deploy/templates/apache-config.erb")
    prepared = template.result(binding)
    put prepared, "/etc/apache2/sites-available/#{application}"
    run "ln -s /etc/apache2/sites-available/#{application} /etc/apache2/sites-enabled/000-#{application}"
    symlink
    set_ownership
    remove_default
    deploy.restart
  end
  
  task :gems do
    run "export LD_LIBRARY_PATH=/usr/local/lib; cd #{deploy_to}/current; /usr/bin/rake gems:install"
  end
  
  task :symlink do
    run "ln -s /var/rails/#{application}/current/public /var/www/#{application}"
  end
  
  desc "Correctly sets ownership of environment.rb for Passenger"
  task :set_ownership do
    run "chown www-data:www-data #{current_path}/config/environment.rb"
  end
      
  task :remove_default do
    run "rm /etc/apache2/sites-enabled/000-default"
  end
  
end
 
namespace :deploy do
  
  desc "Restart Apache"
  task :restart, :roles => :app, :except => { :no_release => true } do
    run "/etc/init.d/apache2 restart"
  end
 
  [:start, :stop].each do |t|
    desc "#{t} task is unnecessary with Passenger"
    task t, :roles => :app do ; end
  end
  
end