rails_pwnerer 0.3.5 → 0.4

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/CHANGELOG CHANGED
@@ -1,3 +1,5 @@
1
+ v0.4. Implemented 'backup'.
2
+
1
3
  v0.3.5. Implemented 'go live' and 'go panic' to take all application processes up / down.
2
4
 
3
5
  v0.3. Supports 'update' to update application from svn. Still no configuration outside of config/main.rb.
@@ -63,6 +63,31 @@ ENDSQL
63
63
  migrate_database app_name
64
64
  end
65
65
 
66
+ # backs up or restores the database
67
+ def backup(app_name, action)
68
+ db_name = RailsPwnage::Config.app_dbname(app_name)
69
+ db_user = RailsPwnage::Config.app_db_user(app_name)
70
+ db_pass = RailsPwnage::Config.app_db_password(app_name)
71
+
72
+ pwnerer_user = RailsPwnage::Config.pwnerer_user
73
+ pwnerer_uid = uid_for_username(pwnerer_user)
74
+ pwnerer_gid = gid_for_username(pwnerer_user)
75
+
76
+ case action
77
+ when :checkpoint
78
+ timestamp = Time.now.strftime '%Y%m%d%H%M%S'
79
+ dump_file = "db/#{app_name}_#{timestamp}.sql"
80
+ with_dir(RailsPwnage::Config.path_to(:backup, app_name)) do
81
+ system "mysqldump --add-drop-database --add-drop-table --extended-insert --single-transaction -u#{db_user} -p#{db_pass} #{db_name} > #{dump_file}"
82
+ # lockdown the file
83
+ File.chmod(0400, dump_file)
84
+ File.chown(pwnerer_uid, pwnerer_gid, dump_file)
85
+ end
86
+ when :rollback
87
+ # TODO: rollback
88
+ end
89
+ end
90
+
66
91
  def control_all(action)
67
92
  case action
68
93
  when :start
@@ -80,7 +105,11 @@ ENDSQL
80
105
  self.new.update(app_name)
81
106
  end
82
107
 
108
+ def self.backup(app_name, action)
109
+ self.new.backup(app_name, action)
110
+ end
111
+
83
112
  def self.control_all(action = :start)
84
113
  self.new.control_all(action)
85
- end
114
+ end
86
115
  end
@@ -15,6 +15,22 @@ module RailsPwnage::App
15
15
  end
16
16
  NginxConfig.update(app_name)
17
17
  end
18
+
19
+ # performs a backup task (checkpoint / release
20
+ def self.backup(app_name, action = :checkpoint)
21
+ case action
22
+ when :checkpoint
23
+ Svn.backup app_name, action
24
+ ClusterConfig.update(app_name) do
25
+ Database.backup app_name, action
26
+ end
27
+ when :rollback
28
+ ClusterConfig.update(app_name) do
29
+ Svn.backup app_name, action
30
+ Database.backup app_name, action
31
+ end
32
+ end
33
+ end
18
34
 
19
35
  # start or stop all apps
20
36
  def self.control_all(action = :start)
@@ -5,7 +5,7 @@ require 'fileutils'
5
5
  class RailsPwnage::App::Svn
6
6
  include RailsPwnage::Base
7
7
 
8
- def checkout(svn_path)
8
+ def svn_checkout(svn_path)
9
9
  app_name = File.basename(svn_path)
10
10
 
11
11
  prod_apps = RailsPwnage::Config.path_to :prod_apps
@@ -17,18 +17,75 @@ class RailsPwnage::App::Svn
17
17
  return app_name
18
18
  end
19
19
 
20
- def update(app_name)
20
+ def svn_update(app_name)
21
21
  with_dir(RailsPwnage::Config.path_to(app_name)) do
22
22
  system "svn update"
23
23
  end
24
24
  end
