Class: RailsPwnerer::App::Config

Inherits:
Object
  • Object
show all
Includes:
Base
Defined in:
lib/rails_pwnerer/app/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

#alloc(app_name, instance_name) ⇒ Object

allocates room for the application and creates the application configuration database



56
57
58
59
60
61
62
63
64
65
# File 'lib/rails_pwnerer/app/config.rb', line 56

def alloc(app_name, instance_name)
  app_db_name = RailsPwnerer::Config.app_db_name(app_name, instance_name)
  app_db = RailsPwnerer::Config.create_db app_db_name
  populate_defaults app_name, instance_name, app_db

  FileUtils.mkpath app_db[:app_path]

  RailsPwnerer::Config.flush_db app_db
  return app_db[:app_path]
end

#manage(app_name, instance_name, action) ⇒ Object



117
118
119
120
121
122
123
# File 'lib/rails_pwnerer/app/config.rb', line 117

def manage(app_name, instance_name, action)
  case action
  when :rekey
    app_config = RailsPwnerer::Config[app_name, instance_name]
    app_config[:db_pass] = random_db_password
  end
end

#populate_defaults(app_name, instance_name, app_db) ⇒ Object

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



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
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/rails_pwnerer/app/config.rb', line 14

def populate_defaults(app_name, instance_name, app_db)
  # the path to application main files
  app_db[:app_path] = File.join(RailsPwnerer::Config.path_to(:apps), app_name + '.' + instance_name)
  # the path to application backups
  app_db[:backup_path] ||= File.join(RailsPwnerer::Config.path_to(:backups), app_name + '.' + instance_name)

  # the user which will receive the "keys" to the production system
  app_db[:pwnerer_user] ||= RailsPwnerer::Config[:host][:pwnerer_user]
  # the number of frontends for the application instance
  app_db[:frontends] ||= 4 # most computers have 2 cores nowadays
  # the number of frontends per core for the application instance
  app_db[:frontends_per_core] ||= 2 # best practice
  # the first internal port for the application instance
  app_db[:port0] = 0  # will be overwritten during allocation
  # the name of the database for the application instance
  app_db[:db_name] ||= (app_name + '_' + instance_name + '_prod')[0...60] # avoiding mySQL breakage
  # the datbase user for the given application
  app_db[:db_user] ||= (app_name + '_' + instance_name)[0...16] # mySQL doesn't like long user names
  # the password of the database user for the given application
  app_db[:db_pass] ||= random_db_password
  # a DNS name for server-based filtering (multiple apps on the same box)
  app_db[:dns_name] ||= ''
  # the environment to run the application in
  app_db[:environment] ||= 'production'
  # main port where the application receives HTTP(S) outside connections
  app_db[:port] ||= 80
  # HTTPS applications also accept HTTP connections here, unless 0
  app_db[:non_ssl_port] ||= 80
  # the maximum request size (megabytes) to be accepted by an application
  app_db[:max_request_mb] ||= 48
  # comma-separated directories that should be writable by the application user
  app_db[:writable_dirs] ||= ''
  # comma-separated gems that should be installed for the application
  app_db[:gems] ||= ''
  # set to disable accidental db resets (on production vs. staging instances)
  app_db[:enable_db_reset] ||= false

  # the number of cores on the platform
  app_db[:detected_cores] ||= cpu_cores.length
end

#random_db_passwordObject



7
8
9
# File 'lib/rails_pwnerer/app/config.rb', line 7

def random_db_password
  (0...16).map { |i| "abcdefghijklmnopqrstuvwxyz"[rand(26),1]}.join
end

#remove(app_name, instance_name) ⇒ Object



129
130
131
132
# File 'lib/rails_pwnerer/app/config.rb', line 129

def remove(app_name, instance_name)
  app_db_name = RailsPwnerer::Config.app_db_name(app_name, instance_name)
  RailsPwnerer::Config.drop_db app_db_name
end

#setup(app_name, instance_name) ⇒ Object



125
126
127
# File 'lib/rails_pwnerer/app/config.rb', line 125

def setup(app_name, instance_name)
  update app_name, instance_name
end

#update(app_name, instance_name) ⇒ Object

pushes config changes from the application file to the database



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
107
108
109
110
111
112
113
114
115
# File 'lib/rails_pwnerer/app/config.rb', line 68

def update(app_name, instance_name)
  app_config = RailsPwnerer::Config[app_name, instance_name]

  db_name, db_user, db_pass = app_config[:db_name], app_config[:db_user], app_config[:db_pass]
  app_config.clear
  # NOTE: we don't restore the password on purpose, to get a new password on the update
  # this is useful so processes that were spawned before the update can't corrupt the db
  app_config[:db_name], app_config[:db_user] = db_name, db_user

  populate_defaults app_name, instance_name, app_config
  Dir.chdir app_config[:app_path] do
    # Populate the default SSL configuration if the right files exist.
    ssl_cert = File.expand_path "config/rails_pwnerer/#{instance_name}.cer"

    ssl_cert2 = File.expand_path "config/rails_pwnerer/#{instance_name}.crt"
    if !File.exists?(ssl_cert) and File.exists?(ssl_cert2)
      ssl_cert = ssl_cert2
    end

    ssl_key = File.expand_path "config/rails_pwnerer/#{instance_name}.pem"
    if File.exists?(ssl_cert) and File.exists?(ssl_key)
      app_config[:ssl_cert] = ssl_cert
      app_config[:ssl_key] = ssl_key
      app_config[:port] = 443
      app_config[:non_ssl_port] = 80
    end

    ["config/rails_pwnerer.yml", "config/rails_pwnerer/#{instance_name}.yml"].each do |fname|
      next unless File.exists? fname
      config_update = File.open(fname, 'r') { |f| YAML.load f } rescue nil
      unless config_update
        print "Configuration file ignored due to parsing error - #{fname}\n"
        config_update = {}
      end
      config_update.each do |key, value|
        app_config[key] = value
      end
    end
  end

  # TODO: if database settings changed, the database should be moved (re-created or re-keyed)
  if db_pass != app_config[:db_pass]
    db_pass = random_db_password if !db_pass || db_pass.empty?
    RailsPwnerer::App::Database.new.manage app_name, instance_name, :rekey
  end

  RailsPwnerer::Config.flush_db RailsPwnerer::Config.app_db_name(app_name, instance_name)
end