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
@@ -0,0 +1,5 @@
1
+ {
2
+ "name": "example_cookbook",
3
+ "version": "0.1.0"
4
+ }
5
+
@@ -0,0 +1,9 @@
1
+ # This is vitally important for cookbooks that ship with context-dependent
2
+ # metadata.rb (to do things like auto incrementing semantic versioning) where
3
+ # the metadata.json means the cookbook is 'finalized' and must be authoritative.
4
+ # We need to never try to parse metadata.rb if the metadata.json exists. It isn't
5
+ # sufficient to simply not use the metadata.rb file, but we must allow the
6
+ # metadata.rb to have an invalid parse due to LoadErrors in the environment where
7
+ # the cookbook is being loaded.
8
+
9
+ raise "we never want to read the metadata.rb if metadata.json exists"
@@ -0,0 +1,8 @@
1
+ #
2
+ # Cookbook Name:: example_cookbook
3
+ # Recipe:: default
4
+ #
5
+ # Copyright (C) 2014
6
+ #
7
+ #
8
+ #
@@ -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");
@@ -555,6 +555,72 @@ describe ChefCLI::PolicyfileCompiler do
555
555
 
556
556
  end
557
557
 
558
+ describe "the metadata DSL keyword used in a cookbook with metadata" do
559
+ let(:policyfile_rb) do
560
+ <<-EOH
561
+ run_list "foo"
562
+ metadata
563
+ EOH
564
+ end
565
+ it "reads the metadata from the current cookbook" do
566
+ expect(File).to receive(:exist?).with("./metadata.rb").and_return(true)
567
+ expect(ChefCLI::CookbookMetadata).to receive(:from_path).and_return(double(cookbook_name: "foo"))
568
+ expect(policyfile.errors.size).to eq(0)
569
+ expect(policyfile.errors.first).to eq(nil)
570
+ end
571
+ end
572
+
573
+ describe "the metadata DSL keyword used in a cookbook without metadata" do
574
+ let(:policyfile_rb) do
575
+ <<-EOH
576
+ run_list "foo"
577
+ metadata
578
+ EOH
579
+ end
580
+ it "throws a missing cookbook metadata error" do
581
+ expected = <<~EOH
582
+ Evaluation of policyfile 'TestPolicyfile.rb' raised an exception
583
+ Exception: ChefCLI::PolicyfileMissingCookbookMetadata "Policyfile specified to use cookbook metadata, but neither ./metadata.rb or ./metadata.json was found."
584
+
585
+ Relevant Code:
586
+ 2: metadata
587
+
588
+ Backtrace:
589
+ TestPolicyfile.rb:2:in `eval_policyfile'
590
+ EOH
591
+ expect(File).to receive(:exist?).with("./metadata.rb").and_return(false)
592
+ expect(File).to receive(:exist?).with("./metadata.json").and_return(false)
593
+ expect(policyfile.errors.size).to eq(1)
594
+ expect(policyfile.errors.first).to eq(expected)
595
+ end
596
+ end
597
+
598
+ describe "the metadata DSL keyword used in a cookbook with bad metadata" do
599
+ let(:policyfile_rb) do
600
+ <<-EOH
601
+ run_list "foo"
602
+ metadata
603
+ EOH
604
+ end
605
+ it "throws a bad cookbook metadata error" do
606
+ expected = <<~EOH
607
+ Evaluation of policyfile 'TestPolicyfile.rb' raised an exception
608
+ Exception: ChefCLI::PolicyfileBadCookbookMetadata "Cookbook metadata for cookbook at . could not be parsed:
609
+ Original Exception: No such file or directory @ rb_sysopen - ./metadata.json"
610
+
611
+ Relevant Code:
612
+ 2: metadata
613
+
614
+ Backtrace:
615
+ TestPolicyfile.rb:2:in `eval_policyfile'
616
+ EOH
617
+ expect(File).to receive(:exist?).with("./metadata.rb").and_return(true)
618
+ expect(File).to receive(:exist?).with("./metadata.json").and_return(true)
619
+ expect(policyfile.errors.size).to eq(1)
620
+ expect(policyfile.errors.first).to eq(expected)
621
+ end
622
+ end
623
+
558
624
  describe "defining attributes" do
