Class: Dockit::Service
- Inherits:
-
Object
- Object
- Dockit::Service
- Defined in:
- lib/dockit/service.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#image ⇒ Object
readonly
Returns the value of attribute image.
Instance Method Summary collapse
- #build ⇒ Object
- #id ⇒ Object
-
#initialize(file = "./Dockit.yaml", locals: {}) ⇒ Service
constructor
A new instance of Service.
- #name ⇒ Object
- #pull(registry, tag = nil, force = false) ⇒ Object
- #push(registry, tag = nil, force = false) ⇒ Object
- #start(options) ⇒ Object
Constructor Details
#initialize(file = "./Dockit.yaml", locals: {}) ⇒ Service
Returns a new instance of Service.
6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/dockit/service.rb', line 6 def initialize(file="./Dockit.yaml", locals: {}) @config = Dockit::Config.new(file, locals) # get the image if it is specified and already exists if name = config.get(:create, :Image) || config.get(:build, :t) begin @image = Dockit::Image.get(name) rescue Docker::Error::NotFoundError end end end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
3 4 5 |
# File 'lib/dockit/service.rb', line 3 def config @config end |
#image ⇒ Object (readonly)
Returns the value of attribute image.
4 5 6 |
# File 'lib/dockit/service.rb', line 4 def image @image end |
Instance Method Details
#build ⇒ Object
18 19 20 |
# File 'lib/dockit/service.rb', line 18 def build @image = Dockit::Image.create(config.get(:build)) end |
#id ⇒ Object
72 73 74 |
# File 'lib/dockit/service.rb', line 72 def id image.id end |
#name ⇒ Object
76 77 78 |
# File 'lib/dockit/service.rb', line 76 def name n = config.get(:create, :name) || config.get(:build, :t).split(':')[0] end |
#pull(registry, tag = nil, force = false) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/dockit/service.rb', line 57 def pull(registry, tag=nil, force=false) unless repo = config.get(:build, 't') STDERR.puts "No such locally built image" exit 1 end name = "#{registry}/#{repo}" image = Docker::Image.create(fromImage: name) do |chunk| Dockit::Log::print_chunk(chunk) end puts "Tagging #{name} as #{repo}:#{tag||'latest'}" image.tag(repo: repo, tag: tag, force: force) end |
#push(registry, tag = nil, force = false) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/dockit/service.rb', line 44 def push(registry, tag=nil, force=false) raise "No image found!" unless image image.tag(repo: "#{registry}/#{config.get(:build, 't')}", force: force) STDOUT.sync = true image.push(tag: tag) do |chunk| chunk = JSON.parse(chunk) progress = chunk['progress'] id = progress ? '' : "#{chunk['id']} " print chunk['status'], ' ', id, progress, progress ? "\r" : "\n" end end |
#start(options) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/dockit/service.rb', line 22 def start() opts = merge_config(:create, stringify([:create])) unless image || opts['Image'] raise "No runnable image found or specified!" end opts['Image'] ||= image.id if image opts['name'] ||= config.get(:build, :t) run = merge_config(:run, stringify([:run])) if [:verbose] cmd = [(opts['Entrypoint']||[]), ((opts['Cmd'] || %w[default]))].flatten puts " * %s (%s)" % [ opts['name'] || 'unnamed', cmd.join(' ') ] puts " * #{run}" if run.length > 0 end Dockit::Container.new(opts).start( run, verbose: [:verbose], transient: [:transient]) end |