hiera-mysql-json-backend-jruby 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1ec0a1921ab9c585ccb31e6daa4f338f2b785091
4
+ data.tar.gz: b868fa9c149b2e0880d7d20103218c7296274ed4
5
+ SHA512:
6
+ metadata.gz: a8982ba5e33879bfebc105bc299fc996079aa825b9a5d7c1d7f4b9d35ccceb1bb58eaa91041321b1e86413ed79eb47155da3a70f703ad1d68fbad1d752b35122
7
+ data.tar.gz: 1ce50e31cd93ea20bb93ca7e690942e2b5f5310506b90185ea5dce3c4a70f48987f8520f5399a116494e2f63026fd3abc3339f1623a0c7ecbd0127185e86b94c
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/CHANGELOG.md ADDED
@@ -0,0 +1,17 @@
1
+ ### 0.0.1
2
+ - Initial Release
3
+
4
+ ### 0.0.2 - 0.0.3
5
+ - Minor Updates
6
+
7
+ ### 0.0.4
8
+ - Connection strings can now be defined per hierarchy file instead of being global
9
+
10
+ ### 0.0.5
11
+ - Added port as one of the settings that can be defined, thanks [Dave Seff](https://github.com/daveseff)
12
+
13
+ ### 0.0.6
14
+
15
+ - Made improvements to the connection Hash
16
+ - Fixed an issue where if no port was defined nill would be passed
17
+ - Added defaults for hostname and port, hostname will default to `localhost` port to `3306`
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.4)
5
+ hiera
6
+ mysql2
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ hiera (1.3.1)
12
+ json_pure
13
+ json_pure (1.8.1)
14
+ mysql2 (0.3.14)
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,98 @@
1
+ [![Gem Version](https://badge.fury.io/rb/hiera-mysql-json-backend.png)](http://badge.fury.io/rb/hiera-mysql-json-backend)
2
+
3
+ ## hiera-mysql-backend
4
+
5
+ Alternate MySQL backend for Hiera
6
+
7
+ 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.
8
+
9
+ ### What is different from hiera-mysql
10
+
11
+ 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.
12
+
13
+ [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.
14
+
15
+ [hiera-mysql](https://github.com/crayfishx/hiera-mysql) used the mysql gem, I am partial to [mysql2](https://github.com/brianmario/mysql2)
16
+
17
+ 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.
18
+
19
+ ### What goes into the sql files.
20
+
21
+ 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)
22
+
23
+ As of version 0.0.4 you can also add connection information to these sql files, this allows you to connect to different databases. This is optional if no connection information is found it will use the default defined in your hiera.yaml config.
24
+
25
+ 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:
26
+
27
+ ```yaml
28
+ ---
29
+ # This is optional, if not present it will use the default connection info from hiera.yaml
30
+ :dbconfig:
31
+ :host: database.example.com
32
+ :user: hieratest
33
+ :pass: sekret
34
+ :database: testhieradb
35
+ :port: 44445
36
+
37
+ applications: SELECT * FROM applications WHERE host='%{fqdn}';
38
+ coats: SELECT cut,name,type FROM coats WHERE color='brown';
39
+ ```
40
+
41
+ If `host` is not defined it will use `localhost` as default.
42
+
43
+ If `port` is not defined it will use the default `3306` mysql port
44
+
45
+ running `hiera applications` would run the query against the configured database.
46
+
47
+
48
+ ### Using
49
+
50
+ `gem install hiera-mysql-backend`
51
+
52
+
53
+ ### Configuring Hiera
54
+
55
+ Hiera configuration is pretty simple
56
+
57
+ ```yaml
58
+ ---
59
+ :backends:
60
+ - yaml
61
+ - mysql2
62
+
63
+ :yaml:
64
+ :datadir: /etc/puppet/hieradata
65
+
66
+ :mysql2:
67
+ :datadir: /etc/puppet/hieradata
68
+ :host: hostname
69
+ :user: username
70
+ :pass: password
71
+ :database: database
72
+ :port: 3306
73
+
74
+ :hierarchy:
75
+ - "%{::clientcert}"
76
+ - "%{::custom_location}"
77
+ - common
78
+
79
+ :logger: console
80
+ ```
81
+
82
+ If `host` is not defined it will use `localhost` as default.
83
+
84
+ If `port` is not defined it will use the default `3306` mysql port
85
+
86
+ ## Known issues
87
+
88
+ 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)
89
+ 2. This README is poorly written.
90
+
91
+
92
+ ## Contributing
93
+
94
+ 1. Fork it
95
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
96
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
97
+ 4. Push to the branch (`git push origin my-new-feature`)
98
+ 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-json-backend-jruby"
5
+ gem.version = "1.0.0"
6
+ gem.authors = ["Hostnet"]
7
+ gem.email = ["opensource@hostnet.nl"]
8
+ gem.description = %q{Alternative MySQL backend with json support for hiera}
9
+ gem.summary = %q{Alternative MySQL backend with json support for hiera}
10
+ gem.homepage = "https://github.com/hostnet/hiera-mysql-json-backend"
11
+ gem.license = "MIT"
12
+
13
+ gem.files = `git ls-files`.split($/)
14
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
15
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
16
+ gem.require_paths = ["lib"]
17
+ gem.add_dependency('jdbc-mysql')
18
+ gem.add_development_dependency('rake')
19
+ end
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |gem|
4
+ gem.name = "hiera-mysql-json-backend"
5
+ gem.version = "1.0.0"
6
+ gem.authors = ["Hostnet"]
7
+ gem.email = ["opensource@hostnet.nl"]
8
+ gem.description = %q{Alternative MySQL backend with json support for hiera}
9
+ gem.summary = %q{Alternative MySQL backend with json support for hiera}
10
+ gem.homepage = "https://github.com/hostnet/hiera-mysql-json-backend"
11
+ gem.license = "MIT"
12
+
13
+ gem.files = `git ls-files`.split($/)
14
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
15
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
16
+ gem.require_paths = ["lib"]
17
+ gem.add_dependency('mysql2')
18
+ gem.add_dependency('hiera')
19
+ gem.add_development_dependency('rake')
20
+ end
@@ -0,0 +1,152 @@
1
+ class Hiera
2
+ module Backend
3
+ class Mysql_json_backend
4
+
5
+ def initialize(cache=nil)
6
+ require 'json'
7
+ if defined?(JRUBY_VERSION)
8
+ require 'jdbc/mysql'
9
+ require 'java'
10
+ else
11
+ begin
12
+ require 'mysql2'
13
+ rescue LoadError
14
+ require 'rubygems'
15
+ require 'mysql2'
16
+ end
17
+ end
18
+
19
+ @cache = cache || Filecache.new
20
+
21
+ Hiera.debug("Hiera mysql_json initialized")
22
+ end
23
+
24
+ def lookup(key, scope, order_override, resolution_type)
25
+ # default answer is set to nil otherwise the lookup ends up returning
26
+ # an Array of nils and causing a Puppet::Parser::AST::Resource failed with error ArgumentError
27
+ # for any other lookup because their default value is overwriten by [nil,nil,nil,nil]
28
+ # so hiera('myvalue', 'test1') returns [nil,nil,nil,nil]
29
+ results = nil
30
+
31
+ Hiera.debug("looking up #{key} in MySQL2 Backend")
32
+ Hiera.debug("resolution type is #{resolution_type}")
33
+
34
+ Backend.datasources(scope, order_override) do |source|
35
+ Hiera.debug("Looking for data source #{source}")
36
+ sqlfile = Backend.datafile(:mysql2, scope, source, "sql") || next
37
+
38
+ next unless File.exist?(sqlfile)
39
+ data = @cache.read(sqlfile, Hash, {}) do |datafile|
40
+ YAML.load(datafile)
41
+ end
42
+
43
+ mysql_config = data.fetch(:dbconfig, {})
44
+ mysql_host = mysql_config.fetch(:host, nil) || Config[:mysql2][:host] || 'localhost'
45
+ mysql_user = mysql_config.fetch(:user, nil) || Config[:mysql2][:user]
46
+ mysql_pass = mysql_config.fetch(:pass, nil) || Config[:mysql2][:pass]
47
+ mysql_port = mysql_config.fetch(:port, nil) || Config[:mysql2][:port] || '3306'
48
+ mysql_database = mysql_config.fetch(:database, nil) || Config[:mysql2][:database]
49
+
50
+ connection_hash = {
51
+ :host => mysql_host,
52
+ :username => mysql_user,
53
+ :password => mysql_pass,
54
+ :database => mysql_database,
55
+ :port => mysql_port,
56
+ :reconnect => true}
57
+
58
+
59
+ Hiera.debug("data #{data.inspect}")
60
+ next if data.empty?
61
+ next unless data.include?(key)
62
+
63
+ Hiera.debug("Found #{key} in #{source}")
64
+
65
+ new_answer = Backend.parse_answer(data[key], scope)
66
+
67
+ results = query(connection_hash, new_answer)
68
+
69
+ next if results.length != 1
70
+ begin
71
+ new_answer = JSON.parse(results[0]['value'])
72
+ rescue
73
+ Hiera.debug("Miserable failure while looking for #{key}.")
74
+ next
75
+ end
76
+
77
+ case resolution_type.is_a?(Hash) ? :hash : resolution_type
78
+ when :array
79
+ raise Exception, "Hiera type mismatch for key '#{key}': expected Array and got #{new_answer.class}" unless new_answer.kind_of? Array or new_answer.kind_of? String
80
+ results ||= []
81
+ results << new_answer
82
+ when :hash
83
+ raise Exception, "Hiera type mismatch for key '#{key}': expected Hash and got #{new_answer.class}" unless new_answer.kind_of? Hash
84
+ results ||= {}
85
+ results = Backend.merge_answer(new_answer, results, resolution_type)
86
+ else
87
+ results = new_answer
88
+ break
89
+ end
90
+ end
91
+ return results
92
+ end
93
+
94
+
95
+ def query(connection_hash, query)
96
+ Hiera.debug("Executing SQL Query: #{query}")
97
+
98
+ data=[]
99
+ mysql_host=connection_hash[:host]
100
+ mysql_user=connection_hash[:username]
101
+ mysql_pass=connection_hash[:password]
102
+ mysql_database=connection_hash[:database]
103
+ mysql_port=connection_hash[:port]
104
+
105
+ if defined?(JRUBY_VERSION)
106
+ Jdbc::MySQL.load_driver
107
+ url = "jdbc:mysql://#{mysql_host}:#{mysql_port}/#{mysql_database}"
108
+ props = java.util.Properties.new
109
+ props.set_property :user, mysql_user
110
+ props.set_property :password, mysql_pass
111
+
112
+ conn = com.mysql.jdbc.Driver.new.connect(url,props)
113
+ stmt = conn.create_statement
114
+
115
+ res = stmt.execute_query(query)
116
+ md = res.getMetaData
117
+ numcols = md.getColumnCount
118
+
119
+ Hiera.debug("Mysql Query returned #{numcols} rows")
120
+
121
+ while ( res.next ) do
122
+ if numcols < 2
123
+ Hiera.debug("Mysql value : #{res.getString(1)}")
124
+ data << res.getString(1)
125
+ else
126
+ row = {}
127
+ (1..numcols).each do |c|
128
+ row[md.getColumnName(c)] = res.getString(c)
129
+ end
130
+ data << row
131
+ end
132
+ end
133
+
134
+ else
135
+ client = Mysql2::Client.new(connection_hash)
136
+ begin
137
+ data = client.query(query).to_a
138
+ Hiera.debug("Mysql Query returned #{data.size} rows")
139
+ rescue => e
140
+ Hiera.debug e.message
141
+ data = nil
142
+ ensure
143
+ client.close
144
+ end
145
+ end
146
+
147
+ return data
148
+
149
+ end
150
+ end
151
+ end
152
+ end
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hiera-mysql-json-backend-jruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Hostnet
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-07-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: jdbc-mysql
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: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Alternative MySQL backend with json support for hiera
42
+ email:
43
+ - opensource@hostnet.nl
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - CHANGELOG.md
50
+ - Gemfile
51
+ - Gemfile.lock
52
+ - LICENSE
53
+ - README.md
54
+ - Rakefile
55
+ - hiera-mysql-json-backend-puppetserver.gemspec
56
+ - hiera-mysql-json-backend.gemspec
57
+ - lib/hiera/backend/mysql_json_backend.rb
58
+ homepage: https://github.com/hostnet/hiera-mysql-json-backend
59
+ licenses:
60
+ - MIT
61
+ metadata: {}
62
+ post_install_message:
63
+ rdoc_options: []
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ requirements: []
77
+ rubyforge_project:
78
+ rubygems_version: 2.6.11
79
+ signing_key:
80
+ specification_version: 4
81
+ summary: Alternative MySQL backend with json support for hiera
82
+ test_files: []