Class: Mildred::Container

Inherits:
Object
  • Object
show all
Defined in:
lib/mildred/container.rb

Defined Under Namespace

Classes: Error

Constant Summary collapse

IMAGE =
"mildred-sandbox:latest"
CONTAINERFILE =
<<~DOCKERFILE
  FROM alpine:3.20
  RUN apk add --no-cache bash coreutils findutils
DOCKERFILE

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(job:) ⇒ Container

Returns a new instance of Container.



19
20
21
22
# File 'lib/mildred/container.rb', line 19

def initialize(job:)
  @job = job
  @id = nil
end

Instance Attribute Details

#id ⇒ Object (readonly)

Returns the value of attribute id.



17
18
19
# File 'lib/mildred/container.rb', line 17

def id
  @id
end

Class Method Details

.cleanup_stale ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/mildred/container.rb', line 43

def self.cleanup_stale
  ids = stale_container_ids
  return if ids.empty?

  ids.each do |id|
    Open3.capture3("container", "stop", id)
    Open3.capture3("container", "rm", id)
  end
end

.ensure_image ⇒ Object



37
38
39
40
41
# File 'lib/mildred/container.rb', line 37

def self.ensure_image
  return if image_exists?

  build_image
end

Instance Method Details

#start ⇒ Object



24
25
26
27
# File 'lib/mildred/container.rb', line 24

def start
  ensure_mount_paths_exist
  @id = run_container
end

#stop ⇒ Object



29
30
31
32
33
34
35
# File 'lib/mildred/container.rb', line 29

def stop
  return unless @id

  run_cli("container", "stop", @id)
  run_cli("container", "rm", @id)
  @id = nil
end