From 6f738cabd2412a285f451d975728ddf2ff2692fe Mon Sep 17 00:00:00 2001 From: nitin sanghi Date: Thu, 29 Aug 2024 11:48:15 +0530 Subject: [PATCH 1/9] Added plan.sh file Signed-off-by: nitin sanghi --- habitat/plan.sh | 58 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 habitat/plan.sh diff --git a/habitat/plan.sh b/habitat/plan.sh new file mode 100644 index 00000000..35192cdc --- /dev/null +++ b/habitat/plan.sh @@ -0,0 +1,58 @@ +pkg_name=chef-cli +pkg_origin=chef +pkg_version="5.6.14" +ruby_pkg="core/ruby31" +pkg_deps=(${ruby_pkg} core/coreutils) +pkg_build_deps=( + core/make + core/sed + core/gcc + core/libarchive + ) +pkg_bin_dirs=(bin) +do_setup_environment() { + build_line 'Setting GEM_HOME="$pkg_prefix/lib"' + export GEM_HOME="$pkg_prefix/lib" + + build_line "Setting GEM_PATH=$GEM_HOME" + export GEM_PATH="$GEM_HOME" +} +do_unpack() { + mkdir -pv "$HAB_CACHE_SRC_PATH/$pkg_dirname" + cp -RT "$PLAN_CONTEXT"/.. "$HAB_CACHE_SRC_PATH/$pkg_dirname/" +} +do_build() { + pushd "$HAB_CACHE_SRC_PATH/$pkg_dirname/" + gem build chef-cli.gemspec + popd +} +do_install() { + pushd "$HAB_CACHE_SRC_PATH/$pkg_dirname/" + gem install chef-cli-*.gem --no-document + popd + wrap_ruby_bin +} +wrap_ruby_bin() { + local bin="$pkg_prefix/bin/$pkg_name" + local real_bin="$GEM_HOME/gems/chef-cli-${pkg_version}/bin/chef-cli" + build_line "Adding wrapper $bin to $real_bin" + cat < "$bin" +#!$(pkg_path_for core/bash)/bin/bash +set -e + +# Set binary path that allows InSpec 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="$GEM_HOME" +export GEM_PATH="$GEM_PATH" + +exec $(pkg_path_for core/ruby31)/bin/ruby $real_bin \$@ +EOF + chmod -v 755 "$bin" +} + + +do_strip() { + return 0 +} \ No newline at end of file From 7a26e94ed1a3e302763888de4371980220fd84d5 Mon Sep 17 00:00:00 2001 From: nitin sanghi Date: Tue, 3 Sep 2024 17:42:23 +0530 Subject: [PATCH 2/9] Change gem file for app bundler on windows and plan.ps1 added Signed-off-by: nitin sanghi --- Gemfile | 43 ++++++++++++------------ chef-cli.gemspec | 55 ++++++++++++++++--------------- habitat/plan.ps1 | 86 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 135 insertions(+), 49 deletions(-) create mode 100644 habitat/plan.ps1 diff --git a/Gemfile b/Gemfile index b6918aaa..b98a246c 100644 --- a/Gemfile +++ b/Gemfile @@ -1,36 +1,35 @@ -source "https://rubygems.org" +source 'https://rubygems.org' gemspec group :test do - gem "rake" - gem "rspec", "~> 3.8" - gem "rspec-expectations", "~> 3.8" - gem "rspec-mocks", "~> 3.8" - gem "cookstyle", "=7.7.2" # this forces dependabot PRs to open which triggers cookstyle CI on the chef generate command - gem "chefstyle", "=1.6.2" - gem "test-kitchen", "=3.5.1" # pinning test-kitchen to 3.5.1 which supports ruby < 3.1 . Need to update this to latest once we update the ruby to 3.1 and chef to 18.x in chef-cli + gem 'chefstyle', '=1.6.2' + gem 'cookstyle', '=7.7.2' # this forces dependabot PRs to open which triggers cookstyle CI on the chef generate command + gem 'rake' + gem 'rspec', '~> 3.8' + gem 'rspec-expectations', '~> 3.8' + gem 'rspec-mocks', '~> 3.8' + gem 'test-kitchen', '=3.5.1' # pinning test-kitchen to 3.5.1 which supports ruby < 3.1 . Need to update this to latest once we update the ruby to 3.1 and chef to 18.x in chef-cli - if Gem::Version.new(RUBY_VERSION) < Gem::Version.new("2.6") - gem "chef-zero", "~> 14" - gem "chef", "~> 15" - gem "chef-utils", "=16.6.14" - end - if Gem::Version.new(RUBY_VERSION) < Gem::Version.new("2.7") - gem "ohai", "~> 16" + if Gem::Version.new(RUBY_VERSION) < Gem::Version.new('2.6') + gem 'chef', '~> 15' + gem 'chef-utils', '=16.6.14' + gem 'chef-zero', '~> 14' end + gem 'ohai', '~> 16' if Gem::Version.new(RUBY_VERSION) < Gem::Version.new('2.7') - gem "simplecov", require: false + gem 'simplecov', require: false end group :development do - gem "pry" - gem "pry-byebug" - gem "rb-readline" + gem 'appbundller' + gem 'pry' + gem 'pry-byebug' + gem 'rb-readline' end group :profile do - gem "stackprof" - gem "stackprof-webnav" - gem "memory_profiler" + gem 'memory_profiler' + gem 'stackprof' + gem 'stackprof-webnav' end diff --git a/chef-cli.gemspec b/chef-cli.gemspec index 2badc276..242ca678 100644 --- a/chef-cli.gemspec +++ b/chef-cli.gemspec @@ -15,43 +15,44 @@ # limitations under the License. # -lib = File.expand_path("lib", __dir__) +lib = File.expand_path('lib', __dir__) $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) -require "chef-cli/version" +require 'chef-cli/version' Gem::Specification.new do |gem| - gem.name = "chef-cli" + gem.name = 'chef-cli' gem.version = ChefCLI::VERSION - gem.authors = ["Chef Software, Inc."] - gem.email = ["info@chef.io"] - gem.description = "A streamlined development and deployment workflow for Chef platform." + gem.authors = ['Chef Software, Inc.'] + gem.email = ['info@chef.io'] + gem.description = 'A streamlined development and deployment workflow for Chef platform.' gem.summary = gem.description - gem.license = "Apache-2.0" - gem.homepage = "https://www.chef.io/" + gem.license = 'Apache-2.0' + gem.homepage = 'https://www.chef.io/' - gem.required_ruby_version = ">= 2.7" + gem.required_ruby_version = '>= 2.7' - gem.files = %w{Rakefile LICENSE} + - Dir.glob("Gemfile*") + # Includes Gemfile and locks - Dir.glob("*.gemspec") + - Dir.glob("{lib,spec}/**/*", File::FNM_DOTMATCH).reject { |f| File.directory?(f) } - gem.executables = %w{ chef-cli } + gem.files = %w[Rakefile LICENSE] + + Dir.glob('Gemfile*') + # Includes Gemfile and locks + Dir.glob('*.gemspec') + + Dir.glob('{lib,spec}/**/*', File::FNM_DOTMATCH).reject { |f| File.directory?(f) } + gem.executables = %w[chef-cli] gem.test_files = gem.files.grep(%r{^spec/}) - gem.require_paths = ["lib"] + gem.require_paths = ['lib'] - gem.add_dependency "mixlib-cli", ">= 1.7", "< 3.0" - gem.add_dependency "mixlib-shellout", ">= 2.0", "< 4.0" - gem.add_dependency "ffi-yajl", ">= 1.0", "< 3.0" - gem.add_dependency "minitar", "~> 0.6" + gem.add_dependency 'ffi-yajl', '>= 1.0', '< 3.0' + gem.add_dependency 'minitar', '~> 0.6' + gem.add_dependency 'mixlib-cli', '>= 1.7', '< 3.0' + gem.add_dependency 'mixlib-shellout', '>= 2.0', '< 4.0' if RUBY_VERSION.match?(/3.1/) - gem.add_dependency "chef", "~> 18.0" + gem.add_dependency 'chef', '~> 18.0' else - gem.add_dependency "chef", "~> 17.0" + gem.add_dependency 'chef', '~> 17.0' end - gem.add_dependency "solve", "< 5.0", "> 2.0" - gem.add_dependency "addressable", ">= 2.3.5", "< 2.9" - gem.add_dependency "cookbook-omnifetch", "~> 0.5" - gem.add_dependency "diff-lcs", ">= 1.0", "< 1.4" # 1.4 changes the output - gem.add_dependency "pastel", "~> 0.7" # used for policyfile differ - gem.add_dependency "license-acceptance", ">= 1.0.11", "< 3" + gem.add_dependency 'addressable', '>= 2.3.5', '< 2.9' + gem.add_dependency 'appbundler' + gem.add_dependency 'cookbook-omnifetch', '~> 0.5' + gem.add_dependency 'diff-lcs', '>= 1.0', '< 1.4' # 1.4 changes the output + gem.add_dependency 'license-acceptance', '>= 1.0.11', '< 3' + gem.add_dependency 'pastel', '~> 0.7' # used for policyfile differ + gem.add_dependency 'solve', '< 5.0', '> 2.0' end diff --git a/habitat/plan.ps1 b/habitat/plan.ps1 new file mode 100644 index 00000000..415020ac --- /dev/null +++ b/habitat/plan.ps1 @@ -0,0 +1,86 @@ +$ErrorActionPreference = "Stop" +$PSDefaultParameterValues['*:ErrorAction']='Stop' + +$pkg_name="chef-cli" +$pkg_origin="chef" +$pkg_version=$(Get-Content "$PLAN_CONTEXT/../VERSION") +$pkg_revision="1" +$pkg_maintainer="The Chef Maintainers " + +$pkg_deps=@( + "chef/ruby31-plus-devkit" + "core/git" +) +$pkg_bin_dirs=@("bin" + "vendor/bin") +$project_root= (Resolve-Path "$PLAN_CONTEXT/../").Path + +function Invoke-SetupEnvironment { + Push-RuntimeEnv -IsPath GEM_PATH "$pkg_prefix/vendor" + + Set-RuntimeEnv APPBUNDLER_ALLOW_RVM "true" # prevent appbundler from clearing out the carefully constructed runtime GEM_PATH + Set-RuntimeEnv FORCE_FFI_YAJL "ext" + Set-RuntimeEnv LANG "en_US.UTF-8" + Set-RuntimeEnv LC_CTYPE "en_US.UTF-8" +} + +function Invoke-Build { + try { + $env:Path += ";c:\\Program Files\\Git\\bin" + Push-Location $project_root + $env:GEM_HOME = "$HAB_CACHE_SRC_PATH/$pkg_dirname/vendor" + + Write-BuildLine " ** Configuring bundler for this build environment" + bundle config --local without integration deploy maintenance + bundle config --local jobs 4 + bundle config --local retry 5 + bundle config --local silence_root_warning 1 + Write-BuildLine " ** Using bundler to retrieve the Ruby dependencies" + bundle install + + gem build chef-cli.gemspec + Write-BuildLine " ** Using gem to install" + gem install chef-cli-*.gem --no-document + + + If ($lastexitcode -ne 0) { Exit $lastexitcode } + } finally { + Pop-Location + } +} + +function Invoke-Install { + Write-BuildLine "** Copy built & cached gems to install directory" + Copy-Item -Path "$HAB_CACHE_SRC_PATH/$pkg_dirname/*" -Destination $pkg_prefix -Recurse -Force -Exclude @("gem_make.out", "mkmf.log", "Makefile", + "*/latest", "latest", + "*/JSON-Schema-Test-Suite", "JSON-Schema-Test-Suite") + + try { + Push-Location $pkg_prefix + bundle config --local gemfile $project_root/Gemfile + Write-BuildLine "** generating binstubs for chef-cli with precise version pins" + Write-BuildLine "** generating binstubs for chef-cli with precise version pins $project_root $pkg_prefix/bin " + Invoke-Expression -Command "appbundler.bat $project_root $pkg_prefix/bin chef-cli" + If ($lastexitcode -ne 0) { Exit $lastexitcode } + Write-BuildLine " ** Running the chef-cli project's 'rake install' to install the path-based gems so they look like any other installed gem." + + If ($lastexitcode -ne 0) { Exit $lastexitcode } + } finally { + Pop-Location + } +} + +function Invoke-After { + # We don't need the cache of downloaded .gem files ... + Remove-Item $pkg_prefix/vendor/cache -Recurse -Force + # We don't need the gem docs. + Remove-Item $pkg_prefix/vendor/doc -Recurse -Force + # We don't need to ship the test suites for every gem dependency, + # only inspec's for package verification. + Get-ChildItem $pkg_prefix/vendor/gems -Filter "spec" -Directory -Recurse -Depth 1 ` + | Where-Object -FilterScript { $_.FullName -notlike "*chef-cli*" } ` + | Remove-Item -Recurse -Force + # Remove the byproducts of compiling gems with extensions + Get-ChildItem $pkg_prefix/vendor/gems -Include @("gem_make.out", "mkmf.log", "Makefile") -File -Recurse ` + | Remove-Item -Force +} \ No newline at end of file From 91c05e41890ed04a5ac26d2fa493cf50e530e437 Mon Sep 17 00:00:00 2001 From: nitin sanghi Date: Fri, 6 Sep 2024 15:17:22 +0530 Subject: [PATCH 3/9] Habitat pkg version sync with version file Signed-off-by: nitin sanghi --- habitat/plan.ps1 | 8 +++++++- habitat/plan.sh | 8 +++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/habitat/plan.ps1 b/habitat/plan.ps1 index 415020ac..58864f5b 100644 --- a/habitat/plan.ps1 +++ b/habitat/plan.ps1 @@ -4,7 +4,6 @@ $PSDefaultParameterValues['*:ErrorAction']='Stop' $pkg_name="chef-cli" $pkg_origin="chef" $pkg_version=$(Get-Content "$PLAN_CONTEXT/../VERSION") -$pkg_revision="1" $pkg_maintainer="The Chef Maintainers " $pkg_deps=@( @@ -15,6 +14,13 @@ $pkg_bin_dirs=@("bin" "vendor/bin") $project_root= (Resolve-Path "$PLAN_CONTEXT/../").Path +function pkg_version { + Get-Content "$SRC_PATH/VERSION" +} + +function Invoke-Before { + Set-PkgVersion +} function Invoke-SetupEnvironment { Push-RuntimeEnv -IsPath GEM_PATH "$pkg_prefix/vendor" diff --git a/habitat/plan.sh b/habitat/plan.sh index 35192cdc..47d460cd 100644 --- a/habitat/plan.sh +++ b/habitat/plan.sh @@ -1,6 +1,5 @@ pkg_name=chef-cli pkg_origin=chef -pkg_version="5.6.14" ruby_pkg="core/ruby31" pkg_deps=(${ruby_pkg} core/coreutils) pkg_build_deps=( @@ -17,6 +16,13 @@ do_setup_environment() { build_line "Setting GEM_PATH=$GEM_HOME" export GEM_PATH="$GEM_HOME" } + +pkg_version() { + cat "$SRC_PATH/VERSION" +} +do_before() { + update_pkg_version +} do_unpack() { mkdir -pv "$HAB_CACHE_SRC_PATH/$pkg_dirname" cp -RT "$PLAN_CONTEXT"/.. "$HAB_CACHE_SRC_PATH/$pkg_dirname/" From 4fe4c3f818b52254361b1a56225593dc64a64df4 Mon Sep 17 00:00:00 2001 From: nitin sanghi Date: Wed, 11 Sep 2024 12:47:10 +0530 Subject: [PATCH 4/9] Removed duplicate dep and check linux gem are not trying to install on windows Signed-off-by: nitin sanghi --- Gemfile | 8 +++++--- chef-cli.gemspec | 1 - 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/Gemfile b/Gemfile index 736ee4d8..35c31929 100644 --- a/Gemfile +++ b/Gemfile @@ -23,7 +23,9 @@ group :development do end group :profile do - gem "stackprof" - gem "stackprof-webnav" - gem "memory_profiler" + unless RUBY_PLATFORM.match?(/mswin|mingw|windows/) + gem "stackprof" + gem "stackprof-webnav" + gem "memory_profiler" + end end \ No newline at end of file diff --git a/chef-cli.gemspec b/chef-cli.gemspec index db6ad53c..ba27ca63 100644 --- a/chef-cli.gemspec +++ b/chef-cli.gemspec @@ -50,5 +50,4 @@ Gem::Specification.new do |gem| gem.add_dependency "diff-lcs", ">= 1.0", "< 1.4" # 1.4 changes the output gem.add_dependency "pastel", "~> 0.7" # used for policyfile differ gem.add_dependency "license-acceptance", ">= 1.0.11", "< 3" - gem.add_dependency "appbundler" end \ No newline at end of file From f18096a37563e0e77239fc67391abf55bd88b4c2 Mon Sep 17 00:00:00 2001 From: nitin sanghi Date: Wed, 18 Sep 2024 16:21:47 +0530 Subject: [PATCH 5/9] Change in linux plan file and Rakefile to add install task Signed-off-by: nitin sanghi --- Rakefile | 3 + habitat/plan.sh | 144 +++++++++++++++++++++++++++++++++--------------- 2 files changed, 104 insertions(+), 43 deletions(-) diff --git a/Rakefile b/Rakefile index 3b92ac1a..f25cfd7f 100644 --- a/Rakefile +++ b/Rakefile @@ -14,6 +14,8 @@ # See the License for the specific language governing permissions and # limitations under the License. # +# +require "bundler/gem_tasks" namespace :style do begin @@ -52,3 +54,4 @@ namespace :style do puts ">>> Gem load error: #{e}, omitting #{task.name}" unless ENV["CI"] end end + diff --git a/habitat/plan.sh b/habitat/plan.sh index 47d460cd..3db4489f 100644 --- a/habitat/plan.sh +++ b/habitat/plan.sh @@ -1,63 +1,121 @@ -pkg_name=chef-cli -pkg_origin=chef -ruby_pkg="core/ruby31" -pkg_deps=(${ruby_pkg} core/coreutils) +_chef_client_ruby="core/ruby31" +pkg_name="chef-cli" +pkg_origin="chef" +pkg_maintainer="The Chef Maintainers " +pkg_description="The Chef CLI" +pkg_license=('Apache-2.0') +pkg_bin_dirs=( + bin + vendor/bin +) pkg_build_deps=( - core/make - core/sed - core/gcc - core/libarchive - ) -pkg_bin_dirs=(bin) -do_setup_environment() { - build_line 'Setting GEM_HOME="$pkg_prefix/lib"' - export GEM_HOME="$pkg_prefix/lib" - - build_line "Setting GEM_PATH=$GEM_HOME" - export GEM_PATH="$GEM_HOME" -} + core/make + core/gcc + core/git + core/libarchive +) +pkg_deps=( + $_chef_client_ruby + core/coreutils +) +pkg_svc_user=root pkg_version() { - cat "$SRC_PATH/VERSION" + cat "${SRC_PATH}/VERSION" } + do_before() { + do_default_before update_pkg_version + # We must wait until we update the pkg_version to use the pkg_version + pkg_filename="${pkg_name}-${pkg_version}.tar.gz" +} + +do_download() { + build_line "Locally creating archive of latest repository commit at ${HAB_CACHE_SRC_PATH}/${pkg_filename}" + # source is in this repo, so we're going to create an archive from the + # appropriate path within the repo and place the generated tarball in the + # location expected by do_unpack + ( cd "${SRC_PATH}" || exit_with "unable to enter hab-src directory" 1 + git archive --prefix="${pkg_name}-${pkg_version}/" --output="${HAB_CACHE_SRC_PATH}/${pkg_filename}" HEAD + ) +} + +do_verify() { + build_line "Skipping checksum verification on the archive we just created." + return 0 } -do_unpack() { - mkdir -pv "$HAB_CACHE_SRC_PATH/$pkg_dirname" - cp -RT "$PLAN_CONTEXT"/.. "$HAB_CACHE_SRC_PATH/$pkg_dirname/" + +do_setup_environment() { + push_runtime_env GEM_PATH "${pkg_prefix}/vendor" + + set_runtime_env APPBUNDLER_ALLOW_RVM "true" # prevent appbundler from clearing out the carefully constructed runtime GEM_PATH + set_runtime_env LANG "en_US.UTF-8" + set_runtime_env LC_CTYPE "en_US.UTF-8" } + +do_prepare() { + export GEM_HOME="${pkg_prefix}/vendor" + export CPPFLAGS="${CPPFLAGS} ${CFLAGS}" + + ( cd "$CACHE_PATH" + bundle config --local jobs "$(nproc)" + bundle config --local without server docgen maintenance pry travis integration ci chefstyle + bundle config --local shebang "$(pkg_path_for "$_chef_client_ruby")/bin/ruby" + bundle config --local retry 5 + bundle config --local silence_root_warning 1 + ) + + build_line "Setting link for /usr/bin/env to 'coreutils'" + if [ ! -f /usr/bin/env ]; then + ln -s "$(pkg_interpreter_for core/coreutils bin/env)" /usr/bin/env + fi +} + do_build() { - pushd "$HAB_CACHE_SRC_PATH/$pkg_dirname/" - gem build chef-cli.gemspec - popd + ( cd "$CACHE_PATH" || exit_with "unable to enter hab-cache directory" 1 + build_line "Installing gem dependencies ..." + bundle install --jobs=3 --retry=3 + build_line "Installing gems from git repos properly ..." + build_line "Installing this project's gems ..." + bundle exec rake install:local + gem install chef-utils chef-config appbundler + ) } + do_install() { - pushd "$HAB_CACHE_SRC_PATH/$pkg_dirname/" - gem install chef-cli-*.gem --no-document - popd - wrap_ruby_bin + ( cd "$pkg_prefix" || exit_with "unable to enter pkg prefix directory" 1 + export BUNDLE_GEMFILE="${CACHE_PATH}/Gemfile" + build_line "** fixing binstub shebangs" + fix_interpreter "${pkg_prefix}/vendor/bin/*" "$_chef_client_ruby" bin/ruby + export BUNDLE_GEMFILE="${CACHE_PATH}/Gemfile" + for gem in chef-cli; do + build_line "** generating binstubs for $gem with precise version pins" + appbundler $CACHE_PATH $pkg_prefix/bin $gem + done + ) } -wrap_ruby_bin() { - local bin="$pkg_prefix/bin/$pkg_name" - local real_bin="$GEM_HOME/gems/chef-cli-${pkg_version}/bin/chef-cli" - build_line "Adding wrapper $bin to $real_bin" - cat < "$bin" -#!$(pkg_path_for core/bash)/bin/bash -set -e -# Set binary path that allows InSpec to use non-Hab pkg binaries -export PATH="/sbin:/usr/sbin:/usr/local/sbin:/usr/local/bin:/usr/bin:/bin:\$PATH" +do_after() { + build_line "Trimming the fat ..." -# Set Ruby paths defined from 'do_setup_environment()' -export GEM_HOME="$GEM_HOME" -export GEM_PATH="$GEM_PATH" + # We don't need the cache of downloaded .gem files ... + rm -rf "$pkg_prefix/vendor/cache" -exec $(pkg_path_for core/ruby31)/bin/ruby $real_bin \$@ -EOF - chmod -v 755 "$bin" + # We don't need the gem docs. + rm -rf "$pkg_prefix/vendor/doc" + # We don't need to ship the test suites for every gem dependency, + # only Chef's for package verification. + find "$pkg_prefix/vendor/gems" -name spec -type d | grep -v "chef-cli-${pkg_version}" \ + | while read spec_dir; do rm -rf "$spec_dir"; done } +do_end() { + if [ "$(readlink /usr/bin/env)" = "$(pkg_interpreter_for core/coreutils bin/env)" ]; then + build_line "Removing the symlink we created for '/usr/bin/env'" + rm /usr/bin/env + fi +} do_strip() { return 0 From b92f67590d5273b80ddd1176226c1a9e2b1bb16f Mon Sep 17 00:00:00 2001 From: nikhil2611 Date: Tue, 8 Oct 2024 14:14:01 +0530 Subject: [PATCH 6/9] added hab path aware code changes Signed-off-by: nikhil2611 --- habitat/plan.sh | 4 ++-- lib/chef-cli/command/exec.rb | 6 +++++- lib/chef-cli/helpers.rb | 20 +++++++++++--------- 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/habitat/plan.sh b/habitat/plan.sh index 3db4489f..110d81be 100644 --- a/habitat/plan.sh +++ b/habitat/plan.sh @@ -1,6 +1,6 @@ _chef_client_ruby="core/ruby31" pkg_name="chef-cli" -pkg_origin="chef" +pkg_origin="ngupta26" pkg_maintainer="The Chef Maintainers " pkg_description="The Chef CLI" pkg_license=('Apache-2.0') @@ -37,7 +37,7 @@ do_download() { # appropriate path within the repo and place the generated tarball in the # location expected by do_unpack ( cd "${SRC_PATH}" || exit_with "unable to enter hab-src directory" 1 - git archive --prefix="${pkg_name}-${pkg_version}/" --output="${HAB_CACHE_SRC_PATH}/${pkg_filename}" HEAD + # git archive --prefix="${pkg_name}-${pkg_version}/" --output="${HAB_CACHE_SRC_PATH}/${pkg_filename}" HEAD ) } diff --git a/lib/chef-cli/command/exec.rb b/lib/chef-cli/command/exec.rb index f1e19bde..ddb14e57 100644 --- a/lib/chef-cli/command/exec.rb +++ b/lib/chef-cli/command/exec.rb @@ -28,9 +28,13 @@ class Exec < ChefCLI::Command::Base banner "Usage: #{ChefCLI::Dist::EXEC} exec SYSTEM_COMMAND" def run(params) + # puts "GEM_ROOT: #{Gem.default_dir}" + # puts "GEM_HOME: #{Gem.user_dir}" + # puts "GEM_PATH: #{Gem.path.inspect}" + # puts "Current PATH: #{ENV['PATH']}" # Set ENV directly on the "parent" process (us) before running #exec to # ensure the custom PATH is honored when finding the command to exec - omnibus_env.each { |var, value| ENV[var] = value } + habitat_env.each { |var, value| ENV[var] = value } exec(*params) raise "Exec failed without an exception, your ruby is buggy" # should never get here end diff --git a/lib/chef-cli/helpers.rb b/lib/chef-cli/helpers.rb index 6c72237e..3026d891 100644 --- a/lib/chef-cli/helpers.rb +++ b/lib/chef-cli/helpers.rb @@ -113,18 +113,20 @@ def git_windows_bin_dir # # environment vars for omnibus # - def omnibus_env - @omnibus_env ||= + def habitat_env + @habitat_env ||= begin - user_bin_dir = File.expand_path(File.join(Gem.user_dir, "bin")) - path = [ omnibus_bin_dir, user_bin_dir, omnibus_embedded_bin_dir, ENV["PATH"].split(File::PATH_SEPARATOR) ] - path << git_bin_dir if Dir.exist?(git_bin_dir) - path << git_windows_bin_dir if Dir.exist?(git_windows_bin_dir) + # Define the necessary paths for the Habitat environment + path = [ + File.join(@pkg_prefix, "bin"), # Path to binaries in your Habitat package + ENV["PATH"].split(File::PATH_SEPARATOR) # Preserve existing PATH + ] + { "PATH" => path.flatten.uniq.join(File::PATH_SEPARATOR), - "GEM_ROOT" => Gem.default_dir, - "GEM_HOME" => Gem.user_dir, - "GEM_PATH" => Gem.path.join(File::PATH_SEPARATOR), + "GEM_ROOT" => Gem.default_dir, # Default directory for gems + "GEM_HOME" => File.join(@pkg_prefix, "vendor"), # Custom GEM_HOME within Habitat + "GEM_PATH" => File.join(@pkg_prefix, "vendor") # GEM_PATH pointing to the vendor directory } end end From 1422e98df0adace75da6a14726021f91bd6a5c8a Mon Sep 17 00:00:00 2001 From: nikhil2611 Date: Tue, 8 Oct 2024 14:14:50 +0530 Subject: [PATCH 7/9] fixing the git issue Signed-off-by: nikhil2611 --- habitat/plan.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/habitat/plan.sh b/habitat/plan.sh index 110d81be..4cb5abbc 100644 --- a/habitat/plan.sh +++ b/habitat/plan.sh @@ -37,7 +37,7 @@ do_download() { # appropriate path within the repo and place the generated tarball in the # location expected by do_unpack ( cd "${SRC_PATH}" || exit_with "unable to enter hab-src directory" 1 - # git archive --prefix="${pkg_name}-${pkg_version}/" --output="${HAB_CACHE_SRC_PATH}/${pkg_filename}" HEAD + git archive --prefix="${pkg_name}-${pkg_version}/" --output="${HAB_CACHE_SRC_PATH}/${pkg_filename}" HEAD ) } From 86cd859b6225bf0c708f161b6848c6ef13c98f4d Mon Sep 17 00:00:00 2001 From: nikhil2611 Date: Wed, 9 Oct 2024 16:33:48 +0530 Subject: [PATCH 8/9] added the hab env and few command fix for hab path Signed-off-by: nikhil2611 --- lib/chef-cli/command/env.rb | 14 +++--- lib/chef-cli/command/exec.rb | 4 -- lib/chef-cli/helpers.rb | 91 +++++++++++++++++++++--------------- 3 files changed, 61 insertions(+), 48 deletions(-) diff --git a/lib/chef-cli/command/env.rb b/lib/chef-cli/command/env.rb index d1aed4b2..c44e4c4e 100644 --- a/lib/chef-cli/command/env.rb +++ b/lib/chef-cli/command/env.rb @@ -73,19 +73,19 @@ def ruby_info def gem_environment h = {} - h["GEM ROOT"] = omnibus_env["GEM_ROOT"] - h["GEM HOME"] = omnibus_env["GEM_HOME"] - h["GEM PATHS"] = omnibus_env["GEM_PATH"].split(File::PATH_SEPARATOR) - rescue OmnibusInstallNotFound + # h["GEM ROOT"] = omnibus_env["GEM_ROOT"] + # h["GEM HOME"] = omnibus_env["GEM_HOME"] + # h["GEM PATHS"] = omnibus_env["GEM_PATH"].split(File::PATH_SEPARATOR) + # rescue OmnibusInstallNotFound h["GEM_ROOT"] = ENV["GEM_ROOT"] if ENV.key?("GEM_ROOT") h["GEM_HOME"] = ENV["GEM_HOME"] if ENV.key?("GEM_HOME") h["GEM PATHS"] = ENV["GEM_PATH"].split(File::PATH_SEPARATOR) if ENV.key?("GEM_PATH") && !ENV.key?("GEM_PATH").nil? - ensure - h + # ensure + # h end def paths - omnibus_env["PATH"].split(File::PATH_SEPARATOR) + habitat_env["PATH"].split(File::PATH_SEPARATOR) rescue OmnibusInstallNotFound ENV["PATH"].split(File::PATH_SEPARATOR) end diff --git a/lib/chef-cli/command/exec.rb b/lib/chef-cli/command/exec.rb index ddb14e57..9b86bbea 100644 --- a/lib/chef-cli/command/exec.rb +++ b/lib/chef-cli/command/exec.rb @@ -28,10 +28,6 @@ class Exec < ChefCLI::Command::Base banner "Usage: #{ChefCLI::Dist::EXEC} exec SYSTEM_COMMAND" def run(params) - # puts "GEM_ROOT: #{Gem.default_dir}" - # puts "GEM_HOME: #{Gem.user_dir}" - # puts "GEM_PATH: #{Gem.path.inspect}" - # puts "Current PATH: #{ENV['PATH']}" # Set ENV directly on the "parent" process (us) before running #exec to # ensure the custom PATH is honored when finding the command to exec habitat_env.each { |var, value| ENV[var] = value } diff --git a/lib/chef-cli/helpers.rb b/lib/chef-cli/helpers.rb index 3026d891..7c6e21ad 100644 --- a/lib/chef-cli/helpers.rb +++ b/lib/chef-cli/helpers.rb @@ -53,24 +53,24 @@ def stderr # # Locates the omnibus directories # - def omnibus_install? - # We also check if the location we're running from (omnibus_root is relative to currently-running ruby) - # includes the version manifest that omnibus packages ship with. If it doesn't, then we're running locally - # or out of a gem - so not as an 'omnibus install' - File.exist?(expected_omnibus_root) && File.exist?(File.join(expected_omnibus_root, "version-manifest.json")) - end + # def omnibus_install? + # # We also check if the location we're running from (omnibus_root is relative to currently-running ruby) + # # includes the version manifest that omnibus packages ship with. If it doesn't, then we're running locally + # # or out of a gem - so not as an 'omnibus install' + # File.exist?(expected_omnibus_root) && File.exist?(File.join(expected_omnibus_root, "version-manifest.json")) + # end - def omnibus_root - @omnibus_root ||= omnibus_expand_path(expected_omnibus_root) - end + # def omnibus_root + # @omnibus_root ||= omnibus_expand_path(expected_omnibus_root) + # end - def omnibus_bin_dir - @omnibus_bin_dir ||= omnibus_expand_path(omnibus_root, "bin") - end + # def omnibus_bin_dir + # @omnibus_bin_dir ||= omnibus_expand_path(omnibus_root, "bin") + # end - def omnibus_embedded_bin_dir - @omnibus_embedded_bin_dir ||= omnibus_expand_path(omnibus_root, "embedded", "bin") - end + # def omnibus_embedded_bin_dir + # @omnibus_embedded_bin_dir ||= omnibus_expand_path(omnibus_root, "embedded", "bin") + # end def package_home @package_home ||= begin @@ -110,39 +110,56 @@ def git_windows_bin_dir @git_windows_bin_dir ||= File.expand_path(File.join(omnibus_root, "embedded", "git", "usr", "bin")) end + def initialize + @pkg_prefix = get_pkg_prefix + end + # - # environment vars for omnibus + # environment vars for habitat # def habitat_env @habitat_env ||= - begin - # Define the necessary paths for the Habitat environment - path = [ - File.join(@pkg_prefix, "bin"), # Path to binaries in your Habitat package - ENV["PATH"].split(File::PATH_SEPARATOR) # Preserve existing PATH - ] - - { - "PATH" => path.flatten.uniq.join(File::PATH_SEPARATOR), - "GEM_ROOT" => Gem.default_dir, # Default directory for gems - "GEM_HOME" => File.join(@pkg_prefix, "vendor"), # Custom GEM_HOME within Habitat - "GEM_PATH" => File.join(@pkg_prefix, "vendor") # GEM_PATH pointing to the vendor directory - } - end + begin + # Define the necessary paths for the Habitat environment + # Custom GEM_HOME within Habitat + vendor_dir = File.join(@pkg_prefix, "vendor") + path = [ + File.join(@pkg_prefix, "bin"), + ENV["PATH"].split(File::PATH_SEPARATOR) # Preserve existing PATH + ].flatten.uniq + + { + "PATH" => path.join(File::PATH_SEPARATOR), + "GEM_ROOT" => Gem.default_dir, # Default directory for gems + "GEM_HOME" => vendor_dir, # GEM_HOME pointing to the vendor directory + "GEM_PATH" => vendor_dir, # GEM_PATH also pointing to the vendor directory + } + end end - def omnibus_expand_path(*paths) - dir = File.expand_path(File.join(paths)) - raise OmnibusInstallNotFound.new unless dir && File.directory?(dir) + def get_pkg_prefix + pkg_name = "ngupta26/chef-cli" # Your origin and package name + path = `hab pkg path #{pkg_name}`.strip - dir + if $?.success? && !path.empty? + path + else + raise "Failed to get pkg_prefix for #{pkg_name}: #{path}" + end end + # def omnibus_expand_path(*paths) + # dir = File.expand_path(File.join(paths)) + # raise OmnibusInstallNotFound.new unless dir && File.directory?(dir) + + # dir + # end + private - def expected_omnibus_root - File.expand_path(File.join(Gem.ruby, "..", "..", "..")) - end + # def expected_omnibus_root + # File.expand_path(File.join(Gem.ruby, "..", "..", "..")) + # end def default_package_home if Chef::Platform.windows? From 809bc85465650c09d940eba5f659e464afb97b28 Mon Sep 17 00:00:00 2001 From: nikhil2611 Date: Wed, 9 Oct 2024 16:39:14 +0530 Subject: [PATCH 9/9] testing for plan.sh as of now Signed-off-by: nikhil2611 --- chef-cli.gemspec | 2 +- habitat/plan.ps1 | 92 ------------------------------------------------ 2 files changed, 1 insertion(+), 93 deletions(-) delete mode 100644 habitat/plan.ps1 diff --git a/chef-cli.gemspec b/chef-cli.gemspec index ba27ca63..968898d2 100644 --- a/chef-cli.gemspec +++ b/chef-cli.gemspec @@ -50,4 +50,4 @@ Gem::Specification.new do |gem| gem.add_dependency "diff-lcs", ">= 1.0", "< 1.4" # 1.4 changes the output gem.add_dependency "pastel", "~> 0.7" # used for policyfile differ gem.add_dependency "license-acceptance", ">= 1.0.11", "< 3" -end \ No newline at end of file +end diff --git a/habitat/plan.ps1 b/habitat/plan.ps1 deleted file mode 100644 index 58864f5b..00000000 --- a/habitat/plan.ps1 +++ /dev/null @@ -1,92 +0,0 @@ -$ErrorActionPreference = "Stop" -$PSDefaultParameterValues['*:ErrorAction']='Stop' - -$pkg_name="chef-cli" -$pkg_origin="chef" -$pkg_version=$(Get-Content "$PLAN_CONTEXT/../VERSION") -$pkg_maintainer="The Chef Maintainers " - -$pkg_deps=@( - "chef/ruby31-plus-devkit" - "core/git" -) -$pkg_bin_dirs=@("bin" - "vendor/bin") -$project_root= (Resolve-Path "$PLAN_CONTEXT/../").Path - -function pkg_version { - Get-Content "$SRC_PATH/VERSION" -} - -function Invoke-Before { - Set-PkgVersion -} -function Invoke-SetupEnvironment { - Push-RuntimeEnv -IsPath GEM_PATH "$pkg_prefix/vendor" - - Set-RuntimeEnv APPBUNDLER_ALLOW_RVM "true" # prevent appbundler from clearing out the carefully constructed runtime GEM_PATH - Set-RuntimeEnv FORCE_FFI_YAJL "ext" - Set-RuntimeEnv LANG "en_US.UTF-8" - Set-RuntimeEnv LC_CTYPE "en_US.UTF-8" -} - -function Invoke-Build { - try { - $env:Path += ";c:\\Program Files\\Git\\bin" - Push-Location $project_root - $env:GEM_HOME = "$HAB_CACHE_SRC_PATH/$pkg_dirname/vendor" - - Write-BuildLine " ** Configuring bundler for this build environment" - bundle config --local without integration deploy maintenance - bundle config --local jobs 4 - bundle config --local retry 5 - bundle config --local silence_root_warning 1 - Write-BuildLine " ** Using bundler to retrieve the Ruby dependencies" - bundle install - - gem build chef-cli.gemspec - Write-BuildLine " ** Using gem to install" - gem install chef-cli-*.gem --no-document - - - If ($lastexitcode -ne 0) { Exit $lastexitcode } - } finally { - Pop-Location - } -} - -function Invoke-Install { - Write-BuildLine "** Copy built & cached gems to install directory" - Copy-Item -Path "$HAB_CACHE_SRC_PATH/$pkg_dirname/*" -Destination $pkg_prefix -Recurse -Force -Exclude @("gem_make.out", "mkmf.log", "Makefile", - "*/latest", "latest", - "*/JSON-Schema-Test-Suite", "JSON-Schema-Test-Suite") - - try { - Push-Location $pkg_prefix - bundle config --local gemfile $project_root/Gemfile - Write-BuildLine "** generating binstubs for chef-cli with precise version pins" - Write-BuildLine "** generating binstubs for chef-cli with precise version pins $project_root $pkg_prefix/bin " - Invoke-Expression -Command "appbundler.bat $project_root $pkg_prefix/bin chef-cli" - If ($lastexitcode -ne 0) { Exit $lastexitcode } - Write-BuildLine " ** Running the chef-cli project's 'rake install' to install the path-based gems so they look like any other installed gem." - - If ($lastexitcode -ne 0) { Exit $lastexitcode } - } finally { - Pop-Location - } -} - -function Invoke-After { - # We don't need the cache of downloaded .gem files ... - Remove-Item $pkg_prefix/vendor/cache -Recurse -Force - # We don't need the gem docs. - Remove-Item $pkg_prefix/vendor/doc -Recurse -Force - # We don't need to ship the test suites for every gem dependency, - # only inspec's for package verification. - Get-ChildItem $pkg_prefix/vendor/gems -Filter "spec" -Directory -Recurse -Depth 1 ` - | Where-Object -FilterScript { $_.FullName -notlike "*chef-cli*" } ` - | Remove-Item -Recurse -Force - # Remove the byproducts of compiling gems with extensions - Get-ChildItem $pkg_prefix/vendor/gems -Include @("gem_make.out", "mkmf.log", "Makefile") -File -Recurse ` - | Remove-Item -Force -} \ No newline at end of file