Class: Rum::Manifest

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Rake::DSL
Defined in:
lib/rumrunner/manifest.rb

Overview

Rum Runner Manifest for managing Docker commands.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, root: nil, &block) ⇒ Manifest

Initialize new manifest with name and root path for caching build digests. Evaluates &block if given.

Example:

Manifest.new(name: "my_image", root: ".docker")

27
28
29
30
31
32
33
# File 'lib/rumrunner/manifest.rb', line 27

def initialize(name:, root:nil, &block)
  @name  = name
  @root  = root || :".docker"
  @image = Docker::Image.parse(name)
  @env   = []
  instance_eval(&block) if block_given?
end

Instance Attribute Details

#imageObject (readonly)

Access Docker image object.


14
15
16
# File 'lib/rumrunner/manifest.rb', line 14

def image
  @image
end

Instance Method Details

#applicationObject

Get application


37
38
39
# File 'lib/rumrunner/manifest.rb', line 37

def application
  Rake.application
end

#artifact(*args, &block) ⇒ Object

Defines docker run task for redirecting a file from a running instance of the dependent stage's container to the local file system.

Example:

artifact :name => [:stage]

126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/rumrunner/manifest.rb', line 126

def artifact(*args, &block)
  name, _, deps = Rake.application.resolve_args(args)

  target  = deps.first
  image   = Docker::Image.parse("#{@image}-#{target}")
  iidfile = File.join(root, *image)
  path    = File.split(name).first
  deps    = [iidfile]

  unless path == "."
    directory path
    deps << path
  end

  artifact_file name, deps, iidfile, &block

  artifact_clobber name, path
end

#build(*args, &block) ⇒ Object

Defines generic docker build task.

Example:

build :name
build :name => [:deps]

60
61
62
63
64
65
# File 'lib/rumrunner/manifest.rb', line 60

def build(*args, &block)
  name, _, deps = Rake.application.resolve_args(args)
  task name => deps do
    sh Docker::Build.new(&block).to_s
  end
end

#default(*args, &block) ⇒ Object

Defines the default task for rum executable.

Example:

default :task_or_file
default :task_or_file => [:deps]

48
49
50
51
# File 'lib/rumrunner/manifest.rb', line 48

def default(*args, &block)
  name = Rake.application.resolve_args(args).first
  task :default => name
end

#installObject

Install any remaining tasks for the manifest.


172
173
174
175
176
# File 'lib/rumrunner/manifest.rb', line 172

def install
  install_clean

  self
end

#run(*args, &block) ⇒ Object

Defines generic docker run task.

Example:

run :name
run :name => [:deps]

74
75
76
77
78
79
# File 'lib/rumrunner/manifest.rb', line 74

def run(*args, &block)
  name, _, deps = Rake.application.resolve_args(args)
  task name => deps do
    sh Docker::Run.new(image: to_s, &block).to_s
  end
end

#shell(*args, &block) ⇒ Object

Defines docker run task for shelling into the given stage.

Example:

shell :stage
shell :stage => [:deps]

152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/rumrunner/manifest.rb', line 152

def shell(*args, &block)
  target  = Rake.application.resolve_args(args).first
  name    = task_name shell: target
  image   = Docker::Image.parse("#{@image}-#{target}")
  iidfile = File.join(root, *image)

  Rake::Task[name].clear if Rake::Task.task_defined?(name)

  desc "Shell into `#{target}` stage"
  task name, [:shell] => iidfile do |t,args|
    digest = File.read(iidfile)
    shell  = args.any? ? args.to_a.join(" ") : "/bin/sh"
    run    = Docker::Run.new(options: run_options, image: digest, &block)
    run.with_defaults(entrypoint: shell, interactive:true, rm: true, tty: true)
    sh run.to_s
  end
end

#stage(*args, &block) ⇒ Object

Defines docker build task for the given stage.

Example:

stage :name
stage :name => [:deps]

88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/rumrunner/manifest.rb', line 88

def stage(*args, &block)
  name, _, deps = Rake.application.resolve_args(args)

  # Assemble image/iidfile from manifest/stage name
  image   = Docker::Image.parse("#{@image}-#{name}")
  iidfile = File.join(root, *image)
  iidpath = File.split(iidfile).first

  # Ensure path to iidfile exists
  iiddeps = if deps.empty?
    directory iidpath
    iidpath
  else
    images = deps.map{|x| Docker::Image.parse("#{@image}-#{x}") }
    images.map{|x| File.join(root, *x) }
  end

  # Build stage and save digest in iidfile
  stage_file iidfile, iiddeps, tag: image, target: name, &block

  # Shortcut to build stage by name
  stage_task name, iidfile

  # Shell into stage
  stage_shell name, iidfile

  # Clean stage
  stage_clean name, iidfile, deps
end