Class: KubeDeployTools::ArtifactRegistry::Driver::GCS

Inherits:
Base
  • Object
show all
Defined in:
lib/kube_deploy_tools/artifact_registry/driver_gcs.rb

Instance Method Summary collapse

Methods inherited from Base

#get_latest_build_number

Constructor Details

#initialize(config:) ⇒ GCS

Returns a new instance of GCS.



14
15
16
17
18
19
20
21
22
# 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

Instance Method Details

#download(project:, build_number:, flavor:, name:, pre_apply_hook:, output_dir:) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/kube_deploy_tools/artifact_registry/driver_gcs.rb', line 59

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) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/kube_deploy_tools/artifact_registry/driver_gcs.rb', line 75

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:) ⇒ Object



40
41
42
# File 'lib/kube_deploy_tools/artifact_registry/driver_gcs.rb', line 40

def get_artifact_name(name:, flavor:)
  "manifests_#{name}_#{flavor}.yaml"
end

#get_local_artifact_path(name:, flavor:, local_dir:) ⇒ Object



24
25
26
27
28
29
30
# 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:) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/kube_deploy_tools/artifact_registry/driver_gcs.rb', line 32

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.
  #
  "#{@bucket}/project/#{project}/build/#{build_number}/artifact/#{get_artifact_name(name: name, flavor: flavor)}"
end

#package(name:, flavor:, input_dir:, output_dir:) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/kube_deploy_tools/artifact_registry/driver_gcs.rb', line 44

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:) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/kube_deploy_tools/artifact_registry/driver_gcs.rb', line 87

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