souschef-config 0.0.1

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: 09866af0387ee6f6896f9a3d55409d22ccf6f6ab
4
+ data.tar.gz: 7ea973448b4280fcbd807cde35fe6a5944220165
5
+ SHA512:
6
+ metadata.gz: 240a7b4f75ec8959a1e0f996bed7504a88834ff52b0a4d313d8ae204687010b999bf04e275ac075f2287f5571347f544c02f8964188704f3ee0ec8b8d58734d6
7
+ data.tar.gz: b30bc0ca9ec187ccba0fcbec7f4248e9e8e56252cd1b0b13de0f4f4280e1f4b4c6e3038c656791e11632044b607627fa082d4341c89b435fbbca8f125f11372c
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 The Blacksmith (a.k.a Saulo Vallory)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, 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,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,57 @@
1
+ SousChef
2
+ ================================================================================
3
+
4
+ A set of helpers for chef
5
+
6
+ **NOTE:** This work is on it's early days and is still under development
7
+
8
+ What sort of helpers?
9
+ --------------------------------------------------------------------------------
10
+
11
+ ### For dynamic configuration
12
+
13
+ - SousChef::Config.compile_attribute
14
+ - SousChef::Config.compile_attribute_hash
15
+
16
+ ### For hashes
17
+
18
+ #### souschef/lib/souschef/hash_extensions.rb
19
+
20
+ > Extends Hash class adding `.access` which doesn't differentiate between
21
+ > `string` and `symbol` key and `deep_merge` from
22
+ > [this gem](https://rubygems.org/gems/deep_merge).
23
+
24
+
25
+ About the config `compile_attribute_hash`
26
+ --------------------------------------------------------------------------------
27
+
28
+ Parses a hash replacing {prop.subprop.subsub} tokens by it's values
29
+
30
+ #### Usage:
31
+
32
+ - Reference other properties using dot-notation {site.certs.root}
33
+ - Use a :__default__ key to define default values
34
+ - Use an :__items__ key to define the items to create using the default values
35
+ - :__key__ and :__item__ keys only exist inside __default__
36
+ - Overwrite defaults by adding a key with the same name of the item
37
+
38
+ #### Example:
39
+
40
+ default[:app] = {
41
+ name: 'myapp',
42
+ user: 'appuser',
43
+ certs: {
44
+ root: '/etc/ssl/certs/{name}',
45
+ cert: '{certs.root}/cert.pem'
46
+ },
47
+ envs: {
48
+ __default__: {
49
+ name: '{__key__}',
50
+ debug: true
51
+ },
52
+ __items__: ['dev', 'staging', 'prod'],
53
+ prod: {
54
+ debug: false
55
+ }
56
+ }
57
+ }
@@ -0,0 +1,123 @@
1
+ require_relative 'hash_extensions'
2
+
3
+ module SousChef
4
+ module Config
5
+ extend self
6
+
7
+ def compile_attribute(value, context, key_stack)
8
+ if value.is_a? Hash
9
+ if value.key? :__items__
10
+ value[:__root__] = context[:__root__]
11
+ context = value
12
+ end
13
+ return compile_attribute_hash(value, context, key_stack)
14
+ elsif value.is_a? String
15
+ return value.gsub(/\{([\w\.0-9]+)\}/) {|m|
16
+ res = context.access $1
17
+
18
+ if not res
19
+ puts "Couldn\'t read attribute #{$1} from.."
20
+ ap context
21
+ raise "Couldn\'t read attribute #{$1}"
22
+ end
23
+
24
+ if res[/\{([\w\.0-9]+)\}/]
25
+ next compile_attribute(res, context, key_stack)
26
+ end
27
+
28
+ res
29
+ }
30
+ end
31
+
32
+ value
33
+ end
34
+
35
+
36
+ # Attribute hash "compiler"
37
+ #
38
+ # Parses a hash replacing {prop.subprop.subsub} tokens by it's values
39
+ #
40
+ # Usage:
41
+ # - Reference other properties using dot-notation {site.certs.root}
42
+ # - Use a :__default__ key to define default values
43
+ # - Use an :__items__ key to define the items to create using the default values
44
+ # - :__key__ and :__item__ keys only exist inside __default__
45
+ # - Overwrite defaults by adding a key with the same name of the item
46
+ #
47
+ # Example:
48
+ # default[:app] = {
49
+ # name: 'myapp',
50
+ # user: 'appuser',
51
+ # certs: {
52
+ # root: '/etc/ssl/certs/{name}',
53
+ # cert: '{certs.root}/cert.pem'
54
+ # },
55
+ # envs: {
56
+ # __default__: {
57
+ # name: '{__key__}',
58
+ # debug: true
59
+ # },
60
+ # __items__: ['dev', 'staging', 'prod'],
61
+ # prod: {
62
+ # debug: false
63
+ # }
64
+ # }
65
+ # }
66
+ def compile_attribute_hash(hash, context=nil, key_stack=[])
67
+ context ||= hash
68
+ context[:__key__] = ''
69
+
70
+ if not context.key? :__root__
71
+ context[:__root__] = context
72
+ end
73
+
74
+ attr_hash = Hash.new
75
+
76
+ hash.each do |key, val|
77
+ if not key.to_s.start_with? '__'
78
+ key_stack.push key
79
+ begin
80
+ attr_hash[key] = compile_attribute(val, context, key_stack)
81
+ rescue Exception => e
82
+ puts "Error compiling value for #{key_stack.join('.')} in..."
83
+ ap context[:__item__]
84
+ raise e
85
+ end
86
+ key_stack.pop
87
+ end
88
+ end
89
+
90
+ if hash.key? :__items__
91
+
92
+ hash[:__items__].each do |item|
93
+ context[:__key__] = item
94
+
95
+ if hash.key? item
96
+ attr_hash[item] = hash[item]
97
+ else
98
+ attr_hash[item] = {}
99
+ end
100
+
101
+ if hash.key? :__default__
102
+ attr_hash[item] = hash[:__default__].deep_merge(attr_hash[item])
103
+ end
104
+
105
+ context[:__item__] = attr_hash[item]
106
+
107
+ attr_hash[item].each do |key, val|
108
+ key_stack.push key
109
+ attr_hash[item][key] = compile_attribute(val, context, key_stack)
110
+ key_stack.pop
111
+ end
112
+
113
+ end
114
+
115
+ end
116
+
117
+ attr_hash.delete :__key__
118
+ attr_hash.delete :__root__
119
+ attr_hash
120
+ end
121
+
122
+ end
123
+ end
@@ -0,0 +1,20 @@
1
+ require 'deep_merge'
2
+
3
+ class Hash
4
+
5
+ def access(path)
6
+ value = self
7
+
8
+ path.to_s.split('.').each do |p|
9
+ if p.to_i.to_s == p
10
+ value = value[p.to_i]
11
+ else
12
+ value = value[p].nil? ? value[p.to_sym] : value[p]
13
+ end
14
+ break if value.nil?
15
+ end
16
+
17
+ value
18
+ end
19
+
20
+ end
data/lib/souschef.rb ADDED
@@ -0,0 +1,5 @@
1
+ require_relative 'souschef/config'
2
+ require_relative 'souschef/hash_extensions'
3
+
4
+ module SousChef
5
+ end
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: souschef-config
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - The Blacksmith
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-04-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: deep_merge
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ description: Some utilities for configuring chef nodes
28
+ email: me@saulovallory.com
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - LICENSE
34
+ - README.md
35
+ - lib/souschef.rb
36
+ - lib/souschef/config.rb
37
+ - lib/souschef/hash_extensions.rb
38
+ homepage: http://gitlab.com/cashfarm/souschef
39
+ licenses:
40
+ - MIT
41
+ metadata: {}
42
+ post_install_message:
43
+ rdoc_options: []
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ requirements: []
57
+ rubyforge_project:
58
+ rubygems_version: 2.5.1
59
+ signing_key:
60
+ specification_version: 4
61
+ summary: SousChef config helper
62
+ test_files: []