25
+
26
+ def scaffold_backup(app_name)
27
+ pwnerer_user = RailsPwnage::Config.pwnerer_user
28
+ pwnerer_uid = uid_for_username(pwnerer_user)
29
+ pwnerer_gid = gid_for_username(pwnerer_user)
30
+
31
+ backup_dir = RailsPwnage::Config.path_to :backup, app_name
32
+ FileUtils.mkpath backup_dir unless File.exists? backup_dir
33
+ File.chown(pwnerer_uid, pwnerer_gid, backup_dir)
34
+
35
+ with_dir(RailsPwnage::Config.path_to(:backup, app_name)) do
36
+ ['db', 'files', 'tmp'].each do |subdir|
37
+ Dir.mkdir subdir unless File.exists? subdir
38
+ File.chown pwnerer_uid, pwnerer_gid, subdir
39
+ end
40
+ end
41
+ end
42
+
43
+ def backup(app_name, action)
44
+ pwnerer_user = RailsPwnage::Config.pwnerer_user
45
+ pwnerer_uid = uid_for_username(pwnerer_user)
46
+ pwnerer_gid = gid_for_username(pwnerer_user)
47
+
48
+ case action
49
+ when :checkpoint
50
+ timestamp = Time.now.strftime '%Y%m%d%H%M%S'
51
+ dump_file = "files/#{app_name}_#{timestamp}.tar.gz"
52
+
53
+ with_dir(RailsPwnage::Config.path_to(:backup, app_name)) do
54
+ # create a cold copy of the application files
55
+ cold_copy = "tmp#{File::SEPARATOR}#{app_name}"
56
+ FileUtils.rm_r cold_copy if File.exists? cold_copy
57
+ FileUtils.cp_r RailsPwnage::Config.path_to(app_name), 'tmp'
58
+
59
+ # pack and protect the cold copy
60
+ with_dir 'tmp' do
61
+ system "tar -czf ../#{dump_file} #{app_name}"
62
+ end
63
+ File.chmod(400, dump_file)
64
+ File.chown(pwnerer_uid, pwnerer_gid, dump_file)
25
65
 
66
+ # clean up
67
+ FileUtils.rm_r cold_copy
68
+ end
69
+ when :rollback
70
+ end
71
+ end
72
+
26
73
  def setup(svn_path)
27
- app_name = checkout(svn_path)
74
+ app_name = svn_checkout(svn_path)
28
75
 
29
76
  # TODO: fetch configuration, feed to system
30
77
 
31
- return app_name
78
+ scaffold_backup(app_name)
79
+
80
+ return app_name
81
+ end
82
+
83
+ def update(app_name)
84
+ scaffold_backup(app_name)
85
+
86
+ # TODO: maybe backup old version before issuing the svn update?
87
+
88
+ svn_update(app_name)
32
89
  end
33
90
 
34
91
  def self.setup(svn_path)
@@ -39,5 +96,9 @@ class RailsPwnage::App::Svn
39
96
  self.new.update(app_name)
40
97
  end
41
98
 
99
+ def self.backup(app_name, action)
100
+ self.new.backup(app_name, action)
101
+ end
102
+
42
103
  # TODO: uninstallation
43
104
  end
@@ -1,6 +1,6 @@
1
1
  module RailsPwnage::Config
2
2
  # the path to something important (e.g. :prod_apps --> path to all production applications)
3
- def self.path_to(what = :prod)
3
+ def self.path_to(what = :prod, app_name = nil)
4
4
  case what
5
5
  when :prod
6
6
  # the directory containing all the production data
@@ -11,12 +11,17 @@ module RailsPwnage::Config
11
11
  when :prod_config
12
12
  # the directory containing the config files
13
13
  '/prod/config'
14
+ when :prod_backups
15
+ '/prod/backups'
14
16
  when :mongrel_configs
15
17
  # the directory containing the mongrel_cluster config files
16
18
  '/etc/mongrel_cluster'
17
19
  when :nginx_configs
18
20
  # the directory containing the nginx config files
19
21
  '/etc/nginx/sites-enabled'
22
+ when :backup
23
+ # the backup directory for an application
24
+ '/prod/backups/' + app_name
20
25
  when String
