Skip to content

Commit 094275a

Browse files
committed
initial commit
0 parents  commit 094275a

File tree

6 files changed

+114
-0
lines changed

6 files changed

+114
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pkg
2+
Gemfile.lock

Gemfile

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
source "http://rubygems.org"
2+
3+
4+
gem "rake"
5+
gem "puppet", ENV['PUPPET_VERSION'] || '~> 3.2.0'
6+
gem "puppetlabs_spec_helper"

Modulefile

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
name 'garethr/hiera_etcd'
2+
version '0.0.1'
3+
source 'git://github.com/garethr/hiera-etcd.git'
4+
author 'Gareth Rushgrove'
5+
summary 'Module for installing a etcd hiera backend'
6+
description 'Use etcd, the distributed configuration service, as a hiera backend'
7+
license 'Apache License, Version 2.0'

README.md

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
[etcd](https://github.com/coreos/etcd) is a highly-available key value store for shared configuration and service discovery. Hiera-etcd provides a Hiera backend which allows for specifying multiple etcd paths from which data can be collected and easily inserted into Puppet manifests.
2+
3+
## Configuration
4+
5+
The following hiera.yaml should get you started.
6+
7+
:backends:
8+
- etcd
9+
10+
:http:
11+
:host: 127.0.0.1
12+
:port: 4001
13+
:paths:
14+
- /configuration/%{fqdn}
15+
- /configuration/common
16+
17+
18+
## Outstanding
19+
20+
* No support for HTTPS yet
21+
* No support for etcd server disappearing
22+
* No tests
23+
24+
I'll likely swap out the Net::HTTP implementation for one using [etcd-rb](https://github.com/iconara/etcd-rb) which should make doing the right thing easier.
25+
26+
27+
## Thanks
28+
29+
The starting point for this backend was the [hiera-http](https://github.com/crayfishx/hiera-http) backend from @crayfishx.

Rakefile

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require 'puppetlabs_spec_helper/rake_tasks'

lib/hiera/backend/etcd_backend.rb

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# Hiera backend for the etcd distributed configuration service
2+
class Hiera
3+
module Backend
4+
class Etcd_backend
5+
6+
def initialize
7+
require 'net/http'
8+
require 'net/https'
9+
require 'json'
10+
@config = Config[:http]
11+
@http = Net::HTTP.new(@config[:host], @config[:port])
12+
@http.read_timeout = @config[:http_read_timeout] || 10
13+
@http.open_timeout = @config[:http_connect_timeout] || 10
14+
end
15+
16+
def lookup(key, scope, order_override, resolution_type)
17+
18+
answer = nil
19+
20+
# Extract multiple etcd paths from the configuration file
21+
paths = @config[:paths].map { |p| Backend.parse_string(p, scope, { 'key' => key }) }
22+
paths.insert(0, order_override) if order_override
23+
24+
paths.each do |path|
25+
Hiera.debug("[hiera-etcd]: Lookup #{@config[:host]}:#{@config[:port]}#{path}/#{key}")
26+
httpreq = Net::HTTP::Get.new("#{path}/#{key}")
27+
28+
# If we can't connect to etcd at all we throw an exception and
29+
# block the run
30+
begin
31+
httpres = @http.request(httpreq)
32+
rescue Exception => e
33+
Hiera.warn("[hiera-etcd]: Net::HTTP threw exception #{e.message}")
34+
raise Exception, e.message
35+
next
36+
end
37+
38+
# If we don't find any results in etcd we continue on to the
39+
# next path
40+
unless httpres.kind_of?(Net::HTTPSuccess)
41+
Hiera.debug("[hiera-etcd]: #{httpres.code} HTTP response from #{@config[:host]}:#{@config[:port]}#{path}/#{key}")
42+
next
43+
end
44+
45+
# On to the next path if we don't have a response
46+
next unless httpres.body
47+
# Parse result from standard etcd JSON response
48+
result = JSON.parse(httpres.body)['value']
49+
parsed_result = Backend.parse_answer(result, scope)
50+
51+
# Format response as specified type, either array, hash or text
52+
case resolution_type
53+
when :array
54+
answer ||= []
55+
answer << parsed_result
56+
when :hash
57+
answer ||= {}
58+
answer = parsed_result.merge answer
59+
else
60+
answer = parsed_result
61+
break
62+
end
63+
end
64+
answer
65+
end
66+
67+
end
68+
end
69+
end

0 commit comments

Comments
 (0)