Class: Docker::Template::Meta

Inherits:
Object
  • Object
show all
Extended by:
Forwardable::Extended
Defined in:
lib/docker/template/meta.rb

Constant Summary collapse

DEFAULTS =

– rubocop:enable Style/MultilineBlockLayout –

HashWithIndifferentAccess.new({
  "squash" => true,
  "startup" => true,
  "aliases" => {},
  "build" => true,
  "cache" => false,
  "type" => "normal",
  "local_prefix" => "local",
  "project_data_dir" => "docker",
  "force" => ENV["CI"] == "true",
  "rootfs_base_img" => "envygeeks/alpine",
  "maintainer" => "Random User <[email protected]>",
  "envygeeks" => ENV["ENVYGEEKS"] && ENV["ENVYGEEKS"] == "true",
  "user" => ENV["USER"] || ENV["USERNAME"] || "random",
  "ci" => ENV["CI"] && ENV["CI"] == "true",
  "name" => Template.root.basename.to_s,
  "project_copy_dir" => "project",
  "rootfs_template" => "alpine",
  "push" => ENV["CI"] != "true",
  "cache_dir" => "cache",
  "repos_dir" => "repos",
  "copy_dir" => "copy",
  "tag" => "latest",
  "clean" => false,
  "tty" => false,
  "tags" => {},

  #

  "log_filters" => [
    /^The push refers to a repository/,
    /\sdigest: sha256:/
  ],

  #

  "project_copy_ignore" => %w(
    .git
    .bundle
    Dockerfile
    vendor/bundle
    .gitattributes
    .node_modules
    .gitignore
    docker
    tmp
    log
  ),
}).freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(overrides, root: nil) ⇒ Meta

– Create a new instance of self.class.

“‘ – rubocop:disable Metrics/AbcSize –

Examples:

“‘

self.class.new({
  :hello => :world
})

Parameters:

  • data (Hash, self.class)
    • the main data.

  • root (Hash, self.class) (defaults to: nil)
    • the root data.



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/docker/template/meta.rb', line 105

def initialize(overrides, root: nil)
  overrides = overrides.to_h :raw => true if overrides.is_a?(self.class)
  root = root.to_h :raw => true if root.is_a?(self.class)

  if root.nil?
    if Template.project?
      load_project_config(
        overrides
      )

    else
      load_normal_config(
        overrides
      )
    end

    @root = true
  else
    @data = overrides.stringify.with_indifferent_access
    @root_data = root.stringify.with_indifferent_access
  end

  debug!
  normalize!
  return
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, shell: false, &block) ⇒ Object (private)



673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
# File 'lib/docker/template/meta.rb', line 673

def method_missing(method, *args, shell: false, &block)
  key  = method.to_s.gsub(/\?$/, "")
  val  = self[key] || self[key.singularize] \
          || self[key.pluralize]

  if !args.empty? || block_given?
    super

  elsif method !~ /\?$/
    string_wrapper(val, {
      :shell => shell
    })

  else
    val = val.fallback if val.is_a?(self.class) && val.queryable?
    [true, false].include?(val) ? val : \
      if val.respond_to?(:empty?)
        then !val.empty? else !!val
      end
  end
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



13
14
15
# File 'lib/docker/template/meta.rb', line 13

def data
  @data
end

Class Method Details

.opts_file(force: nil) ⇒ Object



84
85
86
87
88
# File 'lib/docker/template/meta.rb', line 84

def opts_file(force: nil)
  if force == :project || Template.project?
    then "docker/template.yml" else "opts.yml"
  end
end

Instance Method Details

#[](key) ⇒ Object

Note:

we make the getter slightly more indifferent because of tags.

– Pull an indifferent key from the hash. –

Parameters:

  • key (Anything())

    the key you wish to pull.



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/docker/template/meta.rb', line 219

def [](key)
  val = begin
    if key =~ /^\d+\.\d+$/
      @data[key] || @data[
        key.to_f
      ]

    elsif key =~ /^\d+$/
      @data[key] || @data[
        key.to_i
      ]

    else
      @data[key]
    end
  end

  if val.is_a?(Hash)
    return self.class.new(val, {
      :root => root_data
    })
  end

  val
end

#[]=(key, val) ⇒ Object



