oneblackbear-obbistrano 1.0.7 → 1.0.8

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