oneblackbear-obbistrano 1.0.9 → 1.0.10

Sign up to get free protection for your applications and to get access to all the features.
@@ -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.9"
5
+ s.version = "1.0.10"
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/slicehost.rb", "lib/tasks.rb", "lib/templates/apache_vhost.erb"]
12
+ s.files = ["README.textile", "obbistrano.gemspec", "lib/obbistrano.rb", "lib/slicehost.rb", "lib/obbistrano_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.9
4
+ version: 1.0.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ross Riley
@@ -46,7 +46,7 @@ files:
46
46
  - obbistrano.gemspec
47
47
  - lib/obbistrano.rb
48
48
  - lib/slicehost.rb
49
- - lib/tasks.rb
49
+ - lib/obbistrano_tasks.rb
50
50
  - lib/templates/apache_vhost.erb
51
51
  has_rdoc: false
52
52
  homepage: http://github.com/oneblackbear/obbistrano