Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to extend the supported products of mixlib-install #340

Merged
merged 1 commit into from
Sep 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,25 @@ Bourne install script (`install.sh`) supports `http_proxy`, `https_proxy`, `ftp_

Powershell install script (`install.ps1`) supports `http_proxy` passed as a key to `install_command_options`.


### Extending for other products

Create a ruby file in your application and use the product DSL like this (see [product.rb](lib/mixlib/install/product.rb) for available properties):

```
product "cinc" do
product_name "Cinc Infra Client"
package_name "cinc-client"
api_url "https://packages.cinc.sh"
end
```

Then set an environment variable to load them like this on linux:

`export EXTRA_PRODUCTS_FILE=/path/to/your/file.rb`

Calls to mixlib-install now allow to target your new product, assuming the api_url match pacakges api for `/<channel>/<product>/versions` and `/<channel>/<product>/<version>/artifacts` endpoints.

## Development
VCR is a tool that helps cache and replay http responses. When these responses change or when you add more tests you might need to update cached responses. Check out [spec_helper.rb](https://github.com/chef/mixlib-install/blob/master/spec/spec_helper.rb) for instructions on how to do this.

Expand Down
3 changes: 1 addition & 2 deletions lib/mixlib/install/backend/package_router.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ module Mixlib
class Install
class Backend
class PackageRouter < Base
ENDPOINT = Mixlib::Install::Dist::PRODUCT_ENDPOINT.freeze

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

Expand Down Expand Up @@ -269,7 +268,7 @@ def generate_chef_standard_path(channel, project, version, platform, platform_ve
end

def endpoint
@endpoint ||= ENV.fetch("PACKAGE_ROUTER_ENDPOINT", ENDPOINT)
@endpoint ||= PRODUCT_MATRIX.lookup(options.product_name, options.product_version).api_url
end

def omnibus_project
Expand Down
17 changes: 12 additions & 5 deletions lib/mixlib/install/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Install
class Options
class InvalidOptions < ArgumentError; end

attr_reader :options, :errors, :original_platform_version
attr_reader :options, :errors, :original_platform_version, :supported_product_names

SUPPORTED_ARCHITECTURES = %w{
aarch64
Expand All @@ -46,8 +46,6 @@ class InvalidOptions < ArgumentError; end
:unstable,
]

SUPPORTED_PRODUCT_NAMES = PRODUCT_MATRIX.products

SUPPORTED_SHELL_TYPES = [
:ps1,
:sh,
Expand Down Expand Up @@ -77,6 +75,15 @@ def initialize(options)
# Store original options in cases where we must remap
@original_platform_version = options[:platform_version]

# Eval extra products definition
extra_products = ENV.fetch("EXTRA_PRODUCTS_FILE", nil)
unless extra_products.nil?
PRODUCT_MATRIX.instance_eval(::File.read(extra_products), extra_products)
end

# Store supported product names
@supported_product_names = PRODUCT_MATRIX.products

resolve_platform_version_compatibility_mode!

map_windows_versions!
Expand Down Expand Up @@ -189,10 +196,10 @@ def validate_architecture
end

def validate_product_names
unless SUPPORTED_PRODUCT_NAMES.include? product_name
unless @supported_product_names.include? product_name
errors << <<-EOS
Unknown product name #{product_name}.
Must be one of: #{SUPPORTED_PRODUCT_NAMES.join(", ")}
Must be one of: #{@supported_product_names.join(", ")}
EOS
end
end
Expand Down
3 changes: 3 additions & 0 deletions lib/mixlib/install/product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def initialize(key, &block)
:omnibus_project,
:github_repo,
:downloads_product_page_url,
:api_url,
]

#
Expand Down Expand Up @@ -86,6 +87,8 @@ def default_value_for(prop)
"#{Mixlib::Install::Dist::DOWNLOADS_PAGE}/#{product_key}"
when :github_repo
"#{Mixlib::Install::Dist::GITHUB_ORG}/#{product_key}"
when :api_url
ENV.fetch("PACKAGE_ROUTER_ENDPOINT", Mixlib::Install::Dist::PRODUCT_ENDPOINT)
else
nil
end
Expand Down
5 changes: 5 additions & 0 deletions spec/fixtures/extra/extra_distributions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
product "cinc" do
product_name "Cinc Infra Client"
package_name "cinc-client"
api_url "https://packages.cinc.sh"
end
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

# load version manifest support path
VERSION_MANIFEST_DIR = File.expand_path("../support/version_manifests", __FILE__)
EXTRA_FILE = File.join(File.dirname(__FILE__), "/fixtures/extra/extra_distributions.rb")

RSpec.configure do |config|
config.filter_run focus: true
Expand Down
2 changes: 1 addition & 1 deletion spec/unit/mixlib/install/backend_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def check_url(url)
url.include?("solaris2/5.9")
expect(url).to include(Mixlib::Install::Backend::PackageRouter::COMPAT_DOWNLOAD_URL_ENDPOINT)
else
expect(url).to include(Mixlib::Install::Backend::PackageRouter::ENDPOINT)
expect(url).to include(Mixlib::Install::Dist::PRODUCT_ENDPOINT)
end
end

Expand Down
23 changes: 23 additions & 0 deletions spec/unit/mixlib/install/with_extra_products_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require "spec_helper"
require "mixlib/install"
require "mixlib/install/product"

context "With extra distribution Environment variable" do
let(:mi) do
with_modified_env(EXTRA_PRODUCTS_FILE: EXTRA_FILE) do
Mixlib::Install.new(product_name: "cinc", channel: :stable)
end
end

it "Doesn't raise error" do
expect { mi }.not_to raise_error
end

it "Should include cinc as allowed product" do
expect(mi.options.supported_product_names).to include("cinc")
end

it "Should get the product specific URL" do
expect(PRODUCT_MATRIX.lookup("cinc").api_url).to match("https://packages.cinc.sh")
end
end