AppTower-ubistrano 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,20 @@
1
+ Capistrano::Configuration.instance(:must_exist).load do
2
+
3
+ namespace :log do
4
+ desc "Add logrotate entry for this application"
5
+ task :rotate, :roles => :app do
6
+ if yes(msg(:iptables))
7
+ upload_from_erb '/etc/rotate.conf', binding, :folder => 'log'
8
+ sudo_each [
9
+ 'cp -f /etc/logrotate.conf /etc/logrotate2.conf',
10
+ 'chmod 777 /etc/logrotate2.conf',
11
+ 'cat /etc/rotate.conf >> /etc/logrotate2.conf',
12
+ 'cp -f /etc/logrotate2.conf /etc/logrotate.conf',
13
+ 'rm -f /etc/logrotate2.conf',
14
+ 'rm -f /etc/rotate.conf'
15
+ ]
16
+ end
17
+ end
18
+ end
19
+
20
+ end
@@ -0,0 +1,94 @@
1
+ Capistrano::Configuration.instance(:must_exist).load do
2
+
3
+ namespace :mysql do
4
+ namespace :create do
5
+ desc "Create database and user"
6
+ task :default, :roles => :db do
7
+ mysql.create.db
8
+ mysql.create.user
9
+ end
10
+
11
+ desc "Create database"
12
+ task :db, :roles => :db do
13
+ mysql_run "CREATE DATABASE #{db_table}"
14
+ end
15
+
16
+ desc "Create database user"
17
+ task :user, :roles => :db do
18
+ mysql_run [
19
+ "CREATE USER '#{db_user}'@'localhost' IDENTIFIED BY '#{db_pass}'",
20
+ "GRANT ALL PRIVILEGES ON *.* TO '#{db_user}'@'localhost'"
21
+ ]
22
+ end
23
+ end
24
+
25
+ namespace :update do
26
+ desc 'Update mysql root password'
27
+ task :root_password, :roles => :db do
28
+ old_pass = ask "Current root password? (default: none)"
29
+ new_pass = ask "New root password? (default: none)"
30
+ sudo "mysqladmin -u root #{old_pass.empty? ? '' : "--password=#{old_pass} "}password #{new_pass}"
31
+ end
32
+ end
33
+
34
+ namespace :destroy do
35
+ desc "Destroy database and user"
36
+ task :default, :roles => :db do
37
+ mysql.destroy.db
38
+ mysql.destroy.user
39
+ end
40
+
41
+ desc "Destroy database"
42
+ task :db, :roles => :db do
43
+ mysql_run "DROP DATABASE #{db_table}"
44
+ end
45
+
46
+ desc "Destroy database user"
47
+ task :user, :roles => :db do
48
+ mysql_run [
49
+ "REVOKE ALL PRIVILEGES, GRANT OPTION FROM '#{db_user}'@'localhost'",
50
+ "DROP USER '#{db_user}'@'localhost'"
51
+ ]
52
+ end
53
+ end
54
+
55
+ namespace :backup do
56
+ desc "Upload local backup to remote"
57
+ task :local_to_server, :roles => :db do
58
+ from = File.expand_path("~/db_backups/#{stage}/#{application}/#{backup_name}.bz2")
59
+ if File.exists?(from)
60
+ run_each "mkdir -p #{shared_path}/db_backups"
61
+ upload from, "#{shared_path}/db_backups/#{backup_name}.bz2"
62
+ else
63
+ puts "Does not exist: #{from}"
64
+ end
65
+ end
66
+
67
+ desc "Restore remote database from backup"
68
+ task :restore, :roles => :db do
69
+ run_each "bunzip2 < #{shared_path}/db_backups/#{backup_name}.bz2 | mysql -u #{db_user} --password=#{db_pass} #{db_table}"
70
+ end
71
+
72
+ desc "Backup database to local"
73
+ task :to_local, :roles => :db do
74
+ to_server
75
+ system "mkdir -p ~/db_backups/#{stage}/#{application}"
76
+ get "#{shared_path}/db_backups/#{backup_name}.bz2", File.expand_path("~/db_backups/#{stage}/#{application}/#{backup_name}.bz2")
77
+ end
78
+
79
+ desc "Backup database to remote"
80
+ task :to_server, :roles => :db do
81
+ run_each [
82
+ "mkdir -p #{shared_path}/db_backups",
83
+ "mysqldump --add-drop-table -u #{db_user} -p#{db_pass} #{db_table}_production | bzip2 -c > #{shared_path}/db_backups/#{backup_name}.bz2"
84
+ ]
85
+ end
86
+
87
+ def backup_name
88
+ now = Time.now
89
+ [ now.year, now.month, now.day ].join('-') + '.sql'
90
+ end
91
+ end
92
+ end
93
+
94
+ end
@@ -0,0 +1,76 @@
1
+ Capistrano::Configuration.instance(:must_exist).load do
2
+
3
+ namespace :rails do
4
+ namespace :config do
5
+ desc "Creates database.yml in the shared config"
6
+ task :default, :roles => :app do
7
+ run "mkdir -p #{shared_path}/config"
8
+ Dir[File.expand_path('../../templates/rails/*', File.dirname(__FILE__))].each do |f|
9
+ upload_from_erb "#{shared_path}/config/#{File.basename(f, '.erb')}", binding, :folder => 'rails'
10
+ end
11
+ end
12
+
13
+ desc "Set up app with app_helpers"
14
+ task :app_helpers do
15
+ run "cd #{release_path} && script/plugin install git://github.com/winton/app_helpers.git"
16
+ run "cd #{release_path} && rake RAILS_ENV=production quiet=true app_helpers"
17
+ end
18
+
19
+ desc "Configure attachment_fu"
20
+ task :attachment_fu, :roles => :app do
21
+ run_each [
22
+ "mkdir -p #{shared_path}/media",
23
+ "ln -sf #{shared_path}/media #{release_path}/public/media"
24
+ ]
25
+ sudo_each [
26
+ "mkdir -p #{release_path}/tmp/attachment_fu",
27
+ "chown -R #{user} #{release_path}/tmp/attachment_fu"
28
+ ]
29
+ end
30
+
31
+ desc "Configure asset_packager"
32
+ task :asset_packager do
33
+ run "source ~/.bash_profile && cd #{release_path} && rake RAILS_ENV=production asset:packager:build_all"
34
+ end
35
+
36
+ desc "Configure rails_widget"
37
+ task :rails_widget, :roles => :app do
38
+ run "cd #{release_path} && rake RAILS_ENV=production widget:production"
39
+ end
40
+
41
+ desc "Copies yml files in the shared config folder into our app config"
42
+ task :to_app, :roles => :app do
43
+ run "cp -Rf #{shared_path}/config/* #{release_path}/config"
44
+ end
45
+
46
+ namespace :thinking_sphinx do
47
+ desc "Configures thinking_sphinx"
48
+ task :default, :roles => :app do
49
+ sudo ";cd #{release_path} && rake RAILS_ENV=production ts:config"
50
+ end
51
+
52
+ desc "Stop thinking_sphinx"
53
+ task :stop, :roles => :app do
54
+ sudo ";cd #{release_path} && rake RAILS_ENV=production ts:stop"
55
+ end
56
+
57
+ desc "Start thinking_sphinx"
58
+ task :start, :roles => :app do
59
+ sudo ";cd #{release_path} && rake RAILS_ENV=production ts:start"
60
+ end
61
+
62
+ desc "Restart thinking_sphinx"
63
+ task :restart, :roles => :app do
64
+ rails.config.thinking_sphinx.stop
65
+ rails.config.thinking_sphinx.start
66
+ end
67
+ end
68
+ end
69
+
70
+ desc "Intialize Git submodules"
71
+ task :setup_git, :roles => :app do
72
+ run "cd #{release_path}; git submodule init; git submodule update"
73
+ end
74
+ end
75
+
76
+ end
@@ -0,0 +1,23 @@
1
+ Capistrano::Configuration.instance(:must_exist).load do
2
+
3
+ namespace :sinatra do
4
+ desc "Runs install.rb if exists"
5
+ task :install do
6
+ run_puts "if [ -e #{current_path}/install.rb ]; then sudo ruby #{current_path}/install.rb; fi"
7
+ end
8
+
9
+ namespace :config do
10
+ desc "Creates config.ru in shared config"
11
+ task :default do
12
+ run "mkdir -p #{shared_path}/config"
13
+ upload_from_erb "#{shared_path}/config/config.ru", binding, :folder => 'sinatra'
14
+ end
15
+
16
+ desc "Copies files in the shared config folder into our app"
17
+ task :to_app, :roles => :app do
18
+ run "cp -Rf #{shared_path}/config/* #{release_path}"
19
+ end
20
+ end
21
+ end
22
+
23
+ end
@@ -0,0 +1,51 @@
1
+ Capistrano::Configuration.instance(:must_exist).load do
2
+
3
+ namespace :ssh do
4
+ desc 'Generate ssh keys and upload to server'
5
+ task :default do
6
+ ssh.keys.create
7
+ ssh.keys.upload
8
+ end
9
+
10
+ namespace :keys do
11
+ desc "Creates an rsa ssh key pair (local or remote)"
12
+ task :create do
13
+ if ask(msg(:ssh_keys_create))[0..0].downcase == 'l'
14
+ system('ssh-keygen -t rsa') if yes(msg(:create_keys))
15
+ else
16
+ if yes(msg(:create_server_keys))
17
+ pass = ask "Enter a password for this key:"
18
+ sudo_each [
19
+ "ssh-keygen -t rsa -N '#{pass}' -q -f /home/#{user}/.ssh/id_rsa",
20
+ "chmod 0700 /home/#{user}/.ssh",
21
+ "chown -R #{user} /home/#{user}/.ssh"
22
+ ]
23
+ sudo_puts "tail -1 /home/#{user}/.ssh/id_rsa.pub"
24
+ end
25
+ end unless yes(msg(:have_keys))
26
+ end
27
+
28
+ desc "Uploads local ssh public keys into remote authorized_keys"
29
+ task :upload do
30
+ if yes(msg(:upload_keys))
31
+ keys = ask msg(:ssh_keys_upload), get_ssh_keys
32
+ if keys.empty?
33
+ ssh.setup if yes("No keys found. Generate ssh keys now?")
34
+ else
35
+ sudo_each [
36
+ "mkdir -p /home/#{user}/.ssh",
37
+ "touch /home/#{user}/.ssh/authorized_keys"
38
+ ]
39
+ add_line "/home/#{user}/.ssh/authorized_keys", keys.strip
40
+ sudo_each [
41
+ "chmod 0700 /home/#{user}/.ssh",
42
+ "chmod 0600 /home/#{user}/.ssh/authorized_keys",
43
+ "chown -R #{user} /home/#{user}/.ssh"
44
+ ]
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
50
+
51
+ end
@@ -0,0 +1,30 @@
1
+ Capistrano::Configuration.instance(:must_exist).load do
2
+
3
+ desc 'Set the target stage to staging'
4
+ task :staging do
5
+ set :stage, :staging
6
+ end
7
+
8
+ desc 'Set the target stage to test'
9
+ task :testing do
10
+ set :stage, :test
11
+ end
12
+
13
+ # None of this works in a namespace
14
+ desc 'Set up stage-dependent properties'
15
+ task :setup_stage do
16
+ set :base_dir, "#{ubistrano[:base_dir]}/#{stage}"
17
+ set :deploy_to, "#{base_dir}/#{application}"
18
+ set :db_table, "#{application}#{stage == :staging ? "_#{stage}" : ''}"
19
+
20
+ ubistrano[stage].each do |key, value|
21
+ value = [ value ].flatten if key == :domain
22
+ set key, value
23
+ end
24
+
25
+ role :app, host, :primary => true
26
+ role :web, host
27
+ role :db, host
28
+ end
29
+
30
+ end
@@ -0,0 +1,269 @@
1
+ Capistrano::Configuration.instance(:must_exist).load do
2
+
3
+ namespace :ubuntu do
4
+ desc "Set up a fresh Ubuntu server"
5
+ task :default do
6
+ puts space(msg(:about_templates))
7
+ if yes("Have you already created the user defined in deploy.rb?")
8
+ ssh.default
9
+ ubuntu.config.default
10
+ ubuntu.aptitude.default
11
+ ubuntu.install.default
12
+ ubuntu.restart
13
+ puts space(msg(:ubuntu_finished))
14
+ else
15
+ puts space(msg(:visudo))
16
+ end
17
+ end
18
+
19
+ desc "Restart Ubuntu server"
20
+ task :restart do
21
+ if yes(msg(:ubuntu_restart))
22
+ sudo_each 'shutdown -r now'
23
+ end
24
+ end
25
+
26
+ namespace :aptitude do
27
+ desc 'Run all aptitude tasks'
28
+ task :default do
29
+ if yes(msg(:aptitude_default))
30
+ aptitude.update
31
+ aptitude.upgrade
32
+ aptitude.essential
33
+ else
34
+ msg :aptitude_instructions
35
+ end
36
+ end
37
+
38
+ desc 'Aptitude update'
39
+ task :update do
40
+ sudo_puts 'aptitude update -q -y'
41
+ end
42
+
43
+ desc 'Aptitude upgrade'
44
+ task :upgrade do
45
+ sudo_puts 'aptitude upgrade -q -y'
46
+ end
47
+
48
+ desc 'Aptitude install build-essential'
49
+ task :essential do
50
+ sudo_puts 'aptitude install build-essential -q -y'
51
+ end
52
+ end
53
+
54
+ namespace :config do
55
+ desc 'Run all tasks'
56
+ task :default do
57
+ ubuntu.config.sudoers
58
+ ubuntu.config.sshd_config
59
+ ubuntu.config.iptables
60
+ end
61
+
62
+ desc "Updates server iptables"
63
+ task :iptables do
64
+ if yes(msg(:iptables))
65
+ upload_from_erb '/etc/iptables.rules', binding, :folder => 'ubuntu'
66
+ sudo_each [
67
+ 'iptables-restore < /etc/iptables.rules',
68
+ 'rm /etc/iptables.rules'
69
+ ]
70
+ end
71
+ end
72
+
73
+ desc "Updates sshd_config"
74
+ task :sshd_config do
75
+ if yes(msg(:sshd_config))
76
+ set :ssh_port, port
77
+ set :port, 22
78
+ change_line '/etc/ssh/sshd_config', 'Port 22', "Port #{port}"
79
+ change_line '/etc/ssh/sshd_config', 'PermitRootLogin yes', 'PermitRootLogin no'
80
+ change_line '/etc/ssh/sshd_config', 'X11Forwarding yes', 'X11Forwarding no'
81
+ change_line '/etc/ssh/sshd_config', 'UsePAM yes', 'UsePAM no'
82
+ remove_line '/etc/ssh/sshd_config', 'UseDNS .*'
83
+ add_line '/etc/ssh/sshd_config', 'UseDNS no'
84
+ sudo_puts '/etc/init.d/ssh reload'
85
+ set :port, ssh_port
86
+ end
87
+ end
88
+
89
+ desc "Updates sudoers file"
90
+ task :sudoers do
91
+ if yes(msg(:sudoers))
92
+ add_line '/etc/sudoers', "#{user} ALL=NOPASSWD: ALL"
93
+ end
94
+ end
95
+ end
96
+
97
+ namespace :install do
98
+ desc 'Run all install tasks'
99
+ task :default do
100
+ ubuntu.install.apache
101
+ ubuntu.install.git
102
+ ubuntu.install.mysql
103
+ ubuntu.install.mysqltuner
104
+ ubuntu.install.perl
105
+ ubuntu.install.php
106
+ ubuntu.install.postfix
107
+ ubuntu.install.ruby
108
+ ubuntu.install.rubygems
109
+ ubuntu.install.passenger
110
+ ubuntu.install.god
111
+ ubuntu.install.rails
112
+ ubuntu.install.sinatra
113
+ ubuntu.install.sphinx
114
+ end
115
+
116
+ desc 'Install Apache'
117
+ task :apache, :roles => :web do
118
+ if yes("May I install Apache?")
119
+ sudo_puts [
120
+ 'aptitude install apache2 apache2-mpm-prefork apache2-utils apache2.2-common libapr1 libaprutil1 ssl-cert -q -y',
121
+ 'a2enmod rewrite'
122
+ ]
123
+ end
124
+ end
125
+
126
+ desc 'Install Git'
127
+ task :git, :roles => :app do
128
+ install_source(:git) do |path|
129
+ sudo_puts [
130
+ 'apt-get build-dep git-core -q -y',
131
+ make_install(path)
132
+ ]
133
+ end if yes("May I install Git?")
134
+ end
135
+
136
+ desc 'Install MySQL'
137
+ task :mysql, :roles => :db do
138
+ if yes("May I install MySQL?")
139
+ sudo_puts 'aptitude install mysql-client-5.0 mysql-common mysql-server mysql-server-5.0 libmysqlclient15-dev libmysqlclient15off -q -y'
140
+ ROOT.mysql.create.user
141
+ yes msg(:secure_mysql)
142
+ end
143
+ end
144
+
145
+ desc "Install MySQLTuner"
146
+ task :mysqltuner, :roles => :db do
147
+ if yes(msg(:mysqltuner))
148
+ bin = "/usr/local/bin"
149
+ run_each [
150
+ "cd #{bin} && sudo wget --quiet #{sources[:mysqltuner]}",
151
+ "cd #{bin} && sudo chmod 0700 mysqltuner.pl",
152
+ "cd #{bin} && sudo mv mysqltuner.pl mysqltuner"
153
+ ]
154
+ yes msg(:mysqltuner_instructions)
155
+ end
156
+ end
157
+
158
+ desc 'Install Perl'
159
+ task :perl, :roles => :web do
160
+ if yes("May I install Perl?")
161
+ sudo_puts 'aptitude install libdbi-perl libnet-daemon-perl libplrpc-perl libdbd-mysql-perl -q -y'
162
+ end
163
+ end
164
+
165
+ desc 'Install PHP'
166
+ task :php, :roles => :web do
167
+ if yes("May I install PHP?")
168
+ sudo_puts 'aptitude install php5-common php5-mysql libapache2-mod-php5 php-pear php-mail php-net-smtp -q -y'
169
+ end
170
+ end
171
+
172
+ desc 'Install Postfix'
173
+ task :postfix, :roles => :web do
174
+ if yes("May I install Postfix and set it up as a relay?")
175
+ smtp = ask 'What is your SMTP server address?'
176
+ login = ask 'What is your SMTP server username?'
177
+ pass = ask 'What is your SMTP server password?'
178
+ sudo_puts 'aptitude install postfix -q -y'
179
+ add_line '/etc/postfix/main.cf',
180
+ 'smtp_sasl_auth_enable = yes',
181
+ 'smtp_sasl_security_options = noanonymous',
182
+ 'smtp_sasl_password_maps = hash:/etc/postfix/saslpasswd',
183
+ 'smtp_always_send_ehlo = yes',
184
+ "relayhost = #{smtp}"
185
+ sudo_each 'touch /etc/postfix/saslpasswd'
186
+ add_line '/etc/postfix/saslpasswd', "#{smtp} #{login}:#{pass}"
187
+ sudo_each [
188
+ 'postmap /etc/postfix/saslpasswd',
189
+ '/etc/init.d/postfix restart'
190
+ ]
191
+ end
192
+ end
193
+
194
+ desc 'Install Ruby'
195
+ task :ruby, :roles => :app do
196
+ install_source(:ruby) do |path|
197
+ sudo_puts [
198
+ "aptitude install libopenssl-ruby -q -y",
199
+ make_install(path)
200
+ ]
201
+ end if yes("May I install Ruby?")
202
+ end
203
+
204
+ desc 'Install RubyGems'
205
+ task :rubygems, :roles => :app do
206
+ if yes("May I install RubyGems?")
207
+ install_source(:rubygems) do |path|
208
+ run_puts "cd #{path} && sudo ruby setup.rb"
209
+ end
210
+ gems.update
211
+ end
212
+ end
213
+
214
+ desc 'Install Passenger'
215
+ task :passenger, :roles => :app do
216
+ if yes("May I install Passenger (mod_rails)?")
217
+ sudo_puts 'aptitude install apache2-prefork-dev -q -y'
218
+ gem_install :passenger
219
+ apache.reload if yes(msg(:passenger))
220
+ end
221
+ end
222
+
223
+ desc 'Install God'
224
+ task :god, :roles => :app do
225
+ if yes(msg(:god))
226
+ gem_install 'god'
227
+ upload_from_erb '/etc/init.d/god', binding, :folder => 'ubuntu'
228
+ sudo_each [
229
+ ';cd /etc/init.d && sudo chmod +x god',
230
+ 'mkdir -p /usr/local/etc/god'
231
+ ]
232
+ upload_from_erb('/usr/local/etc/god.god', binding, :folder => 'ubuntu')
233
+ upload_from_erb('/usr/local/etc/god/apache.god', binding, :folder => 'ubuntu') if yes(msg(:god_apache))
234
+ upload_from_erb('/usr/local/etc/god/mysql.god', binding, :folder => 'ubuntu') if yes(msg(:god_mysql))
235
+ upload_from_erb('/usr/local/etc/god/sshd.god', binding, :folder => 'ubuntu') if yes(msg(:god_sshd))
236
+ sudo_puts [
237
+ 'update-rc.d god defaults',
238
+ './etc/init.d/god start'
239
+ ]
240
+ end
241
+ end
242
+
243
+ desc 'Install Rails'
244
+ task :rails, :roles => :app do
245
+ if yes("May I install Rails?")
246
+ gem_install :mysql
247
+ gem_install :rails
248
+ end
249
+ end
250
+
251
+ desc 'Install Sinatra'
252
+ task :sinatra, :roles => :app do
253
+ if yes("May I install Sinatra?")
254
+ gem_install :do_mysql # Datamapper
255
+ gem_install 'dm-core'
256
+ gem_install :sinatra # Sinatra
257
+ end
258
+ end
259
+
260
+ desc 'Install Sphinx'
261
+ task :sphinx, :roles => :app do
262
+ install_source(:sphinx) do |path|
263
+ sudo_puts make_install(path)
264
+ end if yes("May I install Sphinx?")
265
+ end
266
+ end
267
+ end
268
+
269
+ end