Skip to content

Commit c82408c

Browse files
author
Przemysław Niekraś
committed
Add Artifactory Fetcher
Allow to search for latest created artifact in artifactory
1 parent ac881b7 commit c82408c

File tree

4 files changed

+104
-7
lines changed

4 files changed

+104
-7
lines changed

lib/omnibus.rb

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,12 @@ module Omnibus
6363
autoload :ThreadPool, "omnibus/thread_pool"
6464
autoload :Licensing, "omnibus/licensing"
6565

66-
autoload :GitFetcher, "omnibus/fetchers/git_fetcher"
67-
autoload :NetFetcher, "omnibus/fetchers/net_fetcher"
68-
autoload :NullFetcher, "omnibus/fetchers/null_fetcher"
69-
autoload :PathFetcher, "omnibus/fetchers/path_fetcher"
70-
autoload :FileFetcher, "omnibus/fetchers/file_fetcher"
66+
autoload :GitFetcher, "omnibus/fetchers/git_fetcher"
67+
autoload :NetFetcher, "omnibus/fetchers/net_fetcher"
68+
autoload :ArtifactoryFetcher, "omnibus/fetchers/artifactory_fetcher"
69+
autoload :NullFetcher, "omnibus/fetchers/null_fetcher"
70+
autoload :PathFetcher, "omnibus/fetchers/path_fetcher"
71+
autoload :FileFetcher, "omnibus/fetchers/file_fetcher"
7172

7273
autoload :ArtifactoryPublisher, "omnibus/publishers/artifactory_publisher"
7374
autoload :NullPublisher, "omnibus/publishers/null_publisher"

lib/omnibus/fetcher.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,9 @@ def self.resolve_version(version, source)
187187

188188
def self.fetcher_class_for_source(source)
189189
if source
190-
if source[:url]
190+
if source[:repository]
191+
ArtifactoryFetcher
192+
elsif source[:url]
191193
NetFetcher
192194
elsif source[:git]
193195
GitFetcher
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#
2+
# Copyright 2012-2018 Chef Software, Inc.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
17+
require "fileutils"
18+
require "omnibus/download_helpers"
19+
20+
module Omnibus
21+
class ArtifactoryFetcher < NetFetcher
22+
private
23+
24+
#
25+
# A find_url is required if the search in artifactory is required to find its file name
26+
#
27+
# @return [String]
28+
#
29+
def find_source_url
30+
require "base64"
31+
require("digest")
32+
require("artifactory")
33+
34+
log.info(:info) { "Searching Artifactory for #{source[:path]}/#{source[:filename_pattern]} in #{source[:repository]} " }
35+
36+
endpoint = source[:endpoint] || ENV["ARTIFACTORY_ENDPOINT"] || nil
37+
raise "Artifactory endpoint not configured" if endpoint.nil?
38+
39+
Artifactory.endpoint = endpoint
40+
Artifactory.api_key = source[:authorization ] if source[:authorization]
41+
42+
unless source.key?(:authorization)
43+
username = ENV["ARTIFACTORY_USERNAME"] || nil
44+
password = ENV["ARTIFACTORY_PASSWORD"] || nil
45+
error_message = "You have to provide either source[:authorization] or environment variables for artifactory client"
46+
raise error_message if username.nil? || password.nil?
47+
48+
source[:authorization] = "Basic #{Base64.encode64("#{username}:#{password}")}"
49+
end
50+
51+
query = <<~EOM
52+
items.find(
53+
{
54+
"repo": {"$eq":"#{source[:repository]}"},
55+
"name": {"$match":"#{source[:filename_pattern]}"},
56+
"path": {"$match":"#{source[:path]}"}
57+
}
58+
).include("name", "repo", "created", "path", "actual_sha1").sort({"$desc" : ["created"]}).limit(1)
59+
EOM
60+
result = Artifactory.post("/api/search/aql", query, "Content-Type" => "text/plain")
61+
results = result["results"]
62+
63+
log.debug(:debug) { "Search Result #{result}" }
64+
65+
raise "Unable to find #{source[:filename_pattern]} in #{source[:repository]}" if results.empty?
66+
67+
artifact = results[0]
68+
69+
source[:url] = "#{Artifactory.endpoint}/#{artifact['repo']}/#{artifact['path']}/#{artifact['name']}"
70+
source[:sha1] = artifact["actual_sha1"]
71+
72+
log.info(:info) { "Found Artifact #{source[:url]} #{source[:sha1]}" }
73+
end
74+
75+
#
76+
# The path on disk to the downloaded asset. The filename is defined by
77+
# +source :cached_name+. If ommited, then it comes from the software's
78+
# +source :url+ value or from the artifactory search result for newest
79+
# artifact
80+
#
81+
# @return [String]
82+
#
83+
def downloaded_file
84+
unless source.key?(:url)
85+
find_source_url
86+
end
87+
88+
filename = source[:cached_name] if source[:cached_name]
89+
filename ||= File.basename(source[:url], "?*")
90+
File.join(Config.cache_dir, filename)
91+
end
92+
end
93+
end

lib/omnibus/software.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,8 @@ def source(val = NULL)
298298
:md5, :sha1, :sha256, :sha512, # hash type - common to all fetchers
299299
:cookie, :warning, :unsafe, :extract, :cached_name, :authorization, # used by net_fetcher
300300
:options, # used by path_fetcher
301-
:submodules # used by git_fetcher
301+
:submodules, # used by git_fetcher
302+
:repository, :filename_pattern, :endpoint # used by artifactory_fetcher
302303
]
303304
unless extra_keys.empty?
304305
raise InvalidValue.new(:source,

0 commit comments

Comments
 (0)