hieracles 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (54) hide show
  1. checksums.yaml +7 -0
  2. data/.coveralls.yml +2 -0
  3. data/.gitignore +15 -0
  4. data/.rspec +2 -0
  5. data/.ruby-version +1 -0
  6. data/.travis.yml +5 -0
  7. data/CHANGELOG.md +8 -0
  8. data/Gemfile +4 -0
  9. data/LICENSE +22 -0
  10. data/README.md +30 -0
  11. data/Rakefile +14 -0
  12. data/bin/hc +32 -0
  13. data/bin/nodeinfo +41 -0
  14. data/hieracles.gemspec +30 -0
  15. data/lib/hieracles.rb +15 -0
  16. data/lib/hieracles/config.rb +56 -0
  17. data/lib/hieracles/format.rb +58 -0
  18. data/lib/hieracles/formats/console.rb +84 -0
  19. data/lib/hieracles/formats/csv.rb +66 -0
  20. data/lib/hieracles/formats/plain.rb +61 -0
  21. data/lib/hieracles/formats/yaml.rb +36 -0
  22. data/lib/hieracles/help.rb +27 -0
  23. data/lib/hieracles/hiera.rb +27 -0
  24. data/lib/hieracles/node.rb +108 -0
  25. data/lib/hieracles/optparse.rb +63 -0
  26. data/lib/hieracles/utils.rb +59 -0
  27. data/spec/files/config.yml +5 -0
  28. data/spec/files/enc/server.example.com.yaml +7 -0
  29. data/spec/files/enc/server2.example.com.yaml +7 -0
  30. data/spec/files/enc/server3.example.com.yaml +7 -0
  31. data/spec/files/farm_modules/dev.pp +5 -0
  32. data/spec/files/farm_modules/dev2.pp +5 -0
  33. data/spec/files/hiera.yaml +16 -0
  34. data/spec/files/hiera_no_yamlbackend.yaml +16 -0
  35. data/spec/files/hiera_yamlbackend_notfound.yaml +16 -0
  36. data/spec/files/modules/fake_module/manifests/init.pp +3 -0
  37. data/spec/files/modules/fake_module2/manifests/init.pp +3 -0
  38. data/spec/files/modules/fake_module3/manifests/init.pp +3 -0
  39. data/spec/files/params/common/common.yml +2 -0
  40. data/spec/files/params/farm/dev.yaml +1 -0
  41. data/spec/files/params/nodes/server.example.com.yaml +5 -0
  42. data/spec/lib/config_spec.rb +63 -0
  43. data/spec/lib/format_spec.rb +91 -0
  44. data/spec/lib/formats/console_spec.rb +107 -0
  45. data/spec/lib/formats/csv_spec.rb +89 -0
  46. data/spec/lib/formats/plain_spec.rb +94 -0
  47. data/spec/lib/formats/yaml_spec.rb +80 -0
  48. data/spec/lib/help_spec.rb +8 -0
  49. data/spec/lib/hiera_spec.rb +111 -0
  50. data/spec/lib/node_spec.rb +158 -0
  51. data/spec/lib/optparse_spec.rb +65 -0
  52. data/spec/lib/utils_spec.rb +61 -0
  53. data/spec/spec_helper.rb +26 -0
  54. metadata +223 -0
