Skip to content

Commit 5561d8c

Browse files
committed
Updated the shell-init command to work with habitat and omnibus installations
Signed-off-by: Ashique Saidalavi <[email protected]>
1 parent 840b9a2 commit 5561d8c

File tree

15 files changed

+206
-43
lines changed

15 files changed

+206
-43
lines changed

chef-cli.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ Gem::Specification.new do |gem|
4343
gem.add_dependency "mixlib-shellout", ">= 2.0", "< 4.0"
4444
gem.add_dependency "ffi-yajl", ">= 1.0", "< 3.0"
4545
gem.add_dependency "minitar", "~> 0.6"
46-
gem.add_dependency "chef", "= 18.5.0"
46+
gem.add_dependency "chef", "~> 18.0"
4747
gem.add_dependency "solve", "< 5.0", "> 2.0"
4848
gem.add_dependency "addressable", ">= 2.3.5", "< 2.9"
4949
gem.add_dependency "cookbook-omnifetch", "~> 0.5"

lib/chef-cli/command/env.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ def gem_environment
8585
end
8686

8787
def paths
88-
omnibus_env["PATH"].split(File::PATH_SEPARATOR)
88+
env = habitat_install? ? habitat_env : omnibus_env
89+
env["PATH"].split(File::PATH_SEPARATOR)
8990
rescue OmnibusInstallNotFound
9091
ENV["PATH"].split(File::PATH_SEPARATOR)
9192
end

lib/chef-cli/command/exec.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ class Exec < ChefCLI::Command::Base
3030
def run(params)
3131
# Set ENV directly on the "parent" process (us) before running #exec to
3232
# ensure the custom PATH is honored when finding the command to exec
33-
omnibus_env.each { |var, value| ENV[var] = value }
33+
env = habitat_install? ? habitat_env : omnibus_env
34+
env.each { |var, value| ENV[var] = value }
3435
exec(*params)
3536
raise "Exec failed without an exception, your ruby is buggy" # should never get here
3637
end

lib/chef-cli/command/license.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ def run(params)
7575
else
7676
ChefCLI::Licensing::Base.send(remaining_args[0])
7777
end
78+
rescue ChefLicensing::LicenseKeyFetcher::LicenseKeyNotFetchedError
79+
ui.msg("License key not fetched. Please try again.")
7880
end
7981

8082
def debug?

lib/chef-cli/command/shell_init.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ module Mixlib
2929
module ChefCLI
3030

3131
class ShellCompletionTemplateContext
32+
include ChefCLI::Helpers
3233

3334
def commands
3435
ChefCLI.commands_map.command_specs.inject({}) do |cmd_info, (_key, cmd_spec)|
@@ -40,6 +41,10 @@ def commands
4041
def get_binding
4142
binding
4243
end
44+
45+
def habitat?
46+
habitat_install?
47+
end
4348
end
4449

4550
module Command
@@ -103,7 +108,7 @@ def run(argv)
103108
return 1
104109
end
105110

106-
env = habitat_env.dup
111+
env = (habitat_install? ? habitat_env : omnibus_env).dup
107112
path = env.delete("PATH")
108113
export(shell_name, "PATH", path)
109114
env.each do |var_name, value|

lib/chef-cli/completions/bash.sh.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ _chef_comp() {
22
local COMMANDS="<%= commands.keys.join(' ')-%>"
33
COMPREPLY=($(compgen -W "$COMMANDS" -- ${COMP_WORDS[COMP_CWORD]} ))
44
}
5-
complete -F _chef_comp chef
5+
complete -F _chef_comp <%= habitat? ? "chef-cli" : "chef" %>

lib/chef-cli/completions/chef.fish.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55
set -l chef_commands <%= commands.keys.join(' ') %>;
66

77
<% commands.each do |command, desc| -%>
8-
complete -c chef -f -n "not __fish_seen_subcommand_from $chef_commands" -a <%= command %> -d "<%= desc %>";
8+
complete -c <%= habitat? ? "chef-cli" : "chef" %> -f -n "not __fish_seen_subcommand_from $chef_commands" -a <%= command %> -d "<%= desc %>";
99
<% end -%>

lib/chef-cli/completions/zsh.zsh.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,5 @@ function _chef() {
1717
fi
1818
}
1919

20-
compdef _chef chef
20+
compdef _chef <%= habitat? ? "chef-cli" : "chef" %>
2121

lib/chef-cli/dist.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ class Dist
1010
CLI_PRODUCT = "Chef CLI".freeze
1111
CLI_GEM = "chef-cli".freeze
1212

