hiera-mysql-json-backend 1.1.0 → 1.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 484c8f90a35683ae4e4edfb5f8463b09200a7bcb
4
- data.tar.gz: a0a8eeb6c9d7588cf911cc0d0f2b1254ffc6820f
3
+ metadata.gz: 53185036554e275062f16f0fab0556b80b2b1551
4
+ data.tar.gz: 8dafb494f356fdae2f623204778958987f092769
5
5
  SHA512:
6
- metadata.gz: 21908ff7b64277542e964a9ff8a039616ba47f105cab9ae6821c7ce17de8aacef356a767de463880a6138b94e88b6a9b0d4937f5843afa2cda1acf3c07a712cd
7
- data.tar.gz: 154cd16ab985dbd5562a49a46077b1bba0814ca5b41acbc07d172e248a582c8878c8380195aab87c805adad53a73e2c4366de8a678f7b0ae35c1e26a0a463937
6
+ metadata.gz: 0e0e476c2d32b10c2240d7fab945bc277ef67a6c2b99b8191c85b6b2fdb9f2f68a58ae0fb2bf8f8e9118f1a787fcdc9e9a396ef9f568be4c6b1f1036fb042d48
7
+ data.tar.gz: 4fa32e35527dec742eabbf565487d59b2eb4eab2ea170db8550e7acf749822ed15f342e301ba58c73af810d8b52e2e4391392bdfb69c80562f4d9ec848b094eb
data/README.md CHANGED
@@ -1,90 +1,151 @@
1
1
  [![Gem Version](https://badge.fury.io/rb/hiera-mysql-json-backend.png)](http://badge.fury.io/rb/hiera-mysql-json-backend)
2
2
 
3
- ## hiera-mysql-backend
3
+ ## hiera-mysql-json-backend
4
4
 
5
- Alternate MySQL backend for Hiera
5
+ Alternate MySQL backend for Hiera with json support
6
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.
7
+ This is a backend for Hiera based on
8
+ [hiera-mysql-backend](https://github.com/Telmo/hiera-mysql-backend).
8
9
 
9
- ### What is different from hiera-mysql
10
+ ### What makes this backend different from the other mysql backends
10
11
 
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
+ This backend differs from
13
+ [hiera-mysql](https://github.com/crayfishx/hiera-mysql) and
14
+ [hiera-mysql-backend](https://github.com/Telmo/hiera-mysql-backend) in that it
15
+ expects queries to return json. This json is then parsed and the resulting data
16
+ structure is given back to the lookup.
12
17
 
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.
18
+ ### Usage
14
19
 
15
- [hiera-mysql](https://github.com/crayfishx/hiera-mysql) used the mysql gem, I am partial to [mysql2](https://github.com/brianmario/mysql2)
20
+ Puppet client:
16
21
 
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.
22
+ `/opt/puppetlabs/puppet/bin/gem install hiera-mysql-json-backend mysql2`
18
23
 
19
- ### What goes into the sql files.
24
+ Puppet server:
20
25
 
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)
26
+ `/opt/puppetlabs/bin/puppetserver gem install hiera-mysql-json-backend-jruby jdbc-mysql`
22
27
 
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.
28
+ ### Configuring hiera
24
29
 
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:
30
+ The backend is configured like any other in the hiera.conf. Here is the
31
+ simplest possible example:
26
32
 
27
33
  ```yaml
28
34
  ---
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
35
+ :backends:
36
+ - yaml
37
+ - mysql_json
38
+
39
+ :yaml:
40
+ :datadir: /etc/puppet/hieradata
41
+
42
+ :mysql_json:
43
+ :datadir: /etc/puppet/hieradata
36
44
 
37
- applications: SELECT * FROM applications WHERE host='%{fqdn}';
38
- coats: SELECT cut,name,type FROM coats WHERE color='brown';
45
+ :hierarchy:
46
+ - "%{::clientcert}"
47
+ - "%{::custom_location}"
48
+ - common
49
+
50
+ :logger: console
39
51
  ```
40
52
 
41
- If `host` is not defined it will use `localhost` as default.
53
+ This will cause it to try to connect on localhost:3306 with no username and
54
+ password. This will probably not work. The following options can be set:
42
55
 
43
- If `port` is not defined it will use the default `3306` mysql port
56
+ * `host`: mysql host (string)
57
+ * `port`: mysql port (int)
58
+ * `user`: mysql user (string)
59
+ * `pass`: mysql password (string)
60
+ * `database`: mysql database name (string)
61
+ * `datadir`: root of your `mysql_json` hierarchy (string)
62
+ * `only_for`: only perform queries if conditions in this section are met (hash)
63
+ * `ignore_json_parse_errors`: Do not raise an exception when the database
64
+ contains invalid json (boolean, defaults to false)
44
65
 
45
- running `hiera applications` would run the query against the configured database.
66
+ A more complete example might contain:
46
67
 
68
+ ```yaml
69
+ :mysql_json:
70
+ :datadir: /etc/puppet/hierasql
71
+ :host: db042.example.com
72
+ :user: puppetserver
73
+ :pass: secret123
74
+ :port: 3306
75
+ :ignore_json_parse_errors: true # why why why...
76
+ :only_for:
77
+ :fqdn:
78
+ - '^vagrant.+'
79
+ - '^node\d+\.someservice\.example\.com$'
80
+ :domain:
81
+ - '^vagrant.+
47
82
 
48
- ### Using
83
+ ```
49
84
 
50
- `gem install hiera-mysql-backend`
85
+ This will perform lookups if any of the given conditions are met:
51
86
 
87
+ * The nodes fqdn fact starts with 'vagrant'
88
+ * The nodes fqdn fact looks like node01.someservice.example.com
89
+ * The nodes domain fact starts with 'vagrant'
52
90
 
53
- ### Configuring Hiera
91
+ Any node fact may be used to build these conditions.
54
92
 
55
- Hiera configuration is pretty simple
93
+ Note that putting something in the list that always matches makes the whole
94
+ `only_for` block useless.
56
95
 
57
- ```yaml
58
- ---
59
- :backends:
60
- - yaml
61
- - mysql2
96
+ ### Defining queries
62
97
 
63
- :yaml:
64
- :datadir: /etc/puppet/hieradata
98
+ Queries are defined in he poorly named sql files. These are really yaml files
99
+ where the key is the lookup key and the value is the SQL statement (it accepts
100
+ interpolation)
65
101
 
66
- :mysql2:
67
- :datadir: /etc/puppet/hieradata
68
- :host: hostname
69
- :user: username
70
- :pass: password
71
- :database: database
72
- :port: 3306
102
+ As of version 0.0.4 you can also add connection information to these sql files,
103
+ this allows you to connect to different databases. This is optional if no
104
+ connection information is found it will use the default defined in your
105
+ hiera.yaml config.
73
106
 
74
- :hierarchy:
75
- - "%{::clientcert}"
76
- - "%{::custom_location}"
77
- - common
107
+ Lets assume your _datadir_ is `/etc/puppet/hieradata/` and your hierarchy for
108
+ hiera just have a common. hiera-mysql-backend would look for
109
+ /etc/puppet/hieradata/common.sql the common.sql would look like:
78
110
 
79
- :logger: console
111
+ ```yaml
112
+ ---
113
+ # This is optional, if not present it will use the default connection info from hiera.yaml
114
+ :dbconfig:
115
+ :host: database.example.com
116
+ :user: hieratest
117
+ :pass: sekret
118
+ :database: testhieradb
119
+ :port: 44445
120
+
121
+ applications: SELECT value FROM applications WHERE host='%{fqdn}';
80
122
  ```
81
123
 
82
124
  If `host` is not defined it will use `localhost` as default.
83
125
 
84
126
  If `port` is not defined it will use the default `3306` mysql port
85
127
 
128
+ Running `hiera applications` would run the query against the configured
129
+ database, parse the result as json and return the resulting data structure. If
130
+ all you want is a string, strings are valid json, too.
131
+
132
+
133
+ ### Error handling
134
+
135
+ When encountering invalid json, it will raise an exception, which would in turn
136
+ cause catalog compilation to fail. You can disable this behaviour by setting
137
+ `ignore_json_parse_errors`.
138
+
139
+ When no results are returned by your query, it will return nil.
140
+
141
+ When multiple results are returned by your query, it will return nil. A future
142
+ version will introduce `ignore_multiple_results`, defaulting to true, to make
143
+ it possible to trigger a catalog compilation in this case.
144
+
145
+
86
146
  ## Known issues
87
147
 
148
+ 1. Multiple results are currently silently ignored.
88
149
  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
150
  2. This README is poorly written.
90
151
 
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |gem|
4
4
  gem.name = "hiera-mysql-json-backend-jruby"
5
- gem.version = "1.1.0"
5
+ gem.version = "1.1.1"
6
6
  gem.authors = ["Hostnet"]
7
7
  gem.email = ["opensource@hostnet.nl"]
8
8
  gem.description = %q{Alternative MySQL backend with json support for hiera}
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |gem|
4
4
  gem.name = "hiera-mysql-json-backend"
5
- gem.version = "1.1.0"
5
+ gem.version = "1.1.1"
6
6
  gem.authors = ["Hostnet"]
7
7
  gem.email = ["opensource@hostnet.nl"]
8
8
  gem.description = %q{Alternative MySQL backend with json support for hiera}
@@ -25,7 +25,7 @@ class Hiera
25
25
  return true unless constraints.is_a?(Hash)
26
26
  should_lookup = false
27
27
  constraints.each do |item, matchers|
28
- next unless scope.exist?(item.to_s)
28
+ next unless scope[item.to_s]
29
29
  if scope[item.to_s] =~ compile_regexes(matchers)
30
30
  should_lookup = true
31
31
  break
@@ -85,7 +85,7 @@ class Hiera
85
85
  new_answer = Backend.parse_answer(data[key], scope)
86
86
 
87
87
  sql_results = query(connection_hash, new_answer)
88
-
88
+ # TODO: make sure we fail if we have more than 1 result, skip if less than 1.
89
89
  next if sql_results.length != 1
90
90
  begin
91
91
  new_answer = JSON.parse(sql_results[0]['value'])
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hiera-mysql-json-backend
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hostnet
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-07-18 00:00:00.000000000 Z
11
+ date: 2017-08-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mysql2