21
26
  # an application
22
27
  self.path_to(:prod_apps) + File::SEPARATOR + what
@@ -4,38 +4,48 @@ class RailsPwnage::Executor
4
4
  # standalone runner
5
5
  def run(args)
6
6
  case args[0]
7
- when 'scaffold'
8
- case args[1]
9
- when 'dirs'
10
- Dirs.go
11
- when 'gems'
12
- Gems.go
13
- when 'packages'
14
- Packages.go
15
- when 'mongrels'
16
- HookMongrels.go
17
- else
18
- Packages.go
19
- Gems.go
20
- Dirs.go
21
- HookMongrels.go
22
- end
23
-
24
- when 'install'
25
- svn_path = args[1]
26
- RailsPwnage::App.install svn_path
27
-
28
- when 'update'
29
- app_name = args[1]
30
- RailsPwnage::App.update app_name
31
-
32
- when 'go'
33
- case args[1]
34
- when 'live'
35
- RailsPwnage::App.control_all :start
36
- when 'panic'
37
- RailsPwnage::App.control_all :stop
38
- end
7
+ when 'scaffold', 'den00b'
8
+ case args[1]
9
+ when 'dirs'
10
+ Dirs.go
11
+ when 'gems'
12
+ Gems.go
13
+ when 'packages'
14
+ Packages.go
15
+ when 'mongrels'
16
+ HookMongrels.go
17
+ else
18
+ Packages.go
19
+ Gems.go
20
+ Dirs.go
21
+ HookMongrels.go
22
+ end
23
+
24
+ when 'install', 'micro'
25
+ svn_path = args[1]
26
+ RailsPwnage::App.install svn_path
27
+
28
+ when 'update', 'ubermicro'
29
+ app_name = args[1]
30
+ RailsPwnage::App.update app_name
31
+
32
+ when 'go'
33
+ case args[1]
34
+ when 'live', 'pwn'
35
+ RailsPwnage::App.control_all :start
36
+ when 'down', 'panic'
37
+ RailsPwnage::App.control_all :stop
38
+ else
39
+ print "Unrecognized go command #{args[1]}\n"
40
+ end
41
+
42
+ when 'backup', 'checkpoint', 'save'
43
+ RailsPwnage::App.backup args[1], :checkpoint
44
+ when 'restore', 'reload', 'recover', 'rollback'
45
+ RailsPwnage::App.backup args[1], :rollback
46
+
47
+ else
48
+ print "Unrecognized command #{args[0]}\n"
39
49
  end
40
50
  end
41
51
 
@@ -9,9 +9,9 @@ class RailsPwnage::Scaffolds::Dirs
9
9
  def run
10
10
  pwnerer_uid = uid_for_username(RailsPwnage::Config.pwnerer_user)
11
11
  with_dir('/') do
12
- [:prod_apps, :prod_config].map { |k| RailsPwnage::Config.path_to k }.each do |path|
12
+ [:prod_apps, :prod_config, :prod_backups].map { |k| RailsPwnage::Config.path_to k }.each do |path|
13
13
  FileUtils.mkpath path
14
- File.chown(pwnerer_uid, nil, path)
14
+ File.chown(pwnerer_uid, nil, path)
15
15
  end
16
16
  end
17
17
  end
@@ -1,10 +1,10 @@
1
1
 
2
- # Gem::Specification for Rails_pwnerer-0.3.5
2
+ # Gem::Specification for Rails_pwnerer-0.4
3
3
  # Originally generated by Echoe
4
4
 
5
5
  Gem::Specification.new do |s|
6
6
  s.name = %q{rails_pwnerer}
7
- s.version = "0.3.5"
7
+ s.version = "0.4"
8
8
 
9
9
  s.specification_version = 2 if s.respond_to? :specification_version=
10
10
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_pwnerer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.5
4
+ version: "0.4"
5
5
  platform: ruby
6
6
  authors:
7
7
  - Victor Costan