Module: RailsPwnerer::Config
- Extended by:
- Base
- Defined in:
- lib/rails_pwnerer/config/app.rb,
lib/rails_pwnerer.rb,
lib/rails_pwnerer/config/main.rb,
lib/rails_pwnerer/config/paths.rb,
lib/rails_pwnerer/config/ports.rb,
lib/rails_pwnerer/config/frontends.rb,
lib/rails_pwnerer/config/repository.rb
Overview
implements the configuration repository
Constant Summary collapse
- @@db_cache =
maps each database name to its contents
Hash.new
- @@db_dirty =
maps each database name to its dirty flag
Hash.new
Class Method Summary collapse
-
.[](db_or_app_name, instance_name = nil) ⇒ Object
override [] to make the global DB look like a Hash.
-
.all_applications ⇒ Object
all instances of all applications installed on this box.
-
.alloc_ports(nports = 1) ⇒ Object
allocates a contiguous range of ports returns the starting port or nil.
-
.app_db_name(app_name, instance_name) ⇒ Object
the name of the database storing an app’s configuration.
-
.app_frontends(app_name, instance_name) ⇒ Object
the number of frontends for an application.
-
.app_instances(app_name) ⇒ Object
the instances of an application installed on this box.
-
.create_db(db_name) ⇒ Object
creates a new database.
- .databases ⇒ Object
-
.drop_db(db_name) ⇒ Object
drops a database.
-
.flush_db(db_name) ⇒ Object
flushes the contents of the given db from cache.
-
.flush_db_cache ⇒ Object
flushes the entire database cache (used when exiting).
-
.free_ports(start, count = 1) ⇒ Object
returns a continuous range of ports to the free list.
-
.get_db(db_name) ⇒ Object
retrieves the contents of a db (from cache if necessary).
- .init_ports(free_ports = [[8000, 16000]]) ⇒ Object
-
.install_db_hooks(db_contents, db_name) ⇒ Object
installs the database hooks into a Hash.
-
.mark_db_dirty(db_name) ⇒ Object
called when a database is dirty.
-
.path_to(what = :prod, app_name = nil) ⇒ Object
the path to something important (e.g. :apps –> path to all production applications).
-
.static_path_to(what) ⇒ Object
hardcoded paths.
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
Class Method Details
.[](db_or_app_name, instance_name = nil) ⇒ Object
override [] to make the global DB look like a Hash
99 100 101 102 103 104 105 106 |
# File 'lib/rails_pwnerer/config/repository.rb', line 99 def self.[](db_or_app_name, instance_name = nil) if instance_name.nil? db_name = db_or_app_name else db_name = app_db_name(db_or_app_name, instance_name) end get_db(db_name) end |
.all_applications ⇒ Object
all instances of all applications installed on this box
15 16 17 18 19 20 |
# File 'lib/rails_pwnerer/config/app.rb', line 15 def self.all_applications() self.databases().select { |db| db.include? ?. }.map do |db| lastdot = db.rindex ?. [db[0...lastdot], db[(lastdot + 1)..-1]] end end |
.alloc_ports(nports = 1) ⇒ Object
allocates a contiguous range of ports returns the starting port or nil
12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/rails_pwnerer/config/ports.rb', line 12 def self.alloc_ports(nports = 1) free_ports = self[:free_ports][:list] free_ports.each_index do |i| next if free_ports[i][1] - free_ports[i][0] < nports first_port = free_ports[i][0] free_ports[i][0] += nports free_ports.delete_at i if free_ports[i][0] == free_ports[i][1] self[:free_ports][:list] = free_ports self.flush_db :free_ports return first_port end end |
.app_db_name(app_name, instance_name) ⇒ Object
the name of the database storing an app’s configuration
5 6 7 |
# File 'lib/rails_pwnerer/config/app.rb', line 5 def self.app_db_name(app_name, instance_name) return "#{app_name}.#{instance_name}" end |
.app_frontends(app_name, instance_name) ⇒ Object
the number of frontends for an application
5 6 7 8 9 10 11 12 13 14 |
# File 'lib/rails_pwnerer/config/frontends.rb', line 5 def self.app_frontends(app_name, instance_name) # TODO: this is duplicated in cluster_config.rb -- pull up somewhere app_config = self[app_name, instance_name] return nil unless app_config and fixed_frontends = app_config[:frontends] frontends_per_core = app_config[:frontends_per_core] || 0 detected_cores = app_config[:detected_cores] || 0 cores_frontends = frontends_per_core * detected_cores return (cores_frontends != 0) ? cores_frontends : fixed_frontends end |
.app_instances(app_name) ⇒ Object
the instances of an application installed on this box
10 11 12 |
# File 'lib/rails_pwnerer/config/app.rb', line 10 def self.app_instances(app_name) self.databases().select { |db| db.include? ?. }.map { |db| db[0...(db.rindex ?.)] } end |
.create_db(db_name) ⇒ Object
creates a new database
40 41 42 43 44 45 46 47 48 |
# File 'lib/rails_pwnerer/config/repository.rb', line 40 def self.create_db(db_name) db_name = db_name.to_s db_contents = install_db_hooks({}, db_name) @@db_cache[db_name] = db_contents @@db_dirty[db_name] = true flush_db db_name return db_contents end |
.databases ⇒ Object
108 109 110 111 112 113 114 115 116 |
# File 'lib/rails_pwnerer/config/repository.rb', line 108 def self.databases() entries = Dir.entries RailsPwnerer::Config.path_to(:config) databases = [] entries.each do |entry| next unless entry =~ /\.yml(2)?$/ databases << entry.gsub(/\.yml(2)?$/, '') end return databases end |
.drop_db(db_name) ⇒ Object
drops a database
51 52 53 54 55 |
# File 'lib/rails_pwnerer/config/repository.rb', line 51 def self.drop_db(db_name) @@db_cache[db_name] = nil @@db_dirty[db_name] = true flush_db db_name end |
.flush_db(db_name) ⇒ Object
flushes the contents of the given db from cache
76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/rails_pwnerer/config/repository.rb', line 76 def self.flush_db(db_name) db_name = db_name.to_s return unless @@db_dirty[db_name] db_path = RailsPwnerer::Config.path_to :config if @@db_cache[db_name].nil? atomic_erase db_path, db_name else host_config = get_db :host atomic_write @@db_cache[db_name], db_path, db_name, :owner => (host_config && host_config[:pwnerer_user]) end @@db_dirty[db_name] = false end |
.flush_db_cache ⇒ Object
flushes the entire database cache (used when exiting)
91 92 93 94 95 96 |
# File 'lib/rails_pwnerer/config/repository.rb', line 91 def self.flush_db_cache @@db_dirty.each do |db_name, is_dirty| next unless is_dirty flush_db db_name end end |
.free_ports(start, count = 1) ⇒ Object
returns a continuous range of ports to the free list
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/rails_pwnerer/config/ports.rb', line 26 def self.free_ports(start, count = 1) free_ports = self[:free_ports][:list] free_ports << [start, start + count] # concatenate duplicates free_ports.sort! { |a, b| a[0] <=> b[0]} new_ports = [free_ports.first] free_ports.each_index do |i| if new_ports.last[1] >= free_ports[i][0] new_ports.last[1] = [new_ports.last[1], free_ports[i][1]].max else new_ports << free_ports[i] end end # write the new database self[:free_ports][:list] = new_ports self.flush_db :free_ports end |
.get_db(db_name) ⇒ Object
retrieves the contents of a db (from cache if necessary)
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/rails_pwnerer/config/repository.rb', line 58 def self.get_db(db_name) db_name = db_name.to_s unless @@db_cache.has_key? db_name db_path = RailsPwnerer::Config.path_to :config db_contents = atomic_read(db_path, db_name) if db_contents.nil? @@db_cache[db_name] = nil else @@db_cache[db_name] = install_db_hooks db_contents, db_name end @@db_dirty[db_name] = false end return @@db_cache[db_name] end |
.init_ports(free_ports = [[8000, 16000]]) ⇒ Object
4 5 6 7 8 |
# File 'lib/rails_pwnerer/config/ports.rb', line 4 def self.init_ports(free_ports = [[8000, 16000]]) self.create_db :free_ports self[:free_ports][:list] = free_ports self.flush_db :free_ports end |
.install_db_hooks(db_contents, db_name) ⇒ Object
installs the database hooks into a Hash
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/rails_pwnerer/config/repository.rb', line 19 def self.install_db_hooks(db_contents, db_name) # hooks: # (1) automatically convert keys to strings # (2) flip the dirty flag on writes class << db_contents def [](key) super(key.to_s) end def []=(key, value) super(key.to_s, value) RailsPwnerer::Config.mark_db_dirty @db_name end def has_key?(key) super(key.to_s) end end db_contents.instance_variable_set :@db_name, db_name return db_contents end |
.mark_db_dirty(db_name) ⇒ Object
called when a database is dirty
14 15 16 |
# File 'lib/rails_pwnerer/config/repository.rb', line 14 def self.mark_db_dirty(db_name) @@db_dirty[db_name] = true end |
.path_to(what = :prod, app_name = nil) ⇒ Object
the path to something important (e.g. :apps –> path to all production applications)
5 6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/rails_pwnerer/config/paths.rb', line 5 def self.path_to(what = :prod, app_name = nil) # need to hardcode path to config to avoid endless recursion if what == :config return '/prod/config/' end # first try the paths in the database return self[:paths][what] if self[:paths] and self[:paths].has_key? what # then try the global paths static_path = static_path_to what return static_path unless static_path.nil? end |
.static_path_to(what) ⇒ Object
hardcoded paths
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/rails_pwnerer/config/paths.rb', line 20 def self.static_path_to(what) case what when :config # the directory containing the config files '/prod/config' when :prod # the directory containing all the production data '/prod' when :apps # the directory containing the production apps '/prod/apps' when :backups '/prod/backups' else return end end |