class KubeDeployTools::ArtifactRegistry::Driver::GCS

Public Class Methods

new(config:) click to toggle source
# File lib/kube_deploy_tools/artifact_registry/driver_gcs.rb, line 14
def initialize(config:)
  @config = config

  @bucket = @config.fetch('bucket')
  prefix = @config.fetch('prefix', '')
  if !prefix.empty?
    @bucket = "#{@bucket}/#{prefix}"
  end
end

Public Instance Methods

download(project:, build_number:, flavor:, name:, pre_apply_hook:, output_dir:) click to toggle source
# File lib/kube_deploy_tools/artifact_registry/driver_gcs.rb, line 68
def download(project:, build_number:, flavor:, name:, pre_apply_hook:, output_dir:)
  registry_artifact_path = get_registry_artifact_path(
    name: name, flavor: flavor, project: project, build_number: build_number)

  local_artifact_path = download_artifact(registry_artifact_path, output_dir)

  if pre_apply_hook
    out, err, status = Shellrunner.run_call(pre_apply_hook, local_artifact_path)
    if !status.success?
      raise "Failed to run post download hook #{pre_apply_hook}"
    end
  end

  local_artifact_path
end
download_artifact(input_path, output_dir_path) click to toggle source
# File lib/kube_deploy_tools/artifact_registry/driver_gcs.rb, line 84
def download_artifact(input_path, output_dir_path)
  filename = File.basename(input_path)
  output_path = File.join(output_dir_path, filename)
  out, err, status = Shellrunner.run_call('gsutil', 'cp', input_path, output_path)

  if !status.success?
    raise "Failed to download remote deploy artifact #{input_path}"
  end

  output_path
end
get_artifact_name(name:, flavor:) click to toggle source
# File lib/kube_deploy_tools/artifact_registry/driver_gcs.rb, line 49
def get_artifact_name(name:, flavor:)
  "manifests_#{name}_#{flavor}.yaml"
end
get_latest_build_number(project) click to toggle source
# File lib/kube_deploy_tools/artifact_registry/driver_gcs.rb, line 96
def get_latest_build_number(project)
  out = Shellrunner.check_call('gsutil', 'ls', get_registry_build_path(project: project))

  # pick out the build numbers from the list
  build_regex = /([0-9]+)\/$/
  build_entries = out.scan(build_regex)

  build_entries.
  map { |x| x[0].to_s.to_i }.
  max.to_s
end
get_local_artifact_path(name:, flavor:, local_dir:) click to toggle source
# File lib/kube_deploy_tools/artifact_registry/driver_gcs.rb, line 24
def get_local_artifact_path(name:, flavor:, local_dir:)
  artifact_name = get_artifact_name(name: name, flavor: flavor)

  local_artifact_path = File.join(local_dir, artifact_name)

  local_artifact_path
end
get_registry_artifact_path(name:, flavor:, project:, build_number:) click to toggle source
# File lib/kube_deploy_tools/artifact_registry/driver_gcs.rb, line 41
def get_registry_artifact_path(name:, flavor:, project:, build_number:)
  # NOTE(joshk): If the naming format changes, it represents a breaking
  # change where all past clients will not be able to download new builds and
  # new clients will not be able to download old builds. Change with caution.
  #
  "#{get_registry_build_path(project: project)}/#{build_number}/artifact/#{get_artifact_name(name: name, flavor: flavor)}"
end
get_registry_build_path(project:) click to toggle source
# File lib/kube_deploy_tools/artifact_registry/driver_gcs.rb, line 32
def get_registry_build_path(project:)
  # NOTE: If the naming format changes, it represents a breaking change
  # where all past clients will not be able to list/download new builds
  # and new clients will not be able to list/download old builds. Change
  # with caution.
  #
  "#{@bucket}/project/#{project}/build"
end
package(name:, flavor:, input_dir:, output_dir:) click to toggle source
# File lib/kube_deploy_tools/artifact_registry/driver_gcs.rb, line 53
def package(name:, flavor:, input_dir:, output_dir:)
  local_artifact_path = get_local_artifact_path(name: name, flavor: flavor, local_dir: output_dir)
  File.open(local_artifact_path, 'w') do |merged|
    Find.find(input_dir).
      select { |path| path =~ /.*\.yaml$/ }.
      each do |e|
        contents = File.open(e, 'r').read
        contents.each_line do |line|
          merged << line
        end
      end
  end
  local_artifact_path
end
upload(local_dir:, name:, flavor:, project:, build_number:) click to toggle source
# File lib/kube_deploy_tools/artifact_registry/driver_gcs.rb, line 108
def upload(local_dir:, name:, flavor:, project:, build_number:)
  # Pack up contents of each flavor_dir to a correctly named artifact.
  flavor_dir = File.join(local_dir, "#{name}_#{flavor}")

  package(
    name: name,
    flavor: flavor,
    input_dir: flavor_dir,
    output_dir: local_dir,
  )

  local_artifact_path = get_local_artifact_path(
    local_dir: local_dir,
    name: name,
    flavor: flavor,
  )

  registry_artifact_path = get_registry_artifact_path(
    project: project,
    name: name,
    flavor: flavor,
    build_number: build_number,
  )

  Logger.info("Uploading #{local_artifact_path} to #{registry_artifact_path}")
  out, err, status = Shellrunner.run_call('gsutil',  '-m', 'cp', local_artifact_path, registry_artifact_path)
  if !status.success?
    raise "Failed to upload remote deploy artifact from #{local_artifact_path} to #{registry_artifact_path}"
  end

  registry_artifact_path
end