|
| 1 | +require 'open3' |
| 2 | +require_relative 'docker_operations' |
| 3 | + |
| 4 | +# TODO: Deprecate DockerOperations |
| 5 | +class DockerHelper < DockerOperations |
| 6 | + class << self |
| 7 | + def authenticate(username: nil, password: nil, registry: nil) |
| 8 | + puts "Logging in to Docker registry" |
| 9 | + |
| 10 | + stdout, stderr, status = Open3.popen3({}, *%W[docker login --username=#{username} --password-stdin #{registry}]) do |stdin, stdout, stderr, wait_thr| |
| 11 | + stdin.puts(password) |
| 12 | + stdin.close |
| 13 | + |
| 14 | + [stdout.read, stderr.read, wait_thr.value] |
| 15 | + end |
| 16 | + |
| 17 | + return if status.success? |
| 18 | + |
| 19 | + puts "Failed to login to Docker registry." |
| 20 | + puts "Output is: #{stdout}" |
| 21 | + puts stderr |
| 22 | + Kernel.exit 1 |
| 23 | + end |
| 24 | + |
| 25 | + # TODO: When multi-arch images are built by default, modify `platforms` |
| 26 | + # array to include `linux/arm64` also |
| 27 | + def build(location, image, tag, dockerfile: nil, buildargs: nil, platforms: %w[linux/amd64], push: true) |
| 28 | + create_builder |
| 29 | + |
| 30 | + commands = %W[docker buildx build #{location} -t #{image}:#{tag}] |
| 31 | + |
| 32 | + if (env_var_platforms = Gitlab::Util.get_env('DOCKER_BUILD_PLATFORMS')) |
| 33 | + platforms.append(env_var_platforms.split(",").map(&:strip)) |
| 34 | + end |
| 35 | + |
| 36 | + platforms.uniq! |
| 37 | + |
| 38 | + commands += %W[--platform=#{platforms.join(',')}] |
| 39 | + |
| 40 | + # If specified to push, we must push to registry. Even if not, if the |
| 41 | + # image being built is multiarch, we must push to registry. |
| 42 | + commands += %w[--push] if push || platforms.length > 1 |
| 43 | + |
| 44 | + commands += %W[-f #{dockerfile}] if dockerfile |
| 45 | + |
| 46 | + buildargs&.each do |arg| |
| 47 | + commands += %W[--build-arg='#{arg}'] |
| 48 | + end |
| 49 | + |
| 50 | + puts "Running command: #{commands.join(' ')}" |
| 51 | + |
| 52 | + Open3.popen2e(*commands) do |_, stdout_stderr, status| |
| 53 | + while line = stdout_stderr.gets |
| 54 | + puts line |
| 55 | + end |
| 56 | + |
| 57 | + Kernel.exit 1 unless status.value.success? |
| 58 | + end |
| 59 | + end |
| 60 | + |
| 61 | + def create_builder |
| 62 | + cleanup_existing_builder |
| 63 | + |
| 64 | + puts "Creating docker builder instance" |
| 65 | + # TODO: For multi-arch builds, use Kubernetes driver for builder instance |
| 66 | + _, stdout_stderr, status = Open3.popen2e(*%w[docker buildx create --bootstrap --use --name omnibus-gitlab-builder]) |
| 67 | + |
| 68 | + return if status.value.success? |
| 69 | + |
| 70 | + puts "Creating builder instance failed." |
| 71 | + puts "Output: #{stdout_stderr.read}" |
| 72 | + raise |
| 73 | + end |
| 74 | + |
| 75 | + def cleanup_existing_builder |
| 76 | + puts "Cleaning any existing builder instances." |
| 77 | + _, _, status = Open3.popen2e(*%w[docker buildx ls | grep omnibus-gitlab-builder]) |
| 78 | + unless status.value.success? |
| 79 | + puts "omnibus-gitlab-builder instance not found. Not attempting to clean." |
| 80 | + return |
| 81 | + end |
| 82 | + |
| 83 | + _, stdout_stderr, status = Open3.popen2e(*%w[docker buildx rm --force omnibus-gitlab-builder]) |
| 84 | + if status.value.success? |
| 85 | + puts "Successfully cleaned omnibus-gitlab-builder instance." |
| 86 | + else |
| 87 | + puts "Cleaning of omnibus-gitlab-builder instance failed." |
| 88 | + puts "Output: #{stdout_stderr.read}" |
| 89 | + end |
| 90 | + end |
| 91 | + end |
| 92 | +end |
0 commit comments