chef-cli 3.1.6 → 5.2.0

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.
Files changed (31) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +4 -0
  3. data/chef-cli.gemspec +1 -1
  4. data/lib/chef-cli/command/export.rb +14 -6
  5. data/lib/chef-cli/command/generate.rb +5 -3
  6. data/lib/chef-cli/command/generator_commands/cookbook.rb +32 -2
  7. data/lib/chef-cli/command/generator_commands/recipe.rb +7 -0
  8. data/lib/chef-cli/helpers.rb +4 -4
  9. data/lib/chef-cli/policyfile_services/export_repo.rb +9 -13
  10. data/lib/chef-cli/skeletons/code_generator/recipes/cookbook.rb +50 -24
  11. data/lib/chef-cli/skeletons/code_generator/recipes/recipe.rb +11 -3
  12. data/lib/chef-cli/skeletons/code_generator/templates/default/delivery-project.toml.erb +36 -0
  13. data/lib/chef-cli/skeletons/code_generator/templates/default/recipe.yml.erb +18 -0
  14. data/lib/chef-cli/version.rb +1 -1
  15. data/spec/unit/command/export_spec.rb +18 -1
  16. data/spec/unit/command/generate_spec.rb +7 -0
  17. data/spec/unit/command/generator_commands/cookbook_spec.rb +91 -226
  18. data/spec/unit/command/generator_commands/recipe_spec.rb +34 -0
  19. data/spec/unit/helpers_spec.rb +111 -0
  20. data/spec/unit/policyfile_services/export_repo_spec.rb +51 -2
  21. metadata +7 -14
  22. data/lib/chef-cli/command/generator_commands/build_cookbook.rb +0 -126
  23. data/lib/chef-cli/skeletons/code_generator/files/default/build_cookbook/README.md +0 -146
  24. data/lib/chef-cli/skeletons/code_generator/files/default/build_cookbook/kitchen.yml +0 -21
  25. data/lib/chef-cli/skeletons/code_generator/files/default/build_cookbook/test-fixture-recipe.rb +0 -8
  26. data/lib/chef-cli/skeletons/code_generator/files/default/delivery-config.json +0 -17
  27. data/lib/chef-cli/skeletons/code_generator/recipes/build_cookbook.rb +0 -175
  28. data/lib/chef-cli/skeletons/code_generator/templates/default/build_cookbook/Berksfile.erb +0 -7
  29. data/lib/chef-cli/skeletons/code_generator/templates/default/build_cookbook/metadata.rb.erb +0 -10
  30. data/lib/chef-cli/skeletons/code_generator/templates/default/build_cookbook/recipe.rb.erb +0 -9
  31. data/spec/unit/command/generator_commands/build_cookbook_spec.rb +0 -377
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: db59602ecc8f8eccfed56173f71092e36e1cc55ad6ece88e311d26896d7a1e0b
4
- data.tar.gz: a584cf195ad81b8e3ddae995aa5c275545e795782439990ceb11e7dc2450eba6
3
+ metadata.gz: 5f07d3f568a94bf70e402ec732f96521233dafdf566b60bdc0ef9fad586e29af
4
+ data.tar.gz: 6b060133bceeb79fde35c38a41b7c4b6e4a71520f1f631cf37d4f0942838c0ed
5
5
  SHA512:
6
- metadata.gz: d1d34379eda31b8391fd142cc60ad71c0ac8bcd9e8a0fc9734da1a25cbfebdae1b2dda323ef6937ba2a05d11a86cc429f8a6d4692067d073b240dd5da3fdc5a2
7
- data.tar.gz: 27b7a7342e77162a34a7c6e3dcabfc9ba73d3081fb2b1bc6d144c5d01a8fa673bc2c60f6fc87ee2cc3841a970274f407b6cf9ab2c099d4cd6e7fdd845066c10c
6
+ metadata.gz: c429fdd87a900cd5c9178e44edc2eccf48a517a96aa0dd58de2adf5d1d9cdb0ddac522efeacb5676b9a12284bda262030fe8f5e65909ed40e21b5d3fb23845aa
7
+ data.tar.gz: db8f7b5d1a073a314489315aa35db2554575bba13e37f1ac28c99f0795eb57233d0ae0beb1989f7aa98d0970ed0826cd96849367b1161ecf144d76fc224e9167
data/Gemfile CHANGED
@@ -10,11 +10,15 @@ group :test do
10
10
  gem "cookstyle", "=7.7.2" # this forces dependabot PRs to open which triggers cookstyle CI on the chef generate command
