Class: Mixlib::Install::Backend::PackageRouter

Inherits:
Base
  • Object
show all
Defined in:
lib/mixlib/install/backend/package_router.rb

Constant Summary collapse

COMPAT_DOWNLOAD_URL_ENDPOINT =
"http://packages.chef.io".freeze
KNOWN_ARCHITECTURES =

Architecture strings that appear as level-2 keys in PM-structure packages responses (platform -> arch -> pm -> …) vs version strings in standard responses (platform -> version -> arch -> …).

%w{x86_64 aarch64 i386 arm64 ppc64 ppc64le s390x universal x86}.freeze

Instance Attribute Summary

Attributes inherited from Base

#options

Instance Method Summary collapse

Methods inherited from Base

#filter_artifacts, #info, #initialize, #normalize_platform, #platform_filters_available?, #windows_artifact_fixup!

Constructor Details

This class inherits a constructor from Mixlib::Install::Backend::Base

Instance Method Details

#artifact_from_licensed_metadataObject

Fetches a single artifact from the licensed API metadata endpoint. Used when a platform is specified, giving the server full context to derive the package manager and return the correct sha256 — no pm needed.



269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/mixlib/install/backend/package_router.rb', line 269

def 
  version = (options.latest_version? || options.partial_version?) ? "latest" : options.product_version
  query = "v=#{version}&p=#{options.platform}&m=#{Util.normalize_architecture(options.architecture)}"
  query += "&pv=#{options.platform_version}" unless options.platform_version.to_s.empty?

  begin
     = get("/#{options.channel}/#{omnibus_project}/metadata?#{query}")
  rescue Net::HTTPServerException => e
    return [] if e.message.match?(/400|404/)

    raise e
  end

  return [] if .nil? || .empty?

  artifact_map = {
    "omnibus.version" => ["version"],
    "omnibus.platform" => options.platform,
    "omnibus.platform_version" => options.platform_version.to_s,
    "omnibus.architecture" => options.architecture,
    "omnibus.project" => omnibus_project,
    "omnibus.license" => "Apache-2.0",
    "omnibus.sha256" => ["sha256"],
    "omnibus.sha1" => .fetch("sha1", ""),
    "omnibus.md5" => .fetch("md5", ""),
  }
  [create_artifact(artifact_map)]
end

#artifacts_for_version(version) ⇒ Array<ArtifactInfo>

Get artifacts for a given version, channel and product_name

Returns:

  • (Array<ArtifactInfo>)

    Array of info about found artifacts



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/mixlib/install/backend/package_router.rb', line 146

def artifacts_for_version(version)
  begin
    if use_licensed_api?
      # Commercial/trial APIs use the packages endpoint which returns metadata for all platforms
      query = "v=#{version}"
      packages_hash = get("/#{options.channel}/#{omnibus_project}/packages?#{query}")
      # Response structure differs between products:
      # - PM-structure products (e.g. chef-ice, inspec-enterprise): platform -> architecture -> package_manager -> package_info
      # - Standard products: platform -> platform_version -> architecture -> package_info
      results = []
      if pm_structure_response?(packages_hash)
        packages_hash.each do |platform, architectures|
          architectures.each do |arch, package_managers|
            pkg_info = package_managers.values.first
            results << {
              "omnibus.version" => pkg_info["version"],
              "omnibus.platform" => platform,
              "omnibus.platform_version" => "",
              "omnibus.architecture" => arch,
              "omnibus.project" => omnibus_project,
              "omnibus.license" => "Apache-2.0",
              "omnibus.sha256" => pkg_info["sha256"],
              "omnibus.sha1" => pkg_info.fetch("sha1", ""),
              "omnibus.md5" => pkg_info.fetch("md5", ""),
            }
          end
        end
      else
        # Standard structure: platform -> platform_version -> architecture -> package_info
        packages_hash.each do |platform, platform_versions|
          platform_versions.each do |platform_version, architectures|
            architectures.each do |arch, pkg_info|
              results << {
                "omnibus.version" => pkg_info["version"],
                "omnibus.platform" => platform,
                "omnibus.platform_version" => platform_version,
                "omnibus.architecture" => arch,
                "omnibus.project" => omnibus_project,
                "omnibus.license" => "Apache-2.0",
                "omnibus.sha256" => pkg_info["sha256"],
                "omnibus.sha1" => pkg_info.fetch("sha1", ""),
                "omnibus.md5" => pkg_info.fetch("md5", ""),
              }
            end
          end
        end
      end
    else
      results = get("/api/v1/#{options.channel}/#{omnibus_project}/#{version}/artifacts")["results"]
      # Merge artifactory properties to a flat Hash
      results.collect! do |result|
        {
          "filename" => result["name"],
        }.merge(
          map_properties(result["properties"])
        )
      end
    end
  rescue Net::HTTPServerException => e
    if e.message.match?(/404/)
      return []
    else
      raise e
    end
  end

  # Convert results to build records
  results.map { |a| create_artifact(a) }
