Skip to content

Commit ea0b68b

Browse files
committed
Revert "CHEF-18795-Handle chef-cli env with the chef-cli standalone hab package and chef-dke hab package (#264)"
This reverts commit 7b628e9.
1 parent f8fe97e commit ea0b68b

File tree

5 files changed

+33
-227
lines changed

5 files changed

+33
-227
lines changed

lib/chef-cli/command/env.rb

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

4141
def run(params)
4242
info = {}
43-
product_name = get_product_info
44-
info[product_name] = workstation_info
43+
info[ChefCLI::Dist::PRODUCT] = workstation_info
4544
info["Ruby"] = ruby_info
4645
info["Path"] = paths
4746
ui.msg YAML.dump(info)
4847
end
4948

50-
def get_product_info
51-
if omnibus_install?
52-
ChefCLI::Dist::PRODUCT
53-
elsif habitat_chef_dke?
54-
ChefCLI::Dist::CHEF_DK_CLI_PACKAGE
55-
elsif habitat_standalone?
56-
ChefCLI::Dist::CHEF_CLI_PACKAGE
57-
else
58-
ChefCLI::Dist::PRODUCT
59-
end
60-
end
61-
6249
def workstation_info
63-
info = { "Version" => ChefCLI::VERSION }
50+
info = {}
6451
if omnibus_install?
52+
info["Version"] = ChefCLI::VERSION
6553
info["Home"] = package_home
6654
info["Install Directory"] = omnibus_root
6755
info["Policyfile Config"] = policyfile_config
68-
elsif habitat_chef_dke? || habitat_standalone?
69-
info["Home"] = package_home
70-
info["Install Directory"] = get_pkg_install_path
71-
info["Policyfile Config"] = policyfile_config
7256
else
7357
info["Version"] = "Not running from within Workstation"
7458
end
@@ -89,27 +73,19 @@ def ruby_info
8973

9074
def gem_environment
9175
h = {}
92-
if habitat_install?
93-
# Habitat-specific environment variables
94-
h["GEM ROOT"] = habitat_env(show_warning: true)["GEM_ROOT"]
95-
h["GEM HOME"] = habitat_env(show_warning: true)["GEM_HOME"]
96-
h["GEM PATHS"] = habitat_env(show_warning: true)["GEM_PATH"].split(File::PATH_SEPARATOR)
97-
elsif omnibus_install?
98-
# Omnibus-specific environment variables
99-
h["GEM ROOT"] = omnibus_env["GEM_ROOT"]
100-
h["GEM HOME"] = omnibus_env["GEM_HOME"]
101-
h["GEM PATHS"] = omnibus_env["GEM_PATH"].split(File::PATH_SEPARATOR)
102-
else
103-
# Fallback to system environment variables if neither Omnibus nor Habitat
104-
h["GEM_ROOT"] = ENV["GEM_ROOT"] if ENV.key?("GEM_ROOT")
105-
h["GEM_HOME"] = ENV["GEM_HOME"] if ENV.key?("GEM_HOME")
106-
h["GEM PATHS"] = ENV["GEM_PATH"].split(File::PATH_SEPARATOR) if ENV.key?("GEM_PATH") && !ENV["GEM_PATH"].nil?
107-
end
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
10884
h
10985
end
11086

11187
def paths
112-
env = habitat_install? ? habitat_env(show_warning: true) : omnibus_env
88+
env = habitat_install? ? habitat_env : omnibus_env
11389
env["PATH"].split(File::PATH_SEPARATOR)
11490
rescue OmnibusInstallNotFound
11591
ENV["PATH"].split(File::PATH_SEPARATOR)

lib/chef-cli/dist.rb

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@ 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".freeze
11-
129
# the name of the chef-cli gem
1310
CLI_PRODUCT = "Chef CLI".freeze
1411
CLI_GEM = "chef-cli".freeze

lib/chef-cli/helpers.rb

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

101-
# Function to return the Chef CLI path based on standalone or Chef-DKE-enabled package
102-
def get_pkg_install_path
103-
# Check Chef-DKE package path
104-
chef_dk_path = get_pkg_prefix(ChefCLI::Dist::CHEF_DKE_PKG_NAME)
105-
return chef_dk_path if chef_dk_path
106-
107-
# Check Standalone Chef-CLI package path
108-
chef_cli_path = fetch_chef_cli_version_pkg || get_pkg_prefix(ChefCLI::Dist::HAB_PKG_NAME)
109-
chef_cli_path
110-
111-
rescue => e
112-
ChefCLI::UI.new.err("Error fetching Chef-CLI path: #{e.message}")
113-
nil
114-
end
115-
116-
# Check Standalone Chef-cli environment variable for version
117-
def fetch_chef_cli_version_pkg
118-
chef_cli_version = ENV["CHEF_CLI_VERSION"]
119-
return unless chef_cli_version
120-
121-
pkg_path = get_pkg_prefix("#{ChefCLI::Dist::HAB_PKG_NAME}/#{chef_cli_version}")
122-
return pkg_path if pkg_path && Dir.exist?(pkg_path)
123-
124-
nil
125-
end
126-
127101
# Returns the directory that contains our main symlinks.
128102
# On Mac we place all of our symlinks under /usr/local/bin on other
129103
# platforms they are under /usr/bin
@@ -154,27 +128,14 @@ def git_windows_bin_dir
154128
#
155129
# environment vars for habitat
156130
#
157-
def habitat_env(show_warning: false)
131+
def habitat_env
158132
@habitat_env ||=
159133
begin
160-
if habitat_chef_dke?
161-
bin_pkg_prefix = get_pkg_prefix(ChefCLI::Dist::CHEF_DKE_PKG_NAME)
162-
end
163-
versioned_pkg_prefix = fetch_chef_cli_version_pkg if ENV["CHEF_CLI_VERSION"]
164-
165-
if show_warning && ENV["CHEF_CLI_VERSION"] && !versioned_pkg_prefix
166-
ChefCLI::UI.new.msg("Warning: Habitat package '#{ChefCLI::Dist::HAB_PKG_NAME}' with version '#{ENV["CHEF_CLI_VERSION"]}' not found.")
167-
end
168-
# Use the first available package for bin_pkg_prefix
169-
bin_pkg_prefix ||= versioned_pkg_prefix || get_pkg_prefix(ChefCLI::Dist::HAB_PKG_NAME)
170-
raise "Error: Could not determine the Habitat package prefix. Ensure #{ChefCLI::Dist::HAB_PKG_NAME} is installed and CHEF_CLI_VERSION is set correctly." unless bin_pkg_prefix
171-
172-
# Determine vendor_dir by prioritizing the versioned package first
173-
vendor_pkg_prefix = versioned_pkg_prefix || get_pkg_prefix(ChefCLI::Dist::HAB_PKG_NAME)
174-
raise "Error: Could not determine the vendor package prefix. Ensure #{ChefCLI::Dist::HAB_PKG_NAME} is installed and CHEF_CLI_VERSION is set correctly." unless vendor_pkg_prefix
175-
176-
vendor_dir = File.join(vendor_pkg_prefix, "vendor")
177-
# Construct PATH
134+
# Define the necessary paths for the Habitat environment
135+
# If it is a chef-dke installation, we will use the chef-dke bin path.
136+
# Otherwise, we will use the chef-cli bin path.
137+
bin_pkg_prefix = get_pkg_prefix(habitat_chef_dke? ? ChefCLI::Dist::CHEF_DKE_PKG_NAME : ChefCLI::Dist::HAB_PKG_NAME)
138+
vendor_dir = File.join(get_pkg_prefix(ChefCLI::Dist::HAB_PKG_NAME), "vendor")
178139
path = [
179140
File.join(bin_pkg_prefix, "bin"),
180141
ENV["PATH"].split(File::PATH_SEPARATOR), # Preserve existing PATH
@@ -183,8 +144,8 @@ def habitat_env(show_warning: false)
183144
{
184145
"PATH" => path.join(File::PATH_SEPARATOR),
185146
"GEM_ROOT" => Gem.default_dir, # Default directory for gems
186-
"GEM_HOME" => vendor_dir, # Set only if vendor_dir exists
187-
"GEM_PATH" => vendor_dir, # Set only if vendor_dir exists
147+
"GEM_HOME" => vendor_dir, # GEM_HOME pointing to the vendor directory
148+
"GEM_PATH" => vendor_dir, # GEM_PATH also pointing to the vendor directory
188149
}
189150
end
190151
end
@@ -200,17 +161,22 @@ def omnibus_env
200161
path << git_bin_dir if Dir.exist?(git_bin_dir)
201162
path << git_windows_bin_dir if Dir.exist?(git_windows_bin_dir)
202163
{
203-
"PATH" => path.flatten.uniq.join(File::PATH_SEPARATOR),
204-
"GEM_ROOT" => Gem.default_dir,
205-
"GEM_HOME" => Gem.user_dir,
206-
"GEM_PATH" => Gem.path.join(File::PATH_SEPARATOR),
164+
"PATH" => path.flatten.uniq.join(File::PATH_SEPARATOR),
165+
"GEM_ROOT" => Gem.default_dir,
166+
"GEM_HOME" => Gem.user_dir,
167+
"GEM_PATH" => Gem.path.join(File::PATH_SEPARATOR),
207168
}
208169
end
209170
end
210171

211172
def get_pkg_prefix(pkg_name)
212-
path = `hab pkg path #{pkg_name} 2>/dev/null`.strip
213-
path if !path.empty? && Dir.exist?(path) # Return path only if it exists
173+
path = `hab pkg path #{pkg_name}`.strip
174+
175+
if $?.success? && !path.empty?
176+
path
177+
else
178+
raise "Failed to get pkg_prefix for #{pkg_name}: #{path}"
179+
end
214180
end
215181

216182
def omnibus_expand_path(*paths)

spec/unit/command/env_spec.rb

Lines changed: 0 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
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)
4342
command_instance.ui = ui
4443
end
4544

