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

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



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# 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) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
# 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:) ⇒ Object



49
50
51
# 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) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
# 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:) ⇒ 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



41
42
43
44
45
46
47
# 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:) ⇒ Object



32
33
34
35
36
37
38
39
# 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:) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# 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:) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# 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