Class: Pups::Config

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, ignored = nil, tags: nil, skip_tags: nil, extra_params: nil) ⇒ Config

Returns a new instance of Config.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
# File 'lib/pups/config.rb', line 7

def initialize(
  config,
  ignored = nil,
  tags: nil,
  skip_tags: nil,
  extra_params: nil
)
  @config = config

  # remove any ignored config elements prior to any more processing
  ignored&.each { |e| @config.delete(e) }

  filter_tags(include_tags: tags, exclude_tags: skip_tags)

  # set some defaults to prevent checks in various functions
  %w[env_template env labels params].each do |key|
    @config[key] = {} unless @config.has_key?(key)
  end

  # Order here is important.
  Pups::Config.combine_template_and_process_env(@config, ENV)
  Pups::Config.prepare_env_template_vars(@config["env_template"], ENV)

  # Templating is supported in env and label variables.
  Pups::Config.transform_config_with_templated_vars(
    @config["env_template"],
    ENV
  )
  Pups::Config.transform_config_with_templated_vars(
    @config["env_template"],
    @config["env"]
  )
  Pups::Config.transform_config_with_templated_vars(
    @config["env_template"],
    @config["labels"]
  )

  @params = @config["params"]
  if extra_params
    extra_params.each do |val|
      key_val = val.split("=", 2)
      if key_val.length == 2
        @params[key_val[0]] = key_val[1]
      else
        warn "Malformed param #{val}. Expected param to be of the form `key=value`"
      end
    end
  end
  ENV.each { |k, v| @params["$ENV_#{k}"] = v }
  inject_hooks
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



5
6
7
# File 'lib/pups/config.rb', line 5

def config
  @config
end

#paramsObject (readonly)

Returns the value of attribute params.



5
6
7
# File 'lib/pups/config.rb', line 5

def params
  @params
end

Class Method Details

.combine_template_and_process_env(config, env) ⇒ Object



118
119
120
121
122
# File 'lib/pups/config.rb', line 118

def self.combine_template_and_process_env(config, env)
  # Merge all template env variables and process env variables, so that env
  # variables can be provided both by configuration and runtime variables.
  config["env"].each { |k, v| env[k] = v.to_s }
end

.interpolate_params(cmd, params) ⇒ Object



248
249
250
251
252
253
254
# File 'lib/pups/config.rb', line 248

def self.interpolate_params(cmd, params)
  return unless cmd

  processed = cmd.dup
  params.each { |k, v| processed.gsub!("$#{k}", v.to_s) }
  processed
end

.load_config(config, ignored = nil, tags: nil, skip_tags: nil, extra_params: nil) ⇒ Object



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

def self.load_config(
  config,
  ignored = nil,
  tags: nil,
  skip_tags: nil,
  extra_params: nil
)
  Config.new(
    YAML.safe_load(config),
    ignored,
    tags: tags,
    skip_tags: skip_tags,
    extra_params: extra_params
  )
end

.load_file(config_file, ignored = nil, tags: nil, skip_tags: nil, extra_params: nil) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/pups/config.rb', line 59

def self.load_file(
  config_file,
  ignored = nil,
  tags: nil,
  skip_tags: nil,
  extra_params: nil
)
  Config.new(
    YAML.load_file(config_file),
    ignored,
    tags: tags,
    skip_tags: skip_tags,
    extra_params: extra_params
  )
rescue Exception
  warn "Failed to parse #{config_file}"
  warn "This is probably a formatting error in #{config_file}"
  warn "Cannot continue. Edit #{config_file} and try again."
  raise
end

.prepare_env_template_vars(env_template, env) ⇒ Object



96
97
98
99
100
101
102
103
104
# File 'lib/pups/config.rb', line 96

def self.prepare_env_template_vars(env_template, env)
  # Merge env_template variables from env and templates.
  env.each do |k, v|
    if k.include?("env_template_")
      key = k.gsub("env_template_", "")
      env_template[key] = v.to_s
    end
  end