end

#available_artifactsArray<ArtifactInfo>

Create filtered list of artifacts

channel, product name, and product version.

Returns:

  • (Array<ArtifactInfo>)

    list of artifacts for the configured



45
46
47
48
49
50
51
52
53
54
# File 'lib/mixlib/install/backend/package_router.rb', line 45

def available_artifacts
  artifacts = if use_licensed_api? && platform_filters_available?
                
              elsif options.latest_version? || options.partial_version?
                latest_version
              else
                artifacts_for_version(options.product_version)
              end
  windows_artifact_fixup!(artifacts)
end

#available_versionsArray<String>

Gets available versions from Artifactory via AQL. Returning simply the list of versions.

Returns:

  • (Array<String>)

    Array of available versions



61
62
63
64
65
66
67
# File 'lib/mixlib/install/backend/package_router.rb', line 61

def available_versions
  # We are only including a single property, version and that exists
  # under the properties in the following structure:
  # "properties" => [ {"key"=>"omnibus.version", "value"=>"12.13.3"} ]
  ver_list = versions.map { |i| Mixlib::Versioning.parse(extract_version_from_response(i)) }.sort
  ver_list.uniq.map(&:to_s)
end

#create_artifact(artifact_map) ⇒ Object



298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
# File 'lib/mixlib/install/backend/package_router.rb', line 298

def create_artifact(artifact_map)
  # set normalized platform and platform version
  platform, platform_version = normalize_platform(
    artifact_map["omnibus.platform"],
    artifact_map["omnibus.platform_version"]
  )

  # create the standardized file path
  chef_standard_path = generate_chef_standard_path(
    options.channel,
    artifact_map["omnibus.project"],
    artifact_map["omnibus.version"],
    platform,
    platform_version,
    artifact_map["filename"]
  )

  if options.include_metadata?
    # retrieve the metadata using the standardized path
    begin
       = get("#{chef_standard_path}.metadata.json")
      license_content = ["license_content"]
      software_dependencies = .fetch("version_manifest", {})
                                .fetch("software", nil)
    rescue Net::HTTPServerException => e
      if e.message.match?(/404/)
        license_content, software_dependencies = nil
      else
        raise e
      end
    end
  else
    license_content, software_dependencies = nil
  end

  # create the download path with the correct endpoint
  if use_licensed_api?
    # Commercial/trial APIs use the download endpoint with query parameters.

    m_param = Util.normalize_architecture(artifact_map["omnibus.architecture"])
    v_param = artifact_map["omnibus.version"]
    # Use the user's actual platform (e.g. "ubuntu", "el") so the server can derive pm.
    p_param = options.platform || platform
    pv_param = options.platform_version || platform_version
    pv_part = pv_param.to_s.empty? ? "" : "&pv=#{pv_param}"
    download_url = "#{endpoint}/#{options.channel}/#{omnibus_project}/download?p=#{p_param}#{pv_part}&m=#{m_param}&v=#{v_param}&license_id=#{options.license_id}"
  else
    base_url = if use_compat_download_url_endpoint?(platform, platform_version)
                 COMPAT_DOWNLOAD_URL_ENDPOINT
               else
                 endpoint
               end
    download_url = "#{base_url}/#{chef_standard_path}"
  end

  ArtifactInfo.new(
    architecture:          Util.normalize_architecture(artifact_map["omnibus.architecture"]),
    license:               artifact_map["omnibus.license"],
    license_content:       license_content,
    md5:                   artifact_map["omnibus.md5"],
    platform:              platform,
    platform_version:      platform_version,
    product_description:   product_description,
    product_name:          options.product_name,
    sha1:                  artifact_map["omnibus.sha1"],
    sha256:                artifact_map["omnibus.sha256"],
    software_dependencies: software_dependencies,
    url:                   download_url,
    version:               artifact_map["omnibus.version"]
  )
