Class: BiblioTech::Config
- Inherits:
-
Object
- Object
- BiblioTech::Config
- Defined in:
- lib/bibliotech/config.rb,
lib/bibliotech/config/schedule.rb
Defined Under Namespace
Classes: Calendars, MissingConfig, Periods, Schedules
Constant Summary collapse
- CONFIG_STEPS =
{ :database_config_file => [ "database_config_file" ] , :database_config_env => [ "database_config_env" ] , :root_path => [ "path" ] , :host => [ "host" ] , :port => [ "port" ] , :user => [ "user" ] , :rsa_files => [ "rsa_files" ] , :ssh_options => [ "ssh_options" ] , :fetch_dir => [ "fetched_dir" ] , :log_target => [ "log" , "target" ], :log_level => [ "log" , "level" ], :file => [ "backups" , "file" ] , :filename => [ "backups" , "filename" ] , :backup_path => [ "backups" , "dir" ] , :compressor => [ "backups" , "compress" ] , :legacy_prune_schedule => [ "backups" , "keep" ] , :prune_schedule => [ "backups" , "retain" , "periodic" ] , :prune_calendar => [ "backups" , "retain" , "calendar" ] , :backup_name => [ "backups" , "prefix" ] , :backup_frequency => [ "backups" , "frequency" ] , :db_adapter => [ "database_config" , "adapter" ] , :db_host => [ "database_config" , "host" ] , :db_port => [ "database_config" , "port" ] , :db_database => [ "database_config" , "database" ] , :db_username => [ "database_config" , "username" ] , :db_password => [ "database_config" , "password" ] , }
Instance Attribute Summary collapse
- #hash ⇒ Object
-
#valise ⇒ Object
readonly
Returns the value of attribute valise.
File management collapse
- #backup_file ⇒ Object
- #backup_path ⇒ Object
- #compressor ⇒ Object
- #expander ⇒ Object
- #filename ⇒ Object
Database collapse
- #adapter ⇒ Object
- #database ⇒ Object
- #host ⇒ Object
- #password ⇒ Object
- #port ⇒ Object
- #username ⇒ Object
Instance Method Summary collapse
- #app_db_config ⇒ Object
- #backup_frequency ⇒ Object
- #backup_name ⇒ Object
- #database_config ⇒ Object
- #db_get(field) ⇒ Object
- #extract(*steps_chain) ⇒ Object
- #id_file(for_remote) ⇒ Object
-
#initialize(valise) ⇒ Config
constructor
A new instance of Config.
- #local ⇒ Object
- #local_file(filename) ⇒ Object
- #local_get(key) ⇒ Object
- #local_path ⇒ Object
- #log_level ⇒ Object
- #log_target ⇒ Object
- #merge(other_hash) ⇒ Object
- #merge_hashes(left, right) ⇒ Object
- #optional(&block) ⇒ Object (also: #optionally)
- #prune_schedules ⇒ Object
- #remote ⇒ Object
- #remote_file(remote, filename) ⇒ Object
- #remote_get(remote_name, key) ⇒ Object
- #remote_host(remote) ⇒ Object
- #remote_path(remote) ⇒ Object
- #remote_port(remote) ⇒ Object
- #remote_user(remote) ⇒ Object
- #root_dir_on(remote) ⇒ Object
- #ssh_options(for_remote) ⇒ Object
- #steps_for(key) ⇒ Object
-
#stringify_keys(hash) ⇒ Object
sym -> string.
Constructor Details
#initialize(valise) ⇒ Config
Returns a new instance of Config.
36 37 38 |
# File 'lib/bibliotech/config.rb', line 36 def initialize(valise) @valise = valise end |
Instance Attribute Details
#hash ⇒ Object
43 44 45 |
# File 'lib/bibliotech/config.rb', line 43 def hash @hash ||= stringify_keys(valise.exts(".yaml", ".yml").contents("config")) end |
#valise ⇒ Object (readonly)
Returns the value of attribute valise.
40 41 42 |
# File 'lib/bibliotech/config.rb', line 40 def valise @valise end |
Instance Method Details
#adapter ⇒ Object
279 280 281 |
# File 'lib/bibliotech/config.rb', line 279 def adapter db_get(:db_adapter) end |
#app_db_config ⇒ Object
231 232 233 234 235 236 237 238 239 240 |
# File 'lib/bibliotech/config.rb', line 231 def app_db_config @app_db_config ||= begin db_config = YAML::load(File::read(local_get(:database_config_file))) db_config.fetch(local_get(:database_config_env)) do require 'pp' raise KeyError, "No #{local_get(:database_config_env)} in #{db_config.pretty_inspect}" end end end |
#backup_file ⇒ Object
251 252 253 254 255 |
# File 'lib/bibliotech/config.rb', line 251 def backup_file local_get(:file) rescue MissingConfig ::File.join(backup_path, filename) end |
#backup_frequency ⇒ Object
223 224 225 |
# File 'lib/bibliotech/config.rb', line 223 def backup_frequency Periods.new(self).backup_frequency end |
#backup_name ⇒ Object
209 210 211 |
# File 'lib/bibliotech/config.rb', line 209 def backup_name local_get(:backup_name) end |
#backup_path ⇒ Object
261 262 263 |
# File 'lib/bibliotech/config.rb', line 261 def backup_path local_get(:backup_path) end |
#compressor ⇒ Object
273 274 275 |
# File 'lib/bibliotech/config.rb', line 273 def compressor local_get(:compressor) end |
#database ⇒ Object
295 296 297 |
# File 'lib/bibliotech/config.rb', line 295 def database db_get(:db_database) end |
#database_config ⇒ Object
227 228 229 |
# File 'lib/bibliotech/config.rb', line 227 def database_config local_get(:database_config) end |
#db_get(field) ⇒ Object
242 243 244 245 246 247 248 |
# File 'lib/bibliotech/config.rb', line 242 def db_get(field) local_get(field) rescue MissingConfig => mc app_db_config.fetch(steps_for(field).last) do raise mc end end |
#expander ⇒ Object
265 266 267 268 269 270 271 |
# File 'lib/bibliotech/config.rb', line 265 def if remote.nil? local_get(:compressor) else remote_get(remote, :compressor) end end |
#extract(*steps_chain) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/bibliotech/config.rb', line 85 def extract(*steps_chain) steps_chain.each do |steps| begin return steps.inject(hash) do |hash, step| raise MissingConfig if hash.nil? hash.fetch(step) end rescue KeyError end end raise MissingConfig, "No value configured at any of: #{steps_chain.map{|steps| steps.join(">")}.join(", ")}" end |
#filename ⇒ Object
257 258 259 |
# File 'lib/bibliotech/config.rb', line 257 def filename local_get(:filename) end |
#host ⇒ Object
283 284 285 |
# File 'lib/bibliotech/config.rb', line 283 def host db_get(:db_host) end |
#id_file(for_remote) ⇒ Object
159 160 161 162 163 164 165 166 167 168 |
# File 'lib/bibliotech/config.rb', line 159 def id_file(for_remote) steps = steps_for(:rsa_files) + [for_remote] steps_chain = begin [steps, [local] + steps] rescue MissingConfig [steps] end extract(steps_chain) end |
#local ⇒ Object
98 99 100 |
# File 'lib/bibliotech/config.rb', line 98 def local extract(["local"]) end |
#local_file(filename) ⇒ Object
176 177 178 |
# File 'lib/bibliotech/config.rb', line 176 def local_file(filename) File::join(local_path, filename) end |
#local_get(key) ⇒ Object
106 107 108 109 110 111 112 113 114 115 |
# File 'lib/bibliotech/config.rb', line 106 def local_get(key) steps = steps_for(key) steps_chain = begin [[local] + steps, steps] rescue MissingConfig => mc [steps] end extract(*steps_chain) end |
#local_path ⇒ Object
170 171 172 173 174 |
# File 'lib/bibliotech/config.rb', line 170 def local_path local_get(:fetch_dir) rescue MissingConfig local_get(:root_path) end |
#log_level ⇒ Object
140 141 142 143 144 145 146 |
# File 'lib/bibliotech/config.rb', line 140 def log_level level = "debug" optionally do level = local_get(:log_level) end return BiblioTech::Logging.log_level(level) end |
#log_target ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/bibliotech/config.rb', line 122 def log_target target_path = local_get(:log_target) case target_path when "STDERR", "stderr" return $stderr when "STDOUT", "stdout" return $stdout else require 'fileutils' FileUtils.mkdir_p(File.dirname(target_path)) return File.open(target_path, "a") end rescue warn "Trouble opening configured log file - logging to stderr" warn $!.inspect return $STDERR end |
#merge(other_hash) ⇒ Object
69 70 71 72 73 |
# File 'lib/bibliotech/config.rb', line 69 def merge(other_hash) self.class.new(valise).tap do |newbie| newbie.hash = merge_hashes(hash, stringify_keys(other_hash)) end end |
#merge_hashes(left, right) ⇒ Object
59 60 61 62 63 64 65 66 67 |
# File 'lib/bibliotech/config.rb', line 59 def merge_hashes(left, right) left.merge(right) do |key, ours, theirs| if ours.is_a?(Hash) and theirs.is_a?(Hash) merge_hashes(ours, theirs) else theirs end end end |
#optional(&block) ⇒ Object Also known as: optionally
79 80 81 82 |
# File 'lib/bibliotech/config.rb', line 79 def optional(&block) yield rescue MissingConfig end |
#password ⇒ Object
299 300 301 |
# File 'lib/bibliotech/config.rb', line 299 def password db_get(:db_password) end |
#port ⇒ Object
287 288 289 |
# File 'lib/bibliotech/config.rb', line 287 def port db_get(:db_port) end |
#prune_schedules ⇒ Object
214 215 216 217 218 219 220 221 |
# File 'lib/bibliotech/config.rb', line 214 def prune_schedules list = Periods.new(self).schedules + Calendars.new(self).schedules if list.empty? require 'pp' raise "No backups will be kept by prune schedule: #{prune_hash.pretty_inspect}" end list end |
#remote ⇒ Object
102 103 104 |
# File 'lib/bibliotech/config.rb', line 102 def remote extract(["remote"]) end |
#remote_file(remote, filename) ⇒ Object
205 206 207 |
# File 'lib/bibliotech/config.rb', line 205 def remote_file(remote, filename) File::join(remote_path(remote), filename) end |
#remote_get(remote_name, key) ⇒ Object
117 118 119 120 |
# File 'lib/bibliotech/config.rb', line 117 def remote_get(remote_name, key) steps = [remote_name] + steps_for(key) extract(steps, ["remotes"] + steps) end |
#remote_host(remote) ⇒ Object
184 185 186 |
# File 'lib/bibliotech/config.rb', line 184 def remote_host(remote) remote_get(remote, :host) end |
#remote_path(remote) ⇒ Object
196 197 198 199 200 201 202 203 |
# File 'lib/bibliotech/config.rb', line 196 def remote_path(remote) path = "#{remote_host(remote)}:#{root_dir_on(remote)}" begin "#{remote_user(remote)}@#{path}" rescue MissingConfig path end end |
#remote_port(remote) ⇒ Object
188 189 190 |
# File 'lib/bibliotech/config.rb', line 188 def remote_port(remote) remote_get(remote, :port) end |
#remote_user(remote) ⇒ Object
192 193 194 |
# File 'lib/bibliotech/config.rb', line 192 def remote_user(remote) remote_get(remote, :user) end |
#root_dir_on(remote) ⇒ Object
180 181 182 |
# File 'lib/bibliotech/config.rb', line 180 def root_dir_on(remote) remote_get(remote, :root_path) end |
#ssh_options(for_remote) ⇒ Object
148 149 150 151 152 153 154 155 156 157 |
# File 'lib/bibliotech/config.rb', line 148 def (for_remote) steps = steps_for(:ssh_options) + [for_remote] steps_chain = begin [steps, [local] + steps] rescue MissingConfig [steps] end extract(steps_chain) end |
#steps_for(key) ⇒ Object
75 76 77 |
# File 'lib/bibliotech/config.rb', line 75 def steps_for(key) CONFIG_STEPS.fetch(key) end |
#stringify_keys(hash) ⇒ Object
sym -> string
47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/bibliotech/config.rb', line 47 def stringify_keys(hash) # sym -> string hash.keys.each do |key| if key.is_a?(Symbol) hash[key.to_s] = hash.delete(key) end if hash[key.to_s].is_a?(Hash) hash[key.to_s] = stringify_keys(hash[key.to_s]) end end hash end |
#username ⇒ Object
291 292 293 |
# File 'lib/bibliotech/config.rb', line 291 def username db_get(:db_username) end |