hiera-mysql 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/hiera/backend/mysql_backend.rb +74 -0
  2. metadata +87 -0
@@ -0,0 +1,74 @@
1
+ # Class Mysql_backend
2
+ # Description: MySQL back end to Hiera.
3
+ # Author: Craig Dunn <craig@craigdunn.org>
4
+ #
5
+ class Hiera
6
+ module Backend
7
+ class Mysql_backend
8
+ def initialize
9
+ require 'mysql'
10
+ Hiera.debug("mysql_backend initialized")
11
+ mysql_host = Config[:mysql][:host]
12
+ end
13
+ def lookup(key, scope, order_override, resolution_type)
14
+ Hiera.debug("loaded mysql backend")
15
+
16
+
17
+ # Parse the mysql query from the config, we also pass in key
18
+ # to extra_data so this can be interpreted into the query
19
+ # string
20
+ #
21
+ mysql_query = Backend.parse_string(Config[:mysql][:query], scope, { "key" => key })
22
+
23
+
24
+ answer = Backend.empty_answer(resolution_type)
25
+ Hiera.debug("resolution type is #{resolution_type}")
26
+
27
+ results = query(mysql_query)
28
+ unless results.empty?
29
+ case resolution_type
30
+ when :array
31
+ results.each do |ritem|
32
+ answer << Backend.parse_answer(ritem, scope)
33
+ end
34
+ else
35
+ answer = Backend.parse_answer(results[0], scope)
36
+ end
37
+ end
38
+
39
+ return answer
40
+
41
+ end
42
+
43
+
44
+
45
+ def query (sql)
46
+ Hiera.debug("Executing SQL Query: #{sql}")
47
+
48
+ data=[]
49
+ mysql_host=Config[:mysql][:host]
50
+ mysql_user=Config[:mysql][:user]
51
+ mysql_pass=Config[:mysql][:pass]
52
+ mysql_database=Config[:mysql][:database]
53
+
54
+ dbh = Mysql.new(mysql_host, mysql_user, mysql_pass, mysql_database)
55
+ res = dbh.query(sql)
56
+ Hiera.debug("Mysql Query returned #{res.num_rows} rows")
57
+
58
+
59
+ # Currently we'll just return the first element of each row, a future
60
+ # enhancement would be to make this easily support arrays so you can do
61
+ # select foo,bar from table
62
+ res.each do |row|
63
+ Hiera.debug("Mysql value : #{row[0]}")
64
+ data << row[0]
65
+ end
66
+
67
+ return data
68
+
69
+ end
70
+ end
71
+ end
72
+ end
73
+
74
+
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hiera-mysql
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Craig Dunn
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2012-03-03 00:00:00 +00:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: hiera
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
29
+ - 2
30
+ - 0
31
+ version: 0.2.0
32
+ type: :runtime
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: mysql
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ segments:
42
+ - 0
43
+ version: "0"
44
+ type: :runtime
45
+ version_requirements: *id002
46
+ description: Hiera back end for retrieving configuration values from MySQL
47
+ email: craig@craigdunn.org
48
+ executables: []
49
+
50
+ extensions: []
51
+
52
+ extra_rdoc_files: []
53
+
54
+ files:
55
+ - lib/hiera/backend/mysql_backend.rb
56
+ has_rdoc: true
57
+ homepage: http://github.com/crayfishx/hiera-mysql
58
+ licenses: []
59
+
60
+ post_install_message:
61
+ rdoc_options: []
62
+
63
+ require_paths:
64
+ - lib
65
+ required_ruby_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ segments:
70
+ - 0
71
+ version: "0"
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ segments:
77
+ - 0
78
+ version: "0"
79
+ requirements: []
80
+
81
+ rubyforge_project:
82
+ rubygems_version: 1.3.6
83
+ signing_key:
84
+ specification_version: 3
85
+ summary: MySQL backend for Hiera
86
+ test_files: []
87
+