Skip to content
This repository has been archived by the owner on Jul 14, 2021. It is now read-only.

Generate cookbooks with policyfiles by default #2065

Merged
merged 5 commits into from
May 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,19 @@ matrix:
env:
RSPEC: 1
script: bundle exec rspec
- rvm: 2.6.2
- rvm: 2.6.3
env:
RSPEC: 1
script: bundle exec rspec
- rvm: 2.6.2
- rvm: 2.6.3
env:
CHEFSTYLE: 1
script: bundle exec rake style:chefstyle
- rvm: 2.6.2
- rvm: 2.6.3
env:
COOKSTYLE: 1
script: bundle exec rake style:cookstyle
- rvm: 2.6.2
- rvm: 2.6.3
env:
FOODCRITIC: 1
script: bundle exec rake style:foodcritic
Expand Down
2 changes: 2 additions & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@

The `chef generate` command has been updated to produce cookbooks and repositories the match Chef's best practices.
- `chef generate repo` now generates a Chef repository with Policyfiles by default. You can revert to the previous roles / environment behavior with the `--roles` flag.
- `chef generate cookbook` now generates a cookbook with a Policyfile and no Berksfile by default. You can revert to the previous behavior with the `--berks` flag.
- `chef generate cookbook` now includes ChefSpecs that utilize the ChefSpec 7.3+ format. This is a much simpler syntax that requires less updating as older platforms are deprecated.
- `chef generate cookbook` no longer creates cookbbook files with the unecessary `frozen_string_literal: true` comments.
- `chef generate cookbook` now generates cookbooks with metadata requiring Chef 14 or later.
- `chef generate cookbook --kitchen dokken` now generates a fully working kitchen-dokken config.
- `chef generate cookbook` now generates Test Kitchen configs with the `product_name`/`product_version` method of specifying Chef Infra Client releases as `require_chef_omnibus` will be removed in the next major Test Kitchen release.
- `chef generate cookbook_file` no longer places the specified file in a "default" folder as these aren't needed in Chef Infra Client 12 and later.

## Updated Components and Tools

Expand Down
12 changes: 6 additions & 6 deletions lib/chef-dk/command/generator_commands/cookbook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class Cookbook < Base
def initialize(params)
@params_valid = true
@cookbook_name = nil
@berks_mode = true
@policy_mode = true
@enable_delivery = true
@verbose = false
super
Expand Down Expand Up @@ -134,7 +134,7 @@ def setup_context

Generator.add_attr_to_context(:verbose, verbose?)

Generator.add_attr_to_context(:use_berkshelf, berks_mode?)
Generator.add_attr_to_context(:use_policyfile, policy_mode?)
Generator.add_attr_to_context(:pipeline, pipeline)
Generator.add_attr_to_context(:kitchen, kitchen)
end
Expand Down Expand Up @@ -175,8 +175,8 @@ def cookbook_full_path
File.expand_path(cookbook_name_or_path, Dir.pwd)
end

def berks_mode?
@berks_mode
def policy_mode?
@policy_mode
end

def enable_delivery?
Expand Down Expand Up @@ -224,8 +224,8 @@ def read_and_validate_params
@params_valid = false
end

if config[:policy]
@berks_mode = false
if config[:berks]
@policy_mode = false
end

if config[:verbose]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ current_dir = File.dirname(__FILE__)
cookbook_path ["#{current_dir}/../cookbooks"]
```

Which will set `current_dir` to the location of the knife.rb file itself (e.g. `~/chef-repo/.chef/knife.rb`).
Which will set `current_dir` to the location of the config.rb (previously knife.rb) file itself (e.g. `~/chef-repo/.chef/config.rb`).

Configure knife to use your preferred copyright holder, email contact and license. Add the following lines to `.chef/knife.rb`.
Configure knife to use your preferred copyright holder, email contact and license. Add the following lines to `.chef/config.rb`.

```
cookbook_copyright "Example, Com."
Expand Down
27 changes: 12 additions & 15 deletions lib/chef-dk/skeletons/code_generator/recipes/cookbook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,17 @@
# chefignore
cookbook_file "#{cookbook_dir}/chefignore"

if context.use_berkshelf

# Berks
cookbook_file "#{cookbook_dir}/Berksfile" do
action :create_if_missing
end
else

if context.use_policyfile
# Policyfile
template "#{cookbook_dir}/Policyfile.rb" do
source 'Policyfile.rb.erb'
helpers(ChefDK::Generator::TemplateHelper)
end

else
# Berks
cookbook_file "#{cookbook_dir}/Berksfile" do
action :create_if_missing
end
end

# LICENSE
Expand All @@ -73,10 +70,10 @@
if context.kitchen == 'dokken'
# kitchen-dokken configuration works with berkshelf and policyfiles
source 'kitchen_dokken.yml.erb'
elsif context.use_berkshelf
source 'kitchen.yml.erb'
else
elsif context.use_policyfile
source 'kitchen_policyfile.yml.erb'
else
source 'kitchen.yml.erb'
end

helpers(ChefDK::Generator::TemplateHelper)
Expand All @@ -100,10 +97,10 @@
end

cookbook_file "#{cookbook_dir}/spec/spec_helper.rb" do
if context.use_berkshelf
source 'spec_helper.rb'
else
if context.use_policyfile
source 'spec_helper_policyfile.rb'
else
source 'spec_helper.rb'
end

action :create_if_missing
Expand Down
4 changes: 2 additions & 2 deletions lib/chef-dk/skeletons/code_generator/recipes/cookbook_file.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
context = ChefDK::Generator.context
cookbook_dir = File.join(context.cookbook_root, context.cookbook_name)
files_dir = File.join(cookbook_dir, 'files', 'default')
cookbook_file_path = File.join(cookbook_dir, 'files', 'default', context.new_file_basename)
files_dir = File.join(cookbook_dir, 'files')
cookbook_file_path = File.join(files_dir, context.new_file_basename)

directory files_dir do
recursive true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@
driver:
name: vagrant

## The forwarded_port port feature lets you connect to ports on the VM guest via
## localhost on the host.
## see also: https://www.vagrantup.com/docs/networking/forwarded_ports.html

# network:
# - ["forwarded_port", {guest: 80, host: 8080}]

provisioner:
name: chef_zero
# You may wish to disable always updating cookbooks in CI or other testing environments.
Expand Down
2 changes: 1 addition & 1 deletion spec/unit/command/generator_commands/cookbook_file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
include_examples "a file generator" do

let(:generator_name) { "file" }
let(:generated_files) { [ "files/default/new_file.txt" ] }
let(:generated_files) { [ "files/new_file.txt" ] }
let(:new_file_name) { "new_file.txt" }

end
Expand Down
9 changes: 8 additions & 1 deletion spec/unit/command/generator_commands/cookbook_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
test
test/integration
test/integration/default/default_test.rb
Berksfile
Policyfile.rb
chefignore
LICENSE
metadata.rb
Expand Down Expand Up @@ -648,6 +648,13 @@ def with_argv(argv)
driver:
name: vagrant

## The forwarded_port port feature lets you connect to ports on the VM guest via
## localhost on the host.
## see also: https://www.vagrantup.com/docs/networking/forwarded_ports.html

# network:
# - ["forwarded_port", {guest: 80, host: 8080}]

provisioner:
name: chef_zero
# You may wish to disable always updating cookbooks in CI or other testing environments.
Expand Down