chef-cli 4.0.0 → 5.3.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 (44) 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 +15 -2
  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/helpers.rb +4 -4
  11. data/lib/chef-cli/policyfile/dsl.rb +16 -1
  12. data/lib/chef-cli/policyfile_services/export_repo.rb +9 -13
  13. data/lib/chef-cli/skeletons/code_generator/recipes/cookbook.rb +26 -5
  14. data/lib/chef-cli/skeletons/code_generator/recipes/recipe.rb +11 -3
  15. data/lib/chef-cli/skeletons/code_generator/templates/default/delivery-project.toml.erb +1 -1
  16. data/lib/chef-cli/skeletons/code_generator/templates/default/recipe.yml.erb +18 -0
  17. data/lib/chef-cli/version.rb +1 -1
  18. data/spec/unit/command/export_spec.rb +18 -1
  19. data/spec/unit/command/generate_spec.rb +7 -0
  20. data/spec/unit/command/generator_commands/cookbook_spec.rb +49 -235
  21. data/spec/unit/command/generator_commands/recipe_spec.rb +34 -0
  22. data/spec/unit/cookbook_metadata_spec.rb +20 -1
  23. data/spec/unit/fixtures/example_cookbook_both_metadata/.gitignore +17 -0
  24. data/spec/unit/fixtures/example_cookbook_both_metadata/.kitchen.yml +16 -0
  25. data/spec/unit/fixtures/example_cookbook_both_metadata/Berksfile +3 -0
  26. data/spec/unit/fixtures/example_cookbook_both_metadata/README.md +4 -0
  27. data/spec/unit/fixtures/example_cookbook_both_metadata/chefignore +96 -0
  28. data/spec/unit/fixtures/example_cookbook_both_metadata/metadata.json +5 -0
  29. data/spec/unit/fixtures/example_cookbook_both_metadata/metadata.rb +9 -0
  30. data/spec/unit/fixtures/example_cookbook_both_metadata/recipes/default.rb +8 -0
  31. data/spec/unit/helpers_spec.rb +111 -0
  32. data/spec/unit/policyfile_evaluation_spec.rb +67 -1
  33. data/spec/unit/policyfile_services/export_repo_spec.rb +51 -2
  34. metadata +22 -14
  35. data/lib/chef-cli/command/generator_commands/build_cookbook.rb +0 -126
  36. data/lib/chef-cli/skeletons/code_generator/files/default/build_cookbook/README.md +0 -146
  37. data/lib/chef-cli/skeletons/code_generator/files/default/build_cookbook/kitchen.yml +0 -21
  38. data/lib/chef-cli/skeletons/code_generator/files/default/build_cookbook/test-fixture-recipe.rb +0 -8
  39. data/lib/chef-cli/skeletons/code_generator/files/default/delivery-config.json +0 -17
  40. data/lib/chef-cli/skeletons/code_generator/recipes/build_cookbook.rb +0 -175
  41. data/lib/chef-cli/skeletons/code_generator/templates/default/build_cookbook/Berksfile.erb +0 -7
  42. data/lib/chef-cli/skeletons/code_generator/templates/default/build_cookbook/metadata.rb.erb +0 -10
  43. data/lib/chef-cli/skeletons/code_generator/templates/default/build_cookbook/recipe.rb.erb +0 -9
  44. data/spec/unit/command/generator_commands/build_cookbook_spec.rb +0 -377