end

.transform_config_with_templated_vars(env_template, to_transform) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
# File 'lib/pups/config.rb', line 106

def self.transform_config_with_templated_vars(env_template, to_transform)
  # Transform any templated variables prior to copying to params.
  # This has no effect if no env_template was provided.
  env_template.each do |k, v|
    to_transform.each do |key, val|
      if val.to_s.include?("{{#{k}}}")
        to_transform[key] = val.gsub("{{#{k}}}", v.to_s)
      end
    end
  end
end

Instance Method Details

#filter_tags(include_tags: nil, exclude_tags: nil) ⇒ Object

Filter run commands by tag: by default, keep all commands that contain tags. If skip_tags argument is true, keep all commands that DO NOT contain tags.



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/pups/config.rb', line 126

def filter_tags(include_tags: nil, exclude_tags: nil)
  if include_tags
    @config["run"] = @config["run"].select do |row|
      keep = false
      command = row.first
      if command[1].is_a?(Hash)
        tag = command[1]["tag"]
        keep = include_tags.include?(tag)
      end
      keep
    end
  end

  if exclude_tags
    @config["run"] = @config["run"].select do |row|
      keep = true
      command = row.first
      if command[1].is_a?(Hash)
        tag = command[1]["tag"]
        keep = !exclude_tags.include?(tag)
      end
      keep
    end
  end
end

#generate_docker_run_argumentsObject



192
193
194
195
196
197
198
199
200
# File 'lib/pups/config.rb', line 192

def generate_docker_run_arguments
  output = []
  output << Pups::Docker.generate_env_arguments(config["env"])
  output << Pups::Docker.generate_link_arguments(config["links"])
  output << Pups::Docker.generate_expose_arguments(config["expose"])
  output << Pups::Docker.generate_volume_arguments(config["volumes"])
  output << Pups::Docker.generate_label_arguments(config["labels"])
  output.sort!.join(" ").strip
end

#inject_hooksObject



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/pups/config.rb', line 152

def inject_hooks
  return unless hooks = @config["hooks"]

  run = @config["run"]

  positions = {}
  run.each do |row|
    next unless row.is_a?(Hash)

    command = row.first
    if command[1].is_a?(Hash)
      hook = command[1]["hook"]
      positions[hook] = row if hook
    end
  end

  hooks.each do |full, list|
    offset = nil
    name = nil

    if full =~ /^after_/
      name = full[6..-1]
      offset = 1
    end

    if full =~ /^before_/
      name = full[7..-1]
      offset = 0
    end

    index = run.index(positions[name])

    if index && index >= 0
      run.insert(index + offset, *list)
    else
      Pups.log.info "Skipped missing #{full} hook"
    end
  end
end

#interpolate_params(cmd) ⇒ Object



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

def interpolate_params(cmd)
  self.class.interpolate_params(cmd, @params)
end

#runObject



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/pups/config.rb', line 202

def run
  run_commands
rescue StandardError => e
  exit_code = 1
  exit_code = e.exit_code if e.is_a?(Pups::ExecError)
  unless exit_code == 77
    puts
    puts
    puts "FAILED"
    puts "-" * 20
    puts "#{e.class}: #{e}"
    puts "Location of failure: #{e.backtrace[0]}"
    if @last_command
      puts "#{@last_command[:command]} failed with the params #{@last_command[:params].inspect}"
    end
  end
  exit exit_code
end

#run_commandsObject



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/pups/config.rb', line 221

def run_commands
  @config["run"]&.each do |item|
    item.each do |k, v|
      type =
        case k
        when "exec"
          Pups::ExecCommand
        when "merge"
          Pups::MergeCommand
        when "replace"
          Pups::ReplaceCommand
        when "file"
          Pups::FileCommand
        else
          raise SyntaxError, "Invalid run command #{k}"
        end

      @last_command = { command: k, params: v }
      type.run(v, @params)
    end
  end
end