@@ -0,0 +1,5 @@
1
+ ---
2
+ classpath: farm_modules/%s.pp
3
+ modulepath: modules
4
+ hierafile: hiera.yaml
5
+ paramspath: params
@@ -0,0 +1,7 @@
1
+ ---
2
+ classes:
3
+ - test_class
4
+ parameters:
5
+ country: fr
6
+ datacenter: equinix
7
+ farm: dev
@@ -0,0 +1,7 @@
1
+ ---
2
+ classes:
3
+ - test_class
4
+ parameters:
5
+ country: fr
6
+ datacenter: equinix
7
+ farm: dev2
@@ -0,0 +1,7 @@
1
+ ---
2
+ classes:
3
+ - test_class
4
+ parameters:
5
+ country: fr
6
+ datacenter: equinix
7
+ farm: dev3
@@ -0,0 +1,5 @@
1
+ class test_class {
2
+ include fake_module
3
+ include fake_module2
4
+ include fake_module3
5
+ }
@@ -0,0 +1,5 @@
1
+ class test_class {
2
+ include fake_module
3
+ include fake_module2
4
+ include fake_module4
5
+ }
@@ -0,0 +1,16 @@
1
+ ---
2
+ :backends:
3
+ - yaml
4
+ :hierarchy:
5
+ - "nodes/%{fqdn}"
6
+ - "farm_datacenter/%{farm}_%{datacenter}"
7
+ - "farm/%{farm}"
8
+ - "room/%{room}"
9
+ - "datacenter/%{datacenter}"
10
+ - "country/%{country}"
11
+ - "os/%{operatingsystem}-%{lsbdistcodename}"
12
+ - "common/common"
13
+ :yaml:
14
+ :datadir: "params/"
15
+ :puppet:
16
+ :datasource: data
@@ -0,0 +1,16 @@
1
+ ---
2
+ :backends:
3
+ - yaml
4
+ :hierarchy:
5
+ - "nodes/%{fqdn}"
6
+ - "farm_datacenter/%{farm}_%{datacenter}"
7
+ - "farm/%{farm}"
8
+ - "room/%{room}"
9
+ - "datacenter/%{datacenter}"
10
+ - "country/%{country}"
11
+ - "os/%{operatingsystem}-%{lsbdistcodename}"
12
+ - "common/common"
13
+ :json:
14
+ :datadir: "params/"
15
+ :puppet:
16
+ :datasource: data
@@ -0,0 +1,16 @@
1
+ ---
2
+ :backends:
3
+ - yaml
4
+ :hierarchy:
5
+ - "nodes/%{fqdn}"
6
+ - "farm_datacenter/%{farm}_%{datacenter}"
7
+ - "farm/%{farm}"
8
+ - "room/%{room}"
9
+ - "datacenter/%{datacenter}"
10
+ - "country/%{country}"
11
+ - "os/%{operatingsystem}-%{lsbdistcodename}"
12
+ - "common/common"
13
+ :yaml:
14
+ :datadir: "params_no/"
15
+ :puppet:
16
+ :datasource: data
@@ -0,0 +1,3 @@
1
+ class fake_module {
2
+
3
+ }
@@ -0,0 +1,3 @@
1
+ class fake_module2 {
2
+
3
+ }
@@ -0,0 +1,3 @@
1
+ class fake_module3 {
2
+
3
+ }
@@ -0,0 +1,2 @@
1
+ common_param:
2
+ subparam: thing
@@ -0,0 +1 @@
1
+ somefarmparam: false
@@ -0,0 +1,5 @@
1
+ common_param:
2
+ subparam: overriden
3
+ another:
4
+ sublevel:
5
+ thing: always
@@ -0,0 +1,63 @@
1
+ require 'spec_helper'
2
+
3
+ describe Hieracles::Config do
4
+ describe '.load' do
5
+ context 'with an existing file' do
6
+ let(:options) do
7
+ { config: 'spec/files/config.yml', basepath: 'spec/files' }
8
+ end
9
+ before { Hieracles::Config.load options }
10
+
11
+ it 'initialize config values' do
12
+ expect(Hieracles::Config.classpath).to eq 'farm_modules/%s.pp'
13
+ expect(Hieracles::Config.modulepath).to eq 'modules'
14
+ expect(Hieracles::Config.hierafile).to eq 'hiera.yaml'
15
+ expect(Hieracles::Config.format).to eq 'Console'
16
+ end
17
+ end
18
+
19
+ context 'with additional parameters' do
20
+ let(:hierapath) { 'hiera.yaml' }
21
+ let(:options) do
22
+ { config: 'spec/files/config.yml', basepath: 'spec/files', hierafile: hierapath }
23
+ end
24
+ before { Hieracles::Config.load options }
25
+
26
+ it 'initialize config values' do
27
+ expect(Hieracles::Config.classpath).to eq 'farm_modules/%s.pp'
28
+ expect(Hieracles::Config.modulepath).to eq 'modules'
29
+ expect(Hieracles::Config.hierafile).to eq hierapath
30
+ expect(Hieracles::Config.format).to eq 'Console'
31
+ end
32
+ end
33
+
34
+ context 'without an existing config file' do
35
+ let(:options) do
36
+ { config: 'spec/files/config_no.yml' }
37
+ end
38
+ before do
39
+ FileUtils.rm(options[:config]) if File.exist? options[:config]
40
+ Hieracles::Config.load options
41
+ end
42
+ after { FileUtils.rm(options[:config]) if File.exist? options[:config] }
43
+
44
+ it 'creates a default config file' do
45
+ expect(File.exist? options[:config]).to be_truthy
46
+ end
47
+
48
+ it 'initialize config values' do
49
+ expect(Hieracles::Config.classpath).to eq 'manifests/classes/%s.pp'
50
+ end
51
+ end
52
+ end
53
+
54
+ describe '.defaultconfig' do
55
+ it { expect(Hieracles::Config.defaultconfig).to be_truthy }
56
+ end
57
+
58
+ describe '.extract_params' do
59
+ let(:str) { 'bla=blu;one=two' }
60
+ let(:expected) { { bla: 'blu', one: 'two' } }
61
+ it { expect(Hieracles::Config.extract_params(str)).to eq expected }
62
+ end
63
+ end
@@ -0,0 +1,91 @@
1
+ require 'spec_helper'
2
+
3
+ describe Hieracles::Format do
4
+
5
+ let(:node) { double("node") }
6
+ let(:dispatch) { Hieracles::Format.new node }
7
+ let(:not_implemented) { "%s not implemented, please inherit from the Hieracles::Format class to implement a format.\n"}
8
+
9
+ describe '.new' do
10
+ it "defines a node object" do
11
+ expect(dispatch.instance_variable_get :@node).to eq node
12
+ end
13
+ end
14
+
15
+ describe ".info" do
16
+ it "returns a message informing that this class should be inherited" do
17
+ expect(dispatch.info nil).to eq format(not_implemented, 'info')
18
+ end
19
+ end
20
+ describe ".files" do
21
+ it "returns a message informing that this class should be inherited" do
22
+ expect(dispatch.files nil).to eq format(not_implemented, 'files')
23
+ end
24
+ end
25
+ describe ".paths" do
26
+ it "returns a message informing that this class should be inherited" do
27
+ expect(dispatch.paths nil).to eq format(not_implemented, 'paths')
28
+ end
29
+ end
30
+
31
+
32
+ describe ".build_head" do
33
+ it "returns a message informing that this class should be inherited" do
34
+ expect(dispatch.send :build_head, true).to eq format(not_implemented, 'build_head')
35
+ end
36
+ end
37
+ describe ".build_params_line" do
38
+ it "returns a message informing that this class should be inherited" do
39
+ expect(dispatch.send :build_params_line, nil, nil, nil).to eq format(not_implemented, 'build_params_line')
40
+ end
41
+ end
42
+ describe ".build_modules_line" do
43
+ it "returns a message informing that this class should be inherited" do
44
+ expect(dispatch.send :build_modules_line, nil, nil).to eq format(not_implemented, 'build_modules_line')
45
+ end
46
+ end
47
+
48
+ describe ".params" do
49
+ let(:params) { { k: 'v', kk: 'vv'} }
50
+ let(:expected) {
51
+ format(not_implemented, 'build_head') +
52
+ format(not_implemented, 'build_params_line') +
53
+ format(not_implemented, 'build_params_line')
54
+ }
55
+ before { allow(node).to receive(:params).and_return(params) }
56
+ it "returns 4 lines" do
57
+ expect(dispatch.params []).to eq expected
58
+ end
59
+ end
60
+
61
+ describe ".allparams" do
62
+ let(:params) { { k: 'v', kk: 'vv'} }
63
+ let(:expected) {
64
+ format(not_implemented, 'build_head') +
65
+ format(not_implemented, 'build_params_line') +
66
+ format(not_implemented, 'build_params_line')
67
+ }
68
+ before {
69
+ allow(node).to receive(:add_common).and_return(nil)
70
+ allow(node).to receive(:params).and_return(params)
71
+ }
72
+ it "returns 4 lines" do
73
+ expect(dispatch.allparams []).to eq expected
74
+ end
75
+ end
76
+
77
+ describe ".modules" do
78
+ let(:modules) { { k: 'v', kk: 'vv'} }
79
+ let(:expected) {
80
+ format(not_implemented, 'build_modules_line') +
81
+ format(not_implemented, 'build_modules_line')
82
+ }
83
+ before {
84
+ allow(node).to receive(:modules).and_return(modules)
85
+ }
86
+ it "returns 2 lines" do
87
+ expect(dispatch.modules []).to eq expected
88
+ end
89
+ end
90
+
91
+ end
@@ -0,0 +1,107 @@
1
+ require 'spec_helper'
2
+
3
+ describe Hieracles::Formats::Console do
4
+ let(:node) { double("node") }
5
+ let(:console_format) { Hieracles::Formats::Console.new node }
6
+
7
+ describe ".info" do
8
+ let(:expected) {
9
+ "\e[97mNode \e[0m fqdn\n" +
10
+ "\e[97mFarm \e[0m farm\n"
11
+ }
12
+ before {
13
+ allow(node).to receive(:info).and_return(
14
+ {
15
+ 'Node' => 'fqdn',
16
+ 'Farm' => 'farm'
17
+ }
18
+ )
19
+ }
20
+ it "outputs proper text" do
21
+ expect(console_format.info nil).to eq expected
22
+ end
23
+ end
24
+
25
+ describe ".files" do
26
+ let(:expected) { "path1\npath2\n" }
27
+ before {
28
+ allow(node).to receive(:files).and_return(['path1', 'path2'])
29
+ }
30
+ it "outputs proper text" do
31
+ expect(console_format.files nil).to eq expected
32
+ end
33
+ end
34
+
35
+ describe ".paths" do
36
+ let(:expected) { "path1\npath2\n" }
37
+ before {
38
+ allow(node).to receive(:paths).and_return(['path1', 'path2'])
39
+ }
40
+ it "outputs proper text" do
41
+ expect(console_format.paths nil).to eq expected
42
+ end
43
+ end
44
+
45
+ describe ".build_head" do
46
+ let(:expected) { "\e[31m[0] path1\e[0m\n\e[32m[1] path2\e[0m\n\n" }
47
+ before {
48
+ allow(node).to receive(:files).and_return(['path1', 'path2'])
49
+ }
50
+ it "outputs proper text" do
51
+ expect(console_format.send :build_head, true).to eq expected
52
+ end
53
+ end
54
+
55
+ describe ".build_params_line" do
56
+ let(:expected) {
57
+ "\e[31m[0]\e[0m \e[36mparams.this.var\e[0m value1\n"+
58
+ " \e[97m[1] params.this.var value2\e[0m\n"
59
+ }
60
+ let(:params) {
61
+ [
62
+ { file: 'path1', value: 'value1'},
63
+ { file: 'path2', value: 'value2'},
64
+ ]
65
+ }
66
+ before {
67
+ console_format.instance_variable_set(:@colors,
68
+ {'path1' => 0, 'path2' => 1}
69
+ )
70
+ }
71
+ it "outputs proper text" do
72
+ expect(console_format.send :build_params_line,
73
+ "params.this.var",
74
+ params,
75
+ nil).to eq expected
76
+ end
77
+ end
78
+
79
+ describe ".build_modules_line" do
80
+ before {
81
+ allow(node).to receive(:modules).and_return(
82
+ {
83
+ 'module1' => nil,
84
+ 'longmodule2' => nil
85
+ }
86
+ )
87
+ }
88
+ context "module is found" do
89
+ let(:expected) { "module1 value\n" }
90
+ it "outputs proper text" do
91
+ expect(console_format.send :build_modules_line, "module1", "value").to eq expected
92
+ end
93
+ end
94
+ context "module is not found" do
95
+ let(:expected) { "module1 \e[31mnot found\e[0m\n" }
96
+ it "outputs proper text" do
97
+ expect(console_format.send :build_modules_line, "module1", "not found").to eq expected
98
+ end
99
+ end
100
+ context "module is duplicate" do
101
+ let(:expected) { "module1 \e[33m(duplicate)\e[0m\n" }
102
+ it "outputs proper text" do
103
+ expect(console_format.send :build_modules_line, "module1", "(duplicate)").to eq expected
104
+ end
105
+ end
106
+ end
107
+ end
@@ -0,0 +1,89 @@
1
+ require 'spec_helper'
2
+
3
+ describe Hieracles::Formats::Csv do
4
+ let(:node) { double("node") }
5
+ let(:csv_format) { Hieracles::Formats::Csv.new node }
6
+
7
+ describe ".info" do
8
+ let(:expected) {"fqdn;farm\n"}
9
+ before {
10
+ allow(node).to receive(:info).and_return(
11
+ {
12
+ 'Node' => 'fqdn',
13
+ 'Farm' => 'farm'
14
+ }
15
+ )
16
+ }
17
+ it "outputs proper text" do
18
+ expect(csv_format.info nil).to eq expected
19
+ end
20
+ end
21
+
22
+ describe ".files" do
23
+ let(:expected) { "path1;path2\n" }
24
+ before {
25
+ allow(node).to receive(:files).and_return(['path1', 'path2'])
26
+ }
27
+ it "outputs proper text" do
28
+ expect(csv_format.files nil).to eq expected
29
+ end
30
+ end
31
+
32
+ describe ".paths" do
33
+ let(:expected) { "path1;path2\n" }
34
+ before {
35
+ allow(node).to receive(:paths).and_return(['path1', 'path2'])
36
+ }
37
+ it "outputs proper text" do
38
+ expect(csv_format.paths nil).to eq expected
39
+ end
40
+ end
41
+
42
+ describe ".build_head" do
43
+ let(:expected) { "path1;path2;var;value;overriden\n" }
44
+ before {
45
+ allow(node).to receive(:files).and_return(['path1', 'path2'])
46
+ }
47
+ it "outputs proper text" do
48
+ expect(csv_format.send :build_head, true).to eq expected
49
+ end
50
+ end
51
+
52
+ describe ".build_params_line" do
53
+ let(:expected) {
54
+ "1;0;params.this.var;value1;0\n"+
55
+ "0;1;params.this.var;value2;1\n"
56
+ }
57
+ let(:params) {
58
+ [
59
+ { file: 'path1', value: 'value1'},
60
+ { file: 'path2', value: 'value2'},
61
+ ]
62
+ }
63
+ before {
64
+ allow(node).to receive(:files).and_return(['path1', 'path2'])
65
+ }
66
+ it "outputs proper text" do
67
+ expect(csv_format.send :build_params_line,
68
+ "params.this.var",
69
+ params,
70
+ nil).to eq expected
71
+ end
72
+ end
73
+
74
+ describe ".build_modules_line" do
75
+ before {
76
+ allow(node).to receive(:modules).and_return(
77
+ {
78
+ 'module1' => nil,
79
+ 'longmodule2' => nil
80
+ }
81
+ )
82
+ }
83
+ let(:expected) { "module1;value\n" }
84
+ it "outputs proper text" do
85
+ expect(csv_format.send :build_modules_line, "module1", "value").to eq expected
86
+ end
87
+ end
88
+
89
+ end