end

#create_http_request(full_path) ⇒ Object



246
247
248
249
250
251
252
# File 'lib/mixlib/install/backend/package_router.rb', line 246

def create_http_request(full_path)
  request = Net::HTTP::Get.new(full_path)

  request.add_field("User-Agent", Util.user_agent_string(options.user_agent_headers))

  request
end

#endpointObject

Public API detection methods for testing



387
388
389
390
391
392
393
394
395
# File 'lib/mixlib/install/backend/package_router.rb', line 387

def endpoint
  @endpoint ||= if use_trial_api?
                  Mixlib::Install::Dist::TRIAL_API_ENDPOINT
                elsif use_commercial_api?
                  Mixlib::Install::Dist::COMMERCIAL_API_ENDPOINT
                else
                  PRODUCT_MATRIX.lookup(options.product_name, options.product_version).api_url
                end
end

#extract_version_from_response(response) ⇒ Object



138
139
140
# File 'lib/mixlib/install/backend/package_router.rb', line 138

def extract_version_from_response(response)
  response["properties"].find { |item| item["key"] == "omnibus.version" }["value"]
end

#get(url) ⇒ Object

GET request



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/mixlib/install/backend/package_router.rb', line 219

def get(url)
  uri = URI.parse(endpoint)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = (uri.scheme == "https")
  full_path = File.join(uri.path, url)

  # Add license_id as query parameter if using commercial or trial API
  if use_licensed_api?
    separator = full_path.include?("?") ? "&" : "?"
    full_path = "#{full_path}#{separator}license_id=#{options.license_id}"
  end

  res = http.request(create_http_request(full_path))
  res.value
  JSON.parse(res.body)
rescue Net::HTTPClientError, Net::HTTPServerError => e
  # Provide helpful error messages for licensed API failures
  if use_trial_api?
    if options.channel != :stable || (options.product_version != :latest && options.product_version.to_sym != :latest)
      raise "Trial API only supports stable channel and latest version. " \
            "Current settings: channel=#{options.channel}, version=#{options.product_version}. " \
            "Error: #{e.message}"
    end
  end
  raise e
end

#latest_versionArray<ArtifactInfo>

Get artifacts for the latest version, channel and product_name When a partial version is set the results will be filtered before return latest version.

Returns:

  • (Array<ArtifactInfo>)

    Array of info about found artifacts



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/mixlib/install/backend/package_router.rb', line 117

def latest_version
  # Trial API only supports "latest" as the version — skip version resolution
  return artifacts_for_version("latest") if use_trial_api?

  product_versions = if options.partial_version?
                       v = options.product_version
                       partial_version = v.end_with?(".") ? v : v + "."
                       versions.find_all { |ver| extract_version_from_response(ver).start_with?(partial_version) }
                     else
                       versions
                     end

  # Use mixlib versioning to parse and sort versions
  ordered_versions = product_versions.sort_by do |v|
    Mixlib::Versioning.parse(extract_version_from_response(v))
  end.reverse

  version = extract_version_from_response(ordered_versions.first)
  artifacts_for_version(version)
