hiera-postgres-backend 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0cbe610dafd58e2b3222695692a84ea439f4269f
4
+ data.tar.gz: f7f3946bee8abf69ea12f894c7c15f5cb8ea91cd
5
+ SHA512:
6
+ metadata.gz: 8d306bed635ec9e2aa8fcd3e878271dc42d87da02cacbda5e2bd10d5d6b5fdbc6ae916911b5ab4e59baadca635773d02dc4de499fcdd73a2ce11dc56caee933c
7
+ data.tar.gz: 6c3af395d3076858cdacefd7557f4df41e7406e00ea53138673d8ebcb87bd6cd82ba88c25d7092f62d9f32351cdf894a9ae3fd6de22ff5b7f7f69a62edccf69f
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in hiera-postgres-backend.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ hiera-postgres-backend (0.0.1)
5
+ hiera
6
+ pg
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ hiera (1.3.0)
12
+ json_pure
13
+ json_pure (1.8.1)
14
+ pg (0.17.1)
15
+ rake (10.1.1)
16
+
17
+ PLATFORMS
18
+ ruby
19
+
20
+ DEPENDENCIES
21
+ hiera-postgres-backend!
22
+ rake
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2013 Adrian Lopez
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,68 @@
1
+ ## hiera-postgresql-backend
2
+
3
+ Alternate PostgreSQL backend for Hiera
4
+
5
+ This is a PostgreSQL backend for Hiera inspired by [hiera-mysql-backend](https://github.com/Telmo/hiera-mysql-backend).
6
+
7
+
8
+ ### What goes into the sql files.
9
+
10
+ The poorly named sql files are really yaml files where the key is the lookup key and the value is the SQL statement (it accepts interpolation)
11
+
12
+ Lets assume your _datadir_ is `/etc/puppet/hieradata/` and your hierarchy for hiera just have a common. hiera-postgresql-backend would look for /etc/puppet/hieradata/common.sql the common.sql would look like:
13
+
14
+ ```yaml
15
+ ---
16
+ applications: SELECT * FROM applications WHERE host='%{fqdn}';
17
+ coats: SELECT cut,name,type FROM coats WHERE color='brown';
18
+ ```
19
+
20
+ running `hiera applications` would run the query against the configured database.
21
+
22
+
23
+ ### Using
24
+
25
+ `gem install hiera-postgresql-backend`
26
+
27
+
28
+ ### Configuring Hiera
29
+
30
+ Hiera configuration is pretty simple
31
+
32
+ ```yaml
33
+ ---
34
+ :backends:
35
+ - yaml
36
+ - postgres
37
+
38
+ :yaml:
39
+ :datadir: /etc/puppet/hieradata
40
+
41
+ :postgres:
42
+ :datadir: /etc/puppet/hieradata
43
+ :host: hostname
44
+ :user: username
45
+ :pass: password
46
+ :database: database
47
+
48
+ :hierarchy:
49
+ - "%{::clientcert}"
50
+ - "%{::custom_location}"
51
+ - common
52
+
53
+ :logger: console
54
+ ```
55
+
56
+ ## Known issues
57
+
58
+ 1. It always return an Array of hashes regardless of the number of items returned. (I did this on purpose because it is what I needed but I may be persuaded to do otherwise)
59
+ 2. This README is poorly written.
60
+
61
+
62
+ ## Contributing
63
+
64
+ 1. Fork it
65
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
66
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
67
+ 4. Push to the branch (`git push origin my-new-feature`)
68
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |gem|
4
+ gem.name = "hiera-postgres-backend"
5
+ gem.version = "0.0.1"
6
+ gem.authors = ["Adrian"]
7
+ gem.email = ["adrianlzt@gmail.com"]
8
+ gem.description = %q{Alternative PostgreSQL backend for hiera}
9
+ gem.summary = %q{Alternative PostgreSQL backend for hiera}
10
+ gem.homepage = "https://github.com/adrianlzt/hiera-postgres-backend"
11
+
12
+ gem.files = `git ls-files`.split($/)
13
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
14
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
15
+ gem.require_paths = ["lib"]
16
+ gem.add_dependency('pg')
17
+ gem.add_dependency('hiera')
18
+ gem.add_development_dependency('rake')
19
+ end
@@ -0,0 +1,78 @@
1
+ class Hiera
2
+ module Backend
3
+ class Postgres_backend
4
+
5
+ def initialize(cache=nil)
6
+ begin
7
+ require 'pg'
8
+ rescue LoadError
9
+ require 'rubygems'
10
+ require 'pg'
11
+ end
12
+
13
+ @cache = cache || Filecache.new
14
+
15
+ Hiera.debug("Hiera PostgreSQL initialized")
16
+ end
17
+
18
+ def lookup(key, scope, order_override, resolution_type)
19
+ # default answer is set to nil otherwise the lookup ends up returning
20
+ # an Array of nils and causing a Puppet::Parser::AST::Resource failed with error ArgumentError
21
+ # for any other lookup because their default value is overwriten by [nil,nil,nil,nil]
22
+ # so hiera('myvalue', 'test1') returns [nil,nil,nil,nil]
23
+ results = nil
24
+
25
+ Hiera.debug("looking up #{key} in PostgreSQL Backend")
26
+ Hiera.debug("resolution type is #{resolution_type}")
27
+
28
+ Backend.datasources(scope, order_override) do |source|
29
+ Hiera.debug("Looking for data source #{source}")
30
+ sqlfile = Backend.datafile(:postgres, scope, source, "sql") || next
31
+
32
+ next unless File.exist?(sqlfile)
33
+ data = @cache.read(sqlfile, Hash, {}) do |datafile|
34
+ YAML.load(datafile)
35
+ end
36
+
37
+ Hiera.debug("data #{data.inspect}")
38
+ next if data.empty?
39
+ next unless data.include?(key)
40
+
41
+ Hiera.debug("Found #{key} in #{source}")
42
+
43
+ new_answer = Backend.parse_answer(data[key], scope)
44
+ results = query(new_answer)
45
+
46
+ end
47
+ return results
48
+ end
49
+
50
+
51
+ def query(query)
52
+ Hiera.debug("Executing SQL Query: #{query}")
53
+
54
+ data=nil
55
+ pg_host = Config[:postgres][:host]
56
+ pg_user = Config[:postgres][:user]
57
+ pg_pass = Config[:postgres][:pass]
58
+ pg_database = Config[:postgres][:database]
59
+ client = PG::Connection.new(:host => pg_host,
60
+ :user => pg_user,
61
+ :password => pg_pass,
62
+ :dbname => pg_database)
63
+ begin
64
+ data = client.exec(query).to_a
65
+ Hiera.debug("PostgreSQL Query returned #{data.size} rows")
66
+ rescue => e
67
+ Hiera.debug e.message
68
+ data = nil
69
+ ensure
70
+ client.close
71
+ end
72
+
73
+ return data
74
+
75
+ end
76
+ end
77
+ end
78
+ end
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hiera-postgres-backend
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Adrian
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-01-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: pg
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: hiera
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Alternative PostgreSQL backend for hiera
56
+ email:
57
+ - adrianlzt@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - Gemfile
63
+ - Gemfile.lock
64
+ - LICENSE
65
+ - README.md
66
+ - Rakefile
67
+ - hiera-postgres-backend.gemspec
68
+ - lib/hiera/backend/postgres_backend.rb
69
+ homepage: https://github.com/adrianlzt/hiera-postgres-backend
70
+ licenses: []
71
+ metadata: {}
72
+ post_install_message:
73
+ rdoc_options: []
74
+ require_paths:
75
+ - lib
76
+ required_ruby_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - '>='
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ requirements: []
87
+ rubyforge_project:
88
+ rubygems_version: 2.0.7
89
+ signing_key:
90
+ specification_version: 4
91
+ summary: Alternative PostgreSQL backend for hiera
92
+ test_files: []