Skip to content

Instantly share code, notes, and snippets.

@erichelgeson
Last active April 29, 2016 09:20
Show Gist options
  • Save erichelgeson/8742d6f2563409e9d06f to your computer and use it in GitHub Desktop.
Save erichelgeson/8742d6f2563409e9d06f to your computer and use it in GitHub Desktop.
Replace chef-server with consul (because why not?)

Federated Chef-server'ish with consul.

Why?

Why not? I thought it'd be interesting, and maybe useful.

Goals

  • No centralized chef-server
  • Nodes still know about eachother (so chef-searchs work)
  • Use consul-templates / services for things that need to be updated 'realtime'
  • Use chef resources for installation and configuration of the rest

Requirements

  • Chef with local-mode
  • consul setup
  • fileconsul (0.1.0, not 0.1.1)
  • wget http://dl.bintray.com/foostan/fileconsul/0.1.0_linux_amd64.zip
  • 0.1.1 wont work as it cleans all entries on push (so nodes dont append, they just get deleted)
  • See foostan/fileconsul#1

Publish changes

folders="roles clients databags environemnts users"
chefRepo="/home/vagrant/chef-repo"
function consulFilePublish() {
  for folder in $folders; do
    echo "Pushing $folder to consul..."
    fileconsul push --prefix "$folder" --basepath $chefRepo/$folder
  done
}

# Modify some files/cookbooks
consulFile push

Remote server

# Setup a consul cluster https://www.consul.io/intro/getting-started/install.html
./chefconsul.sh
#!/bin/bash
folders="nodes roles clients databags environemnts users cookbooks"
chefRepo="/home/vagrant/chef-repo"
nodeName=$(hostname) # chef node name
for folder in $folders; do
mkdir -p $chefRepo/$folder
done
function consulFile() {
for folder in $folders; do
echo "Pulling $folder..."
fileconsul pull --prefix "$folder" --basepath $chefRepo/$folder
done
}
# Chef Run
cd $chefRepo
echo "Pulling chef-repo from consul to location $chefRepo"
consulFile pull
chef-client -z -r helloworld # Unsure how to set runlist yet? Maybe consul?!
echo "Saving $nodeName back to consul"
fileconsul push --prefix 'nodes' --basepath $chefRepo/nodes
[vagrant@node2 ~]$ ./chefconsul.sh
Pulling chef-repo from consul to location /home/vagrant/chef-repo
Pulling roles...
Already up-to-date.
Pulling clients...
Already up-to-date.
Pulling databags...
Already up-to-date.
Pulling environemnts...
Already up-to-date.
Pulling users...
Already up-to-date.
Pulling latest nodes from consul
Synchronize remote files:
new file: /home/vagrant/chef-repo/nodes/node1.json # <- Note it pulls down node1.json from previous run on a different node
Already up-to-date.
[2015-04-12T19:17:50+00:00] WARN: No config file found or specified on command line, using command line options.
Starting Chef Client, version 12.2.1
resolving cookbooks for run list: ["helloworld"]
Synchronizing Cookbooks:
- helloworld
Compiling Cookbooks...
Converging 1 resources
Recipe: helloworld::default
* file[/home/vagrant/x.txt] action create (up to date)
a
Running handlers:
Running handlers complete
Chef Client finished, 0/1 resources updated in 5.991165036 seconds
Saving node2 back to consul
push modified file: /home/vagrant/chef-repo/nodes/node2.json # <-- Saving this node (node2) back to consul, so node 1 will now see it
# Nodes dir are in sync!
[vagrant@node2 ~]$ ls chef-repo/nodes/
node1.json node2.json
[vagrant@node1 ~]$ ls chef-repo/nodes/
node1.json node2.json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment