Class: BiblioTech::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/bibliotech/config.rb

Defined Under Namespace

Classes: MissingConfig

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"  ] ,
  :prune_schedule       => [ "backups"                , "keep"      ] ,
  :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"  ] ,
}
SCHEDULE_SHORTHANDS =
{
  "hourly"      => 60,
  "hourlies"    => 60,
  "daily"       => 60 * 24,
  "dailies"     => 60 * 24,
  "weekly"      => 60 * 24 * 7,
  "weeklies"    => 60 * 24 * 7,
  "monthly"     => 60 * 24 * 30,
  "monthlies"   => 60 * 24 * 30,
  "quarterly"   => 60 * 24 * 120,
  "quarterlies" => 60 * 24 * 120,
  "yearly"      => 60 * 24 * 365,
  "yearlies"    => 60 * 24 * 365,
}

Instance Attribute Summary collapse

File management collapse

Database collapse

Instance Method Summary collapse

Constructor Details

#initialize(valise) ⇒ Config

Returns a new instance of Config.



34
35
36
# File 'lib/bibliotech/config.rb', line 34

def initialize(valise)
  @valise = valise
end

Instance Attribute Details

#hashObject



41
42
43
# File 'lib/bibliotech/config.rb', line 41

def hash
  @hash ||= stringify_keys(valise.contents("config.yaml"))
end

#valiseObject (readonly)

Returns the value of attribute valise.



38
39
40
# File 'lib/bibliotech/config.rb', line 38

def valise
  @valise
end

Instance Method Details

#adapterObject



296
297
298
299
# File 'lib/bibliotech/config.rb', line 296

def adapter
  database_config
  local_get(:db_adapter)
end

#backup_fileObject



268
269
270
271
272
# File 'lib/bibliotech/config.rb', line 268

def backup_file
  local_get(:file)
rescue MissingConfig
  ::File.join(backup_path, filename)
end

#backup_frequencyObject



231
232
233
# File 'lib/bibliotech/config.rb', line 231

def backup_frequency
  @backup_frequency ||= regularize_frequency(local_get(:backup_frequency))
end

#backup_nameObject



227
228
229
# File 'lib/bibliotech/config.rb', line 227

def backup_name
  local_get(:backup_name)
end

#backup_pathObject



278
279
280
# File 'lib/bibliotech/config.rb', line 278

def backup_path
  local_get(:backup_path)
end

#compressorObject



290
291
292
# File 'lib/bibliotech/config.rb', line 290

def compressor
  local_get(:compressor)
end

#databaseObject



316
317
318
319
# File 'lib/bibliotech/config.rb', line 316

def database
  database_config
  local_get(:db_database)
end

#database_configObject



256
257
258
259
260
261
262
263
264
265
# File 'lib/bibliotech/config.rb', line 256

