hiera-mysql-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.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ coverage
6
+ InstalledFiles
7
+ lib/bundler/man
8
+ pkg
9
+ rdoc
10
+ spec/reports
11
+ test/tmp
12
+ test/version_tmp
13
+ tmp
14
+
15
+ # YARD artifacts
16
+ .yardoc
17
+ _yardoc
18
+ doc/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in hiera-mysql-backend.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,22 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ hiera-mysql-backend (0.0.1)
5
+ hiera
6
+ mysql2
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ hiera (1.2.1)
12
+ json_pure
13
+ json_pure (1.8.0)
14
+ mysql2 (0.3.13)
15
+ rake (10.1.0)
16
+
17
+ PLATFORMS
18
+ ruby
19
+
20
+ DEPENDENCIES
21
+ hiera-mysql-backend!
22
+ rake
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2013 Roberto
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.
data/README.md ADDED
@@ -0,0 +1,77 @@
1
+ ## hiera-mysql-backend
2
+
3
+ Alternate MySQL backend for Hiera
4
+
5
+ This is a MySQL backend for Hiera inspired by [hiera-mysql](https://github.com/crayfishx/hiera-mysql). Unfortunately no work has been done to that backend for the past 9 months and it was missing a couple of features I needed so I decided to pick up the torch and implement them myself.
6
+
7
+ ### What is different from hiera-mysql
8
+
9
+ In [hiera-mysql](https://github.com/crayfishx/hiera-mysql) you define the queries in the hiera.yaml file. I felt this was too restricting so instead hiera-mysql-backend uses, poorly named, sql files. This sql files follow the Hiera hierarchy.
10
+
11
+ [hiera-mysql](https://github.com/crayfishx/hiera-mysql) would also return the last matching query not the first one which I felt it was confusing.
12
+
13
+ [hiera-mysql](https://github.com/crayfishx/hiera-mysql) used the mysql gem, I am partial to [mysql2](https://github.com/brianmario/mysql2)
14
+
15
+ Exception handling. hiera-mysql would cause a puppet run to fail if one of the queries was incorrect. For example a fact that you are distributing with a module is needed for the query to return its data but that fact is not available outside the module having a `SELECT from * %{custom_fact}` would make puppet runs fail.
16
+
17
+ ### What goes into the sql files.
18
+
19
+ 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)
20
+
21
+ Lets assume your _datadir_ is `/etc/puppet/hieradata/` and your hierarchy for hiera just have a common. hiera-mysql-backend would look for /etc/puppet/hieradata/common.sql the common.sql would look like:
22
+
23
+ ```yaml
24
+ ---
25
+ applications: SELECT * FROM applications WHERE host='%{fqdn}';
26
+ coats: SELECT cut,name,type FROM coats WHERE color='brown';
27
+ ```
28
+
29
+ running `hiera applications` would run the query against the configured database.
30
+
31
+
32
+ ### Using
33
+
34
+ `gem install hiera-mysql-backend`
35
+
36
+
37
+ ### Configuring Hiera
38
+
39
+ Hiera configuration is pretty simple
40
+
41
+ ```yaml
42
+ ---
43
+ :backends:
44
+ - yaml
45
+ - mysql2
46
+
47
+ :yaml:
48
+ :datadir: /etc/puppet/hieradata
49
+
50
+ :mysql2:
51
+ :datadir: /etc/puppet/hieradata
52
+ :host: hostname
53
+ :user: username
54
+ :pass: password
55
+ :database: database
56
+
57
+ :hierarchy:
58
+ - "%{::clientcert}"
59
+ - "%{::custom_location}"
60
+ - common
61
+
62
+ :logger: console
63
+ ```
64
+
65
+ ## Known issues
66
+
67
+ 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)
68
+ 2. This README is poorly written.
69
+
70
+
71
+ ## Contributing
72
+
73
+ 1. Fork it
74
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
75
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
76
+ 4. Push to the branch (`git push origin my-new-feature`)
77
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -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-mysql-backend"
5
+ gem.version = "0.0.1"
6
+ gem.authors = ["Telmo"]
7
+ gem.email = ["telmox@gmail.com"]
8
+ gem.description = %q{Alternative MySQL backend for hiera}
9
+ gem.summary = %q{Alternative MySQL backend for hiera}
10
+ gem.homepage = "https://github.com/Telmo/hiera-mysql-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('mysql2')
17
+ gem.add_dependency('hiera')
18
+ gem.add_development_dependency('rake')
19
+ end
@@ -0,0 +1,71 @@
1
+ class Hiera
2
+ module Backend
3
+ class Mysql2_backend
4
+
5
+ def initialize(cache=nil)
6
+ begin
7
+ require 'mysql2'
8
+ rescue LoadError
9
+ require 'rubygems'
10
+ require 'mysql2'
11
+ end
12
+
13
+ @cache = cache || Filecache.new
14
+
15
+ Hiera.debug("Hiera MySQL2 initialized")
16
+ end
17
+
18
+ def lookup(key, scope, order_override, resolution_type)
19
+ # default answer just to make it easier on ourselves
20
+
21
+ Hiera.debug("looking up %{key} in MySQL Backend")
22
+ Hiera.debug("resolution type is #{resolution_type}")
23
+
24
+ Backend.datasources(scope, order_override) do |source|
25
+ Hiera.debug("Looking for data source #{source}")
26
+ sqlfile = Backend.datafile(:mysql2, scope, source, "sql") || next
27
+
28
+ next unless File.exist?(sqlfile)
29
+ data = @cache.read(sqlfile, Hash, {}) do |datafile|
30
+ YAML.load(datafile)
31
+ end
32
+
33
+ next if data.empty?
34
+ next unless data.include?(key)
35
+
36
+ Hiera.debug("Found #{key} in #{source}")
37
+
38
+ new_answer = Backend.parse_answer(data[key], scope)
39
+ results = query(new_answer)
40
+ return results
41
+ end
42
+ end
43
+
44
+
45
+ def query(query)
46
+ Hiera.debug("Executing SQL Query: #{query}")
47
+
48
+ data=[]
49
+ mysql_host = Config[:mysql2][:host]
50
+ mysql_user = Config[:mysql2][:user]
51
+ mysql_pass = Config[:mysql2][:pass]
52
+ mysql_database = Config[:mysql2][:database]
53
+ client = Mysql2::Client.new(:host => mysql_host,
54
+ :username => mysql_user,
55
+ :password => mysql_pass,
56
+ :database => mysql_database,
57
+ :reconnect => true)
58
+ begin
59
+ data = client.query(query).to_a
60
+ Hiera.debug("Mysql Query returned #{data.size} rows")
61
+ rescue => e
62
+ Hiera.debug e.message
63
+ data = nil
64
+ end
65
+
66
+ return data
67
+
68
+ end
69
+ end
70
+ end
71
+ end
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hiera-mysql-backend
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Telmo
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-10-08 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: mysql2
16
+ requirement: &19520820 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *19520820
25
+ - !ruby/object:Gem::Dependency
26
+ name: hiera
27
+ requirement: &19520380 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *19520380
36
+ - !ruby/object:Gem::Dependency
37
+ name: rake
38
+ requirement: &19519900 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *19519900
47
+ description: Alternative MySQL backend for hiera
48
+ email:
49
+ - telmox@gmail.com
50
+ executables: []
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - .gitignore
55
+ - Gemfile
56
+ - Gemfile.lock
57
+ - LICENSE
58
+ - README.md
59
+ - Rakefile
60
+ - hiera-mysql-backend.gemspec
61
+ - lib/hiera/backend/mysql2_backend.rb
62
+ homepage: https://github.com/Telmo/hiera-mysql-backend
63
+ licenses: []
64
+ post_install_message:
65
+ rdoc_options: []
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ! '>='
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ segments:
75
+ - 0
76
+ hash: -490907018975292232
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ none: false
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ segments:
84
+ - 0
85
+ hash: -490907018975292232
86
+ requirements: []
87
+ rubyforge_project:
88
+ rubygems_version: 1.8.11
89
+ signing_key:
90
+ specification_version: 3
91
+ summary: Alternative MySQL backend for hiera
92
+ test_files: []