@@ -82,134 +81,6 @@
8281
end
8382
end
8483
end
85-
86-
describe "when running in a Chef-cli Habitat Standalone package" do
87-
let(:standalone_pkg_base) { "/hab/pkgs/chef/chef-cli" }
88-
let(:standalone_pkg_version) { "1.0.0" }
89-
let(:standalone_pkg_build) { "20240210120000" }
90-
let(:standalone_pkg_path) { "#{standalone_pkg_base}/#{standalone_pkg_version}/#{standalone_pkg_build}" }
91-
92-
let(:ruby_version) { "3.1.0" }
93-
let(:ruby_base) { "/hab/pkgs/core/ruby/#{ruby_version}/20240101000000/lib/ruby/gems" }
94-
let(:cli_gem_home) { "/hab/pkgs/chef/chef-cli/#{standalone_pkg_version}/20240210121000/vendor/bundle/ruby/#{ruby_version}" }
95-
96-
before do
97-
allow(command_instance).to receive(:habitat_install?).and_return(true)
98-
allow(command_instance).to receive(:habitat_standalone?).and_return(true)
99-
allow(command_instance).to receive(:habitat_chef_dke?).and_return(false)
100-
allow(command_instance).to receive(:omnibus_install?).and_return(false)
101-
allow(command_instance).to receive(:get_product_info).and_return(ChefCLI::Dist::CHEF_CLI_PACKAGE)
102-
103-
allow(command_instance).to receive(:get_pkg_install_path).and_return(standalone_pkg_path)
104-
105-
allow(command_instance).to receive(:habitat_env).and_return({
106-
"GEM_ROOT" => ruby_base,
107-
"GEM_HOME" => cli_gem_home,
108-
"GEM_PATH" => cli_gem_home,
109-
"PATH" => "#{standalone_pkg_path}/bin:/usr/local/bin:/usr/bin"
110-
})
111-
112-
command_instance.ui = ui
113-
end
114-
115-
describe "and the env command is run" do
116-
let(:yaml) { YAML.load(ui.output) }
117-
118-
before :each do
119-
run_command
120-
end
121-
122-
it "should include correct chef-cli hab pkg name" do
123-
expect(yaml).to have_key(ChefCLI::Dist::CHEF_CLI_PACKAGE)
124-
end
125-
126-
it "should include correct chef-cli hab pkg version info" do
127-
expect(yaml[ChefCLI::Dist::CHEF_CLI_PACKAGE]["Version"]).to eql ChefCLI::VERSION
128-
end
129-
130-
it "should include correct Habitat installation path" do
131-
expect(yaml[ChefCLI::Dist::CHEF_CLI_PACKAGE]["Install Directory"]).to eql standalone_pkg_path
132-
end
133-
134-
it "should include correct GEM_ROOT path" do
135-
expect(yaml["Ruby"]["RubyGems"]["Gem Environment"]["GEM ROOT"]).to eql ruby_base
136-
end
137-
138-
it "should include correct GEM_HOME path" do
139-
expect(yaml["Ruby"]["RubyGems"]["Gem Environment"]["GEM HOME"]).to eql cli_gem_home
140-
end
141-
142-
it "should include correct GEM_PATH paths" do
143-
expect(yaml["Ruby"]["RubyGems"]["Gem Environment"]["GEM PATHS"]).to eql [cli_gem_home]
144-
end
145-
end
146-
end
147-
148-
describe "when running chef-cli coming with Chef-DKE Habitat package" do
149-
let(:hab_pkg_base) { "/hab/pkgs/chef/chef-development-kit-enterprise" }
150-
let(:hab_pkg_version) { "1.0.0" }
151-
let(:hab_pkg_build) { "20240210120000" }
152-
let(:hab_pkg_path) { "#{hab_pkg_base}/#{hab_pkg_version}/#{hab_pkg_build}" }
153-
154-
let(:ruby_version) { "3.1.0" }
155-
let(:ruby_base) { "/hab/pkgs/core/ruby/#{ruby_version}/20240101000000/lib/ruby/gems" }
156-
let(:cli_gem_home) { "/hab/pkgs/chef/chef-cli/#{hab_pkg_version}/20240210121000/vendor/bundle/ruby/#{ruby_version}" }
157-
158-
before do
159-
# Mock all Habitat-related methods
160-
allow(command_instance).to receive(:habitat_install?).and_return true
161-
allow(command_instance).to receive(:habitat_chef_dke?).and_return true
162-
allow(command_instance).to receive(:habitat_standalone?).and_return false
163-
allow(command_instance).to receive(:omnibus_install?).and_return false
164-
allow(command_instance).to receive(:get_product_info).and_return(ChefCLI::Dist::CHEF_DK_CLI_PACKAGE)
165-
166-
# Mock Habitat package paths
167-
allow(command_instance).to receive(:get_pkg_install_path).and_return(hab_pkg_path)
168-
169-
# Mock habitat_env to reflect correct GEM paths
170-
allow(command_instance).to receive(:habitat_env).and_return({
171-
"GEM_ROOT" => ruby_base,
172-
"GEM_HOME" => cli_gem_home,
173-
"GEM_PATH" => cli_gem_home,
174-
"PATH" => "#{hab_pkg_path}/bin:/usr/local/bin:/usr/bin"
175-
})
176-
177-
command_instance.ui = ui
178-
end
179-
180-
describe "and the env command is run" do
181-
let(:yaml) { YAML.load(ui.output) }
182-
183-
before :each do
184-
run_command
185-
end
186-
187-
it "should include correct product name for Chef-DKE Habitat package" do
188-
expect(yaml).to have_key(ChefCLI::Dist::CHEF_DK_CLI_PACKAGE)
189-
end
190-
191-
it "should include correct version" do
192-
expect(yaml[ChefCLI::Dist::CHEF_DK_CLI_PACKAGE]["Version"]).to eql ChefCLI::VERSION
193-
end
194-
195-
it "should include correct Habitat installation path" do
196-
expect(yaml[ChefCLI::Dist::CHEF_DK_CLI_PACKAGE]["Install Directory"]).to eql hab_pkg_path
197-
end
198-
199-
it "should include correct GEM_ROOT path" do
200-
expect(yaml["Ruby"]["RubyGems"]["Gem Environment"]["GEM ROOT"]).to eql ruby_base
201-
end
202-
203-
it "should include correct GEM_HOME path" do
204-
expect(yaml["Ruby"]["RubyGems"]["Gem Environment"]["GEM HOME"]).to eql cli_gem_home
205-
end
206-
207-
it "should include correct GEM_PATH paths" do
208-
expect(yaml["Ruby"]["RubyGems"]["Gem Environment"]["GEM PATHS"]).to eql [cli_gem_home]
209-
end
210-
end
211-
end
212-
21384
def run_command
21485
command_instance.run_with_default_options(false, command_options)
21586
end

