Class: Kamaze::DockerImage::Runner

Inherits:
Object
  • Object
show all
Includes:
Concern::Containers
Defined in:
lib/kamaze/docker_image/runner.rb

Overview

Runner provide methods to execute image related actions

Defined Under Namespace

Classes: Storage

Constant Summary collapse

Command =

noinspection RubyConstantNamingConvention

Kamaze::DockerImage::Command
ACTIONS =

Available actions

Actions registrable on image.

See Also:

%i[restart start stop exec run build push rm rebuild].sort

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(image) ⇒ Runner

Returns a new instance of Runner.



32
33
34
35
36
37
38
39
# File 'lib/kamaze/docker_image/runner.rb', line 32

def initialize(image)
  @config = image.to_h.reject! { |k| k == :commands }.freeze
  @commands = Storage[image.commands].tap do |store|
    store.config = @config
  end

  @commands.freeze
end

Instance Attribute Details

#commandsHash (readonly, protected)

Get commands

Returns:

  • (Hash)


114
115
116
# File 'lib/kamaze/docker_image/runner.rb', line 114

def commands
  @commands
end

#configHash (readonly, protected)

Returns:

  • (Hash)


109
110
111
# File 'lib/kamaze/docker_image/runner.rb', line 109

def config
  @config
end

Instance Method Details

#actionsArray<Symbol>

Returns:

  • (Array<Symbol>)


86
87
88
# File 'lib/kamaze/docker_image/runner.rb', line 86

def actions
  ACTIONS
end

#build(&block) ⇒ Object

Build image



42
43
44
# File 'lib/kamaze/docker_image/runner.rb', line 42

def build(&block)
  command(:build).run(&block)
end

#command(name, extra = nil) ⇒ Command (protected)

Generate command.

Parameters:

  • name (String|Symbol)
  • extra (String|nil) (defaults to: nil)

Returns:



121
122
123
124
125
# File 'lib/kamaze/docker_image/runner.rb', line 121

def command(name, extra = nil)
  command = commands.fetch(name.to_sym)

  Command.new(command, config, extra)
end

#containers(all: true) ⇒ Hash{String => Hash} (protected) Originally defined in module Concern::Containers

List containers.

Containers are indexed by name.

Returns:

  • (Hash{String => Hash})

#containers_makerProc (protected) Originally defined in module Concern::Containers

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Add some methods on retrieved info.

Returns:

  • (Proc)

#exec(extra = nil, &block) ⇒ Object



60
61
62
63
64
65
# File 'lib/kamaze/docker_image/runner.rb', line 60

def exec(extra = nil, &block)
  default = config.fetch(:exec_command)
  extra ||= default

  command(:exec, extra).run(&block)
end

#executableString (protected) Originally defined in module Concern::Executable

Get executable

Returns:

  • (String)

Raises:

  • (Cliver::Dependency::NotFound)

#push(&block) ⇒ Object

Push image



47
48
49
# File 'lib/kamaze/docker_image/runner.rb', line 47

def push(&block)
  command(:push).run(&block)
end

#rebuild(&block) ⇒ Object

Build image (do not use cache)



52
53
54
# File 'lib/kamaze/docker_image/runner.rb', line 52

def rebuild(&block)
  command(:rebuild).run(&block)
end

#restart(&block) ⇒ Object



79
80
81
82
83
# File 'lib/kamaze/docker_image/runner.rb', line 79

def restart(&block)
  stop(&block)
  rm(&block)
  start(&block)
end

#rm(&block) ⇒ Object



75
76
77
# File 'lib/kamaze/docker_image/runner.rb', line 75

def rm(&block)
  command(:rm).run(&block) if started?
end

#run(extra = nil, &block) ⇒ Object



56
57
58
# File 'lib/kamaze/docker_image/runner.rb', line 56

def run(extra = nil, &block)
  command(:run, extra).run(&block)
end

#running?Boolean

Denote container is running.

Returns:

  • (Boolean)


101
102
103
104
# File 'lib/kamaze/docker_image/runner.rb', line 101

def running?
  # !fetch_containers(config.fetch(:run_as), :running).empty?
  config.fetch(:run_as).yield_self { |name| containers[name]&.running? }
end

#start(&block) ⇒ Object



67
68
69
# File 'lib/kamaze/docker_image/runner.rb', line 67

def start(&block)
  command(:start).run(&block) unless running?
end

#started?Boolean

Denote container is started.

Returns:

  • (Boolean)


93
94
95
96
# File 'lib/kamaze/docker_image/runner.rb', line 93

def started?
  # !fetch_containers(config.fetch(:run_as)).empty?
  config.fetch(:run_as).yield_self { |name| !containers[name].nil? }
end

#stop(&block) ⇒ Object



71
72
73
# File 'lib/kamaze/docker_image/runner.rb', line 71

def stop(&block)
  command(:stop).run(&block) if started?
end