11
11
  gem "chefstyle", "=1.6.2"
12
12
  gem "test-kitchen", ">= 2.11.1"
13
+
13
14
  if Gem::Version.new(RUBY_VERSION) < Gem::Version.new("2.6")
14
15
  gem "chef-zero", "~> 14"
15
16
  gem "chef", "~> 15"
16
17
  gem "chef-utils", "=16.6.14"
17
18
  end
19
+ if Gem::Version.new(RUBY_VERSION) < Gem::Version.new("2.7")
20
+ gem "ohai", "~> 16"
21
+ end
18
22
  end
19
23
 
20
24
  group :development do
data/chef-cli.gemspec CHANGED
@@ -29,7 +29,7 @@ Gem::Specification.new do |gem|
29
29
  gem.license = "Apache-2.0"
30
30
  gem.homepage = "https://www.chef.io/"
31
31
 
32
- gem.required_ruby_version = ">= 2.5"
32
+ gem.required_ruby_version = ">= 2.6"
33
33
 
34
34
  gem.files = %w{Rakefile LICENSE} +
35
35
  Dir.glob("Gemfile*") + # Includes Gemfile and locks
@@ -1,5 +1,5 @@
1
1
  #
2
- # Copyright:: Copyright (c) 2014-2019 Chef Software Inc.
2
+ # Copyright:: Copyright (c) Chef Software Inc.
3
3
  # License:: Apache License, Version 2.0
4
4
  #
5
5
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -67,6 +67,11 @@ module ChefCLI
67
67
  description: "Enable stacktraces and other debug output",
68
68
  default: false
69
69
 
70
+ option :policy_group,
71
+ long: "--policy-group POLICY_GROUP",
72
+ description: "The policy_group to include in the export (default: 'local')",
73
+ default: nil
74
+
70
75
  attr_reader :policyfile_relative_path
71
76
  attr_reader :export_dir
72
77
 
@@ -120,11 +125,14 @@ module ChefCLI
120
125
  end
121
126
 
122
127
  def export_service
123
- @export_service ||= PolicyfileServices::ExportRepo.new(policyfile: policyfile_relative_path,
124
- export_dir: export_dir,
125
- root_dir: Dir.pwd,
126
- archive: archive?,
127
- force: config[:force])
128
+ @export_service ||= PolicyfileServices::ExportRepo.new(
129
+ policyfile: policyfile_relative_path,
130
+ export_dir: export_dir,
131
+ root_dir: Dir.pwd,
132
+ archive: archive?,
133
+ force: config[:force],
134
+ policy_group: config[:policy_group]
135
+ )
128
136
  end
129
137
 
130
138
  def handle_error(error)
@@ -29,7 +29,6 @@ require_relative "generator_commands/template"
29
29
  require_relative "generator_commands/repo"
30
30
  require_relative "generator_commands/policyfile"
31
31
  require_relative "generator_commands/generator_generator"
32
- require_relative "generator_commands/build_cookbook"
33
32
  require_relative "../dist"
34
33
 
35
34
  module ChefCLI
@@ -56,7 +55,7 @@ module ChefCLI
56
55
  generator(:repo, :Repo, "Generate a #{ChefCLI::Dist::INFRA_PRODUCT} code repository")
57
56
  generator(:policyfile, :Policyfile, "Generate a Policyfile for use with the install/push commands")
58
57
  generator(:generator, :GeneratorGenerator, "Copy #{ChefCLI::Dist::PRODUCT}'s generator cookbook so you can customize it")