559
625
 
560
626
  let(:policyfile_rb) do
@@ -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");
@@ -42,12 +42,15 @@ describe ChefCLI::PolicyfileServices::ExportRepo do
42
42
 
43
43
  let(:archive) { false }
44
44
 
45
+ let(:policy_group) { nil }
46
+
45
47
  subject(:export_service) do
46
48
  described_class.new(policyfile: policyfile_rb_explicit_name,
47
49
  root_dir: working_dir,
48
50
  export_dir: export_dir,
49
51
  archive: archive,
50
- force: force_export)
52
+ force: force_export,
53
+ policy_group: policy_group)
51
54
  end
52
55
 
53
56
  it "uses Policyfile.rb as the default Policyfile name" do
@@ -306,6 +309,52 @@ describe ChefCLI::PolicyfileServices::ExportRepo do
306
309
  expect(File).to exist(readme_path)
307
310
  end
308
311
 
312
+ context "when the policy_group is changed" do
313
+ let(:policy_group) { "production" }
314
+ it "creates a policy_group file for a specified policy group with the revision id of the exported policy" do
315
+ exported_policy_group_path = File.join(export_dir, "policy_groups", "production.json")
316
+ exported_policy_group_data = FFI_Yajl::Parser.parse(IO.read(exported_policy_group_path))
317
+
318
+ expected_data = { "policies" => { "install-example" => { "revision_id" => revision_id } } }
319
+
320
+ expect(exported_policy_group_data).to eq(expected_data)
321
+ end
322
+
323
+ it "creates a working local mode configuration file with the changed policy_group" do
324
+ expected_config_text = <<~CONFIG
325
+ ### Chef Infra Client Configuration ###
326
+ # The settings in this file will configure chef to apply the exported policy in
327
+ # this directory. To use it, run:
328
+ #
329
+ # chef-client -z
330
+ #
331
+
332
+ policy_name 'install-example'
333
+ policy_group 'production'
334
+
335
+ use_policyfile true
336
+ policy_document_native_api true
337
+
338
+ # In order to use this repo, you need a version of Chef Infra Client and Chef Zero
339
+ # that supports policyfile "native mode" APIs:
340
+ current_version = Gem::Version.new(Chef::VERSION)
341
+ unless Gem::Requirement.new(">= 12.7").satisfied_by?(current_version)
342
+ puts("!" * 80)
343
+ puts(<<-MESSAGE)
344
+ This Chef Repo requires features introduced in Chef Infra Client 12.7, but you are using
345
+ Chef \#{Chef::VERSION}. Please upgrade to Chef Infra Client 12.7 or later.
346
+ MESSAGE
347
+ puts("!" * 80)
348
+ exit!(1)
349
+ end
350
+
351
+ CONFIG
352
+ config_path = File.join(export_dir, ".chef", "config.rb")
353
+ expect(File).to exist(config_path)
354
+ expect(IO.read(config_path)).to eq(expected_config_text)
355
+ end
356
+ end
357
+
309
358
  end
310
359
 
311
360
  context "when the export dir is empty" do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: chef-cli
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.0.1
4
+ version: 5.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chef Software, Inc.
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-04-29 00:00:00.000000000 Z
11
+ date: 2021-07-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mixlib-cli
@@ -127,7 +127,7 @@ dependencies:
127
127
  version: 2.3.5
128
128
  - - "<"
129
129
  - !ruby/object:Gem::Version
130
- version: '2.8'
130
+ version: '2.9'
131
131
  type: :runtime
132
132
  prerelease: false
133
133
  version_requirements: !ruby/object:Gem::Requirement
@@ -137,7 +137,7 @@ dependencies:
137
137
  version: 2.3.5
138
138
  - - "<"