247
248
249
250
251
252
# File 'lib/docker/template/meta.rb', line 247

def []=(key, val)
  hash = { key => val }.stringify
  @data.update(
    hash
  )
end

#alias?Boolean

– Checks to see if the current meta is an alias of another. This happens when the user has the tag in aliases but it’s not complex. –

Returns:

  • (Boolean)


450
451
452
# File 'lib/docker/template/meta.rb', line 450

def alias?
  !!(aliased_tag && aliased_tag != tag)
end

#aliased_group(tag: current_tag) ⇒ Object



489
490
491
492
493
# File 'lib/docker/template/meta.rb', line 489

def aliased_group(tag: current_tag)
  root_data[:tags][aliased_tag({
    :tag => tag
  })]
end

#aliased_tag(tag: current_tag) ⇒ Object



475
476
477
478
479
480
481
482
483
484
485
# File 'lib/docker/template/meta.rb', line 475

def aliased_tag(tag: current_tag)
  aliases = root_data[:aliases]
  if aliases.nil? || !aliases.key?(tag)
    tag

  else
    aliases[
      tag
    ]
  end
end

#by_group(group: current_group, query_data: @data) ⇒ Object



413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
# File 'lib/docker/template/meta.rb', line 413

def by_group(group: current_group, query_data: @data)
  if query_data.is_a?(self.class)
    then query_data.by_group({
      :group => group
    })

  elsif !query_data || !query_data.is_a?(Hash)
    return nil

  else
    query_data.fetch("group", {}).fetch(
      group, nil
    )
  end
end

#by_parent_group(tag: current_tag, query_data: @data) ⇒ Object



431
432
433
434
435
436
437
438
439
440
441
442
443
# File 'lib/docker/template/meta.rb', line 431

def by_parent_group(tag: current_tag, query_data: @data)
  if aliased_tag == current_tag || !complex_alias?
    return nil

  else
    by_group({
      :query_data => query_data,
      :group => aliased_group({
        :tag => tag
      })
    })
  end
end

#by_parent_tag(tag: current_tag, query_data: @data) ⇒ Object



397
398
399
400
401
402
403
404
405
406
407
408
409
# File 'lib/docker/template/meta.rb', line 397

def by_parent_tag(tag: current_tag, query_data: @data)
  if aliased_tag == current_tag || !complex_alias?
    return nil

  else
    by_tag({
      :query_data => query_data,
      :tag => aliased_tag({
        :tag => tag
      })
    })
  end
end

#by_tag(tag: current_tag, query_data: @data) ⇒ Object



379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
# File 'lib/docker/template/meta.rb', line 379

def by_tag(tag: current_tag, query_data: @data)
  if query_data.is_a?(self.class)
    then query_data.by_tag({
      :tag => tag
    })

  elsif !query_data || !query_data.is_a?(Hash)
    return nil

  else
    query_data.fetch("tag", {}).fetch(
      tag, nil
    )
  end
end

#complex_alias?Boolean

– A complex alias happens when the user has an alias but also tries to add extra data, this allows them to use data from all parties. This allows them to reap the benefits of having shared data but sometimes independent data that diverges into it’s own. –

Returns:

  • (Boolean)


461
462
463
464
465
466
467
468
469
470
471
# File 'lib/docker/template/meta.rb', line 461

def complex_alias?
  if !alias?
    return false

  else
    !!root_data.find do |_, v|
      (v.is_a?(self.class) || v.is_a?(Hash)) && queryable?(:query_data => v) \
        && by_tag(:query_data => v)
    end
  end
end

#current_groupObject Also known as: group



625
626
627
628
# File 'lib/docker/template/meta.rb', line 625

def current_group
  root_data[:tags][current_tag] ||
    "normal"
end

#debug!Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/docker/template/meta.rb', line 150

def debug!
  if root? && root_data["debug"]
    if !key?(:env) || self[:env].queryable?
      self[:env] ||= {}

      merge!({
        :env => {
          :all => {
            :DEBUG => true
          }
        }
      })
    end
  end
end

#fallback(group: current_group, tag: current_tag, query_data: @data) ⇒ Object

– Fallback, determining which route is the best. Tag > Group > All. – rubocop:disable Metrics/CyclomaticComplexity rubocop:disable Metrics/PerceivedComplexity –



