Skip to content

Commit a6251a5

Browse files
committed
handle chef-cli env with the chef-cli hab package and chef-dke hab package
Signed-off-by: nikhil2611 <[email protected]>
1 parent 606159e commit a6251a5

File tree

4 files changed

+131
-9
lines changed

4 files changed

+131
-9
lines changed

lib/chef-cli/command/env.rb

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,39 @@ def initialize(*args)
4040

4141
def run(params)
4242
info = {}
43-
info[ChefCLI::Dist::PRODUCT] = workstation_info
43+
product_name = get_product_info
44+
info[product_name] = workstation_info
4445
info["Ruby"] = ruby_info
4546
info["Path"] = paths
4647
ui.msg YAML.dump(info)
4748
end
4849

50+
def get_product_info
51+
return ChefCLI::Dist::PRODUCT if omnibus_install?
52+
return ChefCLI::Dist::CHEF_DK_CLI_PACKAGE if habitat_chef_dke?
53+
return ChefCLI::Dist::CHEF_CLI_PACKAGE if habitat_standalone?
54+
55+
# Default case when no conditions match
56+
ChefCLI::Dist::PRODUCT
57+
end
58+
4959
def workstation_info
5060
info = {}
5161
if omnibus_install?
5262
info["Version"] = ChefCLI::VERSION
5363
info["Home"] = package_home
5464
info["Install Directory"] = omnibus_root
5565
info["Policyfile Config"] = policyfile_config
66+
elsif habitat_chef_dke?
67+
info["Version"] = ChefCLI::VERSION
68+
info["Home"] = package_home
69+
info["Install Directory"] = get_chef_cli_path
70+
info["Policyfile Config"] = policyfile_config
71+
elsif habitat_standalone?
72+
info["Version"] = ChefCLI::VERSION
73+
info["Home"] = package_home
74+
info["Install Directory"] = get_chef_cli_path
75+
info["Policyfile Config"] = policyfile_config
5676
else
5777
info["Version"] = "Not running from within Workstation"
5878
end
@@ -73,14 +93,22 @@ def ruby_info
7393

7494
def gem_environment
7595
h = {}
76-
h["GEM ROOT"] = omnibus_env["GEM_ROOT"]
77-
h["GEM HOME"] = omnibus_env["GEM_HOME"]
78-
h["GEM PATHS"] = omnibus_env["GEM_PATH"].split(File::PATH_SEPARATOR)
79-
rescue OmnibusInstallNotFound
80-
h["GEM_ROOT"] = ENV["GEM_ROOT"] if ENV.key?("GEM_ROOT")
81-
h["GEM_HOME"] = ENV["GEM_HOME"] if ENV.key?("GEM_HOME")
82-
h["GEM PATHS"] = ENV["GEM_PATH"].split(File::PATH_SEPARATOR) if ENV.key?("GEM_PATH") && !ENV.key?("GEM_PATH").nil?
83-
ensure
96+
if habitat_install?
97+
# Habitat-specific environment variables
98+
h["GEM ROOT"] = habitat_env["GEM_ROOT"]
99+
h["GEM HOME"] = habitat_env["GEM_HOME"]
100+
h["GEM PATHS"] = habitat_env["GEM_PATH"].split(File::PATH_SEPARATOR)
101+
elsif omnibus_install?
102+
# Omnibus-specific environment variables
103+
h["GEM ROOT"] = omnibus_env["GEM_ROOT"]
104+
h["GEM HOME"] = omnibus_env["GEM_HOME"]
105+
h["GEM PATHS"] = omnibus_env["GEM_PATH"].split(File::PATH_SEPARATOR)
106+
else
107+
# Fallback to system environment variables if neither Omnibus nor Habitat
108+
h["GEM_ROOT"] = ENV["GEM_ROOT"] if ENV.key?("GEM_ROOT")
109+
h["GEM_HOME"] = ENV["GEM_HOME"] if ENV.key?("GEM_HOME")
110+
h["GEM PATHS"] = ENV["GEM_PATH"].split(File::PATH_SEPARATOR) if ENV.key?("GEM_PATH") && !ENV["GEM_PATH"].nil?
111+
end
84112
h
85113
end
86114

lib/chef-cli/dist.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ class Dist
66
PRODUCT = "Chef Workstation".freeze
77
PRODUCT_PKG_HOME = "chef-workstation".freeze
88

9+
CHEF_DK_CLI_PACKAGE = "Chef Development Kit Enterprise".freeze
10+
CHEF_CLI_PACKAGE = "Chef-Cli Package".freeze
11+
912
# the name of the chef-cli gem
1013
CLI_PRODUCT = "Chef CLI".freeze
1114
CLI_GEM = "chef-cli".freeze

lib/chef-cli/helpers.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,19 @@ def package_home
9898
end
9999
end
100100

101+
# Function to return the Chef CLI path based on standalone or Chef-DK-enabled package
102+
def get_chef_cli_path
103+
# Check Chef-DK package path
104+
chef_dk_path = get_hab_package_path(ChefCLI::Dist::CHEF_DKE_PKG_NAME)
105+
return chef_dk_path if chef_dk_path && File.exist?(chef_dk_path)
106+
107+
# Check Standalone Chef-CLI package path
108+
chef_cli_path = get_hab_package_path(ChefCLI::Dist::HAB_PKG_NAME)
109+
return chef_cli_path if chef_cli_path && File.exist?(chef_cli_path)
110+
rescue
111+
nil
112+
end
113+
101114
# Returns the directory that contains our main symlinks.
102115
# On Mac we place all of our symlinks under /usr/local/bin on other
103116
# platforms they are under /usr/bin
@@ -232,5 +245,13 @@ def macos?
232245
def hab_pkg_installed?(pkg_name)
233246
`hab pkg list #{pkg_name} 2>/dev/null`.include?(pkg_name) rescue false
234247
end
248+
249+
# Helper function to get the path of the given hab package
250+
def get_hab_package_path(pkg_name)
251+
`hab pkg path #{pkg_name} 2>/dev/null`.strip || nil
252+
rescue
253+
nil
254+
end
255+
235256
end
236257
end

