Class: Conjure::Docker::Template

Inherits:
Object
  • Object
show all
Defined in:
lib/conjure/docker/template.rb

Instance Method Summary collapse

Constructor Details

#initialize(base_image_name) ⇒ Template

Returns a new instance of Template.



7
8
9
10
# File 'lib/conjure/docker/template.rb', line 7

def initialize(base_image_name)
  @commands = ["FROM #{base_image_name}"]
  @file_data = {}
end

Instance Method Details

#add_file(filename, remote_name) ⇒ Object



12
13
14
# File 'lib/conjure/docker/template.rb', line 12

def add_file(filename, remote_name)
  add_file_data File.read(filename), remote_name
end

#add_file_data(data, remote_name) ⇒ Object



16
17
18
19
20
# File 'lib/conjure/docker/template.rb', line 16

def add_file_data(data, remote_name)
  local_name = "file#{@file_data.length+1}"
  @file_data[local_name] = data
  @commands << "ADD #{local_name} #{remote_name}"
end

#environment(hash) ⇒ Object



34
35
36
# File 'lib/conjure/docker/template.rb', line 34

def environment(hash)
  hash.each { |key, value| @commands << "ENV #{key} #{value}" }
end

#run(command) ⇒ Object



22
23
24
# File 'lib/conjure/docker/template.rb', line 22

def run(command)
  @commands << "RUN #{command}"
end

#sourceObject



30
31
32
# File 'lib/conjure/docker/template.rb', line 30

def source
  @commands.join "\n"
end

#start(container_host, command, options = {}) ⇒ Object



38
39
40
41
42
43
44
45
46
47
# File 'lib/conjure/docker/template.rb', line 38

def start(container_host, command, options = {})
  if container_names(options).all? { |name| container_host.running? name }
    puts "Detected all #{options[:name]} containers running."
  else
    puts "Building #{options[:name]} base image..."
    image_name = container_host.build(image_source_files)
    options = options.merge(volume_options(container_host, image_name, options)) if options[:volumes]
    container_host.start(image_name, command, options)
  end
end

#volume(name) ⇒ Object



26
27
28
# File 'lib/conjure/docker/template.rb', line 26

def volume(name)
  @commands << "VOLUME #{name}"
end