Giter Club home page Giter Club logo

kvexpress's Introduction

kvexpress

wercker status

Why?

Small Go utility to:

  1. Put data into Consul's KV store.
  2. Pull data out of Consul's KV store and write it to a file.

Why a dedicated utility though? Can't I just do it with curl?

Yes you can - but we kept wanting to:

  1. Make sure the file was long enough. 0-length configuration files are bad.
  2. Load the file from some other custom templating process - not just from straight KV files.
  3. Put the file into any location in the filesystem.
  4. Restart/reload/stop/start daemon after writing the file.
  5. Run some other custom command after writing the file.
  6. Verify that the file we put into the KV was the same file that was written on the other end.
  7. Stop the process on all nodes - in or out - if we want everything to stay as it is for the moment.

We did this at first with some custom Ruby scripts - but the pattern was apparent and could be applied to many other files as well.

This replaces our previous custom Ruby/shell scripts with a single Go binary we can use to get data in and out of Consul's KV store.

How does it work? - 1000 foot view

There's an introductory blog post available that shows how it can be used.

In

kvexpress in --key hosts --file /etc/consul-template/output/hosts.consul --length 100 --sorted=true

  1. Check that at least --file and --key are passed along with the command. Quit if they're not present - there are no safe defaults for those flags.
  2. Check for the existence of a stop key - if it's there - stop and exit.
  3. Read the file into a string, and sort the string if requested.
  4. Check if the file is long enough - if not - stop and exit.
  5. Save the file to a .compare file - we will use this data from now on.
  6. Check for the existence of a .last file - if it's not there - create it.
  7. Are the .compare and .last files blank? If not - let's continue.
  8. Compare the checksums of the .compare and .last files - if they're different - continue.
  9. Grab the checksum from Consul and compare with the .compare file - if it's different - then let's update. This is to guard against it running on multiple server nodes that might have different .last files.
  10. Save data, and checksum keys.
  11. Copy .compare to .last
  12. If --exec is passed - run that command.

Out

kvexpress out -k hosts -f /etc/hosts.consul -l 100 -e 'sudo pkill -HUP dnsmasq'

  1. Check that at least --file and --key are passed along with the command. Quit if they're not present - there are no safe defaults for those flags.
  2. Check for the existence of a stop key - if it's there - stop and exit.
  3. Pull the data and checksum keys out of Consul.
  4. If data is long enough and the checksum as computed on this side matches the checksum key - then continue.
  5. Write the contents of data to the passed --file location.
  6. If --exec is passed - run that command.

Where can I get it?

Build instructions are available below.

Debian packages for Ubuntu can be downloaded from packagecloud.

Additional binaries can be downloaded from the releases page.

A Chef cookbook is available as well.

How to use it with Consul watches.

Instructions are available here.

Ad-Hoc Usage with Consul exec

If you want to push a file to all nodes in your Consul cluster quickly - you can use Consul and kvexpress.

kvexpress in --prefix tmp --key config --length 5 --url https://git.io/vzz5P

This places the file and checksum in:

tmp/config/data
tmp/config/checksum

To get the file on every node:

consul exec "sudo kvexpress out --prefix tmp --key config --length 5 -f /etc/file.conf -e 'sudo restart something here'"

If you're not seeing it work the way you expect - you can always add --verbose to the kvexpress command - that will add lots of additional output.

Commands Available

A detailed list of commands is available here.

Consul KV Structure

How are keys organized in Consul's KV store to work with kvexpress?

Underneath a global prefix /kvexpress/ - each directory represents a specific file we are distributing through the KV store.

Each directory is named for the unique key and has the following keys underneath it:

  1. data - where the configuration file is stored.
  2. checksum - where the SHA256 of the data is stored.

For example - the hosts file is arranged like this:

/kvexpress/hosts/data
/kvexpress/hosts/checksum

There is an optional stop key - that if present - will cause all in and out processes to stop before writing anything. Allows us to freeze the automatic process if we need to.

Logging

All logs are sent to syslog and are tagged with kvexpress. To enable debug logs, please export KVEXPRESS_DEBUG=1

Build

Can be built with the standard go toolchain: go get -u -v github.com/DataDog/kvexpress

To build manually - clone the repo then: make deps && make

To run integration tests: make deps && make && make test - it will spin up an empty Consul and kill it after the run.

Because we use user.Current() - you can't cross compile this. If you want to build for Linux - you must build on Linux. Closed Issue

To install Consul - there are instructions here.

To launch an empty Consul instance: make consul

Contributing

We love pull requests from anyone. Details are available here.

Code of Conduct

This project adheres to the [Open Code of Conduct][code-of-conduct]. By participating, you are expected to honor this code. [code-of-conduct]: http://todogroup.org/opencodeofconduct/#kvexpress/[email protected]

kvexpress's People

Contributors

athap avatar d33d33 avatar darron avatar isavcic avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

kvexpress's Issues

Blank lines are annoying.

With the --sorted flag - if there are lots of blank lines then it keeps them all.

Need to remove them - don't want to shell out to uniq to do it.

Doesn't copy the file to .last if the checksum in the KV store matches.

You end up in this state:

[staging]root@i-c972d41e:/etc/consul-template/output# ls -al
total 48
drwxr-x--- 2 root root  4096 Nov  3 22:39 .
drwxr-x--- 7 root root  4096 Aug  7 16:33 ..
-rw-r--r-- 1 root root 16516 Nov  3 22:39 config_hosts
-rw-r----- 1 root root 16335 Nov  3 22:39 config_hosts.compare
-rw-r----- 1 root root    22 Nov  3 22:39 config_hosts.last

Where - the files didn't match - but the checksum inside the KV store matched:

[staging]root@i-c972d41e:/etc/consul-template/output# shasum -a 256 config_hosts.compare
deab902e0ab2f432f0ebc68035992f10a743059222cbea991f3345ba195c4dad  config_hosts.compare

So it didn't update the KV store - but it leaves the .last file in the wrong state.

Add some tests.

There's none at the moment.

Would be nice to have some automated ones.

May need to set the data, checksum and updated keys all at once.

To deal with possible race condition where:

  1. We're watching the "data" key.
  2. We update the "data" key - but not the "checksum" or the "updated" keys.
  3. Watches fire - and they checksum against the old version.

A possible alternative solution:

May need to instead watch the "updated" key as that's updated last.

Tokens

We sometimes need to use them.

Right now there's no token awareness at all.

Logging is too verbose.

Which is quite nice - helps with all sorts of debugging - but will need to be tuned when we're in production.

Make a way to do that.

Add compression?

So that we can put larger files in AND/OR use less network traffic.

When there's a folder the path doesn't work right.

Example output:

Oct 30 15:48:58 i-c972d41e kvexpress:  2015/10/30 15:48:58 in: file='compare' full_path='/etc/consul-template/output/etc/consul-template/output/config_consul_config.ini.compare'
Oct 30 15:48:58 i-c972d41e kvexpress:  2015/10/30 15:48:58 in: file='last' full_path='/etc/consul-template/output/etc/consul-template/output/config_consul_config.ini.last'

Add `url` command.

To grab a file from a URL and place it as a file.

So that we could grab files as a replacement if necessary.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.