59
- generator(:'build-cookbook', :BuildCookbook, "Generate a build cookbook for use with #{ChefCLI::Dist::WORKFLOW}")
58
+ generator(:'build-cookbook', :BuildCookbook, "DEPRECATED: Generate a build cookbook for use with #{ChefCLI::Dist::WORKFLOW}")
60
59
 
61
60
  def self.banner_headline
62
61
  <<~E
@@ -83,6 +82,10 @@ module ChefCLI
83
82
  end
84
83
 
85
84
  def run(params)
85
+ if params[0] == "build-cookbook"
86
+ warn "[DEPRECATION] Chef Workflow (Delivery) is end of life (EOL) as of December 31, 2020 and this generator subcommand has been removed".freeze
87
+ return 1
88
+ end
86
89
  if ( generator_spec = generator_for(params[0]) )
87
90
  params.shift
88
91
  generator = GeneratorCommands.build(generator_spec.class_name, params)
@@ -118,7 +121,6 @@ module ChefCLI
118
121
  def have_generator?(name)
119
122
  self.class.generators.map { |g| g.name.to_s }.include?(name)
120
123
  end
121
-
122
124
  end
123
125
  end
124
126
  end
@@ -55,10 +55,17 @@ module ChefCLI
55
55
  boolean: true,
56
56
  default: nil
57
57
 
58
+ option :specs,
59
+ short: "-s",
60
+ long: "--specs",
61
+ description: "Generate a cookbook with sample ChefSpec specs",
62
+ boolean: true,
63
+ default: nil
64
+
58
65
  option :workflow,
59
66
  short: "-w",
60
67
  long: "--workflow",
61
- description: "Generate a cookbook with a full #{ChefCLI::Dist::WORKFLOW} build cookbook.",
68
+ description: "DEPRECATED: Generate a cookbook with a full #{ChefCLI::Dist::WORKFLOW} build cookbook.",
62
69
  boolean: true,
63
70
  default: false
64
71
 
@@ -69,6 +76,13 @@ module ChefCLI
69
76
  boolean: true,
70
77
  default: false
71
78
 
79
+ option :yaml,
80
+ short: "-y",
81
+ long: "--yaml",
82
+ description: "Generate a cookbook with YAML Recipe configuration file as the default.",
83
+ boolean: true,
84
+ default: nil
85
+
72
86
  option :pipeline,
73
87
  long: "--pipeline PIPELINE",
74
88
  description: "Use PIPELINE to set target branch to something other than master for the #{ChefCLI::Dist::WORKFLOW} build_cookbook",
@@ -81,6 +95,7 @@ module ChefCLI
81
95
  @cookbook_name = nil
82
96
  @policy_mode = true
83
97
  @verbose = false
98
+ @specs = false
84
99
  super
85
100
  end
86
101
 
@@ -103,6 +118,7 @@ module ChefCLI
103
118
  end
104
119
 
105
120
  def emit_post_create_message
121
+ default_recipe_file = yaml ? "default.yml" : "default.rb"
106
122
  if have_delivery_config?
107
123
  msg("Your cookbook is ready. To setup the pipeline, type `cd #{cookbook_name_or_path}`, then run `delivery init`")
108
124
  else
@@ -112,7 +128,7 @@ module ChefCLI
112
128
  msg("\nWhy not start by writing an InSpec test? Tests for the default recipe are stored at:\n")
113
129
  msg("test/integration/default/default_test.rb")
114
130
  msg("\nIf you'd prefer to dive right in, the default recipe can be found at:")
115
- msg("\nrecipes/default.rb\n")
131
+ msg("\nrecipes/#{default_recipe_file}\n")
116
132
  end
117
133
  end
118
134
 
@@ -133,11 +149,13 @@ module ChefCLI
133
149
  Generator.add_attr_to_context(:workflow_project_git_initialized, have_git? && !cookbook_path_in_git_repo?)
134
150
 
135
151
  Generator.add_attr_to_context(:verbose, verbose?)
152
+ Generator.add_attr_to_context(:specs, specs?)
136
153
 