13+
CHEF_DKE_PKG_NAME = "chef/chef-development-kit-enterprise".freeze
14+
HAB_PKG_NAME = "chef/chef-cli".freeze
15+
1316
# the name of the overall infra product
1417
INFRA_PRODUCT = "Chef Infra".freeze
1518

lib/chef-cli/helpers.rb

Lines changed: 56 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,21 @@ def omnibus_install?
6060
File.exist?(expected_omnibus_root) && File.exist?(File.join(expected_omnibus_root, "version-manifest.json"))
6161
end
6262

63+
# The habitat version of the chef-cli can be installed with standalone or chef-development-kit-enterprise
64+
# This method checks if the habitat version of chef-cli is installed as standalone
65+
def habitat_standalone?
66+
@hab_standalone ||= (hab_pkg_installed?(ChefCLI::Dist::HAB_PKG_NAME) && !habitat_chef_dke?)
67+
end
68+
69+
# This method checks if the habitat version of chef-cli is installed with chef-development-kit-enterprise
70+
def habitat_chef_dke?
71+
@hab_dke ||= hab_pkg_installed?(ChefCLI::Dist::CHEF_DKE_PKG_NAME)
72+
end
73+
74+
def habitat_install?
75+
habitat_chef_dke? || habitat_standalone?
76+
end
77+
6378
def omnibus_root
6479
@omnibus_root ||= omnibus_expand_path(expected_omnibus_root)
6580
end
@@ -110,29 +125,30 @@ def git_windows_bin_dir
110125
@git_windows_bin_dir ||= File.expand_path(File.join(omnibus_root, "embedded", "git", "usr", "bin"))
111126
end
112127

113-
# #
114-
# # environment vars for habitat
115-
# #
116-
# def habitat_env
117-
# @habitat_env ||=
118-
# begin
119-
# # Define the necessary paths for the Habitat environment
120-
# # Custom GEM_HOME within Habitat
121-
# pkg_prefix = get_pkg_prefix
122-
# vendor_dir = File.join(pkg_prefix, "vendor")
123-
# path = [
124-
# File.join(pkg_prefix, "bin"),
125-
# ENV["PATH"].split(File::PATH_SEPARATOR), # Preserve existing PATH
126-
# ].flatten.uniq
127-
128-
# {
129-
# "PATH" => path.join(File::PATH_SEPARATOR),
130-
# "GEM_ROOT" => Gem.default_dir, # Default directory for gems
131-
# "GEM_HOME" => vendor_dir, # GEM_HOME pointing to the vendor directory
132-
# "GEM_PATH" => vendor_dir, # GEM_PATH also pointing to the vendor directory
133-
# }
134-
# end
135-
# end
128+
#
129+
# environment vars for habitat
130+
#
131+
def habitat_env
132+
@habitat_env ||=
133+
begin
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")
139+
path = [
140+
File.join(bin_pkg_prefix, "bin"),
141+
ENV["PATH"].split(File::PATH_SEPARATOR), # Preserve existing PATH
142+
].flatten.uniq
143+
144+
{
145+
"PATH" => path.join(File::PATH_SEPARATOR),
146+
"GEM_ROOT" => Gem.default_dir, # Default directory for gems
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
149+
}
150+
end
151+
end
136152

137153
#
138154
# environment vars for omnibus
@@ -153,17 +169,15 @@ def omnibus_env
153169
end
154170
end
155171

156-
# def get_pkg_prefix
157-
# pkg_origin = "chef"
158-
# pkg_name = "#{pkg_origin}/chef-cli" # Your origin and package name
159-
# path = `hab pkg path #{pkg_name}`.strip
172+
def get_pkg_prefix(pkg_name)
173+
path = `hab pkg path #{pkg_name}`.strip
160174

161-
# if $?.success? && !path.empty?
162-
# path
163-
# else
164-
# raise "Failed to get pkg_prefix for #{pkg_name}: #{path}"
165-
# end
166-
# end
175+
if $?.success? && !path.empty?
176+
path
177+
else
178+
raise "Failed to get pkg_prefix for #{pkg_name}: #{path}"
179+
end
180+
end
167181

168182
def omnibus_expand_path(*paths)
169183
dir = File.expand_path(File.join(paths))
@@ -209,5 +223,14 @@ def reset!
209223
def macos?
210224
!!(RUBY_PLATFORM =~ /darwin/)
211225
end
226+
227+
# @return [Boolean] Checks if a habitat package is installed.
228+
# If habitat itself is not installed, this method will return false.
229+
#
230+
# @api private
231+
#
232+
def hab_pkg_installed?(pkg_name)
233+
`hab pkg list #{pkg_name} 2>/dev/null`.include?(pkg_name) rescue false
234+
end
212235
end
213236
end

0 commit comments

Comments
 (0)