Class: Gitlab::QA::Runtime::OmnibusConfiguration

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/qa/runtime/omnibus_configuration.rb

Constant Summary collapse

ERB_PATTERN =
/<%=?\s?(\S+)\s?%>/

Instance Method Summary collapse

Constructor Details

#initialize(prefixed_config = nil) ⇒ OmnibusConfiguration

Returns a new instance of OmnibusConfiguration.

Parameters:

  • prefixed_config (defaults to: nil)

    The configuration to be prefixed to the new configuration



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/gitlab/qa/runtime/omnibus_configuration.rb', line 13

def initialize(prefixed_config = nil)
  @config = ["# Generated by GitLab QA Omnibus Configurator at #{Time.now.strftime('%Y-%m-%d %H:%M:%S')}"]

  return unless prefixed_config

  # remove generation statement if it exists within the prefixed configuration that was passed
  # and insert the rest AFTER the very first generation statement
  config_to_insert = prefixed_config.to_s
  generation_regexp = /# Generated by GitLab QA Omnibus Configurator.*\n/
  config_to_insert = config_to_insert.gsub(generation_regexp, '') if config_to_insert.match?(generation_regexp)
  @config.insert(1, config_to_insert)
end

Instance Method Details

#<<(config) ⇒ Object

rubocop:enable Metrics/AbcSize



98
99
100
# File 'lib/gitlab/qa/runtime/omnibus_configuration.rb', line 98

def <<(config)
  @config << config.strip unless config.strip.empty?
end

#configurationObject

Raises:

  • (NotImplementedError)


45
46
47
# File 'lib/gitlab/qa/runtime/omnibus_configuration.rb', line 45

def configuration
  raise NotImplementedError
end

#exec_commandsObject

Commands to execute before tests are run against GitLab (after reconfigure)



55
56
57
# File 'lib/gitlab/qa/runtime/omnibus_configuration.rb', line 55

def exec_commands
  []
end

#expand_config_template(gitlab) ⇒ Array

Execute the ERB code to produce completed template

Examples:

external_url '<%= gitlab.address %>' #=> external_url 'http://gitlab-ee-09d62235.test'

Parameters:

  • gitlab (GitLab::QA::Component::Gitlab)

Returns:

  • (Array)


39
40
41
42
43
# File 'lib/gitlab/qa/runtime/omnibus_configuration.rb', line 39

def expand_config_template(gitlab)
  @config.map! do |item|
    item.match(ERB_PATTERN) ? ERB.new(item).result(binding) : item
  end
end

#prepareObject

Before hook for any additional configuration This would usually be a container that needs to be running

Returns:

  • Any instance of [Gitlab::QA::Component::Base]



52
# File 'lib/gitlab/qa/runtime/omnibus_configuration.rb', line 52

def prepare; end

#sanitize!Object

Ensures no duplicate entries and sanitizes configurations rubocop:disable Metrics/AbcSize

Raises:

  • RuntimeError if competing configurations exist



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/gitlab/qa/runtime/omnibus_configuration.rb', line 62

def sanitize!
  sanitized = @config.map do |config|
    next config if config.start_with?('#') || config.match(/\w+\(/) # allow for comments and method invocations
    next config if config.match(ERB_PATTERN)

    # sometimes "=" is part of a Hash. Only split based on the first "="
    k, v = config.split("=", 2)
    # make sure each config is well-formed
    # e.g., gitlab_rails['packages_enabled'] = true
    #   NOT gitlab_rails['packages_enabled']=true

    v.nil? ? k.strip : "#{k.strip} = #{v.strip.tr('"', "'")}".strip
  end

  sanitized = split_items(sanitized).uniq

  sanitized = merge_arrays(sanitized)

  # check for duplicates
  duplicate_keys = []
  duplicates = sanitized.reject do |n|
    key = n.split('=').first

    duplicate_keys << key unless duplicate_keys.include?(key)
  end

  errors = []
  duplicates.each { |duplicate| errors << "Duplicate entry found: `#{duplicate}`" }

  raise "Errors exist within the Omnibus Configuration!\n#{errors.join(',')}" if errors.any?

  @config = sanitized
end

#to_sObject



26
27
28
# File 'lib/gitlab/qa/runtime/omnibus_configuration.rb', line 26

def to_s
  sanitize!.join("\n")
end