class RailsPwnerer::App::Files

manages an application’s file system

Public Instance Methods

drop_files(app_name, instance_name) click to toggle source

remove the application files

   # File lib/rails_pwnerer/app/files.rb
83 def drop_files(app_name, instance_name)
84   app_config = RailsPwnerer::Config[app_name, instance_name]
85   # exit and don't complain if the app is busted
86   return unless app_config and File.exists? app_config[:app_path]
87 
88   app_path = app_config[:app_path]
89   FileUtils.rm_r app_path if File.exists? app_path
90 end
dump_files(app_name, instance_name) click to toggle source

dump the application files to the backup area

   # File lib/rails_pwnerer/app/files.rb
 7 def dump_files(app_name, instance_name)
 8   pwnerer_user = RailsPwnerer::Config[app_name, instance_name][:pwnerer_user]
 9   pwnerer_uid = uid_for_username(pwnerer_user)
10   pwnerer_gid = gid_for_username(pwnerer_user)
11   
12   timestamp = Time.now.strftime '%Y%m%d%H%M%S'
13   dump_file = "files/#{app_name}.#{instance_name}_#{timestamp}.tar.gz"
14   
15   backup_path = RailsPwnerer::Config[app_name, instance_name][:backup_path]
16   scaffold_backup app_name, instance_name unless File.exist?(backup_path)
17   
18   app_path = RailsPwnerer::Config[app_name, instance_name][:app_path]
19   Dir.chdir backup_path do
20     # create a cold copy of the application files
21     cold_copy = File.join('tmp', File.basename(app_path))
22     FileUtils.rm_r cold_copy if File.exists? cold_copy
23     FileUtils.cp_r app_path, 'tmp'
24     
25     # remove the garbage in the cold copy
26     [RailsPwnerer::App::Git, RailsPwnerer::App::Svn].each do |mod|
27       mod.new.cleanup_app_caches cold_copy, instance_name, true
28     end
29     
30     # pack and protect the cold copy
31     Dir.chdir cold_copy do
32       Kernel.system "tar -czf ../../#{dump_file} ."
33     end
34     File.chmod 0400, dump_file
35     File.chown pwnerer_uid, pwnerer_gid, dump_file
36     
37     # clean up
38     FileUtils.rm_r cold_copy
39   end    
40 end
fix_permissions(app_name, instance_name) click to toggle source

Sets the right permissions on the application’s working areas.

    # File lib/rails_pwnerer/app/files.rb
119 def fix_permissions(app_name, instance_name)
120   pwnerer_user = RailsPwnerer::Config[app_name, instance_name][:pwnerer_user]
121   pwnerer_uid = uid_for_username(pwnerer_user)
122   pwnerer_gid = gid_for_username(pwnerer_user)
123   
124   Dir.chdir(RailsPwnerer::Config[app_name, instance_name][:app_path]) do
125     ['tmp', 'public'].each do |subdir|
126       FileUtils.mkdir_p subdir unless File.exist?(subdir)
127       FileUtils.chmod_R 0775, subdir
128       FileUtils.chown_R pwnerer_uid, pwnerer_gid, subdir
129     end
130   end
131 end
load_files(app_name, instance_name) click to toggle source

loads the latest file dump from the backup area

   # File lib/rails_pwnerer/app/files.rb
61 def load_files(app_name, instance_name)
62   backup_path = RailsPwnerer::Config[app_name, instance_name][:backup_path]
63   app_path = RailsPwnerer::Config[app_name, instance_name][:app_path]
64   
65   dump_file = Dir.glob(File.join(backup_path, "files/#{app_name}.#{instance_name}_*")).max
66   unless dump_file
67     dump_file = Dir.glob(File.join(backup_path, "files/#{app_name}.*")).max
68   end
69   FileUtils.mkdir_p app_path unless File.exists? app_path
70 
71   pwnerer_user = RailsPwnerer::Config[app_name, instance_name][:pwnerer_user]
72   pwnerer_uid = uid_for_username(pwnerer_user)
73   pwnerer_gid = gid_for_username(pwnerer_user)
74   File.chown(pwnerer_uid, pwnerer_gid, app_path)
75 
76   Dir.chdir app_path do
77     # find the latest dump and load it in
78     system "tar -xzf #{dump_file}"
79   end
80 end
manage(app_name, instance_name, action) click to toggle source
    # File lib/rails_pwnerer/app/files.rb
 92 def manage(app_name, instance_name, action)
 93   case action
 94   when :checkpoint
 95     dump_files app_name, instance_name
 96   when :rollback
 97     drop_files app_name, instance_name
 98     load_files app_name, instance_name      
 99   when :console
100     Dir.chdir(RailsPwnerer::Config[app_name, instance_name][:app_path]) do
101       if File.exist? 'script/rails'
102         Kernel.system 'rails console production'
103       else
104         Kernel.system 'ruby script/console production'
105       end
106     end
107   when :db_console
108     Dir.chdir(RailsPwnerer::Config[app_name, instance_name][:app_path]) do
109       if File.exist? 'script/rails'
110         Kernel.system 'rails dbconsole production --include-password'
111       else
112         Kernel.system 'ruby script/dbconsole production --include-password'
113       end
114     end      
115   end
116 end
remove(app_name, instance_name) click to toggle source
    # File lib/rails_pwnerer/app/files.rb
143 def remove(app_name, instance_name)
144   drop_files(app_name, instance_name)
145 end
scaffold_backup(app_name, instance_name) click to toggle source

creates the directory scaffold in the application’s backup dir

   # File lib/rails_pwnerer/app/files.rb
43 def scaffold_backup(app_name, instance_name)
44   pwnerer_user = RailsPwnerer::Config[app_name, instance_name][:pwnerer_user]
45   pwnerer_uid = uid_for_username(pwnerer_user)
46   pwnerer_gid = gid_for_username(pwnerer_user)
47 
48   backup_path = RailsPwnerer::Config[app_name, instance_name][:backup_path]
49   FileUtils.mkpath backup_path unless File.exists? backup_path
50   File.chown(pwnerer_uid, pwnerer_gid, backup_path)
51   
52   Dir.chdir(backup_path) do
53     ['db', 'files', 'tmp'].each do |subdir|
54       Dir.mkdir subdir unless File.exists? subdir
55       File.chown pwnerer_uid, pwnerer_gid, subdir
56     end
57   end
58 end
setup(app_name, instance_name) click to toggle source
    # File lib/rails_pwnerer/app/files.rb
133 def setup(app_name, instance_name)
134   scaffold_backup app_name, instance_name
135   fix_permissions app_name, instance_name
136 end
update(app_name, instance_name) click to toggle source
    # File lib/rails_pwnerer/app/files.rb
138 def update(app_name, instance_name)
139   scaffold_backup app_name, instance_name
140   fix_permissions app_name, instance_name
141 end