Class: Rum::Manifest

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Manifest.



15
16
17
18
19
20
21
# File 'lib/rumrunner/manifest.rb', line 15

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)

Returns the value of attribute image.



9
10
11
# File 'lib/rumrunner/manifest.rb', line 9

def image
  @image
end

Instance Method Details

#artifact(*args, &block) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/rumrunner/manifest.rb', line 99

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

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

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

  desc "Build `#{name}`"
  file name => deps do
    digest = File.read(iidfile)
    run = Docker::Run.new(options: run_options, image: digest, cmd: ["cat", name], &block)
    run.with_defaults(rm: true)
    sh "#{run} > #{name}"
  end

  desc "Remove any generated files"
  task :clobber do
    rm name if File.exists?(name)
    rm_r path if Dir.exists?(path) && path != "."
  end
end

#build(*args, &block) ⇒ Object



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

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



23
24
25
26
# File 'lib/rumrunner/manifest.rb', line 23

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

#installObject



146
147
148
149
150
# File 'lib/rumrunner/manifest.rb', line 146

def install
  install_clean

  self
end

#run(*args, &block) ⇒ Object



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

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

#shell(*args, &block) ⇒ Object



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

def shell(*args, &block)
  target  = Rake.application.resolve_args(args).first
  name    = :"#{target}:shell"
  image   = "#{@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



42
43
44
45
46
47
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
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/rumrunner/manifest.rb', line 42

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

  # Assemble image/iidfile from manifest/stage name
  image   = "#{@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
    deps.map{|x| File.join(root, "#{@image}-#{x}") }
  end

  # Build stage and save digest in iidfile
  file iidfile => iiddeps do
    build = Docker::Build.new(options: build_options, &block)
    build.with_defaults(iidfile: iidfile, tag: image, target: name)
    sh build.to_s
  end

  # Shortcut to build stage by name
  desc "Build `#{name}` stage"
  task name => iidfile

  # Shell into stage
  desc "Shell into `#{name}` stage"
  task :"#{name}:shell", [: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)
      .entrypoint(shell)
      .interactive(true)
      .rm(true)
      .tty(true)
      .image(digest)
    sh run.to_s
  end

  # Clean stage
  desc "Remove any temporary images and products from `#{name}` stage"
  task :"#{name}:clean" do
    if File.exists? iidfile
      sh "docker", "image", "rm", "--force", File.read(iidfile)
      rm iidfile
    end
  end

  # Add stage to general clean
  task :clean => :"#{name}:clean"

  # Ensure subsequent stages are cleaned before this one
  deps.each{|dep| task :"#{dep}:clean" => :"#{name}:clean" }
end