oneblackbear-obbistrano 1.0.10 → 1.0.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/obbistrano.rb CHANGED
@@ -1,2 +1,2 @@
1
1
  require "slicehost"
2
-
2
+ require "obbistrano_tasks"
@@ -1,456 +1,458 @@
1
- desc "Setup Application Config"
2
- task :config_setup do
3
- # options = YAML.load(File.read("config.yml"))
4
- # set :working_server, h rescue nil
5
- # set :working_app, a rescue nil
6
- set :root_pass, root rescue nil
7
- end
8
- #
9
- # task :config_write do
10
- # File.open('config.yml', 'w') { |f| f.puts options.to_yaml }
11
- # #puts "Hello I'm about to mangle your config file"
12
- # end
1
+ Capistrano.configuration(:must_exist).load do
2
+
3
+ desc "Setup Application Config"
4
+ task :config_setup do
5
+ # options = YAML.load(File.read("config.yml"))
6
+ # set :working_server, h rescue nil
7
+ # set :working_app, a rescue nil
8
+ set :root_pass, root rescue nil
9
+ end
10
+ #
11
+ # task :config_write do
12
+ # File.open('config.yml', 'w') { |f| f.puts options.to_yaml }
13
+ # #puts "Hello I'm about to mangle your config file"
14
+ # end
13
15
 
14
- namespace :slicehost do
16
+ namespace :slicehost do
15
17
 
16
- desc "Sets up slicehost DNS for each of the servers specified with a role of web."
17
- task :setup do
18
- get_slice_ip
19
- servers = find_servers :roles => :web
20
- servers.each do |s|
21
- if !zone = Zone.find(:first, :params => {:origin => "#{s}."})
22
- zone = Zone.new(:origin => s, :ttl => TTL)
23
- zone.save
18
+ desc "Sets up slicehost DNS for each of the servers specified with a role of web."
19
+ task :setup do
20
+ get_slice_ip
21
+ servers = find_servers :roles => :web
22
+ servers.each do |s|
23
+ if !zone = Zone.find(:first, :params => {:origin => "#{s}."})
24
+ zone = Zone.new(:origin => s, :ttl => TTL)
25
+ zone.save
26
+ end
27
+ recordOne = Record.new(:record_type => 'A', :zone_id => zone.id, :name => 'www', :data => "#{slice_ip}")
28
+ recordTwo = Record.new(:record_type => 'A', :zone_id => zone.id, :name => '@', :data => "#{slice_ip}")
29
+ recordThree = Record.new(:record_type => 'A', :zone_id => zone.id, :name => 'beta', :data => "#{slice_ip}")
30
+ recordFour = Record.new(:record_type => 'A', :zone_id => zone.id, :name => zone.origin, :data => "#{slice_ip}")
31
+ recordFive = Record.new(:record_type => 'NS', :zone_id => zone.id, :name => zone.origin, :data => 'ns1.slicehost.net.')
32
+ recordSix = Record.new(:record_type => 'NS', :zone_id => zone.id, :name => zone.origin, :data => 'ns2.slicehost.net.')
33
+ recordSeven = Record.new(:record_type => 'NS', :zone_id => zone.id, :name => zone.origin, :data => 'ns3.slicehost.net.')
34
+ [recordOne, recordTwo, recordThree, recordFour, recordFive, recordSix, recordSeven].each {|r| r.save}
24
35
  end
25
- recordOne = Record.new(:record_type => 'A', :zone_id => zone.id, :name => 'www', :data => "#{slice_ip}")
26
- recordTwo = Record.new(:record_type => 'A', :zone_id => zone.id, :name => '@', :data => "#{slice_ip}")
27
- recordThree = Record.new(:record_type => 'A', :zone_id => zone.id, :name => 'beta', :data => "#{slice_ip}")
28
- recordFour = Record.new(:record_type => 'A', :zone_id => zone.id, :name => zone.origin, :data => "#{slice_ip}")
29
- recordFive = Record.new(:record_type => 'NS', :zone_id => zone.id, :name => zone.origin, :data => 'ns1.slicehost.net.')
30
- recordSix = Record.new(:record_type => 'NS', :zone_id => zone.id, :name => zone.origin, :data => 'ns2.slicehost.net.')
31
- recordSeven = Record.new(:record_type => 'NS', :zone_id => zone.id, :name => zone.origin, :data => 'ns3.slicehost.net.')
32
- [recordOne, recordTwo, recordThree, recordFour, recordFive, recordSix, recordSeven].each {|r| r.save}
33
36
  end
34
- end
35
37
 
36
- task :get_slice_ip do
37
- set :slice_ip, get_ip(fetch("host", false))
38
- end
38
+ task :get_slice_ip do
39
+ set :slice_ip, get_ip(fetch("host", false))
40
+ end
39
41
 
40
- desc "Sets up slicehost DNS for Google Apps usage on each of the servers specified with a role of web."
41
- task :googleapps do
42
- API_PASSWORD = "#{slicehost_api_key}"
43
- mx_records = <<-RECORD
44
- ASPMX.L.GOOGLE.COM.
45
- ALT1.ASPMX.L.GOOGLE.COM.
46
- ALT2.ASPMX.L.GOOGLE.COM.
47
- ASPMX2.GOOGLEMAIL.COM.
48
- ASPMX3.GOOGLEMAIL.COM.
49
- RECORD
50
- servers = find_servers :roles => :web
51
- servers.each do |s|
52
- mx_aux = %w[5 10 10 20 20 30 ]
53
- aux_count = 0
54
- zone = Zone.find(:first, :params => {:origin => "#{s}."})
55
- mx_records.each do |rec|
56
- r = Record.new(:record_type => 'MX', :zone_id => zone.id, :name => "#{s}." , :data => "#{rec}", :aux => mx_aux[aux_count])
57
- r.save
58
- aux_count =+ 1
42
+ desc "Sets up slicehost DNS for Google Apps usage on each of the servers specified with a role of web."
43
+ task :googleapps do
44
+ API_PASSWORD = "#{slicehost_api_key}"
45
+ mx_records = <<-RECORD
46
+ ASPMX.L.GOOGLE.COM.
47
+ ALT1.ASPMX.L.GOOGLE.COM.
48
+ ALT2.ASPMX.L.GOOGLE.COM.
49
+ ASPMX2.GOOGLEMAIL.COM.
50
+ ASPMX3.GOOGLEMAIL.COM.
51
+ RECORD
52
+ servers = find_servers :roles => :web
53
+ servers.each do |s|
54
+ mx_aux = %w[5 10 10 20 20 30 ]
55
+ aux_count = 0
56
+ zone = Zone.find(:first, :params => {:origin => "#{s}."})
57
+ mx_records.each do |rec|
58
+ r = Record.new(:record_type => 'MX', :zone_id => zone.id, :name => "#{s}." , :data => "#{rec}", :aux => mx_aux[aux_count])
59
+ r.save
60
+ aux_count =+ 1
61
+ end
62
+ recordOne = Record.new(:record_type => 'CNAME', :zone_id => zone.id, :name => 'mail', :data => "ghs.google.com.")
63
+ recordTwo = Record.new(:record_type => 'CNAME', :zone_id => zone.id, :name => 'docs', :data => "ghs.google.com.")
64
+ [recordOne, recordTwo].each {|r| r.save}
59
65
  end
60
- recordOne = Record.new(:record_type => 'CNAME', :zone_id => zone.id, :name => 'mail', :data => "ghs.google.com.")
61
- recordTwo = Record.new(:record_type => 'CNAME', :zone_id => zone.id, :name => 'docs', :data => "ghs.google.com.")
62
- [recordOne, recordTwo].each {|r| r.save}
63
66
  end
64
- end
65
67
 
66
- end
68
+ end
67
69
 
68
70
 
69
71
 
70
72
 
71
- namespace :host do
72
-
73
- desc "Checks we have information to proceed with server operations"
74
- task :config_check do
75
- config_setup
76
- "#{working_server}" rescue @parent.logger.log 0,"You need to specify a host to run the operation on. Use cap task -s h=yourhost"
77
- @use_applications = [ ]
78
- options["apps"].each do |app, settings|
79
- @use_applications << app if settings["server"]==working_server
73
+ namespace :host do
74
+
75
+ desc "Checks we have information to proceed with server operations"
76
+ task :config_check do
77
+ config_setup
78
+ "#{working_server}" rescue @parent.logger.log 0,"You need to specify a host to run the operation on. Use cap task -s h=yourhost"
79
+ @use_applications = [ ]
80
+ options["apps"].each do |app, settings|
81
+ @use_applications << app if settings["server"]==working_server
82
+ end
80
83
  end