139
139
  - !ruby/object:Gem::Version
140
- version: '2.8'
140
+ version: '2.9'
141
141
  - !ruby/object:Gem::Dependency
142
142
  name: cookbook-omnifetch
143
143
  requirement: !ruby/object:Gem::Requirement
@@ -240,7 +240,6 @@ files:
240
240
  - lib/chef-cli/command/generator_commands.rb
241
241
  - lib/chef-cli/command/generator_commands/attribute.rb
242
242
  - lib/chef-cli/command/generator_commands/base.rb
243
- - lib/chef-cli/command/generator_commands/build_cookbook.rb
244
243
  - lib/chef-cli/command/generator_commands/chef_exts/generator_desc_resource.rb
245
244
  - lib/chef-cli/command/generator_commands/chef_exts/quieter_doc_formatter.rb
246
245
  - lib/chef-cli/command/generator_commands/chef_exts/recipe_dsl_ext.rb
@@ -328,13 +327,9 @@ files:
328
327
  - lib/chef-cli/service_exceptions.rb
329
328
  - lib/chef-cli/shell_out.rb
330
329
  - lib/chef-cli/skeletons/code_generator/files/default/Berksfile
331
- - lib/chef-cli/skeletons/code_generator/files/default/build_cookbook/README.md
332
- - lib/chef-cli/skeletons/code_generator/files/default/build_cookbook/kitchen.yml
333
- - lib/chef-cli/skeletons/code_generator/files/default/build_cookbook/test-fixture-recipe.rb
334
330
  - lib/chef-cli/skeletons/code_generator/files/default/chefignore
335
331
  - lib/chef-cli/skeletons/code_generator/files/default/cookbook_readmes/README-policy.md
336
332
  - lib/chef-cli/skeletons/code_generator/files/default/cookbook_readmes/README.md
337
- - lib/chef-cli/skeletons/code_generator/files/default/delivery-config.json
338
333
  - lib/chef-cli/skeletons/code_generator/files/default/delivery-project.toml
339
334
  - lib/chef-cli/skeletons/code_generator/files/default/gitignore
340
335
  - lib/chef-cli/skeletons/code_generator/files/default/repo/README.md
@@ -354,7 +349,6 @@ files:
354
349
  - lib/chef-cli/skeletons/code_generator/files/default/spec_helper_policyfile.rb
355
350
  - lib/chef-cli/skeletons/code_generator/metadata.rb
356
351
  - lib/chef-cli/skeletons/code_generator/recipes/attribute.rb
357
- - lib/chef-cli/skeletons/code_generator/recipes/build_cookbook.rb
358
352
  - lib/chef-cli/skeletons/code_generator/recipes/cookbook.rb
359
353
  - lib/chef-cli/skeletons/code_generator/recipes/cookbook_file.rb
360
354
  - lib/chef-cli/skeletons/code_generator/recipes/helpers.rb
@@ -372,9 +366,6 @@ files:
372
366
  - lib/chef-cli/skeletons/code_generator/templates/default/Policyfile.rb.erb
373
367
  - lib/chef-cli/skeletons/code_generator/templates/default/README.md.erb
374
368
  - lib/chef-cli/skeletons/code_generator/templates/default/attribute.rb.erb
375
- - lib/chef-cli/skeletons/code_generator/templates/default/build_cookbook/Berksfile.erb
376
- - lib/chef-cli/skeletons/code_generator/templates/default/build_cookbook/metadata.rb.erb
377
- - lib/chef-cli/skeletons/code_generator/templates/default/build_cookbook/recipe.rb.erb
378
369
  - lib/chef-cli/skeletons/code_generator/templates/default/cookbook_file.erb
379
370
  - lib/chef-cli/skeletons/code_generator/templates/default/delivery-project.toml.erb
380
371
  - lib/chef-cli/skeletons/code_generator/templates/default/helpers.rb.erb
@@ -384,6 +375,7 @@ files:
384
375
  - lib/chef-cli/skeletons/code_generator/templates/default/kitchen_policyfile.yml.erb
