hiera-mysql 0.2.0 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/hiera/backend/mysql_backend.rb +113 -74
- metadata +9 -13
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: cbe2732edac5efdfc085c21fbe85932b9577a4cd
|
4
|
+
data.tar.gz: 7ae9728410ab5217383176aea9aefa5dfa86f5a0
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 361de3077a3572eec1cd01d582c71597ad24bee63d93971f64d4c335f555c28c21982352c85c7e022738eac15a170cb252bc268ec329dcf80a4f4e4693d7e311
|
7
|
+
data.tar.gz: 8d2ed4e05b3bfc9b9eb12a9f527cdf3c73ff002afaf04d2f69b43e3e1d1f9e82df31e551f1cec57d1478d0c5f6869eb5312b47088d241ba0f35c6b2eb86e7bb6
|
@@ -3,89 +3,128 @@
|
|
3
3
|
# Author: Craig Dunn <craig@craigdunn.org>
|
4
4
|
#
|
5
5
|
class Hiera
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
# Parse the mysql query from the config, we also pass in key
|
26
|
-
# to extra_data so this can be interpreted into the query
|
27
|
-
# string
|
28
|
-
#
|
29
|
-
queries = [ Config[:mysql][:query] ].flatten
|
30
|
-
queries.map! { |q| Backend.parse_string(q, scope, {"key" => key}) }
|
31
|
-
|
32
|
-
queries.each do |mysql_query|
|
33
|
-
|
34
|
-
results = query(mysql_query)
|
35
|
-
|
36
|
-
unless results.empty?
|
37
|
-
case resolution_type
|
38
|
-
when :array
|
39
|
-
answer ||= []
|
40
|
-
results.each do |ritem|
|
41
|
-
answer << Backend.parse_answer(ritem, scope)
|
42
|
-
end
|
43
|
-
else
|
44
|
-
answer = Backend.parse_answer(results[0], scope)
|
45
|
-
break
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
|
-
end
|
50
|
-
answer
|
51
|
-
end
|
6
|
+
module Backend
|
7
|
+
class Mysql_backend
|
8
|
+
def initialize
|
9
|
+
@use_jdbc = defined?(JRUBY_VERSION) ? true : false
|
10
|
+
if @use_jdbc
|
11
|
+
require 'jdbc/mysql'
|
12
|
+
require 'java'
|
13
|
+
else
|
14
|
+
begin
|
15
|
+
require 'mysql'
|
16
|
+
rescue LoadError
|
17
|
+
require 'rubygems'
|
18
|
+
require 'mysql'
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
Hiera.debug("mysql_backend initialized")
|
23
|
+
Hiera.debug("JDBC mode #{@use_jdbc}")
|
24
|
+
end
|
52
25
|
|
53
|
-
def query (sql)
|
54
|
-
Hiera.debug("Executing SQL Query: #{sql}")
|
55
26
|
|
56
|
-
|
57
|
-
mysql_host=Config[:mysql][:host]
|
58
|
-
mysql_user=Config[:mysql][:user]
|
59
|
-
mysql_pass=Config[:mysql][:pass]
|
60
|
-
mysql_database=Config[:mysql][:database]
|
27
|
+
def lookup(key, scope, order_override, resolution_type)
|
61
28
|
|
62
|
-
|
63
|
-
|
29
|
+
Hiera.debug("mysql_backend invoked lookup")
|
30
|
+
Hiera.debug("resolution type is #{resolution_type}")
|
64
31
|
|
65
|
-
|
66
|
-
Hiera.debug("Mysql Query returned #{res.num_rows} rows")
|
32
|
+
answer = nil
|
67
33
|
|
34
|
+
# Parse the mysql query from the config, we also pass in key
|
35
|
+
# to extra_data so this can be interpreted into the query
|
36
|
+
# string
|
37
|
+
#
|
38
|
+
queries = [ Config[:mysql][:query] ].flatten
|
39
|
+
queries.map! { |q| Backend.parse_string(q, scope, {"key" => key}) }
|
68
40
|
|
69
|
-
|
70
|
-
# enhancement would be to make this easily support hashes so you can do
|
71
|
-
# select foo,bar from table
|
72
|
-
#
|
73
|
-
if res.num_fields < 2
|
74
|
-
res.each do |row|
|
75
|
-
Hiera.debug("Mysql value : #{row[0]}")
|
76
|
-
data << row[0]
|
77
|
-
end
|
41
|
+
queries.each do |mysql_query|
|
78
42
|
|
79
|
-
|
80
|
-
res.each_hash do |row|
|
81
|
-
data << row
|
82
|
-
end
|
83
|
-
end
|
43
|
+
results = query(mysql_query)
|
84
44
|
|
85
|
-
|
45
|
+
unless results.empty?
|
46
|
+
case resolution_type
|
47
|
+
when :array
|
48
|
+
answer ||= []
|
49
|
+
results.each do |ritem|
|
50
|
+
answer << Backend.parse_answer(ritem, scope)
|
51
|
+
end
|
52
|
+
else
|
53
|
+
answer = Backend.parse_answer(results[0], scope)
|
54
|
+
break
|
86
55
|
end
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
59
|
+
answer
|
60
|
+
end
|
61
|
+
|
62
|
+
def query (sql)
|
63
|
+
Hiera.debug("Executing SQL Query: #{sql}")
|
64
|
+
|
65
|
+
data=[]
|
66
|
+
mysql_host=Config[:mysql][:host]
|
67
|
+
mysql_user=Config[:mysql][:user]
|
68
|
+
mysql_pass=Config[:mysql][:pass]
|
69
|
+
mysql_database=Config[:mysql][:database]
|
70
|
+
|
71
|
+
|
72
|
+
if @use_jdbc
|
73
|
+
#
|
74
|
+
# JDBC connection handling, this will be run under jRuby
|
75
|
+
#
|
76
|
+
Jdbc::MySQL.load_driver
|
77
|
+
url = "jdbc:mysql://#{mysql_host}:3306/#{mysql_database}"
|
78
|
+
props = java.util.Properties.new
|
79
|
+
props.set_property :user, mysql_user
|
80
|
+
props.set_property :password, mysql_pass
|
81
|
+
|
82
|
+
conn = com.mysql.jdbc.Driver.new.connect(url,props)
|
83
|
+
stmt = conn.create_statement
|
84
|
+
|
85
|
+
res = stmt.execute_query(sql)
|
86
|
+
md = res.getMetaData
|
87
|
+
numcols = md.getColumnCount
|
88
|
+
|
89
|
+
Hiera.debug("Mysql Query returned #{numcols} rows")
|
90
|
+
|
91
|
+
while ( res.next ) do
|
92
|
+
if numcols < 2
|
93
|
+
Hiera.debug("Mysql value : #{res.getString(1)}")
|
94
|
+
data << res.getString(1)
|
95
|
+
else
|
96
|
+
row = {}
|
97
|
+
(1..numcols).each do |c|
|
98
|
+
row[md.getColumnName(c)] = res.getString(c)
|
99
|
+
end
|
100
|
+
data << row
|
101
|
+
end
|
102
|
+
end
|
103
|
+
else
|
104
|
+
#
|
105
|
+
# Native mysql connection, for calls outside of jRuby
|
106
|
+
#
|
107
|
+
dbh = Mysql.new(mysql_host, mysql_user, mysql_pass, mysql_database)
|
108
|
+
dbh.reconnect = true
|
109
|
+
|
110
|
+
res = dbh.query(sql)
|
111
|
+
Hiera.debug("Mysql Query returned #{res.num_rows} rows")
|
112
|
+
|
113
|
+
if res.num_fields < 2
|
114
|
+
res.each do |row|
|
115
|
+
Hiera.debug("Mysql value : #{row[0]}")
|
116
|
+
data << row[0]
|
117
|
+
end
|
118
|
+
else
|
119
|
+
res.each_hash do |row|
|
120
|
+
data << row
|
121
|
+
end
|
122
|
+
end
|
87
123
|
end
|
88
|
-
end
|
89
|
-
end
|
90
124
|
|
125
|
+
return data
|
91
126
|
|
127
|
+
end
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
metadata
CHANGED
@@ -1,30 +1,27 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hiera-mysql
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
5
|
-
prerelease:
|
4
|
+
version: 1.0.0
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Craig Dunn
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 2015-05-07 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: mysql
|
16
15
|
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
16
|
requirements:
|
19
|
-
- -
|
17
|
+
- - '>='
|
20
18
|
- !ruby/object:Gem::Version
|
21
19
|
version: '0'
|
22
20
|
type: :runtime
|
23
21
|
prerelease: false
|
24
22
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
23
|
requirements:
|
27
|
-
- -
|
24
|
+
- - '>='
|
28
25
|
- !ruby/object:Gem::Version
|
29
26
|
version: '0'
|
30
27
|
description: Hiera back end for retrieving configuration values from MySQL
|
@@ -36,26 +33,25 @@ files:
|
|
36
33
|
- lib/hiera/backend/mysql_backend.rb
|
37
34
|
homepage: http://github.com/crayfishx/hiera-mysql
|
38
35
|
licenses: []
|
36
|
+
metadata: {}
|
39
37
|
post_install_message:
|
40
38
|
rdoc_options: []
|
41
39
|
require_paths:
|
42
40
|
- lib
|
43
41
|
required_ruby_version: !ruby/object:Gem::Requirement
|
44
|
-
none: false
|
45
42
|
requirements:
|
46
|
-
- -
|
43
|
+
- - '>='
|
47
44
|
- !ruby/object:Gem::Version
|
48
45
|
version: '0'
|
49
46
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
50
|
-
none: false
|
51
47
|
requirements:
|
52
|
-
- -
|
48
|
+
- - '>='
|
53
49
|
- !ruby/object:Gem::Version
|
54
50
|
version: '0'
|
55
51
|
requirements: []
|
56
52
|
rubyforge_project:
|
57
|
-
rubygems_version:
|
53
|
+
rubygems_version: 2.2.2
|
58
54
|
signing_key:
|
59
|
-
specification_version:
|
55
|
+
specification_version: 4
|
60
56
|
summary: MySQL backend for Hiera
|
61
57
|
test_files: []
|