<commit_before># Flay - A Customized Chef Cookbook Template with Other Useful Things

[![Build Status](https://travis-ci.org/sweeperio/flay.svg?branch=master)](https://travis-ci.org/sweeperio/flay)
[![Gem Version](https://badge.fury.io/rb/chef-flavor-flay.svg)](https://badge.fury.io/rb/chef-flavor-flay)
[![Coverage Status](https://coveralls.io/repos/sweeperio/flay/badge.svg?branch=master&service=github)](https://coveralls.io/github/sweeperio/flay?branch=master)

This repo is a custom cookbook/recipe template for use with the [ChefDK]. It uses [chef-gen-flavors] to create a custom
template that can be used with `chef generate [cookbook|recipe]` commands.

It's very opinionated and works with the sweeperio infrastructure specifically.

[ChefDK]: https://downloads.chef.io/chef-dk/
[chef-gen-flavors]: https://github.com/jf647/chef-gen-flavors

## Why?

* Automatically create boilerplate cookbook code/files
* Put some standards in place that are used consistently across all cookbooks
* Generate simple, good examples for devs that are new to chef

## What?

* Normal setup in place...[Berkshelf], [ChefSpec], [Test Kitchen], etc.
* _**Only ubuntu**_ is supported and setup in ChefSpec and Test Kitchen (I said it was opinionated)
* Sets up berks to use our berks api
* Adds [core] cookbook to metadata.rb
* Adds [rubocop] with some updated (opinionated) settings
* Updates all templates to pass `bundle exec rubocop && bundle exec rspec`
* Adds a travis file for CI that will use the chefdk to run tests
* Creates a single `test` directory rather than spec/unit and test/integration
* Adds a _dummy_ `encrypted_data_bag_secret` file for [Test Kitchen] (see note about testing data bags below)

[Berkshelf]: https://docs.chef.io/berkshelf.html
[ChefSpec]: https://docs.chef.io/chefspec.html
[Test Kitchen]: https://docs.chef.io/kitchen.html
[core]: https://github.com/sweeperio/chef-core
[rubocop]: https://docs.chef.io/rubocop.html

## Installation

* Make sure you've installed the [ChefDK]
* Run `chef gem install chef-flavor-flay`

Add the following to your `knife.rb` file:

```ruby
if defined?(ChefDK::CLI)
  require "chef_gen/flavors"
  chefdk.generator_cookbook = ChefGen::Flavors.path
end
```

Celebrate! :rocket:

## Usage

* `chef exec flay cookbook my_cookbook`
* `chef exec flay recipe my_cookbook` (from within the cookbook directory)
* `chef exec flay encrypt DATA_BAG ITEM (options)`
* `chef exec flay decrypt DATA_BAG ITEM (options)`
* `chef exec flay release` (see below)

There are a few other commands available. Run `chef exec flay help` for details.

### Releasing Cookbooks

Modelled after bundler's `rake release`, there's `chef exec flay release`.

When the following conditions are met:

* `Dir.pwd` is a git repo (or a subdirectory of one)
* There are no uncommitted changes in git
* _metadata.rb_ exists (at the root of the repo) and contains a valid version

It will run:

* `git tag -a -m "Version #{version}" v#{version}` - unless the tag already exists
* `git push && git push --tags`
* `chef exec berks install`
* `chef exec berks upload`

### Testing Encrypted Data Bags

The _test/integration/data_bags_ directory should contain subdirectories for each data bag you want to test (just like 
your chef repo would).
<commit_msg>documenting data bag things [ci skip]<commit_after># Flay - A Customized Chef Cookbook Template with Other Useful Things

[![Build Status](https://travis-ci.org/sweeperio/flay.svg?branch=master)](https://travis-ci.org/sweeperio/flay)
[![Gem Version](https://badge.fury.io/rb/chef-flavor-flay.svg)](https://badge.fury.io/rb/chef-flavor-flay)
[![Coverage Status](https://coveralls.io/repos/sweeperio/flay/badge.svg?branch=master&service=github)](https://coveralls.io/github/sweeperio/flay?branch=master)

This repo is a custom cookbook/recipe template for use with the [ChefDK]. It uses [chef-gen-flavors] to create a custom
template that can be used with `chef generate [cookbook|recipe]` commands.

It's very opinionated and works with the sweeperio infrastructure specifically.

[ChefDK]: https://downloads.chef.io/chef-dk/
[chef-gen-flavors]: https://github.com/jf647/chef-gen-flavors

## Why?

* Automatically create boilerplate cookbook code/files
* Put some standards in place that are used consistently across all cookbooks
* Generate simple, good examples for devs that are new to chef

## What?

* Normal setup in place...[Berkshelf], [ChefSpec], [Test Kitchen], etc.
* _**Only ubuntu**_ is supported and setup in ChefSpec and Test Kitchen (I said it was opinionated)
* Sets up berks to use our berks api
* Adds [core] cookbook to metadata.rb
* Adds [rubocop] with some updated (opinionated) settings
* Updates all templates to pass `bundle exec rubocop && bundle exec rspec`
* Adds a travis file for CI that will use the chefdk to run tests
* Creates a single `test` directory rather than spec/unit and test/integration
* Adds a _dummy_ `encrypted_data_bag_secret` file for [Test Kitchen] (see note about testing data bags below)

[Berkshelf]: https://docs.chef.io/berkshelf.html
[ChefSpec]: https://docs.chef.io/chefspec.html
[Test Kitchen]: https://docs.chef.io/kitchen.html
[core]: https://github.com/sweeperio/chef-core
[rubocop]: https://docs.chef.io/rubocop.html

## Installation

* Make sure you've installed the [ChefDK]
* Run `chef gem install chef-flavor-flay`

Add the following to your `knife.rb` file:

```ruby
if defined?(ChefDK::CLI)
  require "chef_gen/flavors"
  chefdk.generator_cookbook = ChefGen::Flavors.path
end
```

Celebrate! :rocket:

## Usage

* `chef exec flay cookbook my_cookbook`
* `chef exec flay recipe my_cookbook` (from within the cookbook directory)
* `chef exec flay encrypt DATA_BAG ITEM (options)`
* `chef exec flay decrypt DATA_BAG ITEM (options)`
* `chef exec flay release` (see below)

There are a few other commands available. Run `chef exec flay help` for details.

### Releasing Cookbooks

Modelled after bundler's `rake release`, there's `chef exec flay release`.

When the following conditions are met:

* `Dir.pwd` is a git repo (or a subdirectory of one)
* There are no uncommitted changes in git
* _metadata.rb_ exists (at the root of the repo) and contains a valid version

It will run:

* `git tag -a -m "Version #{version}" v#{version}` - unless the tag already exists
* `git push && git push --tags`
* `chef exec berks install`
* `chef exec berks upload`

### Working With Data Bags

Normally data bags are edited directly on the chef server by using the normal `knife data bag` commands. I'm not fond of
this practise because there is no history there. If someone changes an item, how do you go back to what it was if
something goes wrong?

For this reason, I've added a simple knife plugin that exposes 2 new knife commands `data bag encrypt` and `data bag
descrypt`. These commands work with json files in the `data_bags/` directory of your chef repo. The basic idea is that
you encrypt the items locally, commit to git and then create/update the items from json files.

For example, suppose you have an unencrypted json file at `data_bags/ejson/keys.json` that defines an item. To encrypt
this item you can run the following command:

`chef exec knife data bag encrypt ejson keys -w`

This will encrypt the contents using your `encrypted_data_bag_secret` (pulled from chef config/knife.rb).

Similarily there's a `decrypt` version that does the opposite. `knife data bag decrypt ejson keys -w`

Both of these commands support the following options:

* `-w` - whether or not to write the file. If false, the results will be printed to STDOUT, but not written to the file.
    Default `false`
* `-s` - The path to your encrypted_data_bag_secret file. Default `Chef::Config[:encrypted_data_bag_secret]`
* `-p` - The path to your data bag directory. Default `Chef::Config[:data_bag_path]`

For example to use test data bags with a custom secret file you could run:

`chef exec knife data bag encrypt -w -s /some/path/to/secret -p /custom/data_bags/dir`

#### Flay Wrappers

For convenience, there are equivalent commands added to flay that really just wrap the call to these commands.

* `flay encrypt DATA_BAG ITEM` - Will encrypt the data bag and write to the file
* `flay decrypt DATA_BAG ITEM` - Will decrypt the data bag and write to the file

Both of these support the `--no-write` option to prevent writing the result to the file. There is also the `-t` option,
we sets the secret file and data bag path to `test/integration/encrypted_data_bag_secret` and
`test/integration/data_bags` respectively.

### Testing Encrypted Data Bags

The _test/integration/data_bags_ directory should contain subdirectories for each data bag you want to test (just like 
your chef repo would).

{'fraction_non_alphanumeric': 0.09925415949512335, 'fraction_numerical': 0.0008032128514056225, 'mean_word_length': 4.619600257898131, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 26, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '550789', 'n_tokens_mistral': 2842, 'n_tokens_neox': 2733, 'n_words': 1085}