81
- end
82
84
 
83
85
 
84
- desc "Performs a local backup of the applications and databases on the server"
85
- task :backup do
86
- config_check
87
- @use_applications.each do |app|
88
- set :working_app, app
89
- @parent.app.backup
86
+ desc "Performs a local backup of the applications and databases on the server"
87
+ task :backup do
88
+ config_check
89
+ @use_applications.each do |app|
90
+ set :working_app, app
91
+ @parent.app.backup
92
+ end
90
93
  end
91
- end
92
94
 
93
- desc "Backs up the local backup folder to remote Amazon S3 storage"
94
- task :s3backup do
95
- config_check
96
- end
95
+ desc "Backs up the local backup folder to remote Amazon S3 storage"
96
+ task :s3backup do
97
+ config_check
98
+ end
97
99
 
98
100
 
99
101
 
100
- end
102
+ end
101
103
 
102
- namespace :app do
104
+ namespace :app do
103
105
 
104
106
 
105
- task :config_check do
106
- config_setup
107
- databases rescue set(:databases, ["#{application}"])
108
- end
107
+ task :config_check do
108
+ config_setup
109
+ databases rescue set(:databases, ["#{application}"])
110
+ end
109
111
 
110
- task :needs_root do
111
- config_check
112
- puts "*** This operation needs root access - Please pass in a root password using -s root=password" if !defined? "#{root_pass}"
113
- exit if !defined? "#{root_pass}"
114
- end
112
+ task :needs_root do
113
+ config_check
114
+ puts "*** This operation needs root access - Please pass in a root password using -s root=password" if !defined? "#{root_pass}"
115
+ exit if !defined? "#{root_pass}"
116
+ end
115
117
 
116
118
 
117
119
 
118
- # =============================================================================
119
- # DEPLOYING APPLICATIONS
120
- # =============================================================================
121
-
122
- desc "Uses the specified repository to deploy an application. Also checks for correct versions of PHPWax and plugins."
123
- task :deploy do
124
- config_check
125
- deploy_check
126
- php_wax_deploy if defined? "#{phpwax}"
127
- cms_deploy if defined? "#{cms}"
128
- end
120
+ # =============================================================================
121
+ # DEPLOYING APPLICATIONS
122
+ # =============================================================================
123
+
124
+ desc "Uses the specified repository to deploy an application. Also checks for correct versions of PHPWax and plugins."
125
+ task :deploy do
126
+ config_check
127
+ deploy_check
128
+ php_wax_deploy if defined? "#{phpwax}"
129
+ cms_deploy if defined? "#{cms}"
130
+ end
129
131
 
130
- task :deploy_check do
131
- fetch "repository" rescue abort "You have not specified a repository for this application"
132
- git_deploy if repository.include? "git"
133
- svn_deploy if repository.include? "svn"
134
- end
132
+ task :deploy_check do
133
+ fetch "repository" rescue abort "You have not specified a repository for this application"
134
+ git_deploy if repository.include? "git"
135
+ svn_deploy if repository.include? "svn"
136
+ end
135
137
 
136
- task :git_deploy do
137
- begin
138
- run "ls #{deploy_to}/.git"
139
- rescue
140
- run "git init"
141
- run "git remote add origin #{repository}"
138
+ task :git_deploy do
139
+ begin
140
+ run "ls #{deploy_to}/.git"
141
+ rescue
142
+ run "git init"
143
+ run "git remote add origin #{repository}"
144
+ end
145
+ run "git pull origin #{branch}"
142
146
  end
143
- run "git pull origin #{branch}"
144
- end
145
147
 
146
- task :svn_deploy do
147
- run "svn export #{repository} #{deploy_to} --force"
148
- end
148
+ task :svn_deploy do
149
+ run "svn export #{repository} #{deploy_to} --force"
150
+ end
149
151
 
