squared 0.4.16 → 0.4.18

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -55,15 +55,15 @@ module Squared
55
55
  commit: %w[a|author=q c|change=q m|message=q pause=b?].freeze,
56
56
  inspect: %w[s|size f|format=q].freeze,
57
57
  start: %w[a|attach i|interactive detach-keys=q].freeze,
58
- stop: %w[s|signal=b t|time=i].freeze,
59
- restart: %w[s|signal=b t|time=i].freeze,
58
+ stop: %w[s|signal=b t|time=i t|timeout=i].freeze,
59
+ restart: %w[s|signal=b t|time=i t|timeout=i].freeze,
60
60
  kill: %w[s|signal=b].freeze,
61
61
  stats: %w[no-trunc format|q].freeze
62
62
  }.freeze,
63
63
  image: {
64
64
  list: %w[a|all digests no-trunc f|filter=q format=q].freeze,
65
65
  push: %w[a|all-tags disable-content-trust=b? platform=b q|quiet].freeze,
66
- rm: %w[f|force no-prune].freeze,
66
+ rm: %w[f|force no-prune platform=b].freeze,
67
67
  save: %w[o|output=p platform=b].freeze
68
68
  }.freeze,
69
69
  network: {
@@ -73,8 +73,11 @@ module Squared
73
73
  }.freeze
74
74
  VAL_DOCKER = {
75
75
  run: {
76
- bind: %w[type source src destination dst target readonly ro bind-propagation].freeze,
77
- tmpfs: %w[type destination dst target tmpfs-size tmpfs-mode].freeze
76
+ common: %w[source src destination dst target readonly ro].freeze,
77
+ bind: %w[bind-propagation].freeze,
78
+ volume: %w[volume-subpath volume-nocopy volume-opt].freeze,
79
+ tmpfs: %w[tmpfs-size tmpfs-mode].freeze,
80
+ image: %w[image-path].freeze
78
81
  }.freeze
79
82
  }.freeze
80
83
  private_constant :COMPOSEFILE, :BAKEFILE, :OPT_DOCKER, :VAL_DOCKER
@@ -92,7 +95,7 @@ module Squared
92
95
  end
93
96
 
