Class: KubeDeployTools::ArtifactRegistry::Driver::Artifactory

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

Instance Method Summary collapse

Constructor Details

#initialize(config:) ⇒ Artifactory

Artifactory configuration is configurable by environment variables by default: export ARTIFACTORY_ENDPOINT=my.storage.server/artifactory export ARTIFACTORY_USERNAME=admin export ARTIFACTORY_PASSWORD=password See github.com/chef/artifactory-client#create-a-connection.



23
24
25
26
27
28
# File 'lib/kube_deploy_tools/artifact_registry/driver_artifactory.rb', line 23

def initialize(config:)
  @config = config

  Artifactory.endpoint = @config.fetch('endpoint', '')
  @repo = @config.fetch('repo', '')
end

Instance Method Details

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



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/kube_deploy_tools/artifact_registry/driver_artifactory.rb', line 87

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)

  registry_artifact_full_path = [
    Artifactory.endpoint,
    @repo,
    registry_artifact_path,
  ].join('/')

  local_artifact_path = download_artifact(registry_artifact_full_path, output_dir)
  local_artifact_path = uncompress_artifact(local_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



131
132
133
134
135
136
137
138
139
140
141
# File 'lib/kube_deploy_tools/artifact_registry/driver_artifactory.rb', line 131

def download_artifact(input_path, output_dir_path)
  uri = URI.parse(input_path)
  filename = File.basename(uri.path)
  output_path = File.join(output_dir_path, filename)
  out, err, status = Shellrunner.run_call('curl', '-o', output_path, input_path, '--silent', '--fail')
  if !status.success?
    raise "Failed to download remote deploy artifact #{uri}"
  end

  output_path
end

#get_artifact_name(name:, flavor:) ⇒ Object



75
76
77
# File 'lib/kube_deploy_tools/artifact_registry/driver_artifactory.rb', line 75

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

#get_latest_build_number(project) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/kube_deploy_tools/artifact_registry/driver_artifactory.rb', line 110

def get_latest_build_number(project)
  project_url = [
    Artifactory.endpoint,
    @repo,
    "#{project}/"
  ].join('/')
  project_builds_html = Shellrunner.run_call('curl', project_url).first
  # store build entries string from html into an array
  build_links_pattern = /(?<=">).+(?=\s{4})/
  build_entries = project_builds_html.scan(build_links_pattern) # example of element: 10/</a>    13-Nov-2017 13:51
  build_number_pattern = /^\d+/
  build_number = build_entries.
    map { |x| x.match(build_number_pattern).to_s.to_i }.
    max.
    to_s
  if build_number.empty?
    raise "Failed to find a valid build number. Project URL: #{project_url}"
  end
  build_number
end

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



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

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



39
40
41
42
43
44
# File 'lib/kube_deploy_tools/artifact_registry/driver_artifactory.rb', line 39

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

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



79
80
81
82
83
84
85
# File 'lib/kube_deploy_tools/artifact_registry/driver_artifactory.rb', line 79

def package(name:, flavor:, input_dir:, output_dir:)
  local_artifact_path = get_local_artifact_path(name: name, flavor: flavor, local_dir: output_dir)

  Shellrunner.check_call('tar', '-C', input_dir, '-czf', local_artifact_path, '.')

  local_artifact_path
end

#uncompress_artifact(input_path, output_dir_path) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
# File 'lib/kube_deploy_tools/artifact_registry/driver_artifactory.rb', line 143

def uncompress_artifact(input_path, output_dir_path)
  dirname = File.basename(input_path).chomp(EXT_TAR_GZ)
  output_path = File.join(output_dir_path, dirname)
  FileUtils.mkdir_p(output_path)
  out, err, status = Shellrunner.run_call('tar', '-xzf', input_path, '-C', output_path)
  if !status.success?
    raise "Failed to uncompress deploy artifact #{input_path}"
  end

  output_path
end

#upload(local_dir:, name:, flavor:, project:, build_number:) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/kube_deploy_tools/artifact_registry/driver_artifactory.rb', line 46

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,
  )
  artifactory_url = "#{Artifactory.endpoint}/#{@repo}/#{registry_artifact_path}"
  Logger.info("Uploading #{local_artifact_path} to #{artifactory_url}")
  artifact = Artifactory::Resource::Artifact.new(local_path: local_artifact_path)
  artifact.upload(@repo, registry_artifact_path)
end