150
- task :cms_deploy do
151
- begin
152
- run "ls plugins/cms/.git"
153
- rescue
154
- run "mkdir -p plugins/cms"
155
- run "cd plugins/cms && git init"
156
- run "cd plugins/cms && git remote add origin git://github.com:phpwax/wildfire.git"
152
+ task :cms_deploy do
153
+ begin
154
+ run "ls plugins/cms/.git"
155
+ rescue
156
+ run "mkdir -p plugins/cms"
157
+ run "cd plugins/cms && git init"
158
+ run "cd plugins/cms && git remote add origin git://github.com:phpwax/wildfire.git"
159
+ end
160
+ run "cd plugins/cms && git checkout #{cms}"
157
161
  end
158
- run "cd plugins/cms && git checkout #{cms}"
159
- end
160
162
 
161
- task :php_wax_deploy do
162
- begin
163
- run "ls wax/.git"
164
- rescue
165
- run "mkdir wax"
166
- run "cd wax && git init"
167
- run "cd wax && git remote add origin git://github.com/phpwax/phpwax.git"
168
- run "cd wax && git pull origin master"
163
+ task :php_wax_deploy do
164
+ begin
165
+ run "ls wax/.git"
166
+ rescue
167
+ run "mkdir wax"
168
+ run "cd wax && git init"
169
+ run "cd wax && git remote add origin git://github.com/phpwax/phpwax.git"
170
+ run "cd wax && git pull origin master"
171
+ end
172
+ run "cd wax && git checkout #{phpwax}"
169
173
  end
170
- run "cd wax && git checkout #{phpwax}"
171
- end
172
174
 
173
- ####### ##############
175
+ ####### ##############
174
176
 
175
177
 
176
- # =============================================================================
177
- # GENERAL ADMIN FOR APPLICATIONS
178
- # =============================================================================
178
+ # =============================================================================
179
+ # GENERAL ADMIN FOR APPLICATIONS
180
+ # =============================================================================
179
181
 
180
- desc "Restarts the Apache Server."
181
- task :restart do
182
- config_check
183
- needs_root
184
- with_user("root", "#{root_pass}") do
185
- run "/etc/rc.d/init.d/httpd restart"
182
+ desc "Restarts the Apache Server."
183
+ task :restart do
184
+ config_check
185
+ needs_root
186
+ with_user("root", "#{root_pass}") do
187
+ run "/etc/rc.d/init.d/httpd restart"
188
+ end
186
189
  end
187
- end
188
190
 
189
- task :clearcache do
190
- run "rm -f tmp/cache/*"
191
- end
191
+ task :clearcache do
192
+ run "rm -f tmp/cache/*"
193
+ end
192
194
 
193
- task :clearlogs do
194
- run "rm -f tmp/log/*"
195
- end
195
+ task :clearlogs do
196
+ run "rm -f tmp/log/*"
197
+ end
196
198
 
197
199
 
198
- # =============================================================================
199
- # BACKING UP APPLICATIONS
200
- # =============================================================================
200
+ # =============================================================================
201
+ # BACKING UP APPLICATIONS
202
+ # =============================================================================
201
203
 
202
- desc "Starts the backup process by checking which type to perform then performs the necessary back ups."
203
- task :backup do
204
- config_check
205
- needs_root
206
- backup_check
207
- end
204
+ desc "Starts the backup process by checking which type to perform then performs the necessary back ups."
205
+ task :backup do
206
+ config_check
207
+ needs_root
208
+ backup_check
209
+ end
208
210
 
209
- task :backup_check do
210
- if defined? "#{repository}"
211
- if repository.include? "git"
212
- git_mysql_backup
213
- upload_only_backup
214
- elsif repository.include? "svn"
211
+ task :backup_check do
212
+ if defined? "#{repository}"
213
+ if repository.include? "git"
214
+ git_mysql_backup
215
+ upload_only_backup
216
+ elsif repository.include? "svn"
217
+ standard_mysql_backup
218
+ upload_only_backup
219
+ end
220
+ else
221
+ puts "No repository for #{application}"
215
222
  standard_mysql_backup
216
- upload_only_backup
223
+ simple_fs_backup
217
224
  end
218
- else
219
- puts "No repository for #{application}"
220
- standard_mysql_backup
221
- simple_fs_backup
222
225
  end
223
- end
224
226
 