137
154
  Generator.add_attr_to_context(:use_policyfile, policy_mode?)
138
155
  Generator.add_attr_to_context(:pipeline, pipeline)
139
156
  Generator.add_attr_to_context(:kitchen, kitchen)
140
157
  Generator.add_attr_to_context(:vscode_dir, create_vscode_dir?)
158
+ Generator.add_attr_to_context(:yaml, yaml)
141
159
  end
142
160
 
143
161
  def kitchen
@@ -148,6 +166,10 @@ module ChefCLI
148
166
  config[:pipeline]
149
167
  end
150
168
 
169
+ def yaml
170
+ config[:yaml]
171
+ end
172
+
151
173
  def policy_name
152
174
  cookbook_name
153
175
  end
@@ -188,6 +210,10 @@ module ChefCLI
188
210
  @verbose
189
211
  end
190
212
 
213
+ def specs?
214
+ @specs
215
+ end
216
+
191
217
  #
192
218
  # Is there a .delivery/cli.toml in the current dir or any of the parent dirs
193
219
  #
@@ -223,6 +249,10 @@ module ChefCLI
223
249
  @verbose = true
224
250
  end
225
251
 
252
+ if config[:specs]
253
+ @specs = true
254
+ end
255
+
226
256
  true
227
257
  end
228
258
 
@@ -26,6 +26,13 @@ module ChefCLI
26
26
 
27
27
  banner "Usage: #{ChefCLI::Dist::EXEC} generate recipe [path/to/cookbook] NAME [options]"
28
28
 
29
+ option :yaml,
30
+ short: "-y",
31
+ long: "--yaml",
32
+ description: "Generate recipe using YAML configuration format.",
33
+ boolean: true,
34
+ default: nil
35
+
29
36
  options.merge!(SharedGeneratorOptions.options)
30
37
 
31
38
  def recipe
@@ -117,11 +117,11 @@ module ChefCLI
117
117
  @omnibus_env ||=
118
118
  begin
119
119
  user_bin_dir = File.expand_path(File.join(Gem.user_dir, "bin"))
120
- path = [ omnibus_bin_dir, user_bin_dir, omnibus_embedded_bin_dir, ENV["PATH"] ]
120
+ path = [ omnibus_bin_dir, user_bin_dir, omnibus_embedded_bin_dir, ENV["PATH"].split(File::PATH_SEPARATOR) ]
121
121
  path << git_bin_dir if Dir.exist?(git_bin_dir)
122
122
  path << git_windows_bin_dir if Dir.exist?(git_windows_bin_dir)
