Skip to content

Commit 20f39e4

Browse files
Merge pull request #340 from Tensibai/Extendable-Products
Allow to extend the supported products of mixlib-install
2 parents 69d1464 + eff6c44 commit 20f39e4

File tree

8 files changed

+65
-8
lines changed

8 files changed

+65
-8
lines changed

README.md

+19
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,25 @@ Bourne install script (`install.sh`) supports `http_proxy`, `https_proxy`, `ftp_
254254

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

257+
258+
### Extending for other products
259+
260+
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):
261+
262+
```
263+
product "cinc" do
264+
product_name "Cinc Infra Client"
265+
package_name "cinc-client"
266+
api_url "https://packages.cinc.sh"
267+
end
268+
```
269+
270+
Then set an environment variable to load them like this on linux:
271+
272+
`export EXTRA_PRODUCTS_FILE=/path/to/your/file.rb`
273+
274+
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.
275+
257276
## Development
258277
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.
259278

lib/mixlib/install/backend/package_router.rb

+1-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ module Mixlib
3030
class Install
3131
class Backend
3232
class PackageRouter < Base
33-
ENDPOINT = Mixlib::Install::Dist::PRODUCT_ENDPOINT.freeze
3433

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

@@ -269,7 +268,7 @@ def generate_chef_standard_path(channel, project, version, platform, platform_ve
269268
end
270269

271270
def endpoint
272-
@endpoint ||= ENV.fetch("PACKAGE_ROUTER_ENDPOINT", ENDPOINT)
271+
@endpoint ||= PRODUCT_MATRIX.lookup(options.product_name, options.product_version).api_url
273272
end
274273

275274
def omnibus_project

lib/mixlib/install/options.rb

+12-5
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class Install
2626
class Options
2727
class InvalidOptions < ArgumentError; end
2828

29-
attr_reader :options, :errors, :original_platform_version
29+
attr_reader :options, :errors, :original_platform_version, :supported_product_names
3030

3131
SUPPORTED_ARCHITECTURES = %w{
3232
aarch64
@@ -46,8 +46,6 @@ class InvalidOptions < ArgumentError; end
4646
:unstable,
4747
]
4848

49-
SUPPORTED_PRODUCT_NAMES = PRODUCT_MATRIX.products
50-
5149
SUPPORTED_SHELL_TYPES = [
5250
:ps1,
5351
:sh,
@@ -77,6 +75,15 @@ def initialize(options)
7775
# Store original options in cases where we must remap
7876
@original_platform_version = options[:platform_version]
7977

78+
# Eval extra products definition
79+
extra_products = ENV.fetch("EXTRA_PRODUCTS_FILE", nil)
80+
unless extra_products.nil?
81+
PRODUCT_MATRIX.instance_eval(::File.read(extra_products), extra_products)
82+
end
83+
84+
# Store supported product names
85+
@supported_product_names = PRODUCT_MATRIX.products
86+
8087
resolve_platform_version_compatibility_mode!
8188

8289
map_windows_versions!
@@ -189,10 +196,10 @@ def validate_architecture
189196
end
190197

191198
def validate_product_names
192-
unless SUPPORTED_PRODUCT_NAMES.include? product_name
199+
unless @supported_product_names.include? product_name
193200
errors << <<-EOS
194201
Unknown product name #{product_name}.
195-
Must be one of: #{SUPPORTED_PRODUCT_NAMES.join(", ")}
202+
Must be one of: #{@supported_product_names.join(", ")}
196203
EOS
197204
end
198205
end

lib/mixlib/install/product.rb

+3
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ def initialize(key, &block)
3636
:omnibus_project,
3737
:github_repo,
3838
:downloads_product_page_url,
39+
:api_url,
3940
]
4041

4142
#
@@ -86,6 +87,8 @@ def default_value_for(prop)
8687
"#{Mixlib::Install::Dist::DOWNLOADS_PAGE}/#{product_key}"
8788
when :github_repo
8889
"#{Mixlib::Install::Dist::GITHUB_ORG}/#{product_key}"
90+
when :api_url
91+
ENV.fetch("PACKAGE_ROUTER_ENDPOINT", Mixlib::Install::Dist::PRODUCT_ENDPOINT)
8992
else
9093
nil
9194
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
product "cinc" do
2+
product_name "Cinc Infra Client"
3+
package_name "cinc-client"
4+
api_url "https://packages.cinc.sh"
5+
end

spec/spec_helper.rb

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

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

1415
RSpec.configure do |config|
1516
config.filter_run focus: true

spec/unit/mixlib/install/backend_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def check_url(url)
5757
url.include?("solaris2/5.9")
5858
expect(url).to include(Mixlib::Install::Backend::PackageRouter::COMPAT_DOWNLOAD_URL_ENDPOINT)
5959
else
60-
expect(url).to include(Mixlib::Install::Backend::PackageRouter::ENDPOINT)
60+
expect(url).to include(Mixlib::Install::Dist::PRODUCT_ENDPOINT)
6161
end
6262
end
6363

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
require "spec_helper"
2+
require "mixlib/install"
3+
require "mixlib/install/product"
4+
5+
context "With extra distribution Environment variable" do
6+
let(:mi) do
7+
with_modified_env(EXTRA_PRODUCTS_FILE: EXTRA_FILE) do
8+
Mixlib::Install.new(product_name: "cinc", channel: :stable)
9+
end
10+
end
11+
12+
it "Doesn't raise error" do
13+
expect { mi }.not_to raise_error
14+
end
15+
16+
it "Should include cinc as allowed product" do
17+
expect(mi.options.supported_product_names).to include("cinc")
18+
end
19+
20+
it "Should get the product specific URL" do
21+
expect(PRODUCT_MATRIX.lookup("cinc").api_url).to match("https://packages.cinc.sh")
22+
end
23+
end

0 commit comments

Comments
 (0)