end

#pm_structure_response?(packages_hash) ⇒ Boolean

Detects whether a packages API response uses PM-structure format. Standard: platform -> platform_version -> architecture -> pkg_info

Detection: PM-structure has architecture strings (x86_64, aarch64, …) as the second-level keys; standard has version strings (20.04, 18.04, …).

Returns:

  • (Boolean)


259
260
261
262
263
264
# File 'lib/mixlib/install/backend/package_router.rb', line 259

def pm_structure_response?(packages_hash)
  first_platform = packages_hash.values.first
  return false unless first_platform.is_a?(Hash)

  first_platform.keys.any? { |k| KNOWN_ARCHITECTURES.include?(k) }
end

#use_commercial_api?Boolean

Returns:

  • (Boolean)


401
402
403
# File 'lib/mixlib/install/backend/package_router.rb', line 401

def use_commercial_api?
  !options.license_id.nil? && !options.license_id.to_s.empty? && !use_trial_api?
end

#use_compat_download_url_endpoint?(platform, platform_version) ⇒ boolean

For some older platform & platform_version combinations we need to use COMPAT_DOWNLOAD_URL_ENDPOINT since these versions have an OpenSSL version that can not verify the ENDPOINT based urls

Returns:

  • (boolean)

    use compat download url endpoint



377
378
379
380
381
382
383
384
# File 'lib/mixlib/install/backend/package_router.rb', line 377

def use_compat_download_url_endpoint?(platform, platform_version)
  case "#{platform}-#{platform_version}"
  when "freebsd-9", "el-5", "solaris2-5.9", "solaris2-5.10"
    true
  else
    false
  end
end

#use_licensed_api?Boolean

Returns:

  • (Boolean)


405
406
407
# File 'lib/mixlib/install/backend/package_router.rb', line 405

def use_licensed_api?
  use_trial_api? || use_commercial_api?
end

#use_trial_api?Boolean

Returns:

  • (Boolean)


397
398
399
# File 'lib/mixlib/install/backend/package_router.rb', line 397

def use_trial_api?
  !options.license_id.nil? && !options.license_id.to_s.empty? && options.license_id.start_with?("free-", "trial-")
end

#versionsArray<Array<Hash>] Build records for available versions

Get available versions from Artifactory via AQL. Returning the full API response

Returns:

  • (Array<Array<Hash>] Build records for available versions)

    Array<Array<Hash>] Build records for available versions



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/mixlib/install/backend/package_router.rb', line 73

def versions
  # Commercial and trial APIs use a different URL structure
  if use_licensed_api?
    # Response is a JSON array of version strings
    version_list = get("/#{options.channel}/#{omnibus_project}/versions/all")
    # Convert to the expected format with properties
    items = version_list.map do |version|
      { "properties" => [{ "key" => "omnibus.version", "value" => version }] }
    end
  else
    items = get("/api/v1/#{options.channel}/#{omnibus_project}/versions")["results"]
  end

  # Circumvent early when there are no product artifacts in a specific channel
  if items.empty?
    hint = options.license_id ? "" : "For habitat based products please provide a license ID with -L or set CHEF_LICENSE_KEY."
    raise ArtifactsNotFound, <<-EOF
No artifacts found matching criteria.
  product name: #{options.product_name}
  channel: #{options.channel}
  #{hint}
EOF
  end

  # Filter out the partial builds if we are in :unstable channel
  # In other channels we do not need to do this since all builds are
  # always complete. In fact we should not do this since for some arcane
  # builds like Chef Client 10.X we do not have build record created in
  # artifactory.
  if options.channel == :unstable && !use_licensed_api?
    # We check if "artifacts" field contains something since it is only
    # populated with the build record if "artifact.module.build" exists.
    items.reject! { |i| i["artifacts"].nil? }
  end

  items
end