hiera-mysql-backend 0.0.7-java

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: dd0a2c34bc1f1ba7a39c1dd7f1331fb64d5038a7
4
+ data.tar.gz: bf6ea8a7e6879f7eb3c56cea33846bd09ae064fa
5
+ SHA512:
6
+ metadata.gz: 31a505fc2f20600316f52fd3b2bf6e7d841328b1bdee1d5bfdca042e8e9548d08f2cbeac00c7732d1fc5d329fcac50165f36c8677a4bf84a6a783d7fcae0ed9f
7
+ data.tar.gz: c0307852140fd0cde17b2b9dfcd940428c55a566e9935b60f3ed8aa3c2fce2006e9d4f3fc0674af82e1e001be656d3ae46ede42153cfa4089cb41399a77b395d
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 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |gem|
4
+ gem.name = "hiera-mysql-backend"
5
+ gem.version = "0.0.7"
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
+ gem.platform = "java"
13
+
14
+ gem.files = `git ls-files`.split($/)
15
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
16
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
17
+ gem.require_paths = ["lib"]
18
+ gem.add_dependency('jdbc-mysql', '~> 0')
19
+ gem.add_dependency('hiera', '~> 0')
20
+ gem.add_development_dependency('rake', '~> 0')
21
+ 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.7"
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,91 @@
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
+ else
9
+ begin
10
+ require 'mysql2'
11
+ rescue LoadError
12
+ require 'rubygems'
13
+ require 'mysql2'
14
+ end
15
+ end
16
+
17
+ @cache = cache || Filecache.new
18
+
19
+ Hiera.debug("Hiera MySQL2 initialized")
20
+ end
21
+
22
+ def lookup(key, scope, order_override, resolution_type)
23
+ # default answer is set to nil otherwise the lookup ends up returning
24
+ # an Array of nils and causing a Puppet::Parser::AST::Resource failed with error ArgumentError
25
+ # for any other lookup because their default value is overwriten by [nil,nil,nil,nil]
26
+ # so hiera('myvalue', 'test1') returns [nil,nil,nil,nil]
27
+ results = nil
28
+
29
+ Hiera.debug("looking up #{key} in MySQL2 Backend")
30
+ Hiera.debug("resolution type is #{resolution_type}")
31
+
32
+ Backend.datasources(scope, order_override) do |source|
33
+ Hiera.debug("Looking for data source #{source}")
34
+ sqlfile = Backend.datafile(:mysql2, scope, source, "sql") || next
35
+
36
+ next unless File.exist?(sqlfile)
37
+ data = @cache.read(sqlfile, Hash, {}) do |datafile|
38
+ YAML.load(datafile)
39
+ end
40
+
41
+ mysql_config = data.fetch(:dbconfig, {})
42
+ mysql_host = mysql_config.fetch(:host, nil) || Config[:mysql2][:host] || 'localhost'
43
+ mysql_user = mysql_config.fetch(:user, nil) || Config[:mysql2][:user]
44
+ mysql_pass = mysql_config.fetch(:pass, nil) || Config[:mysql2][:pass]
45
+ mysql_port = mysql_config.fetch(:port, nil) || Config[:mysql2][:port] || '3306'
46
+ mysql_database = mysql_config.fetch(:database, nil) || Config[:mysql2][:database]
47
+
48
+ connection_hash = {
49
+ :host => mysql_host,
50
+ :username => mysql_user,
51
+ :password => mysql_pass,
52
+ :database => mysql_database,
53
+ :port => mysql_port,
54
+ :reconnect => true}
55
+
56
+
57
+ Hiera.debug("data #{data.inspect}")
58
+ next if data.empty?
59
+ next unless data.include?(key)
60
+
61
+ Hiera.debug("Found #{key} in #{source}")
62
+
63
+ new_answer = Backend.parse_answer(data[key], scope)
64
+ results = query(connection_hash, new_answer)
65
+
66
+ end
67
+ return results
68
+ end
69
+
70
+
71
+ def query(connection_hash, query)
72
+ Hiera.debug("Executing SQL Query: #{query}")
73
+
74
+ data=nil
75
+ client = Mysql2::Client.new(connection_hash)
76
+ begin
77
+ data = client.query(query).to_a
78
+ Hiera.debug("Mysql Query returned #{data.size} rows")
79
+ rescue => e
80
+ Hiera.debug e.message
81
+ data = nil
82
+ ensure
83
+ client.close
84
+ end
85
+
86
+ return data
87
+
88
+ end
89
+ end
90
+ end
91
+ end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hiera-mysql-backend
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.7
5
+ platform: java
6
+ authors:
7
+ - Telmo
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-03-25 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: 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 MySQL backend for hiera
56
+ email:
57
+ - telmox@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - CHANGELOG.md
64
+ - Gemfile
65
+ - Gemfile.lock
66
+ - LICENSE
67
+ - README.md
68
+ - Rakefile
69
+ - hiera-mysql-backend-java.gemspec
70
+ - hiera-mysql-backend.gemspec
71
+ - lib/hiera/backend/mysql2_backend.rb
72
+ homepage: https://github.com/Telmo/hiera-mysql-backend
73
+ licenses:
74
+ - MIT
75
+ metadata: {}
76
+ post_install_message:
77
+ rdoc_options: []
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ requirements: []
91
+ rubyforge_project:
92
+ rubygems_version: 2.5.1
93
+ signing_key:
94
+ specification_version: 4
95
+ summary: Alternative MySQL backend for hiera
96
+ test_files: []