123
123
  {
124
- "PATH" => path.join(File::PATH_SEPARATOR),
124
+ "PATH" => path.flatten.uniq.join(File::PATH_SEPARATOR),
125
125
  "GEM_ROOT" => Gem.default_dir,
126
126
  "GEM_HOME" => Gem.user_dir,
127
127
  "GEM_PATH" => Gem.path.join(File::PATH_SEPARATOR),
@@ -129,8 +129,6 @@ module ChefCLI
129
129
  end
130
130
  end
131
131
 
132
- private
133
-
134
132
  def omnibus_expand_path(*paths)
135
133
  dir = File.expand_path(File.join(paths))
136
134
  raise OmnibusInstallNotFound.new unless dir && File.directory?(dir)
@@ -138,6 +136,8 @@ module ChefCLI
138
136
  dir
139
137
  end
140
138
 
139
+ private
140
+
141
141
  def expected_omnibus_root
142
142
  File.expand_path(File.join(Gem.ruby, "..", "..", ".."))
143
143
  end
@@ -1,5 +1,5 @@
1
1
  #
2
- # Copyright:: Copyright (c) 2014-2019 Chef Software Inc.
2
+ # Copyright:: Copyright (c) Chef Software Inc.
3
3
  # License:: Apache License, Version 2.0
4
4
  #
5
5
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -33,20 +33,15 @@ module ChefCLI
33
33
 
34
34
  class ExportRepo
35
35
 
36
- # Policy groups provide namespaces for policies so that a Chef Infra Server can
37
- # have multiple active iterations of a policy at once, but we don't need
38
- # this when serving a single exported policy via Chef Zero, so hardcode
39
- # it to a "well known" value:
40
- POLICY_GROUP = "local".freeze
41
-
42
36
  include Policyfile::StorageConfigDelegation
43
37
 
44
38
  attr_reader :storage_config
45
39
  attr_reader :root_dir
46
40
  attr_reader :export_dir
47
41
  attr_reader :ui
42
+ attr_reader :policy_group
48
43
 
49
- def initialize(policyfile: nil, export_dir: nil, root_dir: nil, archive: false, force: false)
44
+ def initialize(policyfile: nil, export_dir: nil, root_dir: nil, archive: false, force: false, policy_group: nil)
50
45
  @root_dir = root_dir
51
46
  @export_dir = File.expand_path(export_dir)
52
47
  @archive = archive
@@ -55,6 +50,8 @@ module ChefCLI
55
50
 
56
51
  @policy_data = nil
57
52
  @policyfile_lock = nil
53
+ @policy_group = policy_group
54
+ @policy_group ||= "local".freeze
58
55
 
59
56
  policyfile_rel_path = policyfile || "Policyfile.rb"
60
57
  policyfile_full_path = File.expand_path(policyfile_rel_path, root_dir)
@@ -243,7 +240,7 @@ module ChefCLI
243
240
  #
244
241
 
245
242
  policy_name '#{policy_name}'
246
- policy_group 'local'
243
+ policy_group '#{policy_group}'
247
244
 
248
245
  use_policyfile true
249
246
  policy_document_native_api true
@@ -301,9 +298,8 @@ module ChefCLI
301
298
  ### policy_groups/
302
299
 
303
300
  Policy groups are used by Chef Infra Server to manage multiple revisions of the same
304
- policy. However, exported policies contain only a single policy revision, so
305
- this policy group name is hardcoded to "local" and should not be changed.
306
-
301
+ policy. The default "local" policy is recommended for export use since there can be
302
+ no different revisions when not utilizing a server.
307
303
  README
308
304
  end
309
305
  end
@@ -389,7 +385,7 @@ module ChefCLI
389
385
  end
390
386
 
391
387
  def policy_group_repo_item_path
392
- File.join(staging_dir, "policy_groups", "local.json")
388
+ File.join(staging_dir, "policy_groups", "#{policy_group}.json")
393
389
  end
394
390
 
395
391
  def dot_chef_staging_dir
@@ -92,43 +92,56 @@ template "#{cookbook_dir}/test/integration/default/default_test.rb" do
92
92
  end
93
93
 
94
94
  # ChefSpec
95
- directory "#{cookbook_dir}/spec/unit/recipes" do
96
- recursive true
97
- end
98
-
99
- cookbook_file "#{cookbook_dir}/spec/spec_helper.rb" do
100
- if context.use_policyfile
101
- source 'spec_helper_policyfile.rb'
102
- else
103
- source 'spec_helper.rb'
95
+ if context.specs
96
+ directory "#{cookbook_dir}/spec/unit/recipes" do
97
+ recursive true
104
98
  end
105
99
 
106
- action :create_if_missing
107
- end
100
+ cookbook_file "#{cookbook_dir}/spec/spec_helper.rb" do
101
+ if context.use_policyfile
102
+ source 'spec_helper_policyfile.rb'
103
+ else
104
+ source 'spec_helper.rb'
105
+ end
108
106
 
109
- template "#{cookbook_dir}/spec/unit/recipes/default_spec.rb" do
110
- source 'recipe_spec.rb.erb'
111
- helpers(ChefCLI::Generator::TemplateHelper)
112
- action :create_if_missing
107
+ action :create_if_missing
108
+ end
109
+
110
+ template "#{cookbook_dir}/spec/unit/recipes/default_spec.rb" do
111
+ source 'recipe_spec.rb.erb'
112
+ helpers(ChefCLI::Generator::TemplateHelper)
113
+ action :create_if_missing
114
+ end
113
115
  end
114
116
 
115
117
  # Recipes
116
118
  directory "#{cookbook_dir}/recipes"
117
119
 
118
- template "#{cookbook_dir}/recipes/default.rb" do
119
- source 'recipe.rb.erb'
120
- helpers(ChefCLI::Generator::TemplateHelper)
121
- action :create_if_missing
120
+ if context.yaml
121
+ template "#{cookbook_dir}/recipes/default.yml" do
122
+ source 'recipe.yml.erb'
123
+ helpers(ChefCLI::Generator::TemplateHelper)
124
+ action :create_if_missing
125
+ end
126
+ else
127
+ template "#{cookbook_dir}/recipes/default.rb" do
128
+ source 'recipe.rb.erb'
129
+ helpers(ChefCLI::Generator::TemplateHelper)
130
+ action :create_if_missing
131
+ end
122
132
  end
123
133
 
124
134
  # the same will be done below if workflow was enabled so avoid double work and skip this
125
135
  unless context.enable_workflow
126
136
  directory "#{cookbook_dir}/.delivery"
127
137
 
128
- # Adding the delivery local-mode config
129
- cookbook_file "#{cookbook_dir}/.delivery/project.toml" do
130
- source 'delivery-project.toml'
131
- not_if { ::File.exist?("#{cookbook_dir}/.delivery/project.toml") }
138
+ template "#{cookbook_dir}/.delivery/project.toml" do
139
+ variables(
140
+ specs: context.specs
141
+ )
142
+ source 'delivery-project.toml.erb'
143
+ helpers(ChefCLI::Generator::TemplateHelper)
144
+ action :create_if_missing
132
145
  end
133
146
  end
134
147
 
@@ -177,4 +190,17 @@ if context.vscode_dir
177
190
  end
178
191
  end
179
192
 
180
- include_recipe '::build_cookbook' if context.enable_workflow
193
+ if context.enable_workflow
194
+ warn "\n---------WARNING---------\nThe --workflow flag is deprecated as #{ChefCLI::Dist::WORKFLOW} is EOL as of December 2020.\n\nThis flag will be ignored and will be removed in a future release.\n-------------------------\n\n"
195
+ directory "#{cookbook_dir}/.delivery"
196
+
197
+ template "#{cookbook_dir}/.delivery/project.toml" do
198
+ variables(
199
+ specs: context.specs
200
+ )
201
+ source 'delivery-project.toml.erb'
202
+ helpers(ChefCLI::Generator::TemplateHelper)
203
+ action :create_if_missing
204
+ end
205
+
206
+ end
@@ -1,6 +1,7 @@
1
1
  context = ChefCLI::Generator.context
2
2
  cookbook_dir = File.join(context.cookbook_root, context.cookbook_name)
3
3
  recipe_path = File.join(cookbook_dir, 'recipes', "#{context.new_file_basename}.rb")
4
+ recipe_path_yml = File.join(cookbook_dir, 'recipes', "#{context.new_file_basename}.yml")
4
5
  spec_helper_path = File.join(cookbook_dir, 'spec', 'spec_helper.rb')
5
6
  spec_dir = File.join(cookbook_dir, 'spec', 'unit', 'recipes')
6
7
  spec_path = File.join(spec_dir, "#{context.new_file_basename}_spec.rb")
@@ -44,7 +45,14 @@ template inspec_path do
44
45
  end
45
46
 
46
47
  # Recipe
47
- template recipe_path do
48
- source 'recipe.rb.erb'
49
- helpers(ChefCLI::Generator::TemplateHelper)
48
+ if context.yaml
49
+ template recipe_path_yml do
50
+ source 'recipe.yml.erb'
51
+ helpers(ChefCLI::Generator::TemplateHelper)
52
+ end
53
+ else
54
+ template recipe_path do
55
+ source 'recipe.rb.erb'
56
+ helpers(ChefCLI::Generator::TemplateHelper)
57
+ end
50
58
  end