spec/unit/command/env_spec.rb

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
allow(command_instance).to receive(:omnibus_install?).and_return true
4040
allow(command_instance).to receive(:omnibus_embedded_bin_dir).and_return(omnibus_embedded_bin_dir)
4141
allow(command_instance).to receive(:omnibus_bin_dir).and_return(omnibus_bin_dir)
42+
allow(command_instance).to receive(:get_product_info).and_return(ChefCLI::Dist::PRODUCT)
4243
command_instance.ui = ui
4344
end
4445

@@ -81,6 +82,75 @@
8182
end
8283
end
8384
end
85+
86+
describe "when running from a Habitat install (Chef DKE)" do
87+
before do
88+
allow(command_instance).to receive(:habitat_chef_dke?).and_return true
89+
allow(command_instance).to receive(:omnibus_install?).and_return false
90+
allow(command_instance).to receive(:get_product_info).and_return(ChefCLI::Dist::CHEF_DK_CLI_PACKAGE)
91+
allow(command_instance).to receive(:workstation_info).and_return({
92+
"Version" => ChefCLI::VERSION,
93+
"Home" => "/hab/pkgs/chef/chef-dke",
94+
"Install Directory" => "/hab/pkgs/chef/chef-dke/#{ChefCLI::VERSION}",
95+
"Policyfile Config" => "/hab/pkgs/chef/chef-dke/#{ChefCLI::VERSION}/config/policyfile.rb"
96+
})
97+
command_instance.ui = ui
98+
end
99+
100+
describe "and the env command is run" do
101+
let(:yaml) { YAML.load(ui.output) }
102+
before :each do
103+
run_command
104+
end
105+
106+
it "output should be valid yaml" do
107+
expect { yaml }.not_to raise_error
108+
end
109+
110+
it "should include correct Habitat Chef-DKE version info" do
111+
expect(yaml).to have_key ChefCLI::Dist::CHEF_DK_CLI_PACKAGE
112+
expect(yaml[ChefCLI::Dist::CHEF_DK_CLI_PACKAGE]["Version"]).to eql ChefCLI::VERSION
113+
expect(yaml[ChefCLI::Dist::CHEF_DK_CLI_PACKAGE]["Home"]).to eql "/hab/pkgs/chef/chef-dke"
114+
expect(yaml[ChefCLI::Dist::CHEF_DK_CLI_PACKAGE]["Install Directory"]).to eql "/hab/pkgs/chef/chef-dke/#{ChefCLI::VERSION}"
115+
expect(yaml[ChefCLI::Dist::CHEF_DK_CLI_PACKAGE]["Policyfile Config"]).to eql "/hab/pkgs/chef/chef-dke/#{ChefCLI::VERSION}/config/policyfile.rb"
116+
end
117+
end
118+
end
119+
120+
describe "when running from a Habitat install (Standalone)" do
121+
before do
122+
allow(command_instance).to receive(:habitat_standalone?).and_return true
123+
allow(command_instance).to receive(:omnibus_install?).and_return false
124+
allow(command_instance).to receive(:get_product_info).and_return(ChefCLI::Dist::CHEF_CLI_PACKAGE)
125+
allow(command_instance).to receive(:workstation_info).and_return({
126+
"Version" => ChefCLI::VERSION,
127+
"Home" => "/hab/pkgs/chef/chef-cli",
128+
"Install Directory" => "/hab/pkgs/chef/chef-cli/#{ChefCLI::VERSION}",
129+
"Policyfile Config" => "/hab/pkgs/chef/chef-cli/#{ChefCLI::VERSION}/config/policyfile.rb"
130+
})
131+
command_instance.ui = ui
132+
end
133+
134+
describe "and the env command is run" do
135+
let(:yaml) { YAML.load(ui.output) }
136+
before :each do
137+
run_command
138+
end
139+
140+
it "output should be valid yaml" do
141+
expect { yaml }.not_to raise_error
142+
end
143+
144+
it "should include correct Habitat Standalone version info" do
145+
expect(yaml).to have_key ChefCLI::Dist::CHEF_CLI_PACKAGE
146+
expect(yaml[ChefCLI::Dist::CHEF_CLI_PACKAGE]["Version"]).to eql ChefCLI::VERSION
147+
expect(yaml[ChefCLI::Dist::CHEF_CLI_PACKAGE]["Home"]).to eql "/hab/pkgs/chef/chef-cli"
148+
expect(yaml[ChefCLI::Dist::CHEF_CLI_PACKAGE]["Install Directory"]).to eql "/hab/pkgs/chef/chef-cli/#{ChefCLI::VERSION}"
149+
expect(yaml[ChefCLI::Dist::CHEF_CLI_PACKAGE]["Policyfile Config"]).to eql "/hab/pkgs/chef/chef-cli/#{ChefCLI::VERSION}/config/policyfile.rb"
150+
end
151+
end
152+
end
153+
84154
def run_command
85155
command_instance.run_with_default_options(false, command_options)
86156
end

0 commit comments

Comments
 (0)