chef-cli 5.0.1 → 5.3.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +15 -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 +14 -1
  7. data/lib/chef-cli/command/generator_commands/recipe.rb +7 -0
  8. data/lib/chef-cli/cookbook_metadata.rb +1 -1
  9. data/lib/chef-cli/exceptions.rb +13 -1
  10. data/lib/chef-cli/policyfile/dsl.rb +16 -1
  11. data/lib/chef-cli/policyfile_services/export_repo.rb +9 -13
  12. data/lib/chef-cli/skeletons/code_generator/recipes/cookbook.rb +12 -4
  13. data/lib/chef-cli/skeletons/code_generator/recipes/recipe.rb +11 -3
  14. data/lib/chef-cli/skeletons/code_generator/templates/default/recipe.yml.erb +18 -0
  15. data/lib/chef-cli/version.rb +1 -1
  16. data/spec/unit/command/export_spec.rb +18 -1
  17. data/spec/unit/command/generate_spec.rb +7 -0
  18. data/spec/unit/command/generator_commands/cookbook_spec.rb +49 -195
  19. data/spec/unit/command/generator_commands/recipe_spec.rb +34 -0
  20. data/spec/unit/cookbook_metadata_spec.rb +20 -1
  21. data/spec/unit/fixtures/example_cookbook_both_metadata/.gitignore +17 -0
  22. data/spec/unit/fixtures/example_cookbook_both_metadata/.kitchen.yml +16 -0
  23. data/spec/unit/fixtures/example_cookbook_both_metadata/Berksfile +3 -0
  24. data/spec/unit/fixtures/example_cookbook_both_metadata/README.md +4 -0
  25. data/spec/unit/fixtures/example_cookbook_both_metadata/chefignore +96 -0
  26. data/spec/unit/fixtures/example_cookbook_both_metadata/metadata.json +5 -0
  27. data/spec/unit/fixtures/example_cookbook_both_metadata/metadata.rb +9 -0
  28. data/spec/unit/fixtures/example_cookbook_both_metadata/recipes/default.rb +8 -0
  29. data/spec/unit/policyfile_evaluation_spec.rb +67 -1
  30. data/spec/unit/policyfile_services/export_repo_spec.rb +51 -2
  31. metadata +21 -15
  32. data/lib/chef-cli/command/generator_commands/build_cookbook.rb +0 -126
  33. data/lib/chef-cli/skeletons/code_generator/files/default/build_cookbook/README.md +0 -146
  34. data/lib/chef-cli/skeletons/code_generator/files/default/build_cookbook/kitchen.yml +0 -21
  35. data/lib/chef-cli/skeletons/code_generator/files/default/build_cookbook/test-fixture-recipe.rb +0 -8
  36. data/lib/chef-cli/skeletons/code_generator/files/default/delivery-config.json +0 -17
  37. data/lib/chef-cli/skeletons/code_generator/recipes/build_cookbook.rb +0 -175
  38. data/lib/chef-cli/skeletons/code_generator/templates/default/build_cookbook/Berksfile.erb +0 -7
  39. data/lib/chef-cli/skeletons/code_generator/templates/default/build_cookbook/metadata.rb.erb +0 -10
  40. data/lib/chef-cli/skeletons/code_generator/templates/default/build_cookbook/recipe.rb.erb +0 -9
  41. 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: '08825f8c8c80fbf8119e1108de938d1dbe3c108309dfefb4f3454ac9fee3397d'
4
- data.tar.gz: b3327078e2ee12265d9aabbeab42c9b510adfb1dad6a17cef3b9b8951239030f
3
+ metadata.gz: 05bd8553c544e5314b22e44e6a6790b16cf2bc6705945cbd590bc78a5b234f72
4
+ data.tar.gz: ee861a1b0aa6504b3be69bbb2496dc8892a313122516f76efe6f0c3ef5adc8dd
5
5
  SHA512:
6
- metadata.gz: 2b579beed9b27e92883fd1990a8de48c36a04d947ff6eed0a01df1bd49c38b3d325cad36dcf1eb8801e6107f6fd3dbaa8b2d8afb29bd51618a9dab5861883d58
7
- data.tar.gz: 14a6e42f8b8b24d731639ba48eb16b3dae8de21d35adce5f9bda99d59b1ad47accdd4686c883183328573ee04e14fddf114960705f930d110e490beaa017080a
6
+ metadata.gz: aed359d7ff0e72ca0fd3d62a83a4ec4e17af40ddfdb40083b0e115f5fe232e596c8b68286f3d7b2a904516ed88f4b885814aba531b987aa53f3dd185dbba3688
7
+ data.tar.gz: 95185fa6d2557b24a07169d7f36dd0755cd655dbb99245615a428ccfbd4b670ebdeaaf0319690b85ac62da14ed5a0859d28d4dfbe1c5f07b8ba499a7b4ab2438
data/Gemfile CHANGED
@@ -10,6 +10,21 @@ 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
+
14
+ if Gem::Version.new(RUBY_VERSION) < Gem::Version.new("2.6")
15
+ gem "chef-zero", "~> 14"
16
+ gem "chef", "~> 15"
17
+ gem "chef-utils", "=16.6.14"
18
+ end
19
+ if Gem::Version.new(RUBY_VERSION) < Gem::Version.new("2.7")
20
+ gem "ohai", "~> 16"
21
+ end
22
+ end
23
+
24
+ group :development do
25
+ gem "pry"
26
+ gem "pry-byebug"
27
+ gem "rb-readline"
13
28
  end
14
29
 
15
30
  group :profile do
data/chef-cli.gemspec CHANGED
@@ -45,7 +45,7 @@ Gem::Specification.new do |gem|
45
45
  gem.add_dependency "minitar", "~> 0.6"
46
46
  gem.add_dependency "chef", ">= 15.0"
47
47
  gem.add_dependency "solve", "< 5.0", "> 2.0"
48
- gem.add_dependency "addressable", ">= 2.3.5", "< 2.8"
48
+ gem.add_dependency "addressable", ">= 2.3.5", "< 2.9"
49
49
  gem.add_dependency "cookbook-omnifetch", "~> 0.5"
50
50
  gem.add_dependency "diff-lcs", ">= 1.0", "< 1.4" # 1.4 changes the output
51
51
  gem.add_dependency "pastel", "~> 0.7" # used for policyfile differ
@@ -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
@@ -76,6 +76,13 @@ module ChefCLI
76
76
  boolean: true,
77
77
  default: false
78
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
+
79
86
  option :pipeline,
80
87
  long: "--pipeline PIPELINE",
81
88
  description: "Use PIPELINE to set target branch to something other than master for the #{ChefCLI::Dist::WORKFLOW} build_cookbook",
@@ -111,6 +118,7 @@ module ChefCLI
111
118
  end
112
119
 
113
120
  def emit_post_create_message
121
+ default_recipe_file = yaml ? "default.yml" : "default.rb"
114
122
  if have_delivery_config?
115
123
  msg("Your cookbook is ready. To setup the pipeline, type `cd #{cookbook_name_or_path}`, then run `delivery init`")
116
124
  else
@@ -120,7 +128,7 @@ module ChefCLI
120
128
  msg("\nWhy not start by writing an InSpec test? Tests for the default recipe are stored at:\n")
121
129
  msg("test/integration/default/default_test.rb")
122
130
  msg("\nIf you'd prefer to dive right in, the default recipe can be found at:")
123
- msg("\nrecipes/default.rb\n")
131
+ msg("\nrecipes/#{default_recipe_file}\n")
124
132
  end
125
133
  end
126
134
 
@@ -147,6 +155,7 @@ module ChefCLI
147
155
  Generator.add_attr_to_context(:pipeline, pipeline)
148
156
  Generator.add_attr_to_context(:kitchen, kitchen)
149
157
  Generator.add_attr_to_context(:vscode_dir, create_vscode_dir?)
158
+ Generator.add_attr_to_context(:yaml, yaml)
150
159
  end
151
160
 
152
161
  def kitchen
@@ -157,6 +166,10 @@ module ChefCLI
157
166
  config[:pipeline]
158
167
  end
159
168
 
169
+ def yaml
170
+ config[:yaml]
171
+ end
172
+
160
173
  def policy_name
161
174
  cookbook_name
162
175
  end
@@ -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
@@ -1,5 +1,5 @@
1
1
  #
2
- # Copyright:: Copyright (c) 2014-2018 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");
@@ -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");
@@ -71,6 +71,18 @@ module ChefCLI
71
71
  class MissingCookbookLockData < StandardError
72
72
  end
73
73
 
