Class: RailsPwnerer::App::ClusterConfig

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

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

#control_all(action) ⇒ Object



135
136
137
138
139
140
141
142
143
144
# File 'lib/rails_pwnerer/app/cluster_config.rb', line 135

def control_all(action)
  RailsPwnerer::Config.all_applications.each do |ai|
    case action
    when :start
      RailsPwnerer::App::ClusterConfig.new.start ai[0], ai[1]
    when :stop
      RailsPwnerer::App::ClusterConfig.new.stop ai[0], ai[1]        
    end
  end 
end

#fix_permissions(app_name, instance_name) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/rails_pwnerer/app/cluster_config.rb', line 8

def fix_permissions(app_name, instance_name)
  app_config = RailsPwnerer::Config[app_name, instance_name]    
  pwnerer_user = app_config[:pwnerer_user]
  pwnerer_uid = uid_for_username(pwnerer_user)
  pwnerer_group = group_for_username(pwnerer_user)
  
  writable_dirs = %w(log tmp public)
  if app_config[:writable_dirs]      
    writable_dirs += app_config[:writable_dirs].split ','
  end
  
  return unless File.exist?(app_config[:app_path])
  Dir.chdir app_config[:app_path] do
    writable_dirs.each do |writable_dir|
      FileUtils.mkpath writable_dir unless File.exists? writable_dir
      FileUtils.chown_R(pwnerer_uid, pwnerer_group, writable_dir)
    end
  end
end

#manage(app_name, instance_name, action) ⇒ Object



126
127
128
129
130
131
132
133
# File 'lib/rails_pwnerer/app/cluster_config.rb', line 126

def manage(app_name, instance_name, action)
  case action
  when :checkpoint
    # nothing to do here
  when :rollback
    fix_permissions app_name, instance_name
  end
end

#manage_ports(app_name, instance_name, action) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/rails_pwnerer/app/cluster_config.rb', line 28

def manage_ports(app_name, instance_name, action)
  app_config = RailsPwnerer::Config[app_name, instance_name]
  return unless frontends = RailsPwnerer::Config.app_frontends(app_name, instance_name)
  
  case action
  when :alloc
    app_config[:port0] = RailsPwnerer::Config.alloc_ports frontends
  when :free
    RailsPwnerer::Config.free_ports app_config[:port0], frontends
    return if app_config[:port0] == 0 # do not release if ports have already been released
    app_config[:port0] = 0
  end
  RailsPwnerer::Config.flush_db RailsPwnerer::Config.app_db_name(app_name, instance_name)
end

#post_update(app_name, instance_name) ⇒ Object



117
118
119
120
# File 'lib/rails_pwnerer/app/cluster_config.rb', line 117

def post_update(app_name, instance_name)
  manage_ports app_name, instance_name, :alloc
  fix_permissions app_name, instance_name
end

#pre_update(app_name, instance_name, &update_proc) ⇒ Object



114
115
116
# File 'lib/rails_pwnerer/app/cluster_config.rb', line 114

def pre_update(app_name, instance_name, &update_proc)
  manage_ports app_name, instance_name, :free
end

#remove(app_name, instance_name) ⇒ Object



122
123
124
# File 'lib/rails_pwnerer/app/cluster_config.rb', line 122

def remove(app_name, instance_name)
  manage_ports app_name, instance_name, :free
end

#setup(app_name, instance_name) ⇒ Object



108
109
110
111
112
# File 'lib/rails_pwnerer/app/cluster_config.rb', line 108

def setup(app_name, instance_name)
  manage_ports app_name, instance_name, :alloc
  fix_permissions app_name, instance_name
  # no need to configure nginx here, it'll be done by the executor
end

#start(app_name, instance_name) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/rails_pwnerer/app/cluster_config.rb', line 68

def start(app_name, instance_name)
  app_config = RailsPwnerer::Config[app_name, instance_name]
  # silently die if the app was completely busted
  return unless app_config and File.exists? app_config[:app_path]

  app_path, pwnerer_user = app_config[:app_path], app_config[:pwnerer_user]
  pwnerer_group = group_for_username(pwnerer_user)    
  frontends = RailsPwnerer::Config.app_frontends(app_name, instance_name)
  first_port, environment = app_config[:port0], app_config[:environment]

  stop app_name, instance_name
  
  # alloc a port if somehow that slipped through the cracks
  if first_port == 0
    manage_ports app_name, instance_name, :alloc
    first_port = app_config[:port0]
    RailsPwnerer::App::NginxConfig.new.update app_name, instance_name
  end
  
  static_cmd = "thin start -a 127.0.0.1 -c #{app_path} -u #{pwnerer_user} " +
               "-g #{pwnerer_group} -e #{environment} -d " +
               " --tag rpwn_#{app_name}.#{instance_name} "
  
  # TODO: start the servers simultaneously
  Dir.chdir app_path do
    if File.exist? 'config.ru'
      static_cmd << '-R config.ru '
    else    
      static_cmd << '-A rails '
    end
    static_cmd = 'bundle exec ' + static_cmd if File.exist?('Gemfile')
    
    frontends.times do |f|
      fe_port = first_port + f
      cmd = static_cmd + "-p #{fe_port} -l log/fe.#{fe_port}.log -P tmp/pids/fe.#{fe_port}.pid"
      Kernel.system cmd
    end
  end
end

#stop(app_name, instance_name) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/rails_pwnerer/app/cluster_config.rb', line 43

def stop(app_name, instance_name)
  app_config = RailsPwnerer::Config[app_name, instance_name]
  # silently die if the app was completely busted
  return unless app_config and File.exists? app_config[:app_path]
      
  app_path, first_port = app_config[:app_path], app_config[:port0]
  frontends = RailsPwnerer::Config.app_frontends(app_name, instance_name)

  cmdline_patterns = ['thin', nil]
  
  Dir.chdir app_path do
    # TODO: stop processes in parallel
    frontends.times do |f|
      fe_port = first_port + f
      cmdline = "thin stop -a 127.0.0.1 -p #{fe_port} -d " +
                "-P tmp/pids/fe.#{fe_port}.pid"
      cmdline = 'bundle exec ' + cmdline if File.exist?('Gemfile')
      cmdline_patterns[1] = "(127.0.0.1:#{fe_port})"
      RailsPwnerer::Util.kill_process_set(cmdline, "tmp/pids/fe.#{fe_port}.pid",
                         cmdline_patterns, :verbose => false, :sleep_delay => 0.2)
    end
    
  end
end