385
376
  - lib/chef-cli/skeletons/code_generator/templates/default/metadata.rb.erb
386
377
  - lib/chef-cli/skeletons/code_generator/templates/default/recipe.rb.erb
378
+ - lib/chef-cli/skeletons/code_generator/templates/default/recipe.yml.erb
387
379
  - lib/chef-cli/skeletons/code_generator/templates/default/recipe_spec.rb.erb
388
380
  - lib/chef-cli/skeletons/code_generator/templates/default/repo/gitignore.erb
389
381
  - lib/chef-cli/skeletons/code_generator/templates/default/resource.rb.erb
@@ -416,7 +408,6 @@ files:
416
408
  - spec/unit/command/generate_spec.rb
417
409
  - spec/unit/command/generator_commands/attribute_spec.rb
418
410
  - spec/unit/command/generator_commands/base_spec.rb
419
- - spec/unit/command/generator_commands/build_cookbook_spec.rb
420
411
  - spec/unit/command/generator_commands/chef_exts/generator_desc_resource_spec.rb
421
412
  - spec/unit/command/generator_commands/chef_exts/recipe_dsl_ext_spec.rb
422
413
  - spec/unit/command/generator_commands/cookbook_file_spec.rb
@@ -494,6 +485,14 @@ files:
494
485
  - spec/unit/fixtures/example_cookbook/chefignore
495
486
  - spec/unit/fixtures/example_cookbook/metadata.rb
496
487
  - spec/unit/fixtures/example_cookbook/recipes/default.rb
488
+ - spec/unit/fixtures/example_cookbook_both_metadata/.gitignore
489
+ - spec/unit/fixtures/example_cookbook_both_metadata/.kitchen.yml
490
+ - spec/unit/fixtures/example_cookbook_both_metadata/Berksfile
491
+ - spec/unit/fixtures/example_cookbook_both_metadata/README.md
492
+ - spec/unit/fixtures/example_cookbook_both_metadata/chefignore
493
+ - spec/unit/fixtures/example_cookbook_both_metadata/metadata.json
494
+ - spec/unit/fixtures/example_cookbook_both_metadata/metadata.rb
495
+ - spec/unit/fixtures/example_cookbook_both_metadata/recipes/default.rb
497
496
  - spec/unit/fixtures/example_cookbook_metadata_json_only/.gitignore
498
497
  - spec/unit/fixtures/example_cookbook_metadata_json_only/.kitchen.yml
499
498
  - spec/unit/fixtures/example_cookbook_metadata_json_only/Berksfile
@@ -632,7 +631,6 @@ test_files:
632
631
  - spec/unit/command/generate_spec.rb
633
632
  - spec/unit/command/generator_commands/attribute_spec.rb
634
633
  - spec/unit/command/generator_commands/base_spec.rb
635
- - spec/unit/command/generator_commands/build_cookbook_spec.rb
636
634
  - spec/unit/command/generator_commands/chef_exts/generator_desc_resource_spec.rb
637
635
  - spec/unit/command/generator_commands/chef_exts/recipe_dsl_ext_spec.rb
638
636
  - spec/unit/command/generator_commands/cookbook_file_spec.rb
@@ -710,6 +708,14 @@ test_files:
710
708
  - spec/unit/fixtures/example_cookbook/chefignore
711
709
  - spec/unit/fixtures/example_cookbook/metadata.rb
712
710
  - spec/unit/fixtures/example_cookbook/recipes/default.rb
711
+ - spec/unit/fixtures/example_cookbook_both_metadata/.gitignore
712
+ - spec/unit/fixtures/example_cookbook_both_metadata/.kitchen.yml
713
+ - spec/unit/fixtures/example_cookbook_both_metadata/Berksfile
714
+ - spec/unit/fixtures/example_cookbook_both_metadata/README.md
715
+ - spec/unit/fixtures/example_cookbook_both_metadata/chefignore
716
+ - spec/unit/fixtures/example_cookbook_both_metadata/metadata.json
717
+ - spec/unit/fixtures/example_cookbook_both_metadata/metadata.rb
718
+ - spec/unit/fixtures/example_cookbook_both_metadata/recipes/default.rb
713
719
  - spec/unit/fixtures/example_cookbook_metadata_json_only/.gitignore
