Class: RailsPwnerer::App::Files

Inherits:
Object
  • Object
show all
Includes:
Base
Defined in:
lib/rails_pwnerer/app/files.rb

Overview

manages an application’s file system

Instance Method Summary collapse

Methods included from Base

_setup_unix, _setup_windows, all_packages, all_packages_without_caching, #atomic_erase, #atomic_read, #atomic_write, #best_package_matching, #check_rails_root, #control_boot_script, #cpu_cores, #current_user, #gem_exists?, #gid_for_username, #group_for_username, #hook_boot_script, #install_gem, #install_gems, #install_package, #install_package_impl, #install_package_matching, #install_packages, #kill_tree, #os_distro, package_info_hash, #path_to_boot_script, #path_to_boot_script_defaults, #path_to_gemdir, #process_info, #prompt_user_for_password, #remove_package, #remove_packages, #search_packages, #uid_for_username, #unroll_collection, #update_all_packages, #update_all_packages_impl, #update_gems, #update_package_metadata, #upgrade_gem, #upgrade_gems, #upgrade_package, #upgrade_package_impl, #upgrade_packages, #with_package_source, #with_temp_dir

Instance Method Details

#drop_files(app_name, instance_name) ⇒ Object

remove the application files



83
84
85
86
87
88
89
90
# File 'lib/rails_pwnerer/app/files.rb', line 83

def drop_files(app_name, instance_name)
  app_config = RailsPwnerer::Config[app_name, instance_name]
  # exit and don't complain if the app is busted
  return unless app_config and File.exists? app_config[:app_path]

  app_path = app_config[:app_path]
  FileUtils.rm_r app_path if File.exists? app_path
end

#dump_files(app_name, instance_name) ⇒ Object

dump the application files to the backup area



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/rails_pwnerer/app/files.rb', line 7

def dump_files(app_name, instance_name)
  pwnerer_user = RailsPwnerer::Config[app_name, instance_name][:pwnerer_user]
  pwnerer_uid = uid_for_username(pwnerer_user)
  pwnerer_gid = gid_for_username(pwnerer_user)
  
  timestamp = Time.now.strftime '%Y%m%d%H%M%S'
  dump_file = "files/#{app_name}.#{instance_name}_#{timestamp}.tar.gz"
  
  backup_path = RailsPwnerer::Config[app_name, instance_name][:backup_path]
  scaffold_backup app_name, instance_name unless File.exist?(backup_path)
  
  app_path = RailsPwnerer::Config[app_name, instance_name][:app_path]
  Dir.chdir backup_path do
    # create a cold copy of the application files
    cold_copy = File.join('tmp', File.basename(app_path))
    FileUtils.rm_r cold_copy if File.exists? cold_copy
    FileUtils.cp_r app_path, 'tmp'
    
    # remove the garbage in the cold copy
    [RailsPwnerer::App::Git, RailsPwnerer::App::Svn].each do |mod|
      mod.new.cleanup_app_caches cold_copy, instance_name, true
    end
    
    # pack and protect the cold copy
    Dir.chdir cold_copy do
      Kernel.system "tar -czf ../../#{dump_file} ."
    end
    File.chmod 0400, dump_file
    File.chown pwnerer_uid, pwnerer_gid, dump_file
    
    # clean up
    FileUtils.rm_r cold_copy
  end    
end

#fix_permissions(app_name, instance_name) ⇒ Object

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



119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/rails_pwnerer/app/files.rb', line 119

def fix_permissions(app_name, instance_name)
  pwnerer_user = RailsPwnerer::Config[app_name, instance_name][:pwnerer_user]
  pwnerer_uid = uid_for_username(pwnerer_user)
  pwnerer_gid = gid_for_username(pwnerer_user)
  
  Dir.chdir(RailsPwnerer::Config[app_name, instance_name][:app_path]) do
    ['tmp', 'public'].each do |subdir|
      FileUtils.mkdir_p subdir unless File.exist?(subdir)
      FileUtils.chmod_R 0775, subdir
      FileUtils.chown_R pwnerer_uid, pwnerer_gid, subdir
    end
  end
end

#load_files(app_name, instance_name) ⇒ Object

loads the latest file dump from the backup area



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/rails_pwnerer/app/files.rb', line 61

def load_files(app_name, instance_name)
  backup_path = RailsPwnerer::Config[app_name, instance_name][:backup_path]
  app_path = RailsPwnerer::Config[app_name, instance_name][:app_path]
  
  dump_file = Dir.glob(File.join(backup_path, "files/#{app_name}.#{instance_name}_*")).max
  unless dump_file
    dump_file = Dir.glob(File.join(backup_path, "files/#{app_name}.*")).max
  end
  FileUtils.mkdir_p app_path unless File.exists? app_path

  pwnerer_user = RailsPwnerer::Config[app_name, instance_name][:pwnerer_user]
  pwnerer_uid = uid_for_username(pwnerer_user)
  pwnerer_gid = gid_for_username(pwnerer_user)
  File.chown(pwnerer_uid, pwnerer_gid, app_path)

  Dir.chdir app_path do
    # find the latest dump and load it in
    system "tar -xzf #{dump_file}"
  end
end

#manage(app_name, instance_name, action) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/rails_pwnerer/app/files.rb', line 92

def manage(app_name, instance_name, action)
  case action
  when :checkpoint
    dump_files app_name, instance_name
  when :rollback
    drop_files app_name, instance_name
    load_files app_name, instance_name      
  when :console
    Dir.chdir(RailsPwnerer::Config[app_name, instance_name][:app_path]) do
      if File.exist? 'script/rails'
        Kernel.system 'rails console production'
      else
        Kernel.system 'ruby script/console production'
      end
    end
  when :db_console
    Dir.chdir(RailsPwnerer::Config[app_name, instance_name][:app_path]) do
      if File.exist? 'script/rails'
        Kernel.system 'rails dbconsole production --include-password'
      else
        Kernel.system 'ruby script/dbconsole production --include-password'
      end
    end      
  end
end

#remove(app_name, instance_name) ⇒ Object



143
144
145
# File 'lib/rails_pwnerer/app/files.rb', line 143

def remove(app_name, instance_name)
  drop_files(app_name, instance_name)
end

#scaffold_backup(app_name, instance_name) ⇒ Object

creates the directory scaffold in the application’s backup dir



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/rails_pwnerer/app/files.rb', line 43

def scaffold_backup(app_name, instance_name)
  pwnerer_user = RailsPwnerer::Config[app_name, instance_name][:pwnerer_user]
  pwnerer_uid = uid_for_username(pwnerer_user)
  pwnerer_gid = gid_for_username(pwnerer_user)

  backup_path = RailsPwnerer::Config[app_name, instance_name][:backup_path]
  FileUtils.mkpath backup_path unless File.exists? backup_path
  File.chown(pwnerer_uid, pwnerer_gid, backup_path)
  
  Dir.chdir(backup_path) do
    ['db', 'files', 'tmp'].each do |subdir|
      Dir.mkdir subdir unless File.exists? subdir
      File.chown pwnerer_uid, pwnerer_gid, subdir
    end
  end
end

#setup(app_name, instance_name) ⇒ Object



133
134
135
136
# File 'lib/rails_pwnerer/app/files.rb', line 133

def setup(app_name, instance_name)
  scaffold_backup app_name, instance_name
  fix_permissions app_name, instance_name
end

#update(app_name, instance_name) ⇒ Object



138
139
140
141
# File 'lib/rails_pwnerer/app/files.rb', line 138

def update(app_name, instance_name)
  scaffold_backup app_name, instance_name
  fix_permissions app_name, instance_name
end