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:, build_server:, logger:) ⇒ BuildContext

Returns a new instance of BuildContext.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/indocker/build_context.rb', line 6

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

  if build_server
    @session = Indocker::SshSession.new(
      host: build_server.host,
      user: build_server.user,
      port: build_server.port,
      logger: @logger
    )
  end

  @compiled_images = Hash.new(false)
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

#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

#serverObject (readonly)

Returns the value of attribute server.



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

def server
  @server
end

#sessionObject (readonly)

Returns the value of attribute session.



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

def session
  @session
end

Instance Method Details

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



48
49
50
51
52
53
54
55
56
57
58
59
60
61
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
# File 'lib/indocker/build_context.rb', line 48

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
      @logger.error("image compilation :#{image.name} failed")
      @logger.error(res.stdout)
      exit 1
    end

    Indocker::Docker.tag(image.local_registry_url, image.registry_url)
    Indocker::Docker.tag(image.local_registry_url, image.local_registry_url)

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

#busy?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/indocker/build_context.rb', line 40

def busy?
  !!@busy
end

#close_sessionObject



28
29
30
# File 'lib/indocker/build_context.rb', line 28

def close_session
  @session.close if @session
end

#exec!(command) ⇒ Object



24
25
26
# File 'lib/indocker/build_context.rb', line 24

def exec!(command)
  @session.exec!(command)
end

#image_compiled?(image) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/indocker/build_context.rb', line 32

def image_compiled?(image)
  @compiled_images[image]
end

#set_busy(flag) ⇒ Object



36
37
38
# File 'lib/indocker/build_context.rb', line 36

def set_busy(flag)
  @busy = !!flag
end

#set_compiled(image) ⇒ Object



44
45
46
# File 'lib/indocker/build_context.rb', line 44

def set_compiled(image)
  @compiled_images[image] = true
end