spec/unit/helpers_spec.rb

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,10 @@
113113
let(:chef_dke_path) { "/hab/pkgs/chef/chef-development-kit-enterprise/1.0.0/123" }
114114
let(:cli_hab_path) { "/hab/pkgs/chef/chef-cli/1.0.0/123" }
115115
let(:expected_gem_root) { Gem.default_dir }
116-
let(:expected_path) { [File.join(chef_dke_path, "bin"), "/usr/bin:/bin"].flatten }
116+
let(:expected_path) { %W{#{chef_dke_path}/bin /usr/bin:/bin} }
117117
let(:expected_env) do
118118
{
119-
"PATH" => expected_path.join(File::PATH_SEPARATOR),
119+
"PATH" => expected_path.join(File::PATH_SEPARATOR) ,
120120
"GEM_ROOT" => expected_gem_root,
121121
"GEM_HOME" => "#{cli_hab_path}/vendor",
122122
"GEM_PATH" => "#{cli_hab_path}/vendor",
@@ -127,18 +127,14 @@
127127
allow(ChefCLI::Helpers).to receive(:habitat_chef_dke?).and_return true
128128
allow(ChefCLI::Helpers).to receive(:habitat_standalone?).and_return false
129129
allow(ENV).to receive(:[]).with("PATH").and_return("/usr/bin:/bin")
130-
allow(ENV).to receive(:[]).with("CHEF_CLI_VERSION").and_return(nil)
131-
allow(Dir).to receive(:exist?).with("#{cli_hab_path}/vendor").and_return(true) # <-- Add this line
132130
end
133131

134132
it "should return the habitat env" do
135-
allow(ChefCLI::Helpers).to receive(:fetch_chef_cli_version_pkg).and_return(nil) # Ensure no version override
136133
expect(ChefCLI::Helpers).to receive(:get_pkg_prefix).with("chef/chef-development-kit-enterprise").and_return(chef_dke_path)
137134
expect(ChefCLI::Helpers).to receive(:get_pkg_prefix).with("chef/chef-cli").and_return(cli_hab_path)
138135

139136
expect(ChefCLI::Helpers.habitat_env).to eq(expected_env)
140137
end
141138
end
142-
143139
end
144140
end

0 commit comments

Comments
 (0)