hiera-mysql-backend-jruby 0.0.9

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: e00f982bb7dd4763356d8f89b29c82aeeb5d2fe1
4
+ data.tar.gz: 82c298c294e1802ce04b2c459b0d11a67436be84
5
+ SHA512:
6
+ metadata.gz: de60e3a9709a358fdd4fd3972060b7b60cb049f60aceedab06592f7268605e3c02945c7bb0fb5e86e6d616172dd3c26f9485b873608dc21fbf3bcb8caa58865d
7
+ data.tar.gz: fa105db2d5b72b9e85c1b5d0457e408626b235a92dbdadc481a30ebcd477fbad7371089c95749d89a575a5cc66e5419790ae6134113d2325306d4888da78db56
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-backend.png)](http://badge.fury.io/rb/hiera-mysql-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,2 @@
1
+ require "bundler"
2
+ 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-jruby"
5
+ gem.version = "0.0.9"
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
+ 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-backend"
5
+ gem.version = "0.0.9"
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
+ 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,129 @@
1
+ class Hiera
2
+ module Backend
3
+ class Mysql2_backend
4
+
5
+ def initialize(cache=nil)
6
+ if defined?(JRUBY_VERSION)
7
+ require 'jdbc/mysql'
8
+ require 'java'
9
+ else
10
+ begin
11
+ require 'mysql2'
12
+ rescue LoadError
13
+ require 'rubygems'
14
+ require 'mysql2'
15
+ end
16
+ end
17
+
18
+ @cache = cache || Filecache.new
19
+
20
+ Hiera.debug("Hiera MySQL2 initialized")
21
+ end
22
+
23
+ def lookup(key, scope, order_override, resolution_type)
24
+ # default answer is set to nil otherwise the lookup ends up returning
25
+ # an Array of nils and causing a Puppet::Parser::AST::Resource failed with error ArgumentError
26
+ # for any other lookup because their default value is overwriten by [nil,nil,nil,nil]
27
+ # so hiera('myvalue', 'test1') returns [nil,nil,nil,nil]
28
+ results = nil
29
+
30
+ Hiera.debug("looking up #{key} in MySQL2 Backend")
31
+ Hiera.debug("resolution type is #{resolution_type}")
32
+
33
+ Backend.datasources(scope, order_override) do |source|
34
+ Hiera.debug("Looking for data source #{source}")
35
+ sqlfile = Backend.datafile(:mysql2, scope, source, "sql") || next
36
+
37
+ next unless File.exist?(sqlfile)
38
+ data = @cache.read(sqlfile, Hash, {}) do |datafile|
39
+ YAML.load(datafile)
40
+ end
41
+
42
+ mysql_config = data.fetch(:dbconfig, {})
43
+ mysql_host = mysql_config.fetch(:host, nil) || Config[:mysql2][:host] || 'localhost'
44
+ mysql_user = mysql_config.fetch(:user, nil) || Config[:mysql2][:user]
45
+ mysql_pass = mysql_config.fetch(:pass, nil) || Config[:mysql2][:pass]
46
+ mysql_port = mysql_config.fetch(:port, nil) || Config[:mysql2][:port] || '3306'
47
+ mysql_database = mysql_config.fetch(:database, nil) || Config[:mysql2][:database]
48
+
49
+ connection_hash = {
50
+ :host => mysql_host,
51
+ :username => mysql_user,
52
+ :password => mysql_pass,
53
+ :database => mysql_database,
54
+ :port => mysql_port,
55
+ :reconnect => true}
56
+
57
+
58
+ Hiera.debug("data #{data.inspect}")
59
+ next if data.empty?
60
+ next unless data.include?(key)
61
+
62
+ Hiera.debug("Found #{key} in #{source}")
63
+
64
+ new_answer = Backend.parse_answer(data[key], scope)
65
+ results = query(connection_hash, new_answer)
66
+
67
+ end
68
+ return results
69
+ end
70
+
71
+
72
+ def query(connection_hash, query)
73
+ Hiera.debug("Executing SQL Query: #{query}")
74
+
75
+ data=[]
76
+ mysql_host=connection_hash[:host]
77
+ mysql_user=connection_hash[:username]
78
+ mysql_pass=connection_hash[:password]
79
+ mysql_database=connection_hash[:database]
80
+ mysql_port=connection_hash[:port]
81
+
82
+ if defined?(JRUBY_VERSION)
83
+ Jdbc::MySQL.load_driver
84
+ url = "jdbc:mysql://#{mysql_host}:#{mysql_port}/#{mysql_database}"
85
+ props = java.util.Properties.new
86
+ props.set_property :user, mysql_user
87
+ props.set_property :password, mysql_pass
88
+
89
+ conn = com.mysql.jdbc.Driver.new.connect(url,props)
90
+ stmt = conn.create_statement
91
+
92
+ res = stmt.execute_query(query)
93
+ md = res.getMetaData
94
+ numcols = md.getColumnCount
95
+
96
+ Hiera.debug("Mysql Query returned #{numcols} rows")
97
+
98
+ while ( res.next ) do
99
+ if numcols < 2
100
+ Hiera.debug("Mysql value : #{res.getString(1)}")
101
+ data << res.getString(1)
102
+ else
103
+ row = {}
104
+ (1..numcols).each do |c|
105
+ row[md.getColumnName(c)] = res.getString(c)
106
+ end
107
+ data << row
108
+ end
109
+ end
110
+
111
+ else
112
+ client = Mysql2::Client.new(connection_hash)
113
+ begin
114
+ data = client.query(query).to_a
115
+ Hiera.debug("Mysql Query returned #{data.size} rows")
116
+ rescue => e
117
+ Hiera.debug e.message
118
+ data = nil
119
+ ensure
120
+ client.close
121
+ end
122
+ end
123
+
124
+ return data
125
+
126
+ end
127
+ end
128
+ end
129
+ end
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hiera-mysql-backend-jruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.9
5
+ platform: ruby
6
+ authors:
7
+ - Telmo
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-04-10 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 for hiera
42
+ email:
43
+ - telmox@gmail.com
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-backend-puppetserver.gemspec
56
+ - hiera-mysql-backend.gemspec
57
+ - lib/hiera/backend/mysql2_backend.rb
58
+ homepage: https://github.com/Telmo/hiera-mysql-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.8
79
+ signing_key:
80
+ specification_version: 4
81
+ summary: Alternative MySQL backend for hiera
82
+ test_files: []