class RailsPwnerer::App::Config

Public Instance Methods

alloc(app_name, instance_name) click to toggle source

allocates room for the application and creates the application configuration database

   # File lib/rails_pwnerer/app/config.rb
56 def alloc(app_name, instance_name)
57   app_db_name = RailsPwnerer::Config.app_db_name(app_name, instance_name)
58   app_db = RailsPwnerer::Config.create_db app_db_name
59   populate_defaults app_name, instance_name, app_db
60 
61   FileUtils.mkpath app_db[:app_path]
62 
63   RailsPwnerer::Config.flush_db app_db
64   return app_db[:app_path]
65 end
manage(app_name, instance_name, action) click to toggle source
    # File lib/rails_pwnerer/app/config.rb
117 def manage(app_name, instance_name, action)
118   case action
119   when :rekey
120     app_config = RailsPwnerer::Config[app_name, instance_name]
121     app_config[:db_pass] = random_db_password
122   end
123 end
populate_defaults(app_name, instance_name, app_db) click to toggle source

fills inexistent keys with their default values setup: this effectively creates the baseline configuration db update: this adds keys that might have been added in new versions of rpwn

   # File lib/rails_pwnerer/app/config.rb
14 def populate_defaults(app_name, instance_name, app_db)
15   # the path to application main files
16   app_db[:app_path] = File.join(RailsPwnerer::Config.path_to(:apps), app_name + '.' + instance_name)
17   # the path to application backups
18   app_db[:backup_path] ||= File.join(RailsPwnerer::Config.path_to(:backups), app_name + '.' + instance_name)
19 
20   # the user which will receive the "keys" to the production system
21   app_db[:pwnerer_user] ||= RailsPwnerer::Config[:host][:pwnerer_user]
22   # the number of frontends for the application instance
23   app_db[:frontends] ||= 4 # most computers have 2 cores nowadays
24   # the number of frontends per core for the application instance
25   app_db[:frontends_per_core] ||= 2 # best practice
26   # the first internal port for the application instance
27   app_db[:port0] = 0  # will be overwritten during allocation
28   # the name of the database for the application instance
29   app_db[:db_name] ||= (app_name + '_' + instance_name + '_prod')[0...60] # avoiding mySQL breakage
30   # the datbase user for the given application
31   app_db[:db_user] ||= (app_name + '_' + instance_name)[0...16] # mySQL doesn't like long user names
32   # the password of the database user for the given application
33   app_db[:db_pass] ||= random_db_password
34   # a DNS name for server-based filtering (multiple apps on the same box)
35   app_db[:dns_name] ||= ''
36   # the environment to run the application in
37   app_db[:environment] ||= 'production'
38   # main port where the application receives HTTP(S) outside connections
39   app_db[:port] ||= 80
40   # HTTPS applications also accept HTTP connections here, unless 0
41   app_db[:non_ssl_port] ||= 80
42   # the maximum request size (megabytes) to be accepted by an application
43   app_db[:max_request_mb] ||= 48
44   # comma-separated directories that should be writable by the application user
45   app_db[:writable_dirs] ||= ''
46   # comma-separated gems that should be installed for the application
47   app_db[:gems] ||= ''
48   # set to disable accidental db resets (on production vs. staging instances)
49   app_db[:enable_db_reset] ||= false
50 
51   # the number of cores on the platform
52   app_db[:detected_cores] ||= cpu_cores.length
53 end
random_db_password() click to toggle source
  # File lib/rails_pwnerer/app/config.rb
7 def random_db_password
8   (0...16).map { |i| "abcdefghijklmnopqrstuvwxyz"[rand(26),1]}.join
9 end
remove(app_name, instance_name) click to toggle source
    # File lib/rails_pwnerer/app/config.rb
129 def remove(app_name, instance_name)
130   app_db_name = RailsPwnerer::Config.app_db_name(app_name, instance_name)
131   RailsPwnerer::Config.drop_db app_db_name
132 end
setup(app_name, instance_name) click to toggle source
    # File lib/rails_pwnerer/app/config.rb
125 def setup(app_name, instance_name)
126   update app_name, instance_name
127 end
update(app_name, instance_name) click to toggle source

pushes config changes from the application file to the database

    # File lib/rails_pwnerer/app/config.rb
 68 def update(app_name, instance_name)
 69   app_config = RailsPwnerer::Config[app_name, instance_name]
 70 
 71   db_name, db_user, db_pass = app_config[:db_name], app_config[:db_user], app_config[:db_pass]
 72   app_config.clear
 73   # NOTE: we don't restore the password on purpose, to get a new password on the update
 74   # this is useful so processes that were spawned before the update can't corrupt the db
 75   app_config[:db_name], app_config[:db_user] = db_name, db_user
 76 
 77   populate_defaults app_name, instance_name, app_config
 78   Dir.chdir app_config[:app_path] do
 79     # Populate the default SSL configuration if the right files exist.
 80     ssl_cert = File.expand_path "config/rails_pwnerer/#{instance_name}.cer"
 81 
 82     ssl_cert2 = File.expand_path "config/rails_pwnerer/#{instance_name}.crt"
 83     if !File.exists?(ssl_cert) and File.exists?(ssl_cert2)
 84       ssl_cert = ssl_cert2
 85     end
 86 
 87     ssl_key = File.expand_path "config/rails_pwnerer/#{instance_name}.pem"
 88     if File.exists?(ssl_cert) and File.exists?(ssl_key)
 89       app_config[:ssl_cert] = ssl_cert
 90       app_config[:ssl_key] = ssl_key
 91       app_config[:port] = 443
 92       app_config[:non_ssl_port] = 80
 93     end
 94 
 95     ["config/rails_pwnerer.yml", "config/rails_pwnerer/#{instance_name}.yml"].each do |fname|
 96       next unless File.exists? fname
 97       config_update = File.open(fname, 'r') { |f| YAML.load f } rescue nil
 98       unless config_update
 99         print "Configuration file ignored due to parsing error - #{fname}\n"
100         config_update = {}
101       end
102       config_update.each do |key, value|
103         app_config[key] = value
104       end
105     end
106   end
107 
108   # TODO: if database settings changed, the database should be moved (re-created or re-keyed)
109   if db_pass != app_config[:db_pass]
110     db_pass = random_db_password if !db_pass || db_pass.empty?
111     RailsPwnerer::App::Database.new.manage app_name, instance_name, :rekey
112   end
113 
114   RailsPwnerer::Config.flush_db RailsPwnerer::Config.app_db_name(app_name, instance_name)
115 end