338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
# File 'lib/docker/template/meta.rb', line 338

def fallback(group: current_group, tag: current_tag, query_data: @data)
  if query_data.is_a?(self.class)
    then query_data.fallback({
      :group => group, :tag => tag
    })

  elsif !query_data || !query_data.is_a?(Hash) || query_data.empty?
    return nil

  else
    if !(v = by_tag(:tag => tag, :query_data => query_data)).nil? then return v
      elsif !(v = by_parent_tag(:tag => tag, :query_data => query_data)).nil? then return v
      elsif !(v = by_group(:group => group, :query_data => query_data)).nil? then return v
      elsif !(v = by_parent_group(:tag => tag, :query_data => query_data)).nil? then return v
      else return for_all(:query_data => query_data)
    end
  end
end

#for_all(query_data: @data) ⇒ Object

– rubocop:enable Metrics/CyclomaticComplexity rubocop:enable Metrics/PerceivedComplexity –



362
363
364
365
366
367
368
369
370
371
372
373
374
375
# File 'lib/docker/template/meta.rb', line 362

def for_all(query_data: @data)
  if query_data.is_a?(self.class)
    then query_data \
      .for_all

  elsif !query_data || !query_data.is_a?(Hash)
    return nil

  else
    query_data.fetch(
      "all", nil
    )
  end
end

#groupsObject

– HELPER: Get a list of all the groups. –



642
643
644
# File 'lib/docker/template/meta.rb', line 642

def groups
  root_data["tags"].values.uniq
end

#include?(val) ⇒ Boolean

– Check if a part of the hash or a value is inside. –

Examples:

meta.include?(:key => :val) => true|false

meta.include?(:key) => true|false

Parameters:

  • val (Anytning(), Hash)
    • The key or key => val you wish check.

Returns:

  • (Boolean)


196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/docker/template/meta.rb', line 196

def include?(val)
  if val.is_a?(Hash)
    then val.stringify.each do |k, v|
      unless @data.key?(k) && @data[k] == v
        return false
      end
    end

  else
    return @data.include?(
      val
    )
  end

  true
end

#merge(new_) ⇒ Object Also known as: deep_merge

– Merge a hash into the meta. If you merge non-queryable data it will then get merged into the queryable data. –



281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/docker/template/meta.rb', line 281

def merge(new_)
  if !queryable?(:query_data => new_) && queryable?
    new_ = {
      :all => new_
    }
  end

  new_ = new_.stringify
  self.class.new(@data.deep_merge(new_), {
    :root => root_data
  })
end

#merge!(new_) ⇒ Object

