kitchen-omnibus-chef 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,167 @@
1
+ #
2
+ # Author:: Fletcher Nichol (<fnichol@nichol.ca>)
3
+ #
4
+ # Copyright (C) 2013, Fletcher Nichol
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # https://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+
18
+ require_relative "chef_base"
19
+
20
+ module Kitchen
21
+ module Provisioner
22
+ # Chef Zero provisioner.
23
+ #
24
+ # @author Fletcher Nichol <fnichol@nichol.ca>
25
+ class ChefInfra < ChefBase
26
+ kitchen_provisioner_api_version 2
27
+
28
+ plugin_version Kitchen::VERSION
29
+
30
+ default_config :client_rb, {}
31
+ default_config :named_run_list, {}
32
+ default_config :json_attributes, true
33
+ default_config :chef_zero_host, nil
34
+ default_config :chef_zero_port, 8889
35
+
36
+ default_config :chef_client_path do |provisioner|
37
+ provisioner
38
+ .remote_path_join(%W{#{provisioner[:chef_omnibus_root]} bin chef-client})
39
+ .tap { |path| path.concat(".bat") if provisioner.windows_os? }
40
+ end
41
+
42
+ default_config :ruby_bindir do |provisioner|
43
+ provisioner
44
+ .remote_path_join(%W{#{provisioner[:chef_omnibus_root]} embedded bin})
45
+ end
46
+
47
+ # (see Base#create_sandbox)
48
+ def create_sandbox
49
+ super
50
+ prepare_validation_pem
51
+ prepare_config_rb
52
+ end
53
+
54
+ def run_command
55
+ cmd = "#{sudo(config[:chef_client_path])} --local-mode".tap { |str| str.insert(0, "& ") if powershell_shell? }
56
+
57
+ chef_cmd(cmd)
58
+ end
59
+
60
+ private
61
+
62
+ # Adds optional flags to a chef-client command, depending on
63
+ # configuration data. Note that this method mutates the incoming Array.
64
+ #
65
+ # @param args [Array<String>] array of flags
66
+ # @api private
67
+ # rubocop:disable Metrics/CyclomaticComplexity
68
+ def add_optional_chef_client_args!(args)
69
+ if config[:json_attributes]
70
+ json = remote_path_join(config[:root_path], "dna.json")
71
+ args << "--json-attributes #{json}"
72
+ end
73
+
74
+ args << "--logfile #{config[:log_file]}" if config[:log_file]
75
+
76
+ # these flags are chef-client local mode only and will not work
77
+ # on older versions of chef-client
78
+ if config[:chef_zero_host]
79
+ args << "--chef-zero-host #{config[:chef_zero_host]}"
80
+ end
81
+
82
+ if config[:chef_zero_port]
83
+ args << "--chef-zero-port #{config[:chef_zero_port]}"
84
+ end
85
+
86
+ args << "--profile-ruby" if config[:profile_ruby]
87
+
88
+ if config[:slow_resource_report]
89
+ if config[:slow_resource_report].is_a?(Integer)
90
+ args << "--slow-report #{config[:slow_resource_report]}"
91
+ else
92
+ args << "--slow-report"
93
+ end
94
+ end
95
+ end
96
+ # rubocop:enable Metrics/CyclomaticComplexity
97
+
98
+ # Returns an Array of command line arguments for the chef client.
99
+ #
100
+ # @return [Array<String>] an array of command line arguments
101
+ # @api private
102
+ def chef_args(client_rb_filename)
103
+ level = config[:log_level]
104
+ args = [
105
+ "--config #{remote_path_join(config[:root_path], client_rb_filename)}",
106
+ "--log_level #{level}",
107
+ "--force-formatter",
108
+ "--no-color",
109
+ ]
110
+ add_optional_chef_client_args!(args)
111
+
112
+ args
113
+ end
114
+
115
+ # Generates a string of shell environment variables needed for the
116
+ # chef-client-zero.rb shim script to properly function.
117
+ #
118
+ # @return [String] a shell script string
119
+ # @api private
120
+ def chef_client_zero_env
121
+ root = config[:root_path]
122
+ gem_home = gem_path = remote_path_join(root, "chef-client-zero-gems")
123
+ gem_cache = remote_path_join(gem_home, "cache")
124
+
125
+ [
126
+ shell_env_var("CHEF_REPO_PATH", root),
127
+ shell_env_var("GEM_HOME", gem_home),
128
+ shell_env_var("GEM_PATH", gem_path),
129
+ shell_env_var("GEM_CACHE", gem_cache),
130
+ ].join("\n").concat("\n")
131
+ end
132
+
133
+ # Writes a fake (but valid) validation.pem into the sandbox directory.
134
+ #
135
+ # @api private
136
+ def prepare_validation_pem
137
+ info("Preparing validation.pem")
138
+ debug("Using a dummy validation.pem")
139
+
140
+ source = File.join(File.dirname(__FILE__),
141
+ %w{.. .. .. support dummy-validation.pem})
142
+ FileUtils.cp(source, File.join(sandbox_path, "validation.pem"))
143
+ end
144
+
145
+ # Returns the command that will run a backwards compatible shim script
146
+ # that approximates local mode in a modern chef-client run.
147
+ #
148
+ # @return [String] the command string
149
+ # @api private
150
+ def shim_command
151
+ ruby = remote_path_join(config[:ruby_bindir], "ruby")
152
+ .tap { |path| path.concat(".exe") if windows_os? }
153
+ shim = remote_path_join(config[:root_path], "chef-client-zero.rb")
154
+
155
+ "#{chef_client_zero_env}\n#{sudo(ruby)} #{shim}"
156
+ end
157
+
158
+ # This provisioner supports policyfiles, so override the default (which
159
+ # is false)
160
+ # @return [true] always returns true
161
+ # @api private
162
+ def supports_policyfile?
163
+ true
164
+ end
165
+ end
166
+ end
167
+ end
@@ -0,0 +1,82 @@
1
+ #
2
+ # Author:: Fletcher Nichol (<fnichol@nichol.ca>)
3
+ #
4
+ # Copyright (C) 2013, Fletcher Nichol
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # https://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+
18
+ require_relative "chef_base"
19
+
20
+ module Kitchen
21
+ module Provisioner
22
+ # Chef Solo provisioner.
23
+ #
24
+ # @author Fletcher Nichol <fnichol@nichol.ca>
25
+ class ChefSolo < ChefBase
26
+ kitchen_provisioner_api_version 2
27
+
28
+ plugin_version Kitchen::VERSION
29
+
30
+ # ChefSolo is dependent on Berkshelf, which is not thread-safe.
31
+ # See discussion on https://github.com/test-kitchen/test-kitchen/issues/1307
32
+ no_parallel_for :converge
33
+
34
+ default_config :solo_rb, {}
35
+
36
+ default_config :chef_solo_path do |provisioner|
37
+ provisioner
38
+ .remote_path_join(%W{#{provisioner[:chef_omnibus_root]} bin chef-solo})
39
+ .tap { |path| path.concat(".bat") if provisioner.windows_os? }
40
+ end
41
+
42
+ # (see Base#config_filename)
43
+ def config_filename
44
+ "solo.rb"
45
+ end
46
+
47
+ # (see Base#create_sandbox)
48
+ def create_sandbox
49
+ super
50
+ prepare_config_rb
51
+ end
52
+
53
+ # (see Base#run_command)
54
+ def run_command
55
+ cmd = sudo(config[:chef_solo_path]).dup
56
+ .tap { |str| str.insert(0, "& ") if powershell_shell? }
57
+
58
+ chef_cmd(cmd)
59
+ end
60
+
61
+ private
62
+
63
+ # Returns an Array of command line arguments for the chef client.
64
+ #
65
+ # @return [Array<String>] an array of command line arguments
66
+ # @api private
67
+ def chef_args(solo_rb_filename)
68
+ args = [
69
+ "--config #{remote_path_join(config[:root_path], solo_rb_filename)}",
70
+ "--log_level #{config[:log_level]}",
71
+ "--force-formatter",
72
+ "--no-color",
73
+ "--json-attributes #{remote_path_join(config[:root_path], "dna.json")}",
74
+ ]
75
+ args << "--logfile #{config[:log_file]}" if config[:log_file]
76
+ args << "--profile-ruby" if config[:profile_ruby]
77
+ args << "--legacy-mode" if config[:legacy_mode]
78
+ args
79
+ end
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,130 @@
1
+ #
2
+ # Author:: Thomas Heinen (<thomas.heinen@gmail.com>)
3
+ #
4
+ # Copyright (C) 2023, Thomas Heinen
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # https://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+
18
+ require_relative "chef_infra"
19
+
20
+ module Kitchen
21
+ module Provisioner
22
+ # Chef Target provisioner.
23
+ #
24
+ # @author Thomas Heinen <thomas.heinen@gmail.com>
25
+ class ChefTarget < ChefInfra
26
+ MIN_VERSION_REQUIRED = "19.0.0".freeze
27
+ class ChefVersionTooLow < UserError; end
28
+ class ChefClientNotFound < UserError; end
29
+ class RequireTrainTransport < UserError; end
30
+
31
+ default_config :install_strategy, "none"
32
+ default_config :sudo, true
33
+
34
+ def install_command; ""; end
35
+ def init_command; ""; end
36
+ def prepare_command; ""; end
37
+
38
+ def chef_args(client_rb_filename)
39
+ # Dummy execution to initialize and test remote connection
40
+ connection = instance.remote_exec("echo Connection established")
41
+
42
+ check_transport(connection)
43
+ check_local_chef_client
44
+
45
+ instance_name = instance.name
46
+ credentials_file = File.join(kitchen_basepath, ".kitchen", instance_name + ".ini")
47
+ File.write(credentials_file, connection.credentials_file)
48
+
49
+ super.push(
50
+ "--target #{instance_name}",
51
+ "--credentials #{credentials_file}"
52
+ )
53
+ end
54
+
55
+ def check_transport(connection)
56
+ debug("Checking for active transport")
57
+
58
+ unless connection.respond_to? "train_uri"
59
+ error("Chef Target Mode provisioner requires a Train-based transport like kitchen-transport-train")
60
+ raise RequireTrainTransport.new("No Train transport")
61
+ end
62
+
63
+ debug("Kitchen transport responds to train_uri function call, as required")
64
+ end
65
+
66
+ def check_local_chef_client
67
+ debug("Checking for chef-client version")
68
+
69
+ begin
70
+ client_version = `chef-client -v`.chop.split(":")[-1]
71
+ rescue Errno::ENOENT => e
72
+ error("Error determining Chef Infra version: #{e.exception.message}")
73
+ raise ChefClientNotFound.new("Need chef-client installed locally")
74
+ end
75
+
76
+ minimum_version = Gem::Version.new(MIN_VERSION_REQUIRED)
77
+ installed_version = Gem::Version.new(client_version)
78
+
79
+ if installed_version < minimum_version
80
+ error("Found Chef Infra version #{installed_version}, but require #{minimum_version} for Target Mode")
81
+ raise ChefVersionTooLow.new("Need version #{MIN_VERSION_REQUIRED} or higher")
82
+ end
83
+
84
+ debug("Chef Infra found and version constraints match")
85
+ end
86
+
87
+ def kitchen_basepath
88
+ instance.driver.config[:kitchen_root]
89
+ end
90
+
91
+ def create_sandbox
92
+ super
93
+
94
+ # Change config.rb to point to the local sandbox path, not to /tmp/kitchen
95
+ config[:root_path] = sandbox_path
96
+ prepare_config_rb
97
+ end
98
+
99
+ def call(state)
100
+ remote_connection = instance.transport.connection(state)
101
+
102
+ config[:uploads].to_h.each do |locals, remote|
103
+ debug("Uploading #{Array(locals).join(", ")} to #{remote}")
104
+ remote_connection.upload(locals.to_s, remote)
105
+ end
106
+
107
+ # no installation
108
+ create_sandbox
109
+ # no prepare command
110
+
111
+ # Stream output to logger
112
+ require "open3"
113
+ Open3.popen2e(run_command) do |_stdin, output, _thread|
114
+ output.each { |line| logger << line }
115
+ end
116
+
117
+ info("Downloading files from #{instance.to_str}")
118
+ config[:downloads].to_h.each do |remotes, local|
119
+ debug("Downloading #{Array(remotes).join(", ")} to #{local}")
120
+ remote_connection.download(remotes, local)
121
+ end
122
+ debug("Download complete")
123
+ rescue Kitchen::Transport::TransportFailed => ex
124
+ raise ActionFailed, ex.message
125
+ ensure
126
+ cleanup_sandbox
127
+ end
128
+ end
129
+ end
130
+ end
@@ -0,0 +1,12 @@
1
+ # Deprecated AS PER THE PR - https://github.com/test-kitchen/test-kitchen/pull/1730
2
+ require_relative "chef_infra"
3
+
4
+ module Kitchen
5
+ module Provisioner
6
+ # Chef Zero provisioner.
7
+ #
8
+ # @author Fletcher Nichol <fnichol@nichol.ca>
9
+ class ChefZero < ChefInfra
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,23 @@
1
+ #
2
+ # Author:: Sean OMeara (<sean@sean.io>)
3
+ #
4
+ # Copyright (C) 2015, Sean OMeara
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+
18
+ module Kitchen
19
+ module Provisioner
20
+ # Version string for Dokken Kitchen driver
21
+ OMNIBUS_CHEF_VERSION = "0.0.1".freeze
22
+ end
23
+ end
@@ -0,0 +1,15 @@
1
+ # Handler to kill the run if any resource is updated
2
+ class UpdatedResources < ::Chef::Handler
3
+ def report
4
+ if updated_resources.size > 0
5
+ puts "First chef run should have reached a converged state."
6
+ puts "Resources updated in a second chef-client run:"
7
+ updated_resources.each do |r|
8
+ puts "- #{r}"
9
+ end
10
+ # exit 203 # chef handler catch Exception instead of StandardException
11
+ Process.kill("KILL", Process.pid)
12
+ end
13
+ end
14
+ end
15
+ report_handlers << UpdatedResources.new
@@ -0,0 +1,18 @@
1
+ Function Delete-AllDirs($dirs) {
2
+ $dirs | ForEach-Object {
3
+ if (Test-Path ($path = Unresolve-Path $_)) { Remove-Item $path -Recurse -Force }
4
+ }
5
+ }
6
+
7
+ Function Unresolve-Path($p) {
8
+ if ($p -eq $null) { return $null }
9
+ else { return $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($p) }
10
+ }
11
+
12
+ Function Make-RootPath($p) {
13
+ $p = Unresolve-Path $p
14
+ if (-Not (Test-Path $p)) { New-Item $p -ItemType directory | Out-Null }
15
+ }
16
+
17
+ Delete-AllDirs $dirs
18
+ Make-RootPath $root_path
@@ -0,0 +1,2 @@
1
+ $sudo_rm -rf $dirs
2
+ mkdir -p $root_path
@@ -0,0 +1,85 @@
1
+ $ErrorActionPreference = "stop"
2
+
3
+ Function Check-UpdateChef($root, $version) {
4
+ if (-Not (Test-Path $root)) { return $true }
5
+ elseif ("$version" -eq "true") { return $false }
6
+ elseif ("$version" -eq "latest") { return $true }
7
+ Try { $chef_version = (Get-Content $root\version-manifest.txt | select-object -first 1) }
8
+ Catch {
9
+ Try { $chef_version = (& $root\bin\chef-solo.bat -v) }
10
+ Catch { $chef_version = " " }
11
+ }
12
+
13
+ if ($chef_version.split(" ", 2)[1].StartsWith($version)) { return $false }
14
+ else { return $true }
15
+ }
16
+
17
+ Function Get-ChefMetadata($url) {
18
+ Try { $response = ($c = Make-WebClient).DownloadString($url) }
19
+ Finally { if ($c -ne $null) { $c.Dispose() } }
20
+
21
+ $md = ConvertFrom-StringData $response.Replace("`t", "=")
22
+ return @($md.url, $md.md5)
23
+ }
24
+
25
+ Function Get-MD5Sum($src) {
26
+ Try {
27
+ $c = New-Object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider
28
+ $bytes = $c.ComputeHash(($in = (Get-Item $src).OpenRead()))
29
+ return ([System.BitConverter]::ToString($bytes)).Replace("-", "").ToLower()
30
+ } Finally { if (($c -ne $null) -and ($c.GetType().GetMethod("Dispose") -ne $null)) { $c.Dispose() }; if ($in -ne $null) { $in.Dispose() } }
31
+ }
32
+
33
+ Function Download-Chef($md_url, $dst) {
34
+ $url, $md5 = Get-ChefMetadata $md_url
35
+
36
+ Try {
37
+ Log "Downloading package from $url"
38
+ ($c = Make-WebClient).DownloadFile($url, $dst)
39
+ Log "Download complete."
40
+ } Finally { if ($c -ne $null) { $c.Dispose() } }
41
+
42
+ if (($dmd5 = Get-MD5Sum $dst) -eq $md5) { Log "Successfully verified $dst" }
43
+ else { throw "MD5 for $dst $dmd5 does not match $md5" }
44
+ }
45
+
46
+ Function Install-Chef($msi) {
47
+ Log "Installing Chef Infra Client package $msi"
48
+ $p = Start-Process -FilePath "msiexec.exe" -ArgumentList "/qn /i $msi" -Passthru -Wait
49
+
50
+ if ($p.ExitCode -ne 0) { throw "msiexec was not successful. Received exit code $($p.ExitCode)" }
51
+
52
+ Remove-Item $msi -Force
53
+ Log "Installation complete"
54
+ }
55
+
56
+ Function Log($m) { Write-Host " $m`n" }
57
+
58
+ Function Make-WebClient {
59
+ $proxy = New-Object -TypeName System.Net.WebProxy
60
+ $proxy.Address = $env:http_proxy
61
+ $client = New-Object -TypeName System.Net.WebClient
62
+ $client.Proxy = $proxy
63
+ return $client
64
+ }
65
+
66
+ Function Unresolve-Path($p) {
67
+ if ($p -eq $null) { return $null }
68
+ else { return $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($p) }
69
+ }
70
+
71
+ Try {
72
+ $chef_omnibus_root = Unresolve-Path $chef_omnibus_root
73
+ $msi = Unresolve-Path $msi
74
+
75
+ if (Check-UpdateChef $chef_omnibus_root $version) {
76
+ Write-Host "-----> Installing Chef Infra Client package ($pretty_version)`n"
77
+ Download-Chef "$chef_metadata_url" $msi
78
+ Install-Chef $msi
79
+ } else {
80
+ Write-Host "-----> Chef Infra Client package installation detected ($pretty_version)`n"
81
+ }
82
+ Catch {
83
+ Write-Error ($_ | ft -Property * | out-string) -ErrorAction Continue
84
+ exit 1
85
+ }