225
- task :simple_fs_backup do
226
- with_user("root", "#{root_pass}") do
227
- run "mkdir -p /backup/#{application}"
228
- run "rsync -avzh /home/#{application}/ /backup/#{application}/"
227
+ task :simple_fs_backup do
228
+ with_user("root", "#{root_pass}") do
229
+ run "mkdir -p /backup/#{application}"
230
+ run "rsync -avzh /home/#{application}/ /backup/#{application}/"
231
+ end
229
232
  end
230
- end
231
233
 
232
- task :upload_only_backup do
233
- with_user("root", "#{root_pass}") do
234
- run "mkdir -p /backup/#{application}"
235
- run "rsync -avzh /home/#{application}/public/files/ /backup/#{application}/"
234
+ task :upload_only_backup do
235
+ with_user("root", "#{root_pass}") do
236
+ run "mkdir -p /backup/#{application}"
237
+ run "rsync -avzh /home/#{application}/public/files/ /backup/#{application}/"
238
+ end
236
239
  end
237
- end
238
240
 
239
- task :standard_mysql_backup do
240
- run "mkdir -p public/files"
241
- databases.each do |db|
242
- run "mysqldump #{db} --skip-comments --add-drop-table -u#{user} -p#{password} > public/files/#{db}.sql";
241
+ task :standard_mysql_backup do
242
+ run "mkdir -p public/files"
243
+ databases.each do |db|
244
+ run "mysqldump #{db} --skip-comments --add-drop-table -u#{user} -p#{password} > public/files/#{db}.sql";
245
+ end
246
+ upload_only_backup
243
247
  end
244
- upload_only_backup
245
- end
246
248
 
247
- task :git_mysql_backup do
248
- transaction do
249
- run "mkdir -p tmp/backup"
250
- run "ln -s ../../.git/ tmp/backup/.git"
251
- begin
252
- run "cd tmp/backup && git branch db"
253
- run "cd tmp/backup && git branch -d db"
254
- rescue
249
+ task :git_mysql_backup do
250
+ transaction do
251
+ run "mkdir -p tmp/backup"
252
+ run "ln -s ../../.git/ tmp/backup/.git"
253
+ begin
254
+ run "cd tmp/backup && git branch db"
255
+ run "cd tmp/backup && git branch -d db"
256
+ rescue
257
+ end
258
+ run "cd tmp/backup && git symbolic-ref HEAD refs/heads/db"
259
+ run "cd tmp/backup && mv .git/index .git/index_old"
260
+ databases.each do |db|
261
+ run "cd tmp/backup && mysqldump #{db} --skip-comments --add-drop-table -u#{user} -p#{password} > #{db}.sql";
262
+ end
263
+ run "cd tmp/backup && git add ."
264
+ run "cd tmp/backup && git commit -m 'database update'" rescue ""
265
+ run "cd tmp/backup && git push origin db"
266
+ run "rm -Rf ./tmp/backup"
267
+ run "mv -f .git/index_old .git/index" rescue ""
268
+ run "git symbolic-ref HEAD refs/heads/#{branch}"
269
+ on_rollback do
270
+ run "rm -Rf ./tmp/backup"
271
+ run "mv -f .git/index_old .git/index" rescue ""
272
+ run "git symbolic-ref HEAD refs/heads/#{branch}"
273
+ end
274
+ end
275
+ end
276
+
277
+ # =============================================================================
278
+ # RESTORING BACKED-UP APPLICATIONS
279
+ # =============================================================================
280
+
281
+ desc "Restores a backed up application, database and other files."
282
+ task :restore do
283
+ if defined? repository
284
+ if repository.include? "git"
285
+ upload_only_restore
286
+ git_mysql_restore
287
+ elsif repository.include? "svn"
288
+ upload_only_restore
289
+ standard_mysql_restore
290
+ end
291
+ else
292
+ simple_fs_restore
293
+ standard_mysql_restore
255
294
  end
295
+ end
296
+
297
+ task :upload_only_restore do
298
+ with_user("root", "#{root_pass}") do
299
+ run "rsync -avzh /backup/#{application}/ /home/#{application}/public/files/"
300
+ end
301
+ end
302
+
303
+ task :git_mysql_restore do
304
+ run "mkdir -p tmp/backup"
305
+ run "ln -s ../../ tmp/backup/.git"
256
306
  run "cd tmp/backup && git symbolic-ref HEAD refs/heads/db"
