Class: YleTf::Config::Migration

Inherits:
Object
  • Object
show all
Includes:
Helpers::Hash
Defined in:
lib/yle_tf/config/migration.rb

Constant Summary collapse

BACKEND_MIGRATIONS =
{
  'file'  => {
    'file' => 'path'
  },
  's3'    => {
    'region'  => 'region',
    'bucket'  => 'bucket',
    'file'    => 'key',
    'encrypt' => 'encrypt'
  },
  'swift' => {
    'region'            => 'region_name',
    'container'         => 'container',
    'archive_container' => 'archive_container'
  }
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers::Hash

deep_copy, deep_merge

Constructor Details

#initialize(config, **opts) ⇒ Migration

Returns a new instance of Migration.



35
36
37
38
# File 'lib/yle_tf/config/migration.rb', line 35

def initialize(config, **opts)
  @config = config
  @config_source = opts.fetch(:source)
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



33
34
35
# File 'lib/yle_tf/config/migration.rb', line 33

def config
  @config
end

#config_sourceObject (readonly)

Returns the value of attribute config_source.



33
34
35
# File 'lib/yle_tf/config/migration.rb', line 33

def config_source
  @config_source
end

Class Method Details

.migrate_old_config(config, **opts) ⇒ Object



29
30
31
# File 'lib/yle_tf/config/migration.rb', line 29

def self.migrate_old_config(config, **opts)
  new(config, **opts).migrated_config
end

Instance Method Details

#copy_with_defaults(config, defaults) ⇒ Object



104
105
106
# File 'lib/yle_tf/config/migration.rb', line 104

def copy_with_defaults(config, defaults)
  deep_merge(deep_copy(config), defaults)
end

#deprecation_warningObject

Returns a ‘Proc` to print deprecation warnings unless denied by an env var



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/yle_tf/config/migration.rb', line 49

def deprecation_warning
  return nil if ENV['TF_OLD_CONFIG_WARNINGS'] == 'false'

  lambda do |new_config|
    Logger.warn("Old configuration found in #{config_source}")
    Logger.warn("Please migrate to relevant parts of:\n" \
                "#{sanitize_config(new_config)}")
    Logger.warn(
      'See https://github.com/Yleisradio/yle_tf/wiki/Migrating-Configuration for more details'
    )
  end
end

#find_old_backend_config_keys(keys) ⇒ Object



93
94
95
96
97
98
99
100
101
102
# File 'lib/yle_tf/config/migration.rb', line 93

def find_old_backend_config_keys(keys)
  return {} if !old_backend_config.is_a?(Hash)

  keys.select do |old_key, _new_key|
    old_backend_config.key?(old_key) &&
      # Special case for 'file' as it is now used for option Hash for the
      # 'file' backend
      !(old_key == 'file' && old_backend_config['file'].is_a?(Hash))
  end
end

#migrate_old_backend_config {|new_config| ... } ⇒ Object

TODO: Remove support in v2.0

Yields:

  • (new_config)


63
64
65
66
67
68
69
70
71
72
73
# File 'lib/yle_tf/config/migration.rb', line 63

def migrate_old_backend_config
  changed = false

  new_config = BACKEND_MIGRATIONS.inject(config) do |prev_config, (type, keys)|
    migrate_old_backend_config_keys(prev_config, type, keys) { changed = true }
  end

  yield(new_config) if changed && block_given?

  new_config
end

#migrate_old_backend_config_keys(config, type, keys) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/yle_tf/config/migration.rb', line 75

def migrate_old_backend_config_keys(config, type, keys)
  migrated_keys = find_old_backend_config_keys(keys)
  return config if migrated_keys.empty?

  defaults = {
    'backend' => {
      type => {}
    }
  }
  copy_with_defaults(config, defaults).tap do |new_config|
    migrated_keys.each do |old_key, new_key|
      new_config['backend'][type][new_key] = old_backend_config[old_key]
    end

    yield new_config
  end
end

#migrated_configObject



44
45
46
# File 'lib/yle_tf/config/migration.rb', line 44

def migrated_config
  migrate_old_backend_config(&deprecation_warning)
end

#old_backend_configObject



40
41
42
# File 'lib/yle_tf/config/migration.rb', line 40

def old_backend_config
  config['backend']
end

#sanitize_config(config) ⇒ Object



108
109
110
111
112
113
114
# File 'lib/yle_tf/config/migration.rb', line 108

def sanitize_config(config)
  backend_config = config['backend'].select do |key, value|
    key == 'type' || value.is_a?(Hash)
  end

  YAML.dump('backend' => backend_config)
end