74
+ class PolicyfileMissingCookbookMetadata < StandardError
75
+ def initialize(cookbook_root)
76
+ super("Policyfile specified to use cookbook metadata, but neither #{cookbook_root}/metadata.rb or #{cookbook_root}/metadata.json was found.")
77
+ end
78
+ end
79
+
80
+ class PolicyfileBadCookbookMetadata < StandardError
81
+ def initialize(cookbook_root, e)
82
+ super("Cookbook metadata for cookbook at #{cookbook_root} could not be parsed:\n Original Exception: #{e}")
83
+ end
84
+ end
85
+
74
86
  class InvalidLockfile < StandardError
75
87
  end
76
88
 
@@ -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");
@@ -138,6 +138,21 @@ module ChefCLI
138
138
  end
139
139
  end
140
140
 
141
+ def metadata
142
+ cookbook_root = storage_config.relative_paths_root
143
+ unless File.exist?(File.join(cookbook_root, "metadata.rb")) || File.exist?(File.join(cookbook_root, "metadata.json"))
144
+ raise PolicyfileMissingCookbookMetadata.new(cookbook_root)
145
+ end
146
+
147
+ begin
148
+ cookbook_name = CookbookMetadata.from_path(cookbook_root).cookbook_name
149
+ rescue Exception => e
150
+ raise PolicyfileBadCookbookMetadata.new(cookbook_root, e)
151
+ end
152
+ name cookbook_name if name.nil?
153
+ cookbook(cookbook_name, path: ".")
154
+ end
155
+
141
156
  def default
142
157
  @node_attributes.default
143
158
  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
@@ -117,10 +117,18 @@ end
117
117
  # Recipes
118
118
  directory "#{cookbook_dir}/recipes"
119
119
 
120
- template "#{cookbook_dir}/recipes/default.rb" do
121
- source 'recipe.rb.erb'
122
- helpers(ChefCLI::Generator::TemplateHelper)
123
- 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
124
132
  end
125
133
 
126
134
  # the same will be done below if workflow was enabled so avoid double work and skip this
@@ -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
@@ -0,0 +1,18 @@
1
+ #
2
+ # Cookbook:: <%= cookbook_name %>
3
+ # Recipe:: <%= recipe_name %>
4
+ #
5
+ <%= license_description('#') %>
6
+
7
+ ---
8
+ resources:
9
+ # Example Syntax
10
+ # Additional snippets are available using the Chef Infra Extension for Visual Studio Code
11
+ # - type: file
12
+ # name: '/path/to/file'
13
+ # content: 'content'
14
+ # owner: 'root'
15
+ # group: 'root'
16
+ # mode: '0755'
17
+ # action:
18
+ # - create
@@ -16,5 +16,5 @@
16
16
  #
17
17
 
18
18
  module ChefCLI
19
- VERSION = "5.0.1".freeze
19
+ VERSION = "5.3.1".freeze
20
20
  end
@@ -1,5 +1,5 @@
1
1
  #
2
- # Copyright:: Copyright (c) 2014-2018 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");
@@ -84,6 +84,10 @@ describe ChefCLI::Command::Export do
84
84
  expect(command.export_service.policyfile_filename).to eq(File.expand_path("Policyfile.rb"))
85
85
  end
86
86
 
87
+ it "uses the default policy_group name" do
88
+ expect(command.export_service.policy_group).to eq("local")
89
+ end
90
+
87
91
  end
88
92
 
89
93
  context "when a Policyfile relative path and export path are given" do
@@ -98,6 +102,19 @@ describe ChefCLI::Command::Export do
98
102
  expect(command.export_service.policyfile_filename).to eq(File.expand_path("CustomNamedPolicy.rb"))
99
103
  end
100
104
  end
105
+
106
+ context "when a policy_group is given" do
107
+
108
+ let(:params) { [ "path/to/export", "--policy_group", "production" ] }
109
+
110
+ it "configures the export service with the export path" do
111
+ expect(command.export_service.export_dir).to eq(File.expand_path("path/to/export"))
112
+ end
113
+
114
+ it "configures the export service with the policyfile relative path" do
115
+ expect(command.export_service.policy_group).to eq("production")
116
+ end
117
+ end
101
118
  end
102
119
 
103
120
  describe "running the export" do
@@ -138,5 +138,12 @@ describe ChefCLI::Command::Generate do
138
138
 
139
139
  end
140
140
 
141
+ describe "When build-cookbookis given as subcommand" do
142
+ it "shows deprecation warning when" do
143
+ result = generate.run(%w{build-cookbook example})
144
+ expect(result).to eq(1)
145
+ end
146
+ end
147
+
141
148
  end
142
149
  end