94
97
  subtasks({
95
- 'build' => %i[tag context bake].freeze,
98
+ 'build' => %i[tag context].freeze,
96
99
  'compose' => %i[build run exec up].freeze,
97
100
  'bake' => %i[build check].freeze,
98
101
  'image' => %i[list rm push tag save].freeze,
@@ -125,7 +128,7 @@ module Squared
125
128
 
126
129
  def populate(*, **)
127
130
  super
128
- return unless ref?(Docker.ref)
131
+ return unless ref?(Docker.ref) || @only
129
132
 
130
133
  namespace name do
131
134
  Docker.subtasks do |action, flags|
@@ -134,7 +137,7 @@ module Squared
134
137
  namespace action do
135
138
  flags.each do |flag|
136
139
  case action
137
- when 'build', 'bake'
140
+ when 'build'
138
141
  case flag
139
142
  when :tag, :context
140
143
  format_desc(action, flag, 'opts*', before: flag == :tag ? 'name' : 'dir')
@@ -142,9 +145,12 @@ module Squared
142
145
  param = param_guard(action, flag, args: args, key: flag)
143
146
  buildx(:build, args.extras, "#{flag}": param)
144
147
  end
145
- when :bake, :build
146
- next unless bake?
148
+ end
149
+ when 'bake'
150
+ break unless bake?
147
151
 
152
+ case flag
153
+ when :build
148
154
  format_desc action, flag, ':?,opts*,target*,context?'
149
155
  task flag do |_, args|
150
156
  args = args.to_a
@@ -155,8 +161,6 @@ module Squared
155
161
  end
156
162
  end
157
163
  when :check
158
- next unless bake?
159
-
160
164
  format_desc action, flag, 'target'
161
165
  task flag, [:target] do |_, args|
162
166
  target = param_guard(action, flag, args: args, key: :target)
@@ -164,6 +168,8 @@ module Squared
164
168
  end
165
169
  end
166
170
  when 'compose'
171
+ break unless compose?
172
+
167
173
  case flag
168
174
  when :build, :up
169
175
  format_desc action, flag, 'opts*,service*'
@@ -350,7 +356,7 @@ module Squared
350
356
  when :build, :up
351
357
  op.append(escape: true)
352
358
  when :exec, :run
353
- append_command(flag, service, op.extras, from: from)
359
+ append_command flag, service, op.extras
354
360
  end
355
361
  run(from: from)
356
362
  end
@@ -367,44 +373,54 @@ module Squared
367
373
  when :run, :create, :exec
368
374
  if rc && !op.arg?('mount')
369
375
  run = VAL_DOCKER[:run]
370
- both = run[:bind] + run[:tmpfs]
371
- diff = run[:bind].reject { |val| run[:tmpfs].include?(val) }
372
- delim = Regexp.new(",\\s*(?=#{both.join('|')})")
376
+ all = collect_hash VAL_DOCKER[:run]
377
+ delim = Regexp.new(",\\s*(?=#{all.join('|')})")
373
378
  Array(@mounts).each do |val|
374
379
  args = []
375
- tmpfs = true
380
+ type = nil
376
381
  val.split(delim).each do |opt|
377
- k, v, q = split_option(opt)
378
- next unless both.include?(k)
379
-
382
+ k, v, q = split_option opt
380
383
  if k == 'type'
381
- tmpfs = false if v == 'bind'
382
- next
383
- elsif diff.include?(k)
384
- tmpfs = false
385
- end
386
- case k
387
- when 'readonly', 'ro'
388
- args << k
389
- next
390
- when 'source', 'src', 'destination', 'dst', 'target'
391
- v = path + v
392
- v = shell_quote(v, option: false, force: false) if q == ''
393
- tmpfs = false if k[0] == 's'
384
+ case v
385
+ when 'bind', 'volume', 'image', 'tmpfs'
386
+ type = v
387
+ else
388
+ raise_error("unknown type: #{v}", hint: flag)
389
+ end
390
+ elsif all.include?(k)
391
+ unless type
392
+ run.each_pair do |key, val|
393
+ if val.include?(k)
394
+ type = key.to_s unless key == :common
395
+ break
396
+ end
397
+ end
398
+ end
399
+ case k
400
+ when 'readonly', 'ro'
401
+ args << k
402
+ next
403
+ when 'source', 'src', 'destination', 'dst', 'target', 'volume-subpath', 'image-path'
404
+ v = path + v
405
+ v = shell_quote(v, option: false, force: false) if q == ''
406
+ end
407
+ args << "#{k}=#{q + v + q}"
408
+ elsif verbose
409
+ log_message(Logger::INFO, 'unrecognized option', subject: from, hint: k)
394
410
  end
395
- args << "#{k}=#{q + v + q}"
396
411
  end
397
- cmd << "--mount type=#{tmpfs ? 'tmpfs' : 'bind'},#{args.join(',')}"
412
+ raise_error('missing type', hint: flag) unless type
413
+ cmd << "--mount type=#{type},#{args.join(',')}"
398
414
  end
399
415
  end
400
- append_command(flag, id || tagmain, op.extras, from: from)
416
+ append_command(flag, id || tagmain, op.extras)
401
417
  when :update
402
- raise_error('missing container', hint: from) if op.empty?
418
+ raise_error('missing container', hint: flag) if op.empty?
403
419
  op.append(escape: true)
404
420
  when :commit
405
421
  latest = op.shift || tagmain
406
422
  cmd << id << latest
407
- raise_error("unknown args: #{op.join(', ')}", hint: from) unless op.empty?
423
+ raise_error("unknown args: #{op.join(', ')}", hint: flag) unless op.empty?
408
424
  return unless confirm_command(cmd.to_s, title: from, target: id, as: latest)
409
425
 
410
426
  registry = option('registry') || @registry
@@ -447,7 +463,7 @@ module Squared
447
463
  when :rm
448
464
  status = %w[created exited dead]
449
465
  end
450
- ps = docker_output('ps -a', *status.map { |s| "--filter=\"status=#{s}\"" })
466
+ ps = docker_output('ps -a', *status.map { |s| quote_option('filter', "status=#{s}") })
451
467
  list_image(flag, ps, no: no, hint: "status: #{status.join(', ')}", from: from) do |img|
452
468
  run(cmd.temp(img), from: from)
453
469
  end
@@ -511,8 +527,8 @@ module Squared
511
527
  when :push
512
528
  id ||= option('tag', ignore: false) || tagmain
513
529
  registry ||= op.shift || option('registry') || @registry
514
- raise_error(id ? "unknown args: #{op.join(', ')}" : 'no id/tag given', hint: from) unless id && op.empty?
515
- raise_error('username/registry not provided', hint: from) unless registry
530
+ raise_error(id ? "unknown args: #{op.join(', ')}" : 'no id/tag given', hint: flag) unless id && op.empty?
531
+ raise_error('username/registry not provided', hint: flag) unless registry
516
532
  registry.chomp!('/')
517
533
  uri = shell_quote("#{registry}/#{id}")
518
534
  op << uri
@@ -546,6 +562,14 @@ module Squared
546
562
  super || dockerfile.exist?
547
563
  end
548
564
 
565
+ def compose?(file = dockerfile)
566
+ COMPOSEFILE.include?(File.basename(file))
567
+ end
568
+
569
+ def bake?(file = dockerfile)
570
+ BAKEFILE.include?(File.basename(file))
571
+ end
572
+
549
573
  def dockerfile(val = nil)
550
574
  if val == 'Dockerfile'
551
575
  @file = false
@@ -575,7 +599,7 @@ module Squared
575
599
  session('docker', *cmd, main: false, options: false, **kwargs)
576
600
  end
577
601
 
578
- def append_command(flag, val, list, target: @session, from: nil)
602
+ def append_command(flag, val, list, target: @session)
579
603
  if list.delete(':')
580
604
  list << readline('Enter command [args]', force: true)
581
605
  elsif (args = env('DOCKER_ARGS'))
@@ -587,7 +611,7 @@ module Squared
587
611
  target << basic_option('name', dnsname("#{name}_%s" % rand_s(6)))
588
612
  end
589
613
  when :exec
590
- raise_error('no command args', hint: from) if list.empty?
614
+ raise_error('no command args', hint: flag) if list.empty?
591
615
  end
592
616
  target << val << list.shift
593
617
  target << list.join(' && ') unless list.empty?
@@ -633,7 +657,7 @@ module Squared
633
657
  index = 0
634
658
  all = option('all', prefix: 'docker')
635
659
  y = from == :'image:rm' && option('y', prefix: 'docker')
636
- pat = /^(?:#{dnsname(name)}|#{tagname(project)}|#{tagmain.split(':', 2).first})(?:[_.,:-]|$)/
660
+ pat = /\b(?:#{dnsname(name)}|#{tagname(project)}|#{tagmain.split(':', 2).first})\b/
637
661
  IO.popen(session_done(cmd << '--format=json')).each do |line|
638
662
  data = JSON.parse(line)
639
663
  id = data['ID']
@@ -682,7 +706,7 @@ module Squared
682
706
  end
683
707
  yield id
684
708
  end
685
- puts log_message(Logger::INFO, 'none detected', subject: "#{name}:#{from}", hint: hint) if found || y
709
+ puts log_message(Logger::INFO, 'none detected', subject: name, hint: hint || from) if !found && !y
686
710
  end
687
711
  rescue StandardError => e
688
712
  on_error e, from
@@ -801,14 +825,6 @@ module Squared
801
825
  def tagmain
802
826
  tag.is_a?(Array) ? tag.first : tag
803
827
  end
804
-
805
- def compose?(file = dockerfile)
806
- COMPOSEFILE.include?(File.basename(file))
807
- end
808
-
809
- def bake?(file = dockerfile)
810
- BAKEFILE.include?(File.basename(file))
811
- end
812
828
  end
813
829
 
814
830
  Application.implement Docker