|
| 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 |
0 commit comments