257
307
  run "cd tmp/backup && mv .git/index .git/index_old"
258
- databases.each do |db|
259
- run "cd tmp/backup && mysqldump #{db} --skip-comments --add-drop-table -u#{user} -p#{password} > #{db}.sql";
308
+ "#{databases}".each do |db|
309
+ run "cd tmp/backup && mysql #{db} -u#{user} -p#{password} < #{db}.sql"
260
310
  end
261
- run "cd tmp/backup && git add ."
262
- run "cd tmp/backup && git commit -m 'database update'" rescue ""
263
- run "cd tmp/backup && git push origin db"
264
311
  run "rm -Rf ./tmp/backup"
265
312
  run "mv -f .git/index_old .git/index" rescue ""
266
313
  run "git symbolic-ref HEAD refs/heads/#{branch}"
267
- on_rollback do
268
- run "rm -Rf ./tmp/backup"
269
- run "mv -f .git/index_old .git/index" rescue ""
270
- run "git symbolic-ref HEAD refs/heads/#{branch}"
271
- end
272
- end
273
- end
314
+ end
274
315
 
275
- # =============================================================================
276
- # RESTORING BACKED-UP APPLICATIONS
277
- # =============================================================================
278
-
279
- desc "Restores a backed up application, database and other files."
280
- task :restore do
281
- if defined? repository
282
- if repository.include? "git"
283
- upload_only_restore
284
- git_mysql_restore
285
- elsif repository.include? "svn"
286
- upload_only_restore
287
- standard_mysql_restore
316
+ desc "Just runs rSync back to the home directory"
317
+ task :simple_fs_restore do
318
+ with_user("root", "#{root_pass}") do
319
+ run "rsync -avzh /backup/#{application}/ /home/#{application}/"
288
320
  end
289
- else
290
- simple_fs_restore
291
- standard_mysql_restore
292
321
  end
293
- end
294
322
 
295
- task :upload_only_restore do
296
- with_user("root", "#{root_pass}") do
297
- run "rsync -avzh /backup/#{application}/ /home/#{application}/public/files/"
323
+ task :standard_mysql_restore do
324
+ "#{databases}".each do |db|
325
+ run "cd tmp/backup && mysql #{db} -u#{user} -p#{password} < public/files/#{db}.sql"
326
+ end
298
327
  end
299
- end
300
-
301
- task :git_mysql_restore do
302
- run "mkdir -p tmp/backup"
303
- run "ln -s ../../ tmp/backup/.git"
304
- run "cd tmp/backup && git symbolic-ref HEAD refs/heads/db"
305
- run "cd tmp/backup && mv .git/index .git/index_old"
306
- "#{databases}".each do |db|
307
- run "cd tmp/backup && mysql #{db} -u#{user} -p#{password} < #{db}.sql"
308
- end
309
- run "rm -Rf ./tmp/backup"
310
- run "mv -f .git/index_old .git/index" rescue ""
311
- run "git symbolic-ref HEAD refs/heads/#{branch}"
312
- end
313
328
 
314
- desc "Just runs rSync back to the home directory"
315
- task :simple_fs_restore do
316
- with_user("root", "#{root_pass}") do
317
- run "rsync -avzh /backup/#{application}/ /home/#{application}/"
329
+ # =============================================================================
330
+ # USER AND APPLICATION SETUP AND INITIALISATION
331
+ # =============================================================================
332
+
333
+ desc "General setup task which creates a new user on the host, sets up a mysql database and login, creates an apache vhost file and finally generates an ssh key for the user."
334
+ task :setup do
335
+ config_check
336
+ try_login
337
+ setup_mysql
338
+ vhost
339
+ ssh_key
318
340
  end
319
- end
320
341
 
321
- task :standard_mysql_restore do
322
- "#{databases}".each do |db|
323
- run "cd tmp/backup && mysql #{db} -u#{user} -p#{password} < public/files/#{db}.sql"
342
+
343
+ task :setup_user do
344
+ needs_root
345
+ set :user_to_add, "#{user}"
346
+ set :passwd_to_add, "#{password}"
347
+ with_user("root", "#{root_pass}") do
348
+ run "useradd -p `openssl passwd #{passwd_to_add}` #{user_to_add}"
349
+ end
324
350
  end