714
720
  - spec/unit/fixtures/example_cookbook_metadata_json_only/.kitchen.yml
715
721
  - spec/unit/fixtures/example_cookbook_metadata_json_only/Berksfile
@@ -1,126 +0,0 @@
1
- #
2
- # Copyright:: Copyright (c) 2016-2019 Chef Software Inc.
3
- # License:: Apache License, Version 2.0
4
- #
5
- # Licensed under the Apache License, Version 2.0 (the "License");
6
- # you may not use this file except in compliance with the License.
7
- # You may obtain a copy of the License at
8
- #
9
- # http://www.apache.org/licenses/LICENSE-2.0
10
- #
11
- # Unless required by applicable law or agreed to in writing, software
12
- # distributed under the License is distributed on an "AS IS" BASIS,
13
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
- # See the License for the specific language governing permissions and
15
- # limitations under the License.
16
- #
17
-
18
- require_relative "base"
19
- require_relative "../../dist"
20
-
21
- module ChefCLI
22
- module Command
23
- module GeneratorCommands
24
-
25
- class BuildCookbook < Base
26
-
27
- banner "Usage: #{ChefCLI::Dist::EXEC} generate build-cookbook NAME [options]"
28
-
29
- attr_reader :errors
30
-
31
- attr_reader :cookbook_name_or_path
32
-
33
- option :pipeline,
34
- long: "--pipeline PIPELINE",
35
- description: "Use PIPELINE to set target branch to something other than master for the build_cookbook",
36
- default: "master"
37
-
38
- options.merge!(SharedGeneratorOptions.options)
39
-
40
- def initialize(params)
41
- @params_valid = true
42
- @cookbook_name = nil
43
- super
44
- end
45
-
46
- def run
47
- read_and_validate_params
48
- if params_valid?
49
- setup_context
50
- chef_runner.converge
51
- 0
52
- else
53
- err(opt_parser)
54
- 1
55
- end
56
- rescue ChefCLI::ChefRunnerError => e
57
- err("ERROR: #{e}")
58
- 1
59
- end
60
-
61
- def setup_context
62
- super
63
- Generator.add_attr_to_context(:workflow_project_dir, workflow_project_dir)
64
-
65
- Generator.add_attr_to_context(:workflow_project_git_initialized, workflow_project_git_initialized?)
66
- Generator.add_attr_to_context(:build_cookbook_parent_is_cookbook, build_cookbook_parent_is_cookbook?)
67
-
68
- Generator.add_attr_to_context(:pipeline, pipeline)
69
- end
70
-
71
- def pipeline
72
- config[:pipeline]
73
- end
74
-
75
- def recipe
76
- "build_cookbook"
77
- end
78
-
79
- def build_cookbook_parent_is_cookbook?
80
- metadata_json_path = File.join(workflow_project_dir, "metadata.json")
81
- metadata_rb_path = File.join(workflow_project_dir, "metadata.rb")
82
-
83
- File.exist?(metadata_json_path) || File.exist?(metadata_rb_path)
84
- end
85
-
86
- def workflow_project_dir
87
- project_dir = File.expand_path(cookbook_name_or_path, Dir.pwd)
88
- # Detect if we were invoked with arguments like
89
- #
90
- # chef generate build-cookbook project/.delivery/build_cookbook
91
- #
92
- # If so, normalize paths so we don't make a directory structure like
93
- # `.delivery/.delivery/build_cookbook`.
94
- #
95
- # Note that we don't check the name of the build cookbook the user
96
- # asked for and we hard-code to naming it "build_cookbook". We also
97
- # don't catch the case that the user requested something like
98
- # `project/.delivery/build_cookbook/extra-thing-that-shouldn't-be-here`
99
- Pathname.new(project_dir).ascend do |dir|
100
- if File.basename(dir) == ".delivery"
101
- project_dir = File.dirname(dir)
102
- end
103
- end
104
- project_dir
105
- end
106
-
107
- def workflow_project_git_initialized?
108
- File.exist?(File.join(workflow_project_dir, ".git"))
109
- end
110
-
111
- def read_and_validate_params
112
- arguments = parse_options(params)
113
- @cookbook_name_or_path = arguments[0]
114
- unless @cookbook_name_or_path
115
- @params_valid = false
116
- end
117
- end
118
-
119
- def params_valid?
120
- @params_valid
121
- end
122
-
123
- end
124
- end
125
- end
126
- end
@@ -1,146 +0,0 @@
1
- # build_cookbook
2
-
3
- A build cookbook for running the parent project through Chef Workflow
4
-
5
- This build cookbook should be customized to suit the needs of the parent project. Using this cookbook can be done outside of Chef Workflow, too. If the parent project is a Chef cookbook, we've detected that and "wrapped" [delivery-truck](https://github.com/chef-cookbooks/delivery-truck). That means it is a dependency, and each of its pipeline phase recipes is included in the appropriate phase recipes in this cookbook. If the parent project is not a cookbook, it's left as an exercise to the reader to customize the recipes as needed for each phase in the pipeline.
6
-
7
- ## .delivery/config.json
8
-
9
- In the parent directory to this build_cookbook, the `config.json` can be modified as necessary. For example, phases can be skipped, publishing information can be added, and so on. Refer to customer support or the Chef Workflow documentation for assistance on what options are available for this configuration.
10
-
11
- ## Test Kitchen - Local Verify Testing
12
-
13
- This cookbook also has a `kitchen.yml` which can be used to create local build nodes with Test Kitchen to perform the verification phases, `unit`, `syntax`, and `lint`. When running `kitchen converge`, the instances will be set up like Chef Workflow "build nodes" with the [delivery_build cookbook](https://github.com/chef-cookbooks/delivery_build). The reason for this is to make sure that the same exact kind of nodes are used by this build cookbook are run on the local workstation as would run Workflow. It will run `delivery job verify PHASE` for the parent project.
14
-
15
- Modify the `kitchen.yml` if necessary to change the platforms or other configuration to run the verify phases. After making changes in the parent project, `cd` into this directory (`.delivery/build_cookbook`), and run:
16
-
17
- ```
18
- kitchen test
19
- ```
20
-
21
- ## Recipes
22
-
23
- Each of the recipes in this build_cookbook are run in the named phase during the Chef Workflow pipeline. The `unit`, `syntax`, and `lint` recipes are additionally run when using Test Kitchen for local testing as noted in the above section.
24
-
25
- ## Making Changes - Cookbook Example
26
-
27
- When making changes in the parent project (that which lives in `../..` from this directory), or in the recipes in this build cookbook, there is a bespoke workflow for Chef Workflow. As an example, we'll discuss a Chef Cookbook as the parent.
28
-
29
- First, create a new branch for the changes.
30
-
31
- ```
32
- git checkout -b testing-build-cookbook
33
- ```
34
-
35
- Next, increment the version in the metadata.rb. This should be in the _parent_, not in this, the build_cookbook. If this is not done, the verify phase will fail.
36
-
37
- ```
38
- % git diff
39
- <SNIP>
40
- -version '0.1.0'
41
- +version '0.1.1'
42
- ```
43
-
44
- The change we'll use for an example is to install the `zsh` package. Write a failing ChefSpec in the cookbook project's `spec/unit/recipes/default_spec.rb`.
45
-
46
- ```ruby
47
- require 'spec_helper'
48
-
49
- describe 'godzilla::default' do
50
- context 'When all attributes are default, on Ubuntu 18.04' do
51
- let(:chef_run) do
52
- runner = ChefSpec::ServerRunner.new(platform: 'ubuntu', version: '18.04')
53
- runner.converge(described_recipe)
54
- end
55
-
56
- it 'installs zsh' do
57
- expect(chef_run).to install_package('zsh')
58
- end
59
- end
60
- end
61
- ```
62
-
63
- Commit the local changes as work in progress. The `delivery job` expects to use a clean git repository.
64
-
65
- ```
66
- git add ../..
67
- git commit -m 'WIP: Testing changes'
68
- ```
69
-
70
- From _this_ directory (`.delivery/build_cookbook`, relative to the parent cookbook project), run
71
-
72
- ```
73
- cd .delivery/build_cookbook
74
- kitchen converge
75
- ```
76
-
77
- This will take some time at first, because the VMs need to be created, Chef Infra Client installed, the Delivery CLI installed, etc. Later runs will be faster until they are destroyed. It will also fail on the first VM, as expected, because we wrote the test first. Now edit the parent cookbook project's default recipe to install `zsh`.
78
-
79
- ```
80
- cd ../../
81
- $EDITOR/recipes/default.rb
82
- ```
83
-
84
- It should look like this:
85
-
86
- ```
87
- package 'zsh'
88
- ```
89
-
90
- Create another commit.
91
-
92
- ```
93
- git add .
94
- git commit -m 'WIP: Install zsh in default recipe'
95
- ```
96
-
97
- Now rerun kitchen from the build_cookbook.
98
-
99
- ```
100
- cd .delivery/build_cookbook
101
- kitchen converge
102
- ```
103
-
104
- This will take a while because it will now pass on the first VM and then create the second VM. We should have warned you this was a good time for a coffee break.
105
-
106
- ```
107
- Recipe: test::default
108
-
109
- - execute HOME=/home/vagrant delivery job verify unit --server localhost --ent test --org kitchen
110
- * execute[HOME=/home/vagrant delivery job verify lint --server localhost --ent test --org kitchen] action run
111
- - execute HOME=/home/vagrant delivery job verify lint --server localhost --ent test --org kitchen
112
-
113
- - execute HOME=/home/vagrant delivery job verify syntax --server localhost --ent test --org kitchen
114
-
115
- Running handlers:
116
- Running handlers complete
117
- Chef Infra Client finished, 3/32 resources updated in 54.665445968 seconds
118
- Finished converging <default-centos-8> (1m26.83s).
119
- ```
120
-
121
- Victory is ours! Our verify phase passed on the build nodes.
122
-
123
- We are ready to run this through our Workflow pipeline. Simply run `delivery review` on the local system from the parent project, and it will open a browser window up to the change we just added.
124
-
125
- ```
126
- cd ../..
127
- delivery review
128
- ```
129
-
130
- ## FAQ
131
-
132
- ### Why don't I just run rspec and cookstyle on my local system?
133
-
134
- An objection to the Test Kitchen approach is that it is much faster to run the unit, lint, and syntax commands for the project on the local system. That is totally true, and also totally valid. Do that for the really fast feedback loop. However, the dance we do with Test Kitchen brings a much higher degree of confidence in the changes we're making, that everything will run on the build nodes in Chef Workflow. We strongly encourage this approach before actually pushing the changes to Workflow.
135
-
136
- ### Why do I have to make a commit every time?
137
-
138
- When running `delivery job`, it expects to merge the commit for the changeset against the clean master branch. If we don't save our progress by making a commit, our local changes aren't run through `delivery job` in the Test Kitchen build instances. We can always perform an interactive rebase, and modify the original changeset message in Workflow with `delivery review --edit`. The latter won't modify the git commits, only the changeset in Workflow.
139
-
140
- ### What do I do next?
141
-
142
- Make changes in the cookbook project as required for organizational goals and needs. Modify the `build_cookbook` as necessary for the pipeline phases that the cookbook should go through.
143
-
144
- ### What if I get stuck?
145
-
146
- Contact Chef Support, or your Chef Customer Success team and they will help you get unstuck.