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" ]          ,
  :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.



30
31
32
# File 'lib/bibliotech/config.rb', line 30

def initialize(valise)
  @valise = valise
end

Instance Attribute Details

#hashObject



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

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

#valiseObject (readonly)

Returns the value of attribute valise.



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

def valise
  @valise
end

Instance Method Details

#adapterObject



266
267
268
269
# File 'lib/bibliotech/config.rb', line 266

def adapter
  database_config
  local_get(:db_adapter)
end

#backup_fileObject



238
239
240
241
242
# File 'lib/bibliotech/config.rb', line 238

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

#backup_frequencyObject



201
202
203
# File 'lib/bibliotech/config.rb', line 201

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

#backup_nameObject



197
198
199
# File 'lib/bibliotech/config.rb', line 197

def backup_name
  local_get(:backup_name)
end

#backup_pathObject



248
249
250
# File 'lib/bibliotech/config.rb', line 248

def backup_path
  local_get(:backup_path)
end

#compressorObject



260
261
262
# File 'lib/bibliotech/config.rb', line 260

def compressor
  local_get(:compressor)
end

#databaseObject



286
287
288
289
# File 'lib/bibliotech/config.rb', line 286

def database
  database_config
  local_get(:db_database)
end

#database_configObject



226
227
228
229
230
231
232
233
234
235
# File 'lib/bibliotech/config.rb', line 226

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

#each_prune_scheduleObject



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/bibliotech/config.rb', line 205

def each_prune_schedule
  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
    [real_frequency, limit]
  end.sort_by do |frequency, limit|
    frequency
  end.each do |frequency, limit|
    yield(frequency, limit)
  end
end

#expanderObject



252
253
254
255
256
257
258
# File 'lib/bibliotech/config.rb', line 252

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

#extract(*steps_chain) ⇒ Object

Raises:



79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/bibliotech/config.rb', line 79

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



244
245
246
# File 'lib/bibliotech/config.rb', line 244

def filename
  local_get(:filename)
end

#hostObject



271
272
273
274
# File 'lib/bibliotech/config.rb', line 271

def host
  database_config
  local_get(:db_host)
end

#id_file(for_remote) ⇒ Object



127
128
129
130
131
132
133
134
135
136
# File 'lib/bibliotech/config.rb', line 127

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



92
93
94
# File 'lib/bibliotech/config.rb', line 92

def local
  extract(["local"])
end

#local_file(filename) ⇒ Object



144
145
146
# File 'lib/bibliotech/config.rb', line 144

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

#local_get(key) ⇒ Object



100
101
102
103
104
105
106
107
108
109
# File 'lib/bibliotech/config.rb', line 100

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

#local_pathObject



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

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

#merge(other_hash) ⇒ Object



63
64
65
66
67
# File 'lib/bibliotech/config.rb', line 63

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



53
54
55
56
57
58
59
60
61
# File 'lib/bibliotech/config.rb', line 53

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



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

def optional(&block)
  yield
rescue MissingConfig
end

#passwordObject



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

def password
  database_config
  local_get(:db_password)
end

#portObject



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

def port
  database_config
  local_get(:db_port)
end

#regularize_frequency(frequency) ⇒ Object



191
192
193
194
195
# File 'lib/bibliotech/config.rb', line 191

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



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

def remote
  extract(["remote"])
end

#remote_file(remote, filename) ⇒ Object



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

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

#remote_get(remote_name, key) ⇒ Object



111
112
113
114
# File 'lib/bibliotech/config.rb', line 111

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

#remote_host(remote) ⇒ Object



152
153
154
# File 'lib/bibliotech/config.rb', line 152

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

#remote_path(remote) ⇒ Object



164
165
166
167
168
169
170
171
# File 'lib/bibliotech/config.rb', line 164

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



156
157
158
# File 'lib/bibliotech/config.rb', line 156

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

#remote_user(remote) ⇒ Object



160
161
162
# File 'lib/bibliotech/config.rb', line 160

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

#root_dir_on(remote) ⇒ Object



148
149
150
# File 'lib/bibliotech/config.rb', line 148

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

#ssh_options(for_remote) ⇒ Object



116
117
118
119
120
121
122
123
124
125
# File 'lib/bibliotech/config.rb', line 116

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



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

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

#stringify_keys(hash) ⇒ Object

sym -> string



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/bibliotech/config.rb', line 41

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



281
282
283
284
# File 'lib/bibliotech/config.rb', line 281

def username
  database_config
  local_get(:db_username)
end