Class: Config

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

Constant Summary collapse

CONFIG_LOCATIONS =
%w(.rops.yaml platform/rops.yaml config/rops.yaml).freeze
CONFIG_DEFAULTS =
{
  'repository' => nil,
  'default_branch' => 'master',
  'registry' => 'docker.io/r360',
  'default_context' => 'staging',
  'production_context' => 'production',
  'images' => [],
  'contexts' => {},
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#contextsObject (readonly)

Returns the value of attribute contexts.



19
20
21
# File 'lib/config.rb', line 19

def contexts
  @contexts
end

#default_branchObject (readonly)

Returns the value of attribute default_branch.



18
19
20
# File 'lib/config.rb', line 18

def default_branch
  @default_branch
end

#default_contextObject (readonly)

Returns the value of attribute default_context.



18
19
20
# File 'lib/config.rb', line 18

def default_context
  @default_context
end

#imagesObject (readonly)

Returns the value of attribute images.



19
20
21
# File 'lib/config.rb', line 19

def images
  @images
end

#production_contextObject (readonly)

Returns the value of attribute production_context.



18
19
20
# File 'lib/config.rb', line 18

def production_context
  @production_context
end

#registryObject (readonly)

Returns the value of attribute registry.



18
19
20
# File 'lib/config.rb', line 18

def registry
  @registry
end

#repositoryObject (readonly)

Returns the value of attribute repository.



18
19
20
# File 'lib/config.rb', line 18

def repository
  @repository
end

#ssh_hostObject (readonly)

Returns the value of attribute ssh_host.



18
19
20
# File 'lib/config.rb', line 18

def ssh_host
  @ssh_host
end

Instance Method Details

#load(root) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/config.rb', line 21

def load(root)
  conf_path = find(root)
  conf = conf_path ? YAML.load_file(conf_path) : {}
  conf = conf.reverse_merge(CONFIG_DEFAULTS)

  @repository, @registry, @default_branch, @default_context, @production_context, @ssh_host, contexts, images =
    conf.values_at('repository', 'registry', 'default_branch', 'default_context', 'production_context', 'ssh', 'contexts', 'images').map(&:presence)
  @repository ||= root

  images ||= [{ 'name' => File.basename(File.absolute_path(repository)) }]
  @images = images.map do |image|
    name = image['name']
    dockerfile = image['dockerfile'].presence || 'Dockerfile'
    Image.new(name: name, repository: repository, dockerfile: dockerfile, registry: registry, commit: nil, tag: nil)
  end

  @contexts = HashWithIndifferentAccess.new(contexts.compact)
  @contexts.each_value do |context|
    notify = context[:notify]
    next  unless notify

    # base64 decode Slack URLs
    if (url = notify[:url])
      notify_url = URI.parse(url)
      unless notify_url.is_a?(URI::HTTP)
        notify_url = URI.parse(Base64.decode64(url))
      end
      notify[:url] = notify_url
    end

    # parse liquid templates
    if (text = notify[:text])
      notify[:text] = Liquid::Template.parse(text)
    end
  end

  self
end