def database_config
  hash["database_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

#expanderObject



282
283
284
285
286
287
288
# File 'lib/bibliotech/config.rb', line 282

def expander
  if remote.nil?
    local_get(:compressor)
  else
    remote_get(remote, :compressor)
  end
end

#extract(*steps_chain) ⇒ Object

Raises:



83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/bibliotech/config.rb', line 83

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

#filenameObject



274
275
276
# File 'lib/bibliotech/config.rb', line 274

def filename
  local_get(:filename)
end

#hostObject



301
302
303
304
# File 'lib/bibliotech/config.rb', line 301

def host
  database_config
  local_get(:db_host)
end

#id_file(for_remote) ⇒ Object



157
158
159
160
161
162
163
164
165
166
# File 'lib/bibliotech/config.rb', line 157

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

#localObject



96
97
98
# File 'lib/bibliotech/config.rb', line 96

def local
  extract(["local"])
end

#local_file(filename) ⇒ Object



174
175
176
# File 'lib/bibliotech/config.rb', line 174

def local_file(filename)
  File::join(local_path, filename)
end

#local_get(key) ⇒ Object



104
105
106
107
108
109
110
111
112
113
# File 'lib/bibliotech/config.rb', line 104

def local_get(key)
  steps = steps_for(key)
  steps_chain =
    begin
      [steps, [local] + steps]
    rescue MissingConfig
      [steps]
    end
  extract(*steps_chain)
end

#local_pathObject



168
169
170
171
172
# File 'lib/bibliotech/config.rb', line 168

def local_path
  local_get(:fetch_dir)
rescue MissingConfig
  local_get(:root_path)
end

#log_levelObject



138
139
140
141
142
143
144
# File 'lib/bibliotech/config.rb', line 138

def log_level
  level = "debug"
  optionally do
    level = local_get(:log_level)
  end
  return BiblioTech::Logging.log_level(level)
end

#log_targetObject



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/bibliotech/config.rb', line 120

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



67
68
69
70
71
# File 'lib/bibliotech/config.rb', line 67

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



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

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



77
78
79
80
# File 'lib/bibliotech/config.rb', line 77

def optional(&block)
  yield
rescue MissingConfig
end

#passwordObject



321
322
323
324
# File 'lib/bibliotech/config.rb', line 321

def password
  database_config
  local_get(:db_password)
end

#portObject



306
307
308
309
# File 'lib/bibliotech/config.rb', line 306

def port
  database_config
  local_get(:db_port)
end

#prune_schedulesObject



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/bibliotech/config.rb', line 235

def prune_schedules
  local_get(:prune_schedule).map do |frequency, limit|
    real_frequency = regularize_frequency(frequency)
    unless real_frequency % backup_frequency == 0
      raise "Pruning frequency #{real_frequency}:#{frequency} is not a multiple of backup frequency: #{backup_frequency}:#{local_get(:backup_frequency)}"
    end
    limit =
      case limit
      when "all"
        nil
      else
        Integer(limit)
      end
    [frequency, real_frequency, limit]
  end.sort_by do |freq_name, frequency, limit|
    frequency
  end.map do |freq_name, frequency, limit|
    Backups::Scheduler.new(freq_name, frequency, limit)
  end
end

#regularize_frequency(frequency) ⇒ Object



221
222
223
224
225
# File 'lib/bibliotech/config.rb', line 221

def regularize_frequency(frequency)
  Integer( SCHEDULE_SHORTHANDS.fetch(frequency){ frequency } )
rescue ArgumentError
  raise "#{frequency.inspect} is neither a number of minutes or a shorthand. Try:\n  #{SCHEDULE_SHORTHANDS.keys.join(" ")}"
end

#remoteObject



100
101
102
# File 'lib/bibliotech/config.rb', line 100

def remote
  extract(["remote"])
end

#remote_file(remote, filename) ⇒ Object



203
204
205
# File 'lib/bibliotech/config.rb', line 203

def remote_file(remote, filename)
  File::join(remote_path(remote), filename)
end

#remote_get(remote_name, key) ⇒ Object



115
116
117
118
# File 'lib/bibliotech/config.rb', line 115

def remote_get(remote_name, key)
  steps = [remote_name] + steps_for(key)
  extract(steps, ["remotes"] + steps)
end

#remote_host(remote) ⇒ Object



182
183
184
# File 'lib/bibliotech/config.rb', line 182

def remote_host(remote)
  remote_get(remote, :host)
end

#remote_path(remote) ⇒ Object



194
195
196
197
198
199
200
201
# File 'lib/bibliotech/config.rb', line 194

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



186
187
188
# File 'lib/bibliotech/config.rb', line 186

def remote_port(remote)
  remote_get(remote, :port)
end

#remote_user(remote) ⇒ Object



190
191
192
# File 'lib/bibliotech/config.rb', line 190

def remote_user(remote)
  remote_get(remote, :user)
end

#root_dir_on(remote) ⇒ Object



178
179
180
# File 'lib/bibliotech/config.rb', line 178

def root_dir_on(remote)
  remote_get(remote, :root_path)
end

#ssh_options(for_remote) ⇒ Object



146
147
148
149
150
151
152
153
154
155
# File 'lib/bibliotech/config.rb', line 146

def ssh_options(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



73
74
75
# File 'lib/bibliotech/config.rb', line 73

def steps_for(key)
  CONFIG_STEPS.fetch(key)
end

#stringify_keys(hash) ⇒ Object

sym -> string



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/bibliotech/config.rb', line 45

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

#usernameObject



311
312
313
314
# File 'lib/bibliotech/config.rb', line 311

def username
  database_config
  local_get(:db_username)
end