Class: KubeDeployTools::Push

Inherits:
Object
  • Object
show all
Defined in:
lib/kube_deploy_tools/push.rb,
lib/kube_deploy_tools/image_registry/image.rb

Defined Under Namespace

Classes: Image, Optparser

Instance Method Summary collapse

Constructor Details

#initialize(config, local_prefix, registries, images, tag) ⇒ Push

Returns a new instance of Push.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/kube_deploy_tools/push.rb', line 14

def initialize(config, local_prefix, registries, images, tag)
  @config = config
  @local_prefix = local_prefix

  # If |registries| is empty, assume we will push them all,
  # if not, subtract unwanted ones from |config.image_registries|
  if registries.empty?
    to_instantiate = config.image_registries.values
  else
    to_instantiate = []
    config.image_registries.each do |name, registry|
      if registries.member? name
        to_instantiate.push registry
      end
    end
  end

  @drivers = to_instantiate.map do |registry|
    driver_class = ImageRegistry::Driver::MAPPINGS.fetch(registry.driver)
    [registry, driver_class.new(registry: registry)]
  end.to_h
  @base_image_names = images
  @tag = tag
end

Instance Method Details

#publishObject



39
40
41
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
# File 'lib/kube_deploy_tools/push.rb', line 39

def publish
  dirname = File.dirname(BUILT_ARTIFACTS_FILE)
  FileUtils.mkdir_p(dirname)

  driver_images = []

  @drivers.each_pair do |registry, driver|
    driver_images.unshift [driver, tag_images(registry, @base_image_names)]
    # Does whatever is necessary to authorize against this registry
    driver.authorize
  end

  # Push first images to each registry in parallel
  driver_images.map do |driver, all_images|
    Thread.new { driver.push_image all_images[0] }
  end.each(&:join)

  # Push the rest of the images to each registry in parallel
  driver_images.map do |driver, all_images|
    _, *remaining_images = all_images
    remaining_images.map do |i|
      Thread.new { driver.push_image i }
    end
  end.flatten.each(&:join)

  # Can't lock the file if it doesn't exist. Create the file as a
  # placeholder until more content is loaded
  File.open(BUILT_ARTIFACTS_FILE, File::CREAT|File::RDWR) do |file|
    flock(file, File::LOCK_EX) do |file|
      driver_images.each do |_, all_images|
        update_built_artifacts(all_images, file)
      end
    end
  end

# Clean registry authorization in the end no matter what
ensure
  Logger.info "Removing registry authorizations"
  @drivers.each_pair do |registry, driver|
    driver.unauthorize
  end
end