325
- end
326
-
327
- # =============================================================================
328
- # USER AND APPLICATION SETUP AND INITIALISATION
329
- # =============================================================================
330
-
331
- desc "General setup task which creates a new user on the host, sets up a mysql database and login, creates an apache vhost file and finally generates an ssh key for the user."
332
- task :setup do
333
- config_check
334
- try_login
335
- setup_mysql
336
- vhost
337
- ssh_key
338
- end
339
351
 
352
+ task :setup_mysql do
353
+ with_user("root", "#{root_pass}") do
354
+ "#{databases}".each do |db|
355
+ run "mysql -uroot -p#{root_pass} -e 'CREATE DATABASE #{db}'"
356
+ run "musql -uroot -p#{root_pass} -e 'GRANT ALL PRIVILEGES ON `#{db}` . * TO '#{user_to_add}'@'localhost' IDENTIFIED BY '#{passwd_to_add}';"
357
+ end
358
+ end
340
359
 
341
- task :setup_user do
342
- needs_root
343
- set :user_to_add, "#{user}"
344
- set :passwd_to_add, "#{password}"
345
- with_user("root", "#{root_pass}") do
346
- run "useradd -p `openssl passwd #{passwd_to_add}` #{user_to_add}"
347
360
  end
348
- end
349
361
 
350
- task :setup_mysql do
351
- with_user("root", "#{root_pass}") do
352
- "#{databases}".each do |db|
353
- run "mysql -uroot -p#{root_pass} -e 'CREATE DATABASE #{db}'"
354
- run "musql -uroot -p#{root_pass} -e 'GRANT ALL PRIVILEGES ON `#{db}` . * TO '#{user_to_add}'@'localhost' IDENTIFIED BY '#{passwd_to_add}';"
362
+ task :try_login do
363
+ config_check
364
+ begin
365
+ run "ls"
366
+ puts "Logged in ok"
367
+ rescue
368
+ print "==== The user does not yet exist. Would you like to create? [Y/N]"
369
+ line = STDIN.gets.upcase.strip
370
+ puts "*** Could not continue as the login does not exist" if line !="Y"
371
+ exit if line != "Y"
372
+ setup_user
355
373
  end
356
374
  end
357
-
358
- end
359
375
 
360
- task :try_login do
361
- config_check
362
- begin
363
- run "ls"
364
- puts "Logged in ok"
365
- rescue
366
- print "==== The user does not yet exist. Would you like to create? [Y/N]"
367
- line = STDIN.gets.upcase.strip
368
- puts "*** Could not continue as the login does not exist" if line !="Y"
369
- exit if line != "Y"
370
- setup_user
376
+ desc "Creates or gets an ssh key for the application"
377
+ task :ssh_key do
378
+ config_check
379
+ begin
380
+ run "cat .ssh/id_rsa.pub"
381
+ rescue
382
+ run "ssh-keygen -t rsa -f .ssh/id_rsa -N ''"
383
+ run "cat .ssh/id_rsa.pub"
384
+ end
371
385
  end
372
- end
373
386
 
374
- desc "Creates or gets an ssh key for the application"
375
- task :ssh_key do
376
- config_check
377
- begin
378
- run "cat .ssh/id_rsa.pub"
379
- rescue
380
- run "ssh-keygen -t rsa -f .ssh/id_rsa -N ''"
381
- run "cat .ssh/id_rsa.pub"
387
+ desc "Creates an Apache virtual host file"
388
+ task :vhost do
389
+ config_check
390
+ needs_root
391
+ with_user("root", "#{root_pass}") do
392
+ public_ip = ""
393
+ run "ifconfig eth0 | grep inet | awk '{print $2}' | sed 's/addr://'" do |_, _, public_ip| end
394
+ public_ip = public_ip.strip
395
+ f = File.open('templates/apache_vhost.erb')
396
+ contents = f.read
397
+ f.close
398
+ buffer = ERB.new(contents)
399
+ config = buffer.result(binding())
400
+ put config, "/etc/httpd/conf.d/#{application}-apache-vhost.conf"
401
+ end
402
+
382
403
  end
