Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions habitat/bin/kitchen
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/bin/bash

origin="praj"
package="chef-test-kitchen-enterprise"
cmd="kitchen"
env_version=$KITCHEN_VERSION # setup in the OS env for the user running the application
pkg_path=$(hab pkg path $origin/$package)
hab_context=$TKE_VERSION # setup in the plan.sh

# When the user does not binlink the hab pkg, hab pkg exec will use this script -> in $pkg_path/bin/kitchen
# $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

# When the user does binlink the hab pkg, kitchen <cmd> will use this script -> in $pkg_path/bin/kitchen
# $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

# Where pkg_path=$(hab pkg path $origin/$package)

if [[ -z "$hab_context" ]]; then
# Not in the hab context
# This script is being executed as the binlinked script hence it is safe to call the hab pkg exec
if [[ -z "$env_version" ]]; then
# Execute latest installed version (not version that was last installed)
echo "KITCHEN_VERSION is not set in the environment; The script is running as a binlinked script (Not in the hab context)"
hab pkg exec $origin/$package $cmd "$@"
else # the KITCHEN_VERSION could be in the form <semver> or <semver/date>
# Do not need to export any paths since the hab pkg exec will have the GEM_PATH and PATH available
# This will effectively call this same script but in the hab context, since TKE_VERSION will be available.
echo "KITCHEN_VERSION: $env_version set in the environment; The script is running as a binlinked script (Not in the hab context)"
hab pkg exec $origin/$package/$env_version $cmd "$@"
fi
else #in hab context
# The script is being executed as hab pkg exec
# The GEM_PATH and PATH will be available as setup in `hab pkg env`
# env_version is available in the hab context for the same user, if running as root pass the env variables with the -E flag.
if [[ -z "$env_version" ]]; then
# Execute latest installed version (not version that was last installed)
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)"
exec hab pkg exec $origin/$package ruby $pkg_path/vendor/bin/$cmd "$@"
else
# the KITCHEN_VERSION could be in the form <semver> or <semver/date>
env_path=$(hab pkg path $origin/$package/$env_version)
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)"
exec hab pkg exec $origin/$package/$env_version ruby $env_path/vendor/bin/$cmd "$@"
fi
fi
12 changes: 12 additions & 0 deletions habitat/bin/kitchen_gem
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash

# expected to be invoked as kitchen install-gem <gem_name>
export GEM_HOME="/hab/kitchen"
env_version=$KITCHEN_VERSION

if [[ -z "$env_version" ]]; then
# it will install the latest version if a new version installed unless the version env variable was set
hab pkg exec $origin/$package gem install -- $@
else
hab pkg exec $origin/$package/$env_version gem install -- $@
fi
40 changes: 11 additions & 29 deletions habitat/plan.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pkg_deps=(
${_chef_client_ruby}
core/coreutils
core/git
chef/chef-cli/5.6.17/20250205114629
)
pkg_svc_user=root

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

build_line "Setting GEM_PATH=$GEM_HOME"
export GEM_PATH="$GEM_HOME"

# these will be available at runtime; after the package is built and installed
set_runtime_env "GEM_HOME" "$GEM_HOME"
set_runtime_env -f "GEM_PATH" "$GEM_HOME"

set_runtime_env "TKE_VERSION" "$pkg_version"
}

do_unpack() {
Expand All @@ -57,39 +64,14 @@ do_build() {
}

do_install() {
export GEM_HOME="$pkg_prefix/vendor"
# The bin/kitchen is the executable that will
cp "habitat/bin/kitchen" "$pkg_prefix/bin"
chmod 755 "$pkg_prefix/bin/kitchen"

export GEM_HOME="$pkg_prefix/vendor"
build_line "Setting GEM_PATH=$GEM_HOME"
export GEM_PATH="$GEM_HOME"
gem install chef-test-kitchen-enterprise-*.gem --no-document
wrap_ruby_kitchen
set_runtime_env "GEM_PATH" "${pkg_prefix}/vendor"
}

wrap_ruby_kitchen() {
local bin="$pkg_prefix/bin/kitchen"
local real_bin="$GEM_HOME/gems/chef-test-kitchen-enterprise-${pkg_version}/bin/kitchen"
wrap_bin_with_ruby "$bin" "$real_bin"
}

wrap_bin_with_ruby() {
local bin="$1"
local real_bin="$2"
build_line "Adding wrapper $bin to $real_bin"
cat <<EOF > "$bin"
#!$(pkg_path_for core/bash)/bin/bash
set -e

# Set binary path that allows chef-test-kitchen-enterprise to use non-Hab pkg binaries
export PATH="/sbin:/usr/sbin:/usr/local/sbin:/usr/local/bin:/usr/bin:/bin:\$PATH"

# Set Ruby paths defined from 'do_setup_environment()'
export GEM_HOME="$pkg_prefix/vendor"
export GEM_PATH="\$GEM_HOME"

exec $(pkg_path_for $_chef_client_ruby)/bin/ruby $real_bin \$@
EOF
chmod -v 755 "$bin"
}

do_strip() {
Expand Down