spruce

command module
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 1, 2016 License: Apache-2.0 Imports: 17 Imported by: 1

README

          *          .---. ,---.  ,---.  .-. .-.  ,--,  ,---.         *
         /.\        ( .-._)| .-.\ | .-.\ | | | |.' .')  | .-'        /.\
        /..'\      (_) \   | |-' )| `-'/ | | | ||  |(_) | `-.       /..'\
        /'.'\      _  \ \  | |--' |   (  | | | |\  \    | .-'       /'.'\
       /.''.'\    ( `-'  ) | |    | |\ \ | `-')| \  `-. |  `--.    /.''.'\
       /.'.'.\     `----'  /(     |_| \)\`---(_)  \____\/( __.'    /.'.'.\
"'""""/'.''.'.\""'"'""""""(__)""""""""(__)"""""""""""""(__)""'""""/'.''.'.\""'"'"
      ^^^[_]^^^                                                   ^^^[_]^^^

Build Status

Introducing Spruce

spruce is a domain-specific YAML merging tool, for generating BOSH manifests.

It was written with the goal of being the most intuitive solution for merging BOSH templates. As such, it pulls in a few semantics that may seem familiar to those used to merging with the other merging tool, but there are a few key differences.

Installation

Spruce is now available via Homebrew, just brew tap starkandwayne/cf; brew install spruce

Alternatively, you can download a prebuilt binaries for 64-bit Linux, or Mac OS X, or you can install via go get (provided you have installed go):

go get github.com/geofffranks/spruce

Merging Rules

Merging in spruce is designed to be pretty intuitive. Files to merge are listed in-order on the command line. The first file serves as the base to the file structure, and subsequent files are merged on top, adding when keys are new, replacing when keys exist. This differs slightly in mentality from spiff, but hopefully the results are more predictable.

A word on 'meta'

meta was a convention used quite often in templates merged with spiff. This convention is not necessary with spruce. If you want to merge two hashes together, simply include the new keys in the file merged on top of the original.

What about arrays?

Arrays can be merged in three ways - prepending data, appending data, and completely replacing data.

  • To append data to an existing array, ensure that the first element in the new array is

    - (( append ))
    
  • To prepend the data to an existing array, ensure that the first element in the new array is

    - (( prepend ))
    
  • To replace the first array with the second, ensure that the first element in the new array is

    - (( replace ))
    
  • To merge two arrays by way of their index, just make the first element

    - (( inline ))
    
  • To merge two arrays of maps together (using a specific key for identifying like objects), ensure that the first element in the new array is either

    - (( merge ))
    


    or

    - (( merge on <key> ))
    


The first merges using name as the key to determine like objects in the array elements. The second is used to customize which key to use. See Merging Arrays of Maps for an example.

  • If you don't specify a specific merge strategy, the array will be merged automatically; using keys if they exist (i.e. (( merge )), and array indices otherwise ((( inline ))).

Cleaning Up After Yourself

To prune a map key from the final output

spruce merge --prune key.1.to.prune --prune key.2.to.prune file1.yml file2.yml

Referencing Other Data

Need to reference existing data in your datastructure? No problem! spruce will wait until all the data is merged together before dereferencing anything, but to handle this, you can use the (( grab <thing> )) syntax:

data:
  color: blue

pen:
  color: (( grab data.color ))

You can even reference multiple values at once, getting back an array of their data, for things like getting all IPs of multi-AZ jobs in a BOSH manifest, just do it like so:

(( grab jobs.myJob_z1.networks.myNet1.static_ips jobs.myJob_z2.networks.myNet2.static_ips ))

You can also provide alternatives to your grab operation, by using the || (or) operator:

key:      (( grab site.key || nil ))
domain:   (( grab global.domain || "example.com" ))
protocol: (( grab site.protocol || global.protocol || "http" ))

In these examples, if the referenced key does not exist, the next reference is attempted, or the literal value (nil, numbers or strings) is used. Spruce recognizes the following keywords and uses the appropriate literal value:

  • nil, null and ~ map to the YAML null value
  • true is the YAML boolean value for truth
  • false is the YAML boolean value for non-truth

Other types of literals include double-quoted strings (with embedded double quotes escaped with a single backslash - \), integer literals (a string of digits) and floating point literals (a string of digits, a period, and another string of digits). Scientific notation is not currently supported.

Hmm.. How about auto-calculating static IPs for a BOSH manifest?

spruce supports that too! Just use the same (( static_ips(x, y, z) )) syntax that you're used to with spiff, to specify the offsets in the static IP range for a job's network.

Behind the scenes, there are a couple behavior improvements upon spiff. First, since all the merging is done first, then post-processing, there's no need to worry about getting the instances + networks defined before (( static_ips() )) is merged in. Second, the error messaging output should be a lot better to aid in tracking down why static_ips() calls fail.

Check out the static_ips() example

But I Want To Make Strings!!

Yeah, spruce can do that!

env: production
cluster:
  name: mjolnir
ident: (( concat cluster.name "//" env ))

Which will give you an ident: key of "mjolnir/production"

How About Some Examples?

Basic Example

Here's a pretty broad example, that should cover all the functionality of spruce, to be used as a reference.

If I start with this data:

# examples/basic/main.yml
top:
  orig_key: This is a string attached to a key
  number: 50
  array1:
  - first element
  - second element
  - third element
  map:
    key1: v1
    key2: v2
    key3:
      subkey1: vv1
      subkey2: vv2
      subkey3:
      - nested element 1
      - nested element 2

  1: 430.0
  2: this starts as a string
  array2:
  - 1
  - 2
  - 3
  - 4
  inline_array_merge:
  - will be overwritten
  - this: will
    be: merged

And want to merge in this:

# examples/basic/merge.yml
top:
  new_key: this is added
  orig_key: this is replaced
  map:
    key4: added key
    key1: replaced key
    key2: ~
    key3:
      subkey3:
      - (( append ))
      - nested element 3
  array1:
  - (( prepend ))
  - prepend this
  array2:
  - over
  - ridden
  - array
  1: You can change types too
  2:
    even: drastically
    to:   from scalars to maps/lists
  inline_array_merge:
  - (( inline ))
  - this has been overwritten
  - be: overwritten
    merging: success!
othertop: you can add new top level keys too

I would use spruce like this:

$ spruce merge main.yml merge.yml
othertop: you can add new top level keys too
top:
  1: You can change types too
  2:
    even: drastically
    to: from scalars to maps/lists
  array1:
  - prepend this
  - first element
  - second element
  - third element
  array2:
  - over
  - ridden
  - array
  - 4
  inline_array_merge:
  - this has been overwritten
  - be: overwritten
    merging: success!
    this: will
  map:
    key1: replaced key
    key2: null
    key3:
      subkey1: vv1
      subkey2: vv2
      subkey3:
      - nested element 1
      - nested element 2
      - nested element 3
    key4: added key
  new_key: this is added
  number: 50
  orig_key: this is replaced

Map Replacement

One of spiff's quirks was that it quite easily allowed you to completely replace an entire map, with new data (rather than merging by default). That result is still possible with spruce, but it takes a little bit more work, since the primary use case is to merge two maps together:

We start with this yaml:

# examples/map-replacement/original.yml
untouched:
  map: stays
  the: same
map_to_replace:
  has: upstream
  data: that
  we: do
  not: want

Next, create a YAML file to clear out the map:

# examples/map-replacement/delete.yml
map_to_replace: ~

Now, create a YAML file to insert the data you want in the end:

# examples/map-replacement/insert.yml
map_to_replace:
  my: special
  data: here

And finally, merge it all together:

$ spruce merge original.yml delete.yml insert.yml
map_to_replace:
  my: special
  data: here
untouched:
  map: stays
  the: same

<a name-"ex-key-removal">

Key Removal

How about deleting keys outright? Use the --prune flag to the merge command:

# examples/key-removal/original.yml
deleteme:
  thing:
    foo: 1
    bar: 2
# examples/key-removal/things.yml
things:
- name: first-thing
  foo: (( grab deleteme.thing.foo ))
- name: second-thing
  bar: (( grab deleteme.thing.bar ))
$ spruce merge --prune deleteme original.yml things.yml

The deleteme key is only useful for holding a temporary value, so we'd really rather not see it in the final output. --prune drops it.

Lists of Maps

Let's say you have a list of maps that you would like to merge into another list of maps, while preserving as much data as possible.

Given this original.yml:

# examples/list-of-maps/original.yml
jobs:
- name: concatenator_z1
  instances: 5
  resource_pool: small
  properties:
    spruce: is cool
- name: oldjob_z1
  instances: 4
  resource_pool: small
  properties:
    this: will show up in the end

And this new.yml:

# examples/list-of-maps/new.yml
jobs:
- name: newjob_z1
  instances: 3
  resource_pool: small
  properties:
    this: is a job defined solely in new.yml
- name: concatenator_z1
  properties:
    this: is a new property added to an existing job

You would get this when merged:

$ spruce merge original.yml new.yml
jobs:
- instances: 5
  name: concatenator_z1
  properties:
    spruce: is cool
    this: is a new property added to an existing job
  resource_pool: small
- instances: 4
  name: oldjob_z1
  properties:
    this: will show up in the end
  resource_pool: small
- instances: 3
  name: newjob_z1
  properties:
    this: is a job defined solely in new.yml
  resource_pool: small

Pretty sweet, huh?

Static IPs

Lets define our jobs.yml:

# examples/static-ips/jobs.yml
jobs:
- name: staticIP_z1
  instances: 3
  networks:
  - name: net1
    static_ips: (( static_ips(0, 2, 4) ))
- name: api_z1
  instances: 3
  networks:
  - name: net1
    static_ips: (( static_ips(1, 3, 5) ))

Next, we'll define our properties.yml:

# examples/static-ips/properties.yml
properties:
  staticIP_servers: (( grab jobs.staticIP_z1.networks.net1.static_ips ))
  api_servers: (( grab jobs.api_z1.networks.net1.static_ips ))

And lastly, define our networks.yml:

# examples/static-ips/networks.yml
networks:
- name: net1
  subnets:
  - cloud_properties: random
    static:
    - 192.168.0.2 - 192.168.0.10

Merge it all together, and see what we get:

$ spruce merge jobs.yml properties.yml networks.yml
jobs:
- instances: 3
  name: staticIP_z1
  networks:
  - name: net1
    static_ips:
    - 192.168.0.2
    - 192.168.0.4
    - 192.168.0.6
- instances: 3
  name: api_z1
  networks:
  - name: net1
    static_ips:
    - 192.168.0.3
    - 192.168.0.5
    - 192.168.0.7
networks:
- name: net1
  subnets:
  - cloud_properties: random
    static:
    - 192.168.0.2 - 192.168.0.10
properties:
  api_servers:
  - 192.168.0.3
  - 192.168.0.5
  - 192.168.0.7
  staticIP_servers:
  - 192.168.0.2
  - 192.168.0.4
  - 192.168.0.6

Injecting Subtrees

One of the great things about YAML is the oft-overlooked << inject operator, which lets you start with a copy of another part of the YAML tree and override keys, like this:

# examples/inject/all-in-one.yml
meta:
  template: &template
    color: blue
    size: small

green:
  <<: *template
  color: green

Here, $.green.size will be small, but $.green.color stays as green:

$ spruce merge --prune meta all-in-one.yml
green:
  color: green
  size: small

That works great if you are in the same file, but what if you want to inject data from a different file into your current map and then override some things?

That's where (( inject ... )) really shines.

# examples/inject/templates.yml
meta:
  template:
    color: blue
    size: small
# examples/inject/green.yml
green:
  woot: (( inject meta.template ))
  color: green
$ spruce merge --prune meta templates.yml green.yml
green:
  color: green
  size: small

Note: The key used for the (( inject ... )) call (in this case, woot) is removed from the final tree as part of the injection operator.

Parameters

Sometimes, you may want to start with a good starting-point template, but require other YAML files to provide certain values. Parameters to the rescue!

# examples/params/global.yml
disks:
  small: 4096
  medium: 8192
  large:  102400
  networks: (( param "please define the networks" ))
  os:
    - ubuntu
    - centos
    - fedora

And then combine that with these local definitions:

# examples/params/local.yml
disks:
  medium: 16384
  networks:
    - name: public
      range: 10.40.0.0/24
    - name: inside
      range: 10.60.0.0/16

This works, but if local.yml forgot to specify the top-level networks key, or an error should be emitted.

Author

Written By Geoff Franks and James Hunt, inspired by spiff

Thanks to Long Nguyen for breaking it repeatedly in the interest of improvement and quality assurance.

License

Licensed under the Apache License v2.0

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis
Godeps
_workspace/src/github.com/geofffranks/simpleyaml
a Go package to interact with arbitrary YAML.
a Go package to interact with arbitrary YAML.
_workspace/src/github.com/jtolds/gls
Package gls implements goroutine-local storage.
Package gls implements goroutine-local storage.
_workspace/src/github.com/smartystreets/assertions
Package assertions contains the implementations for all assertions which are referenced in goconvey's `convey` package (github.com/smartystreets/goconvey/convey) for use with the So(...) method.
Package assertions contains the implementations for all assertions which are referenced in goconvey's `convey` package (github.com/smartystreets/goconvey/convey) for use with the So(...) method.
_workspace/src/github.com/smartystreets/assertions/internal/oglematchers
Package oglematchers provides a set of matchers useful in a testing or mocking framework.
Package oglematchers provides a set of matchers useful in a testing or mocking framework.
_workspace/src/github.com/smartystreets/assertions/internal/oglemock/createmock command
createmock is used to generate source code for mock versions of interfaces from installed packages.
createmock is used to generate source code for mock versions of interfaces from installed packages.
_workspace/src/github.com/smartystreets/assertions/internal/oglemock/generate
Package generate implements code generation for mock classes.
Package generate implements code generation for mock classes.
_workspace/src/github.com/smartystreets/assertions/internal/oglemock/generate/test_cases/complicated_pkg
Package complicated_pkg contains an interface with lots of interesting cases, for use in integration testing.
Package complicated_pkg contains an interface with lots of interesting cases, for use in integration testing.
_workspace/src/github.com/smartystreets/assertions/internal/oglemock/generate/test_cases/renamed_pkg
A package that calls itself something different than its package path would have you believe.
A package that calls itself something different than its package path would have you believe.
_workspace/src/github.com/smartystreets/assertions/internal/ogletest
Package ogletest provides a framework for writing expressive unit tests.
Package ogletest provides a framework for writing expressive unit tests.
_workspace/src/github.com/smartystreets/assertions/should
package should is simply a rewording of the assertion functions in the assertions package.
package should is simply a rewording of the assertion functions in the assertions package.
_workspace/src/github.com/smartystreets/goconvey/convey
Package convey contains all of the public-facing entry points to this project.
Package convey contains all of the public-facing entry points to this project.
_workspace/src/github.com/smartystreets/goconvey/convey/gotest
Package gotest contains internal functionality.
Package gotest contains internal functionality.
_workspace/src/github.com/smartystreets/goconvey/convey/reporting
Package reporting contains internal functionality related to console reporting and output.
Package reporting contains internal functionality related to console reporting and output.
_workspace/src/github.com/voxelbrain/goptions
package goptions implements a flexible parser for command line options.
package goptions implements a flexible parser for command line options.
_workspace/src/gopkg.in/yaml.v2
Package yaml implements YAML support for the Go language.
Package yaml implements YAML support for the Go language.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL