From 4170a16e2366285f17aa3646a4d9757a94b0425a Mon Sep 17 00:00:00 2001 From: Ashique Saidalavi Date: Tue, 22 Oct 2024 18:59:35 +0530 Subject: [PATCH 1/4] Hab based installation of chef-infra-client Signed-off-by: Ashique Saidalavi --- lib/kitchen/driver/dokken.rb | 25 +++++++++++++++++++------ lib/kitchen/provisioner/dokken.rb | 14 +++++++++++--- 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/lib/kitchen/driver/dokken.rb b/lib/kitchen/driver/dokken.rb index 45dc115..d1e9135 100644 --- a/lib/kitchen/driver/dokken.rb +++ b/lib/kitchen/driver/dokken.rb @@ -39,8 +39,11 @@ class Dokken < Kitchen::Driver::Base default_config :cap_add, nil default_config :cap_drop, nil default_config :cgroupns_host, false + default_config :hab_installation, true default_config :chef_image, "chef/chef" default_config :chef_version, "latest" + default_config :habitat_image, "ashiqueps/chef-habitat" + default_config :habitat_version, "19.0.35" default_config :data_image, "dokken/kitchen-cache:latest" default_config :dns, nil default_config :dns_search, nil @@ -117,6 +120,14 @@ def destroy(_state) dokken_delete_sandbox end + def installer + @installer ||= if config[:hab_installation] + "habitat" + else + "chef" + end + end + private class PartialHash < Hash @@ -401,11 +412,11 @@ def create_chef_container(state) rescue ::Docker::Error::NotFoundError debug "Chef container does not exist, creating a new Chef container" with_retries do - debug "driver - creating volume container #{chef_container_name} from #{chef_image}" + debug "driver - creating volume container #{chef_container_name} from #{installer_image}" config = { "name" => chef_container_name, "Cmd" => "true", - "Image" => registry_image_path(chef_image), + "Image" => registry_image_path(installer_image), "HostConfig" => { "NetworkMode" => self[:network_mode], }, @@ -474,8 +485,8 @@ def pull_platform_image end def pull_chef_image - debug "driver - pulling #{short_image_path(chef_image)}" - config[:pull_chef_image] ? pull_image(chef_image) : pull_if_missing(chef_image) + debug "driver - pulling #{short_image_path(installer_image)}" + config[:pull_chef_image] ? pull_image(installer_image) : pull_if_missing(installer_image) end def delete_image(name) @@ -611,10 +622,12 @@ def wait_running_state(name, v) end def chef_container_name - config[:platform] != "" ? "chef-#{chef_version}-" + config[:platform].sub("/", "-") : "chef-#{chef_version}" + config[:platform] != "" ? "#{installer}-#{chef_version}-" + config[:platform].sub("/", "-") : "#{installer}-#{chef_version}" end - def chef_image + def installer_image + return "#{config[:habitat_image]}:#{config[:habitat_version]}" if installer == "habitat" + "#{config[:chef_image]}:#{chef_version}" end diff --git a/lib/kitchen/provisioner/dokken.rb b/lib/kitchen/provisioner/dokken.rb index 296d28a..0e1d76f 100644 --- a/lib/kitchen/provisioner/dokken.rb +++ b/lib/kitchen/provisioner/dokken.rb @@ -31,6 +31,7 @@ class Dokken < Kitchen::Provisioner::ChefZero default_config :root_path, "/opt/kitchen" default_config :chef_binary, "/opt/chef/bin/chef-client" + default_config :hab_chef_binary, "/hab/hab_bin" default_config :chef_options, " -z" default_config :chef_log_level, "warn" default_config :chef_output_format, "doc" @@ -102,14 +103,15 @@ def validate_config # patching Kitchen::Provisioner::ChefZero#run_command def run_command validate_config - cmd = config[:chef_binary] + cmd = chef_executable cmd << config[:chef_options].to_s cmd << " -l #{config[:chef_log_level]}" cmd << " -F #{config[:chef_output_format]}" cmd << " -c /opt/kitchen/client.rb" cmd << " -j /opt/kitchen/dna.json" - cmd << "--profile-ruby" if config[:profile_ruby] - cmd << "--slow-report" if config[:slow_resource_report] + cmd << " --profile-ruby" if config[:profile_ruby] + cmd << " --slow-report" if config[:slow_resource_report] + cmd << " --chef-license-key=#{config[:chef_license_key]}" if instance.driver.installer == "habitat" && config[:chef_license_key] chef_cmd(cmd) end @@ -128,6 +130,12 @@ def cleanup_dokken_sandbox debug("Cleaning up local sandbox in #{sandbox_path}") FileUtils.rmtree(Dir.glob("#{sandbox_path}/*")) end + + def chef_executable + return "HAB_LICENSE='accept-no-persist' #{config[:hab_chef_binary]} pkg exec ashiqueps/chef-infra-client -- chef-client " if instance.driver.installer == "habitat" + + "#{config[:chef_binary]}" + end end end end From a4357a78cb4c338b44b9f81c8136bc18db792f3b Mon Sep 17 00:00:00 2001 From: Ashique Saidalavi Date: Wed, 6 Nov 2024 14:54:18 +0530 Subject: [PATCH 2/4] Updated the hab image version key Signed-off-by: Ashique Saidalavi --- lib/kitchen/driver/dokken.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/kitchen/driver/dokken.rb b/lib/kitchen/driver/dokken.rb index d1e9135..aea2a41 100644 --- a/lib/kitchen/driver/dokken.rb +++ b/lib/kitchen/driver/dokken.rb @@ -43,7 +43,7 @@ class Dokken < Kitchen::Driver::Base default_config :chef_image, "chef/chef" default_config :chef_version, "latest" default_config :habitat_image, "ashiqueps/chef-habitat" - default_config :habitat_version, "19.0.35" + default_config :habitat_image_version, "19.0.35" default_config :data_image, "dokken/kitchen-cache:latest" default_config :dns, nil default_config :dns_search, nil @@ -626,7 +626,7 @@ def chef_container_name end def installer_image - return "#{config[:habitat_image]}:#{config[:habitat_version]}" if installer == "habitat" + return "#{config[:habitat_image]}:#{config[:habitat_image_version]}" if installer == "habitat" "#{config[:chef_image]}:#{chef_version}" end From 9957a675ab6b4ebf2965763f64d31b15a12b0311 Mon Sep 17 00:00:00 2001 From: Ashique Saidalavi Date: Fri, 8 Nov 2024 00:32:22 +0530 Subject: [PATCH 3/4] Chef image changes Signed-off-by: Ashique Saidalavi --- lib/kitchen/driver/dokken.rb | 19 +++++++------------ lib/kitchen/provisioner/dokken.rb | 2 +- 2 files changed, 8 insertions(+), 13 deletions(-) diff --git a/lib/kitchen/driver/dokken.rb b/lib/kitchen/driver/dokken.rb index aea2a41..3d2d6ab 100644 --- a/lib/kitchen/driver/dokken.rb +++ b/lib/kitchen/driver/dokken.rb @@ -39,11 +39,8 @@ class Dokken < Kitchen::Driver::Base default_config :cap_add, nil default_config :cap_drop, nil default_config :cgroupns_host, false - default_config :hab_installation, true - default_config :chef_image, "chef/chef" + default_config :chef_image, "ashiqueps/chef-habitat" default_config :chef_version, "latest" - default_config :habitat_image, "ashiqueps/chef-habitat" - default_config :habitat_image_version, "19.0.35" default_config :data_image, "dokken/kitchen-cache:latest" default_config :dns, nil default_config :dns_search, nil @@ -121,7 +118,7 @@ def destroy(_state) end def installer - @installer ||= if config[:hab_installation] + @installer ||= if config[:chef_image].include?("habitat") "habitat" else "chef" @@ -412,11 +409,11 @@ def create_chef_container(state) rescue ::Docker::Error::NotFoundError debug "Chef container does not exist, creating a new Chef container" with_retries do - debug "driver - creating volume container #{chef_container_name} from #{installer_image}" + debug "driver - creating volume container #{chef_container_name} from #{chef_image}" config = { "name" => chef_container_name, "Cmd" => "true", - "Image" => registry_image_path(installer_image), + "Image" => registry_image_path(chef_image), "HostConfig" => { "NetworkMode" => self[:network_mode], }, @@ -485,8 +482,8 @@ def pull_platform_image end def pull_chef_image - debug "driver - pulling #{short_image_path(installer_image)}" - config[:pull_chef_image] ? pull_image(installer_image) : pull_if_missing(installer_image) + debug "driver - pulling #{short_image_path(chef_image)}" + config[:pull_chef_image] ? pull_image(chef_image) : pull_if_missing(chef_image) end def delete_image(name) @@ -625,9 +622,7 @@ def chef_container_name config[:platform] != "" ? "#{installer}-#{chef_version}-" + config[:platform].sub("/", "-") : "#{installer}-#{chef_version}" end - def installer_image - return "#{config[:habitat_image]}:#{config[:habitat_image_version]}" if installer == "habitat" - + def chef_image "#{config[:chef_image]}:#{chef_version}" end diff --git a/lib/kitchen/provisioner/dokken.rb b/lib/kitchen/provisioner/dokken.rb index 0e1d76f..2d0b204 100644 --- a/lib/kitchen/provisioner/dokken.rb +++ b/lib/kitchen/provisioner/dokken.rb @@ -132,7 +132,7 @@ def cleanup_dokken_sandbox end def chef_executable - return "HAB_LICENSE='accept-no-persist' #{config[:hab_chef_binary]} pkg exec ashiqueps/chef-infra-client -- chef-client " if instance.driver.installer == "habitat" + return "HAB_LICENSE='accept-no-persist' #{config[:hab_chef_binary]} pkg exec chef/chef-infra-client -- chef-client " if instance.driver.installer == "habitat" "#{config[:chef_binary]}" end From 95b77e75274025311dd300c898b97bd9247a92f3 Mon Sep 17 00:00:00 2001 From: Ashique Saidalavi Date: Mon, 25 Nov 2024 16:18:54 +0530 Subject: [PATCH 4/4] Updated the image name Signed-off-by: Ashique Saidalavi --- lib/kitchen/driver/dokken.rb | 6 ++++-- lib/kitchen/provisioner/dokken.rb | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/kitchen/driver/dokken.rb b/lib/kitchen/driver/dokken.rb index 3d2d6ab..cf27468 100644 --- a/lib/kitchen/driver/dokken.rb +++ b/lib/kitchen/driver/dokken.rb @@ -39,7 +39,7 @@ class Dokken < Kitchen::Driver::Base default_config :cap_add, nil default_config :cap_drop, nil default_config :cgroupns_host, false - default_config :chef_image, "ashiqueps/chef-habitat" + default_config :chef_image, "chef/chef-hab" default_config :chef_version, "latest" default_config :data_image, "dokken/kitchen-cache:latest" default_config :dns, nil @@ -117,8 +117,10 @@ def destroy(_state) dokken_delete_sandbox end + # TODO: This method currently checks if the installer is a hab or omnibus based on the image name. + # Find a better way to determine the installer. def installer - @installer ||= if config[:chef_image].include?("habitat") + @installer ||= if config[:chef_image].include?("hab") "habitat" else "chef" diff --git a/lib/kitchen/provisioner/dokken.rb b/lib/kitchen/provisioner/dokken.rb index 2d0b204..5648b97 100644 --- a/lib/kitchen/provisioner/dokken.rb +++ b/lib/kitchen/provisioner/dokken.rb @@ -31,7 +31,7 @@ class Dokken < Kitchen::Provisioner::ChefZero default_config :root_path, "/opt/kitchen" default_config :chef_binary, "/opt/chef/bin/chef-client" - default_config :hab_chef_binary, "/hab/hab_bin" + default_config :hab_chef_binary, "/hab/hab" default_config :chef_options, " -z" default_config :chef_log_level, "warn" default_config :chef_output_format, "doc"