@@ -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.
@@ -1,21 +0,0 @@
1
- ---
2
- driver:
3
- name: vagrant
4
- synced_folders:
5
- - [<%= File.join(ENV['PWD'], '..', '..')%>, '/tmp/repo-data']
6
-
7
- provisioner:
8
- name: chef_zero
9
- encrypted_data_bag_secret_key_path: 'secrets/fakey-mcfakerton'
10
- data_bags_path: './data_bags'
11
- product_name: chefdk
12
-
13
- platforms:
14
- - name: ubuntu-20.04
15
- - name: centos-8
16
-
17
- suites:
18
- - name: default
19
- run_list:
20
- - recipe[test]
21
- attributes:
@@ -1,8 +0,0 @@
1
- %w(unit lint syntax).each do |phase|
2
- # TODO: This works on Linux/Unix. Not Windows.
3
- execute "HOME=/home/vagrant delivery job verify #{phase} --server localhost --ent test --org kitchen" do
4
- cwd '/tmp/repo-data'
5
- user 'vagrant'
6
- environment('GIT_DISCOVERY_ACROSS_FILESYSTEM' => '1')
7
- end
8
- end
@@ -1,17 +0,0 @@
1
- {
2
- "version": "2",
3
- "build_cookbook": {
4
- "name": "build_cookbook",
5
- "path": ".delivery/build_cookbook"
6
- },
7
- "delivery-truck": {
8
- "lint": {
9
- "enable_cookstyle": true
10
- }
11
- },
12
- "skip_phases": [],
13
- "job_dispatch": {
14
- "version": "v2"
15
- },
16
- "dependencies": []
17
- }
@@ -1,175 +0,0 @@
1
- context = ChefCLI::Generator.context
2
- workflow_project_dir = context.workflow_project_dir
3
- pipeline = context.pipeline
4
- dot_delivery_dir = File.join(workflow_project_dir, '.delivery')
5
- config_json = File.join(dot_delivery_dir, 'config.json')
6
- project_toml = File.join(dot_delivery_dir, 'project.toml')
7
-
8
- generator_desc('Ensuring delivery CLI configuration')
9
-
10
- directory dot_delivery_dir
11
-
12
- cookbook_file config_json do
13
- source 'delivery-config.json'
14
- not_if { ::File.exist?(config_json) }
15
- end
16
-
17
- # Adding the delivery local-mode config
18
- cookbook_file project_toml do
19
- source 'delivery-project.toml'
20
- not_if { ::File.exist?(project_toml) }
21
- end
22
-
23
- generator_desc('Ensuring correct Workflow (Delivery) build cookbook content')
24
-
25
- build_cookbook_dir = File.join(dot_delivery_dir, 'build_cookbook')
26
-
27
- # cookbook root dir
28
- directory build_cookbook_dir
29
-
30
- # metadata.rb
31
- template "#{build_cookbook_dir}/metadata.rb" do
32
- source 'build_cookbook/metadata.rb.erb'
33
- helpers(ChefCLI::Generator::TemplateHelper)
34
- action :create_if_missing
35
- end
36
-
37
- # README
38
- cookbook_file "#{build_cookbook_dir}/README.md" do
39
- source 'build_cookbook/README.md'
40
- action :create_if_missing
41
- end
42
-
43
- # LICENSE
44
- template "#{build_cookbook_dir}/LICENSE" do
45
- source "LICENSE.#{context.license}.erb"
46
- helpers(ChefCLI::Generator::TemplateHelper)
47
- action :create_if_missing
48
- end
49
-
50
- # chefignore
51
- cookbook_file "#{build_cookbook_dir}/chefignore"
52
-
53
- # Berksfile
54
- template "#{build_cookbook_dir}/Berksfile" do
55
- source 'build_cookbook/Berksfile.erb'
56
- helpers(ChefCLI::Generator::TemplateHelper)
57
- action :create_if_missing
58
- end
59
-
60
- # Recipes
61
- directory "#{build_cookbook_dir}/recipes"
62
-
63
- %w(default deploy functional lint provision publish quality security smoke syntax unit).each do |phase|
64
- template "#{build_cookbook_dir}/recipes/#{phase}.rb" do
65
- source 'build_cookbook/recipe.rb.erb'
66
- helpers(ChefCLI::Generator::TemplateHelper)
67
- variables phase: phase
68
- action :create_if_missing
69
- end
70
- end
71
-
72
- # Test Kitchen build node
73
- cookbook_file "#{build_cookbook_dir}/kitchen.yml" do
74
- source 'build_cookbook/kitchen.yml'
75
- end
76
-
77
- directory "#{build_cookbook_dir}/data_bags/keys" do
78
- recursive true
79
- end
80
-
81
- file "#{build_cookbook_dir}/data_bags/keys/delivery_builder_keys.json" do
82
- content '{"id": "delivery_builder_keys"}'
83
- end
84
-
85
- directory "#{build_cookbook_dir}/secrets"
86
-
87
- file "#{build_cookbook_dir}/secrets/fakey-mcfakerton"
88
-
89
- directory "#{build_cookbook_dir}/test/fixtures/cookbooks/test/recipes" do
90
- recursive true
91
- end
92
-
93
- file "#{build_cookbook_dir}/test/fixtures/cookbooks/test/metadata.rb" do
94
- content %(name 'test'
95
- version '0.1.0')
96
- end
97
-
98
- cookbook_file "#{build_cookbook_dir}/test/fixtures/cookbooks/test/recipes/default.rb" do
99
- source 'build_cookbook/test-fixture-recipe.rb'
100
- end
101
-
102
- # Construct git history as if we did all the work in a feature branch which we
103
- # merged into master at the end, which looks like this:
104
- #
105
- # ```
106
- # git log --graph --oneline
107
- # * 5fec5bd Merge branch 'add-delivery-configuration'
108
- # |\
109
- # | * 967bb9f Add generated delivery build cookbook
110
- # | * 1558e0a Add generated delivery configuration
111
- # |/
112
- # * db22790 Add generated cookbook content
113
- # ```
114
- #
115
- if context.have_git && context.workflow_project_git_initialized && !context.skip_git_init
116
-
117
- generator_desc('Adding delivery configuration to feature branch')
118
-
119
- execute('git-create-feature-branch') do
120
- command('git checkout -t -b add-delivery-configuration')
121
- cwd workflow_project_dir
122
- not_if { shell_out('git branch', cwd: workflow_project_dir).stdout.match(/add-delivery-configuration/) }
123
- end
124
-
125
- execute('git-add-delivery-config-json') do
126
- command('git add .delivery/config.json')
127
- cwd workflow_project_dir
128
- only_if { shell_out('git status -u --porcelain', cwd: workflow_project_dir).stdout.match(%r{.delivery/config.json}) }
129
- end
130
-
131
- # Adding the new prototype file to the feature branch
132
- # so it gets checked in with the delivery config commit
133
- execute('git-add-delivery-project-toml') do
134
- command('git add .delivery/project.toml')
135
- cwd workflow_project_dir
136
- only_if { shell_out('git status -u --porcelain', cwd: workflow_project_dir).stdout.match(%r{.delivery/project.toml}) }
137
- end
138
-
139
- execute('git-commit-delivery-config') do
140
- command('git commit -m "Add generated delivery configuration"')
141
- cwd workflow_project_dir
142
- only_if { shell_out('git status -u --porcelain', cwd: workflow_project_dir).stdout.match(/config\.json|project\.toml/) }
143
- end
144
-
145
- generator_desc('Adding build cookbook to feature branch')
146
-
147
- execute('git-add-delivery-build-cookbook-files') do
148
- command('git add .delivery')
149
- cwd workflow_project_dir
150
- only_if { shell_out('git status -u --porcelain', cwd: workflow_project_dir).stdout.match(/\.delivery/) }
151
- end
152
-
153
- execute('git-commit-delivery-build-cookbook') do
154
- command('git commit -m "Add generated delivery build cookbook"')
155
- cwd workflow_project_dir
156
- only_if { shell_out('git status -u --porcelain', cwd: workflow_project_dir).stdout.match(/\.delivery/) }
157
- end
158
-
159
- execute("git-return-to-#{pipeline}-branch") do
160
- command("git checkout #{pipeline}")
161
- cwd workflow_project_dir
162
- end
163
-
164
- generator_desc('Merging delivery content feature branch to master')
165
-
166
- execute('git-merge-delivery-config-branch') do
167
- command('git merge --no-ff add-delivery-configuration')
168
- cwd workflow_project_dir
169
- end
170
-
171
- execute('git-remove-delivery-config-branch') do
172
- command('git branch -D add-delivery-configuration')
173
- cwd workflow_project_dir
174
- end
175
- end