Class: Indocker::BuildContext

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration:, logger:, global_logger:) ⇒ BuildContext

Returns a new instance of BuildContext.



6
7
8
9
10
11
# File 'lib/indocker/build_context.rb', line 6

def initialize(configuration:, logger:, global_logger:)
  @configuration = configuration
  @logger = logger
  @helper = Indocker::BuildContextHelper.new(@configuration, @build_server)
  @global_logger = global_logger
end

Instance Attribute Details

#configurationObject (readonly)

Returns the value of attribute configuration.



4
5
6
# File 'lib/indocker/build_context.rb', line 4

def configuration
  @configuration
end

#global_loggerObject (readonly)

Returns the value of attribute global_logger.



4
5
6
# File 'lib/indocker/build_context.rb', line 4

def global_logger
  @global_logger
end

#helperObject (readonly)

Returns the value of attribute helper.



4
5
6
# File 'lib/indocker/build_context.rb', line 4

def helper
  @helper
end

#loggerObject (readonly)

Returns the value of attribute logger.



4
5
6
# File 'lib/indocker/build_context.rb', line 4

def logger
  @logger
end

Instance Method Details

#build_image(image, build_dir, args: []) ⇒ Object



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
# File 'lib/indocker/build_context.rb', line 13

def build_image(image, build_dir, args: [])
  image_name = image.image_name
  registry   = image.registry
  tag        = image.tag

  FileUtils.cd(build_dir) do
    if @logger.debug?
      @logger.debug("#{"Docker image content:".yellow}")

      Dir[File.join(build_dir, '*')]
        .map {|path|
          file = path.gsub(build_dir, '')
          @logger.debug("  .#{file}".yellow)
        }
        .join("\n")
    end

    if !@logger.debug?
      args = args.push('-q')
    end

    build_args = args.join(' ')

    res = Indocker::Docker.build(image.local_registry_url, build_args)
    
    if res.exit_status != 0
      @global_logger.error("image compilation :#{image.name} failed")
      @global_logger.error(res.stdout)
      exit 1
    end

    Indocker::Docker.tag(image.local_registry_url, image.registry_url)
    if image.registry_url != image.local_registry_url
      Indocker::Docker.tag(image.local_registry_url, image.local_registry_url)
    end

    if !image.registry.is_local?
      Indocker::Docker.push(image.registry_url)
    end
  end
end