Module: Docker::Compose::Future::Session

Included in:
Session
Defined in:
lib/docker/compose/future/session.rb

Constant Summary collapse

SUBSTITUTION =

Pattern that matches an environment substitution in a docker-compose YML file.

See Also:

  • #substitute
/\$\{([A-Z0-9:_-]+)\}/

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(host) ⇒ Object

Hook in env-var substitution by aliasing a method chain for run!



11
12
13
14
15
16
17
# File 'lib/docker/compose/future/session.rb', line 11

def self.included(host)
  done = host.instance_methods.include?(:run_without_substitution!)
  host.instance_eval do
    alias_method :run_without_substitution!, :run!
    alias_method :run!, :run_with_substitution!
  end unless done
end

Instance Method Details

#run_with_substitution!(*cmd) ⇒ Object

Read docker-compose YML; perform environment variable substitution; write a temp file; invoke run! with the new file; delete the temp file afterward.

This is a complete reimplementation of run! and we only alias the original to be good citizens.



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
59
# File 'lib/docker/compose/future/session.rb', line 25

def run_with_substitution!(*cmd)
  temp = nil
  project = File.basename(@dir)

  # Find and purge the 'file' flag if it exists; otherwise assume we will
  # substitute our default (session) file.
  fn = nil
  cmd.each do |item|
    fn ||= item.delete(:file) if item.is_a?(Hash)
  end
  fn ||= @file

  # Rewrite YML if the file exists and the file:false "flag" wasn't
  # explicitly passed to us.
  Dir.chdir(@dir) do
    yml = YAML.load(File.read(fn))
    yml = substitute(yml)
    temp = Tempfile.new(fn, @dir)
    temp.write(YAML.dump(yml))
    temp.close

    project_opts = {
      file: temp.path,
      project: File.basename(@dir)
    }

    result, output, error =
      @shell.command('docker-compose', project_opts, *cmd)
    (result == 0) ||
      raise(Docker::Compose::Error.new(cmd.first, result, error))
    output
  end
ensure
  temp.unlink if temp
end