Welcome to mirror list, hosted at ThFree Co, Russian Federation.

26_container_images.rb « development « fixtures « db - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: fdee57c73e33198bb1dd4e746191027850a14bc0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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
# frozen_string_literal: true

class Gitlab::Seeder::ContainerImages
  attr_reader :tmp_dir, :project, :images_count

  DOCKER_FILE_CONTENTS = <<~EOS
    FROM scratch
    ARG tag
    ENV tag=$tag
  EOS

  def initialize(project, images_count)
    @project = project
    @images_count = images_count
    initialize_tmp_dir
  end

  def seed!
    images_count.times do |i|
      image_path = "#{project.container_registry_url}:tag_#{i}"
      build_image(image_path)
      push_image(image_path)
      puts '.'
    end
  ensure
    FileUtils.remove_entry tmp_dir
  end

  private

  def build_image(image_path)
    system(*%W[docker build -t #{image_path} --build-arg tag=gitlab_container_image_seed .], chdir: @tmp_dir)
  end

  def push_image(image_path)
    system(*%W[docker push #{image_path}], chdir: @tmp_dir)
  end

  def initialize_tmp_dir
    @tmp_dir = Dir.mktmpdir('gitlab_seeder_container_images')

    File.write(File.join(@tmp_dir, 'Dockerfile'), DOCKER_FILE_CONTENTS)
  end
end

Gitlab::Seeder.quiet do
  flag = 'SEED_CONTAINER_IMAGES'

  if ENV[flag]
    admin_user = User.admins.first
    images_count = Integer(ENV[flag])

    Project.not_mass_generated.visible_to_user(admin_user).sample(1).each do |project|
      puts "\nSeeding #{images_count} container images to the '#{project.full_path}' project."

      seeder = Gitlab::Seeder::ContainerImages.new(project, images_count)
      seeder.seed!
    rescue => e
      puts "\nSeeding container images failed with #{e.message}."
      puts "Make sure that the registry is running (https://gitlab.com/gitlab-org/gitlab-development-kit/blob/main/doc/howto/registry.md) and that Docker CLI (https://www.docker.com/products/docker-desktop) is installed."
    end
  else
    puts "Skipped. Use the `#{flag}` environment variable to seed container images to the registry."
  end
end