– Destructive merging (@see self#merge) –



298
299
300
301
302
303
304
305
306
307
308
309
310
# File 'lib/docker/template/meta.rb', line 298

def merge!(new_)
  if !queryable?(:query_data => new_) && queryable?
    new_ = {
      :all => new_
    }
  end

  @data = @data.deep_merge(
    new_.stringify
  )

  self
end

#mergeable_array?(key = nil) ⇒ Boolean

Returns:

  • (Boolean)


605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
# File 'lib/docker/template/meta.rb', line 605

def mergeable_array?(key = nil)
  return false unless queryable?
  vals = [by_parent_tag, by_parent_group, \
    by_tag, for_all, by_group].compact

  if key
    vals = vals.map do |val|
      val[key]
    end
  end

  !vals.empty? && !vals.any? do |val|
    !val.is_a?(
      Array
    )
  end
end

#mergeable_hash?(key = nil) ⇒ Boolean

– rubocop:enable Metrics/AbcSize –

Returns:

  • (Boolean)


585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
# File 'lib/docker/template/meta.rb', line 585

def mergeable_hash?(key = nil)
  return false unless queryable?
  vals = [by_parent_tag, by_parent_group, \
    by_tag, for_all, by_group].compact

  if key
    vals = vals.map do |val|
      val[key]
    end
  end

  !vals.empty? && !vals.any? do |val|
    !val.is_a?(Hash) && !val.is_a?(
      self.class
    )
  end
end

#normalize!Object



134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/docker/template/meta.rb', line 134

def normalize!
  if root?
    opts = {
      :allowed_keys => [],
      :allowed_vals => []
    }

    merge!({
      "tags"    => @data[   "tags"].stringify(**opts),
      "aliases" => @data["aliases"].stringify(**opts)
    })
  end
end

#queryable?(query_data: @data) ⇒ Boolean

– Check if a hash is queryable. AKA has “all”, “group”, “tag”. –

Returns:

  • (Boolean)


316
317
318
319
320
321
322
323
324
325
326
327
328
329
# File 'lib/docker/template/meta.rb', line 316

def queryable?(query_data: @data)
  if query_data.is_a?(self.class)
    then query_data
      .queryable?

  elsif !query_data || !query_data.is_a?(Hash) || query_data.empty?
    return false

  else
    (query_data.keys - %w(
      group tag all
    )).empty?
  end
end

#rootObject



174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/docker/template/meta.rb', line 174

def root
  if Template.project?
    then return Template.root.join(root_data[
      :project_data_dir
    ])

  else
    Template.root.join(
      root_data[:repos_dir], root_data[
        :name
      ]
    )
  end
end

#root_dataObject



168
169
170
# File 'lib/docker/template/meta.rb', line 168

def root_data
  return @root_data || @data
end

#tagsObject

– HELPER: Get a list of all the tags. –



634
635
636
# File 'lib/docker/template/meta.rb', line 634

def tags
  (root_data[:tags] || {}).keys | (root_data[:aliases] || {}).keys
end

#to_a(raw: false, shell: false) ⇒ Object

– rubocop:disable Metrics/CyclomaticComplexity rubocop:disable Metrics/PerceivedComplexity –



519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
# File 'lib/docker/template/meta.rb', line 519

def to_a(raw: false, shell: false)
  if raw
    return to_h({
      :raw => true
    }).to_a

  elsif !mergeable_array?
    to_h.each_with_object([]) do |(k, v), a|
      a << "#{k}=#{
        shell ? v.to_s.shellescape : v
      }"
    end
  else
    (for_all || []) | (by_parent_group || []) | (by_group || []) | \
      (by_parent_tag || []) | (by_tag || [])
  end
end

#to_enumObject



264
265
266
267
268
269
270
271
272
273
274
# File 'lib/docker/template/meta.rb', line 264

def to_enum
  @data.each_with_object({}) do |(k, v), h|
    if v.is_a?(Hash)
      then v = self.class.new(v, {
        :root => root_data
      })
    end

    h[k] = v
  end.to_enum
end

#to_h(raw: false) ⇒ Object

– rubocop:eanble Metrics/CyclomaticComplexity rubocop:eanble Metrics/PerceivedComplexity – Convert a ‘Meta’ into a normal hash. If ‘self’ is queryable then we go and start merging values smartly. This means that we will merge all the arrays into one another and we will merge hashes into hashes. – rubocop:disable Metrics/AbcSize –



548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
# File 'lib/docker/template/meta.rb', line 548

def to_h(raw: false)
  return @data.to_h if raw || !queryable? || !mergeable_hash?
  keys = [for_all, by_group, by_parent_group, by_tag, \
    by_parent_tag].compact.map(&:keys)

  keys.reduce(:+).each_with_object({}) do |k, h|
    vals = [for_all, by_group, by_parent_group, by_tag, \
      by_parent_tag].compact

    h[k] = \
      if mergeable_array?(k)
        vals.map { |v| v[k].to_a } \
          .compact.reduce(
            :+
          )

      elsif mergeable_hash?(k)
        vals.map { |v| v[k].to_h } \
          .compact.reduce(
            :deep_merge
          )

      else
        vals.find do |v|
          v.key?(
            k
          )
        end \
        [k]
      end
  end
end

#to_s(raw: false, shell: false) ⇒ Object

– Converts the current meta into a string. –



499
500
501
502
503
504
505
506
507
508
509
510
511
512
# File 'lib/docker/template/meta.rb', line 499

def to_s(raw: false, shell: false)
  if !raw && (mergeable_hash? || mergeable_array?)
    to_a(:shell => shell).join(" #{
      "\n" if shell
    }")

  elsif !raw && queryable?
    then fallback \
      .to_s

  else
    @data.to_s
  end
end

#update(hash) ⇒ Object



256
257
258
259
260
# File 'lib/docker/template/meta.rb', line 256

def update(hash)
  @data.update(
    hash.stringify
  )
end