summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Smith <tsmith@chef.io>2020-02-27 09:18:23 -0800
committerGitHub <noreply@github.com>2020-02-27 09:18:23 -0800
commit4b3ce78511a70ba84bf7a57acf2893e563632099 (patch)
treee3c37fd5d9dabe416adfdc52e718b6c16741a290
parenteaa308a34c24aef7b374c16098fca360db449ea8 (diff)
parenta3169e349311f43afa34a5243c6e34b8fc384100 (diff)
downloadchef-4b3ce78511a70ba84bf7a57acf2893e563632099.tar.gz
Merge pull request #9403 from cinc-project/backport_dist_bootstrap
Backport dist bootstrap
-rw-r--r--.gitignore1
-rw-r--r--Rakefile9
-rw-r--r--chef-config/lib/chef-config/config.rb18
-rw-r--r--chef-config/lib/chef-config/dist.rb4
-rw-r--r--distro/templates/powershell/chef/chef.psm1.erb (renamed from distro/powershell/chef/chef.psm1)36
-rw-r--r--lib/chef/knife/bootstrap/templates/chef-full.erb18
-rw-r--r--lib/chef/knife/bootstrap/templates/windows-chef-client-msi.erb4
-rw-r--r--lib/chef/knife/core/windows_bootstrap_context.rb16
-rw-r--r--spec/unit/knife/core/windows_bootstrap_context_spec.rb6
9 files changed, 66 insertions, 46 deletions
diff --git a/.gitignore b/.gitignore
index 7435401e97..89b8af9757 100644
--- a/.gitignore
+++ b/.gitignore
@@ -80,6 +80,7 @@ docs_site
# Rendered Templates
ext/win32-eventlog/chef-log.man
+distro/powershell/chef/chef.psm1
# tool logs
ext/win32-eventlog/mkmf.log
diff --git a/Rakefile b/Rakefile
index 643d70015b..b54411af4c 100644
--- a/Rakefile
+++ b/Rakefile
@@ -22,6 +22,7 @@ begin
require_relative "tasks/dependencies"
require_relative "tasks/announce"
require_relative "tasks/docs"
+ require_relative "lib/chef/dist"
rescue LoadError => e
puts "Skipping missing rake dep: #{e}"
end
@@ -35,6 +36,14 @@ task :super_install do
Dir.chdir(path)
sh("rake install")
end
+
+# Templating the powershell extensions so we can inject distro constants
+ template_file = ::File.join(::File.dirname(__FILE__), "distro", "templates", "powershell", "chef", "chef.psm1.erb")
+ psm1_path = ::File.join(::File.dirname(__FILE__), "distro", "powershell", "chef")
+ FileUtils.mkdir_p psm1_path
+ template = ERB.new(IO.read(template_file))
+ chef_psm1 = template.result
+ File.open(::File.join(psm1_path, "chef.psm1"), "w") { |f| f.write(chef_psm1) }
end
task install: :super_install
diff --git a/chef-config/lib/chef-config/config.rb b/chef-config/lib/chef-config/config.rb
index 484e1ea2f6..17ab31ead5 100644
--- a/chef-config/lib/chef-config/config.rb
+++ b/chef-config/lib/chef-config/config.rb
@@ -75,20 +75,20 @@ module ChefConfig
end
# On *nix, /etc/chef
- def self.etc_chef_dir
- path = ChefUtils.windows? ? c_chef_dir : PathHelper.join("/etc", ChefConfig::Dist::DIR_SUFFIX)
+ def self.etc_chef_dir(is_windows = ChefUtils.windows?)
+ path = is_windows ? c_chef_dir : PathHelper.join("/etc", ChefConfig::Dist::DIR_SUFFIX)
PathHelper.cleanpath(path)
end
# On *nix, /var/chef
- def self.var_chef_dir
- path = ChefUtils.windows? ? c_chef_dir : PathHelper.join("/var", ChefConfig::Dist::DIR_SUFFIX)
+ def self.var_chef_dir(is_windows = ChefUtils.windows?)
+ path = is_windows ? c_chef_dir : PathHelper.join("/var", ChefConfig::Dist::DIR_SUFFIX)
PathHelper.cleanpath(path)
end
# On *nix, the root of /var/, used to test if we can create and write in /var/chef
- def self.var_root_dir
- path = ChefUtils.windows? ? c_chef_dir : "/var"
+ def self.var_root_dir(is_windows = ChefUtils.windows?)
+ path = is_windows ? c_chef_dir : "/var"
PathHelper.cleanpath(path)
end
@@ -99,6 +99,12 @@ module ChefConfig
PathHelper.cleanpath(path)
end
+ def self.c_opscode_dir
+ drive = windows_installation_drive || "C:"
+ path = PathHelper.join(drive, ChefConfig::Dist::LEGACY_CONF_DIR, ChefConfig::Dist::DIR_SUFFIX)
+ PathHelper.cleanpath(path)
+ end
+
# the drive where Chef is installed on a windows host. This is determined
# either by the drive containing the current file or by the SYSTEMDRIVE ENV
# variable
diff --git a/chef-config/lib/chef-config/dist.rb b/chef-config/lib/chef-config/dist.rb
index d46c9c99d6..e75b26730f 100644
--- a/chef-config/lib/chef-config/dist.rb
+++ b/chef-config/lib/chef-config/dist.rb
@@ -15,5 +15,9 @@ module ChefConfig
# The user's configuration directory
USER_CONF_DIR = ".chef".freeze
+
+ # The legacy conf folder: C:/opscode/chef. Specifically the "opscode" part
+ # DIR_SUFFIX is appended to it in code where relevant
+ LEGACY_CONF_DIR = "opscode".freeze
end
end
diff --git a/distro/powershell/chef/chef.psm1 b/distro/templates/powershell/chef/chef.psm1.erb
index 05fee05e5e..652dec04f6 100644
--- a/distro/powershell/chef/chef.psm1
+++ b/distro/templates/powershell/chef/chef.psm1.erb
@@ -412,40 +412,40 @@ function Run-RubyCommand($command, $argList) {
}
-function chef-apply {
- Run-RubyCommand 'chef-apply' $args
+function <%= Chef::Dist::APPLY %> {
+ Run-RubyCommand '<%= Chef::Dist::APPLY %>' $args
}
-function chef-client {
- Run-RubyCommand 'chef-client' $args
+function <%= Chef::Dist::CLIENT %> {
+ Run-RubyCommand '<%= Chef::Dist::CLIENT %>' $args
}
-function chef-service-manager {
- Run-RubyCommand 'chef-service-manager' $args
+function <%= Chef::Dist::EXEC %>-service-manager {
+ Run-RubyCommand '<%= Chef::Dist::EXEC %>-service-manager' $args
}
-function chef-shell {
- Run-RubyCommand 'chef-shell' $args
+function <%= Chef::Dist::SHELL %> {
+ Run-RubyCommand '<%= Chef::Dist::SHELL %>' $args
}
-function chef-solo {
- Run-RubyCommand 'chef-solo' $args
+function <%= Chef::Dist::SOLOEXEC %> {
+ Run-RubyCommand '<%= Chef::Dist::SOLOEXEC %>' $args
}
-function chef-windows-service {
- Run-RubyCommand 'chef-windows-service' $args
+function <%= Chef::Dist::EXEC %>-windows-service {
+ Run-RubyCommand '<%= Chef::Dist::EXEC %>-windows-service' $args
}
function knife {
Run-RubyCommand 'knife' $args
}
-Export-ModuleMember -function chef-apply
-Export-ModuleMember -function chef-client
-Export-ModuleMember -function chef-service-manager
-Export-ModuleMember -function chef-shell
-Export-ModuleMember -function chef-solo
-Export-ModuleMember -function chef-windows-service
+Export-ModuleMember -function <%= Chef::Dist::APPLY %>
+Export-ModuleMember -function <%= Chef::Dist::CLIENT %>
+Export-ModuleMember -function <%= Chef::Dist::EXEC %>-service-manager
+Export-ModuleMember -function <%= Chef::Dist::SHELL %>
+Export-ModuleMember -function <%= Chef::Dist::SOLOEXEC %>
+Export-ModuleMember -function <%= Chef::Dist::EXEC %>-windows-service
Export-ModuleMember -function knife
# To debug this module, uncomment the line below
diff --git a/lib/chef/knife/bootstrap/templates/chef-full.erb b/lib/chef/knife/bootstrap/templates/chef-full.erb
index 7743142332..263203ca76 100644
--- a/lib/chef/knife/bootstrap/templates/chef-full.erb
+++ b/lib/chef/knife/bootstrap/templates/chef-full.erb
@@ -185,50 +185,50 @@ if test "x$tmp_dir" != "x"; then
rm -r "$tmp_dir"
fi
-mkdir -p /etc/chef
+mkdir -p <%= ChefConfig::Config.etc_chef_dir(false) %>
<% if client_pem -%>
-(umask 077 && (cat > /etc/chef/client.pem <<'EOP'
+(umask 077 && (cat > <%= ChefConfig::Config.etc_chef_dir(false) %>/client.pem <<'EOP'
<%= ::File.read(::File.expand_path(client_pem)) %>
EOP
)) || exit 1
<% end -%>
<% if validation_key -%>
-(umask 077 && (cat > /etc/chef/validation.pem <<'EOP'
+(umask 077 && (cat > <%= ChefConfig::Config.etc_chef_dir(false) %>/validation.pem <<'EOP'
<%= validation_key %>
EOP
)) || exit 1
<% end -%>
<% if encrypted_data_bag_secret -%>
-(umask 077 && (cat > /etc/chef/encrypted_data_bag_secret <<'EOP'
+(umask 077 && (cat > <%= ChefConfig::Config.etc_chef_dir(false) %>/encrypted_data_bag_secret <<'EOP'
<%= encrypted_data_bag_secret %>
EOP
)) || exit 1
<% end -%>
<% unless trusted_certs.empty? -%>
-mkdir -p /etc/chef/trusted_certs
+mkdir -p <%= ChefConfig::Config.etc_chef_dir(false) %>/trusted_certs
<%= trusted_certs %>
<% end -%>
<%# Generate Ohai Hints -%>
<% unless @chef_config[:knife][:hints].nil? || @chef_config[:knife][:hints].empty? -%>
-mkdir -p /etc/chef/ohai/hints
+mkdir -p <%= ChefConfig::Config.etc_chef_dir(false) %>/ohai/hints
<% @chef_config[:knife][:hints].each do |name, hash| -%>
-cat > /etc/chef/ohai/hints/<%= name %>.json <<'EOP'
+cat > <%= ChefConfig::Config.etc_chef_dir(false) %>/ohai/hints/<%= name %>.json <<'EOP'
<%= Chef::JSONCompat.to_json(hash) %>
EOP
<% end -%>
<% end -%>
-cat > /etc/chef/client.rb <<'EOP'
+cat > <%= ChefConfig::Config.etc_chef_dir(false) %>/client.rb <<'EOP'
<%= config_content %>
EOP
-cat > /etc/chef/first-boot.json <<'EOP'
+cat > <%= ChefConfig::Config.etc_chef_dir(false) %>/first-boot.json <<'EOP'
<%= Chef::JSONCompat.to_json(first_boot) %>
EOP
diff --git a/lib/chef/knife/bootstrap/templates/windows-chef-client-msi.erb b/lib/chef/knife/bootstrap/templates/windows-chef-client-msi.erb
index 1ac0b23755..3fe397ed9e 100644
--- a/lib/chef/knife/bootstrap/templates/windows-chef-client-msi.erb
+++ b/lib/chef/knife/bootstrap/templates/windows-chef-client-msi.erb
@@ -120,11 +120,11 @@ If !ERRORLEVEL!==0 (
)
:install
-@rem If user has provided the custom installation command for <%= Chef::Dist::CLIENT %> then execute it
+@rem If user has provided the custom installation command, execute it
<% if @chef_config[:knife][:bootstrap_install_command] %>
<%= @chef_config[:knife][:bootstrap_install_command] %>
<% else %>
- @rem Install Chef using <%= Chef::Dist::CLIENT %> MSI installer
+ @rem Install Chef using the MSI installer
@set "LOCAL_DESTINATION_MSI_PATH=<%= local_download_path %>"
@set "CHEF_CLIENT_MSI_LOG_PATH=%TEMP%\<%= Chef::Dist::CLIENT %>-msi%RANDOM%.log"
diff --git a/lib/chef/knife/core/windows_bootstrap_context.rb b/lib/chef/knife/core/windows_bootstrap_context.rb
index 977bfadc16..d3f55ce94c 100644
--- a/lib/chef/knife/core/windows_bootstrap_context.rb
+++ b/lib/chef/knife/core/windows_bootstrap_context.rb
@@ -59,9 +59,9 @@ class Chef
client_rb = <<~CONFIG
chef_server_url "#{@chef_config[:chef_server_url]}"
validation_client_name "#{@chef_config[:validation_client_name]}"
- file_cache_path "c:/chef/cache"
- file_backup_path "c:/chef/backup"
- cache_options ({:path => "c:/chef/cache/checksums", :skip_expires => true})
+ file_cache_path "#{ChefConfig::Config.var_chef_dir(true)}/cache"
+ file_backup_path "#{ChefConfig::Config.var_chef_dir(true)}/backup"
+ cache_options ({:path => "#{ChefConfig::Config.etc_chef_dir(true)}/cache/checksums", :skip_expires => true})
CONFIG
unless @chef_config[:chef_license].nil?
@@ -124,11 +124,11 @@ class Chef
end
if @config[:secret]
- client_rb << %Q{encrypted_data_bag_secret "c:/chef/encrypted_data_bag_secret"\n}
+ client_rb << %Q{encrypted_data_bag_secret "#{ChefConfig::Config.etc_chef_dir(true)}/encrypted_data_bag_secret"\n}
end
unless trusted_certs_script.empty?
- client_rb << %Q{trusted_certs_dir "c:/chef/trusted_certs"\n}
+ client_rb << %Q{trusted_certs_dir "#{ChefConfig::Config.etc_chef_dir(true)}/trusted_certs"\n}
end
if Chef::Config[:fips]
@@ -158,8 +158,8 @@ class Chef
def start_chef
bootstrap_environment_option = bootstrap_environment.nil? ? "" : " -E #{bootstrap_environment}"
- start_chef = "SET \"PATH=%SystemRoot%\\system32;%SystemRoot%;%SystemRoot%\\System32\\Wbem;%SYSTEMROOT%\\System32\\WindowsPowerShell\\v1.0\\;C:\\ruby\\bin;C:\\opscode\\chef\\bin;C:\\opscode\\chef\\embedded\\bin\;%PATH%\"\n"
- start_chef << "chef-client -c c:/chef/client.rb -j c:/chef/first-boot.json#{bootstrap_environment_option}\n"
+ start_chef = "SET \"PATH=%SystemRoot%\\system32;%SystemRoot%;%SystemRoot%\\System32\\Wbem;%SYSTEMROOT%\\System32\\WindowsPowerShell\\v1.0\\;C:\\ruby\\bin;#{ChefConfig::Config.c_opscode_dir}\\#{ChefConfig::Dist::DIR_SUFFIX}\\bin;#{ChefConfig::Config.c_opscode_dir}\\#{ChefConfig::Dist::DIR_SUFFIX}\\embedded\\bin\;%PATH%\"\n"
+ start_chef << "chef-client -c #{ChefConfig::Config.etc_chef_dir(true)}/client.rb -j #{ChefConfig::Config.etc_chef_dir(true)}/first-boot.json#{bootstrap_environment_option}\n"
end
def win_wget
@@ -260,7 +260,7 @@ class Chef
end
def bootstrap_directory
- "C:\\chef"
+ ChefConfig::Config.etc_chef_dir(true)
end
def local_download_path
diff --git a/spec/unit/knife/core/windows_bootstrap_context_spec.rb b/spec/unit/knife/core/windows_bootstrap_context_spec.rb
index e5a4e955e8..2c538975e8 100644
--- a/spec/unit/knife/core/windows_bootstrap_context_spec.rb
+++ b/spec/unit/knife/core/windows_bootstrap_context_spec.rb
@@ -164,9 +164,9 @@ describe Chef::Knife::Core::WindowsBootstrapContext do
expected = <<~EXPECTED
echo.chef_server_url "http://chef.example.com:4444"
echo.validation_client_name "chef-validator-testing"
- echo.file_cache_path "c:/chef/cache"
- echo.file_backup_path "c:/chef/backup"
- echo.cache_options ^({:path =^> "c:/chef/cache/checksums", :skip_expires =^> true}^)
+ echo.file_cache_path "C:/chef/cache"
+ echo.file_backup_path "C:/chef/backup"
+ echo.cache_options ^({:path =^> "C:/chef/cache/checksums", :skip_expires =^> true}^)
echo.# Using default node name ^(fqdn^)
echo.log_level :info
echo.log_location STDOUT