Skip to content

Commit 7b8b216

Browse files
fix: Include chef-cli as a dep for tke. Include GEM_HOME and GEM_PATH as a part of the runtime_env in the setup. Also include TKE_VERSION to help identify running in the hab context. Since the PATH is set to include the tke/vendor directory in the hab context, it eliminates the need to create a wrapper script to call the kitchen executables.
fix: include the bin/kitchen executable. This identifies if kitchen is called in the hab context or as a binlinked command and appropriately calls the kitchen binary. It will also use the KITCHEN_VERSION of the kitchen hab conponent if set in the env. It uses the ruby bundled with tke to execute kitchen. fix: include the bin/kitchen_gem executable. This will set an install path for kitchen custom plugins outside of the vendor dire, so they do not become unused by a new tke install. Signed-off-by: Prajakta Purohit <purohit@progress.com>
1 parent 48422cf commit 7b8b216

File tree

3 files changed

+68
-29
lines changed

3 files changed

+68
-29
lines changed

habitat/bin/kitchen

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/bin/bash
2+
3+
origin="praj"
4+
package="chef-test-kitchen-enterprise"
5+
cmd="kitchen"
6+
env_version=$KITCHEN_VERSION # setup in the OS env for the user running the application
7+
pkg_path=$(hab pkg path $origin/$package)
8+
hab_context=$TKE_VERSION # setup in the plan.sh
9+
10+
# When the user does not binlink the hab pkg, hab pkg exec will use this script -> in $pkg_path/bin/kitchen
11+
# $pkg_path/bin/kitchen will call $pkg_path/vendor/bin/kitchen which will in turn call $pkg_path/vendor/gems/chef-test-kitchen-enterprise-${pkg_version}/bin/kitchen
12+
13+
# When the user does binlink the hab pkg, kitchen <cmd> will use this script -> in $pkg_path/bin/kitchen
14+
# $pkg_path/bin/kitchen will call $pkg_path/vendor/bin/kitchen which will in turn call $pkg_path/vendor/gems/chef-test-kitchen-enterprise-${pkg_version}/bin/kitchen
15+
16+
# Where pkg_path=$(hab pkg path $origin/$package)
17+
18+
if [[ -z "$hab_context" ]]; then
19+
# Not in the hab context
20+
# This script is being executed as the binlinked script hence it is safe to call the hab pkg exec
21+
if [[ -z "$env_version" ]]; then
22+
# Execute latest installed version (not version that was last installed)
23+
echo "KITCHEN_VERSION is not set in the environment; The script is running as a binlinked script (Not in the hab context)"
24+
hab pkg exec $origin/$package $cmd "$@"
25+
else # the KITCHEN_VERSION could be in the form <semver> or <semver/date>
26+
# Do not need to export any paths since the hab pkg exec will have the GEM_PATH and PATH available
27+
# This will effectively call this same script but in the hab context, since TKE_VERSION will be available.
28+
echo "KITCHEN_VERSION: $env_version set in the environment; The script is running as a binlinked script (Not in the hab context)"
29+
hab pkg exec $origin/$package/$env_version $cmd "$@"
30+
fi
31+
else #in hab context
32+
# The script is being executed as hab pkg exec
33+
# The GEM_PATH and PATH will be available as setup in `hab pkg env`
34+
# env_version is available in the hab context for the same user, if running as root pass the env variables with the -E flag.
35+
if [[ -z "$env_version" ]]; then
36+
# Execute latest installed version (not version that was last installed)
37+
echo "KITCHEN_VERSION is not set in the environment; The script is running as hab pkg exec or calling itself recursively from the binlinked script (In the hab context)"
38+
exec hab pkg exec $origin/$package ruby $pkg_path/vendor/bin/$cmd "$@"
39+
else
40+
# the KITCHEN_VERSION could be in the form <semver> or <semver/date>
41+
env_path=$(hab pkg path $origin/$package/$env_version)
42+
echo "KITCHEN_VERSION: $env_version set in the environment; The script is running as hab pkg exec or calling itself recursively from the binlinked script (In the hab context)"
43+
exec hab pkg exec $origin/$package/$env_version ruby $env_path/vendor/bin/$cmd "$@"
44+
fi
45+
fi

habitat/bin/kitchen_gem

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/bash
2+
3+
# expected to be invoked as kitchen install-gem <gem_name>
4+
export GEM_HOME="/hab/kitchen"
5+
env_version=$KITCHEN_VERSION
6+
7+
if [[ -z "$env_version" ]]; then
8+
# it will install the latest version if a new version installed unless the version env variable was set
9+
hab pkg exec $origin/$package gem install -- $@
10+
else
11+
hab pkg exec $origin/$package/$env_version gem install -- $@
12+
fi

habitat/plan.sh

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ pkg_deps=(
1818
${_chef_client_ruby}
1919
core/coreutils
2020
core/git
21+
chef/chef-cli/5.6.17/20250205114629
2122
)
2223
pkg_svc_user=root
2324

@@ -35,6 +36,12 @@ do_setup_environment() {
3536

3637
build_line "Setting GEM_PATH=$GEM_HOME"
3738
export GEM_PATH="$GEM_HOME"
39+
40+
# these will be available at runtime; after the package is built and installed
41+
set_runtime_env "GEM_HOME" "$GEM_HOME"
42+
set_runtime_env -f "GEM_PATH" "$GEM_HOME"
43+
44+
set_runtime_env "TKE_VERSION" "$pkg_version"
3845
}
3946

4047
do_unpack() {
@@ -57,39 +64,14 @@ do_build() {
5764
}
5865

5966
do_install() {
60-
export GEM_HOME="$pkg_prefix/vendor"
67+
# The bin/kitchen is the executable that will
68+
cp "habitat/bin/kitchen" "$pkg_prefix/bin"
69+
chmod 755 "$pkg_prefix/bin/kitchen"
6170

71+
export GEM_HOME="$pkg_prefix/vendor"
6272
build_line "Setting GEM_PATH=$GEM_HOME"
6373
export GEM_PATH="$GEM_HOME"
6474
gem install chef-test-kitchen-enterprise-*.gem --no-document
65-
wrap_ruby_kitchen
66-
set_runtime_env "GEM_PATH" "${pkg_prefix}/vendor"
67-
}
68-
69-
wrap_ruby_kitchen() {
70-
local bin="$pkg_prefix/bin/kitchen"
71-
local real_bin="$GEM_HOME/gems/chef-test-kitchen-enterprise-${pkg_version}/bin/kitchen"
72-
wrap_bin_with_ruby "$bin" "$real_bin"
73-
}
74-
75-
wrap_bin_with_ruby() {
76-
local bin="$1"
77-
local real_bin="$2"
78-
build_line "Adding wrapper $bin to $real_bin"
79-
cat <<EOF > "$bin"
80-
#!$(pkg_path_for core/bash)/bin/bash
81-
set -e
82-
83-
# Set binary path that allows chef-test-kitchen-enterprise to use non-Hab pkg binaries
84-
export PATH="/sbin:/usr/sbin:/usr/local/sbin:/usr/local/bin:/usr/bin:/bin:\$PATH"
85-
86-
# Set Ruby paths defined from 'do_setup_environment()'
87-
export GEM_HOME="$pkg_prefix/vendor"
88-
export GEM_PATH="\$GEM_HOME"
89-
90-
exec $(pkg_path_for $_chef_client_ruby)/bin/ruby $real_bin \$@
91-
EOF
92-
chmod -v 755 "$bin"
9375
}
9476

9577
do_strip() {

0 commit comments

Comments
 (0)