383
- end
384
404
 
385
- desc "Creates an Apache virtual host file"
386
- task :vhost do
387
- config_check
388
- needs_root
389
- with_user("root", "#{root_pass}") do
390
- public_ip = ""
391
- run "ifconfig eth0 | grep inet | awk '{print $2}' | sed 's/addr://'" do |_, _, public_ip| end
392
- public_ip = public_ip.strip
393
- f = File.open('templates/apache_vhost.erb')
394
- contents = f.read
395
- f.close
396
- buffer = ERB.new(contents)
397
- config = buffer.result(binding())
398
- put config, "/etc/httpd/conf.d/#{application}-apache-vhost.conf"
405
+ # =============================================================================
406
+ # +MIGRATING+ APPLICATIONS
407
+ # =============================================================================
408
+
409
+ desc "Runs a backup of an application, copies to another server and then sets up on new server"
410
+ task :copy_site do
411
+ config_check
412
+ needs_root
413
+ backup
414
+ print "==== Which server would you like to copy #{application} to? [Full Domain Name] "
415
+ line = STDIN.gets.strip
416
+ begin
417
+ new_server = options["servers"][line]["domain"]
418
+ rescue
419
+ puts "*** Can't find that new server in the config"
420
+ exit
421
+ end
422
+ with_user("root", "#{root_pass}") do
423
+ run "rsync -avzh . -e ssh root@#{new_server}:/backup/#{application}/ --exclude 'tmp/*' --exclude '.git/*'"
424
+ end
425
+ options["apps"]["#{application}"]["server"] = line
426
+ config_write
427
+ try_login
428
+ restore
399
429
  end
400
-
401
- end
402
430
 
403
- # =============================================================================
404
- # +MIGRATING+ APPLICATIONS
405
- # =============================================================================
406
-
407
- desc "Runs a backup of an application, copies to another server and then sets up on new server"
408
- task :copy_site do
409
- config_check
410
- needs_root
411
- backup
412
- print "==== Which server would you like to copy #{application} to? [Full Domain Name] "
413
- line = STDIN.gets.strip
414
- begin
415
- new_server = options["servers"][line]["domain"]
416
- rescue
417
- puts "*** Can't find that new server in the config"
418
- exit
419
- end
420
- with_user("root", "#{root_pass}") do
421
- run "rsync -avzh . -e ssh root@#{new_server}:/backup/#{application}/ --exclude 'tmp/*' --exclude '.git/*'"
422
- end
423
- options["apps"]["#{application}"]["server"] = line
424
- config_write
425
- try_login
426
- restore
427
431
  end
428
-
429
- end
430
432
 
431
433
 
432
- def get_ip(domain)
433
- require 'socket'
434
- t = TCPSocket.gethostbyname(domain)
435
- t[3]
436
- end
437
-
438
- def with_user(new_user, new_pass, &block)
439
- old_user, old_pass = user, password
440
- set :user, new_user
441
- set :password, new_pass
442
- close_sessions
443
- yield
444
- set :user, old_user
445
- set :password, old_pass
446
- close_sessions
447
- end
448
- def close_sessions
449
- sessions.values.each { |session| session.close }
450
- sessions.clear
451
- end
434
+ def get_ip(domain)
435
+ require 'socket'
436
+ t = TCPSocket.gethostbyname(domain)
437
+ t[3]
438
+ end
452
439
 
440
+ def with_user(new_user, new_pass, &block)
441
+ old_user, old_pass = user, password
442
+ set :user, new_user
443
+ set :password, new_pass
444
+ close_sessions
445
+ yield
446
+ set :user, old_user
447
+ set :password, old_pass
448
+ close_sessions
449
+ end
450
+ def close_sessions
451
+ sessions.values.each { |session| session.close }
452
+ sessions.clear
453
+ end
453
454
 
455
+ end
454
456
 
455
457
 
456
458
 
data/obbistrano.gemspec CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{obbistrano}
5
- s.version = "1.0.10"
5
+ s.version = "1.0.11"
6
6
  s.authors = ["Ross Riley", "One Black Bear"]
7
7
  s.date = Time.now
8
8
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: oneblackbear-obbistrano
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.10
4
+ version: 1.0.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ross Riley