nugrant 0.0.11

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 (49) hide show
  1. data/.buildpath +6 -0
  2. data/.gitattributes +33 -0
  3. data/.gitignore +19 -0
  4. data/.project +17 -0
  5. data/CHANGELOG.md +35 -0
  6. data/Gemfile +4 -0
  7. data/LICENSE.txt +22 -0
  8. data/README.md +198 -0
  9. data/Rakefile +10 -0
  10. data/lib/nugrant.rb +12 -0
  11. data/lib/nugrant/config.rb +51 -0
  12. data/lib/nugrant/parameter_bag.rb +68 -0
  13. data/lib/nugrant/parameters.rb +104 -0
  14. data/lib/nugrant/vagrant/user_command.rb +86 -0
  15. data/lib/nugrant/vagrant/user_parameters.rb +27 -0
  16. data/lib/nugrant/version.rb +3 -0
  17. data/lib/vagrant_init.rb +7 -0
  18. data/nugrant.gemspec +31 -0
  19. data/test/lib/nugrant/test_config.rb +125 -0
  20. data/test/lib/nugrant/test_parameters.rb +205 -0
  21. data/test/lib/nugrant/test_parameters_bag.rb +164 -0
  22. data/test/resources/.vagrantuser +3 -0
  23. data/test/resources/README.md +52 -0
  24. data/test/resources/Vagrantfile +5 -0
  25. data/test/resources/json/params_combinations.json +81 -0
  26. data/test/resources/json/params_defaults_at_root.json +3 -0
  27. data/test/resources/json/params_defaults_not_at_root.json +5 -0
  28. data/test/resources/json/params_project_1.json +6 -0
  29. data/test/resources/json/params_project_2.json +29 -0
  30. data/test/resources/json/params_simple.json +3 -0
  31. data/test/resources/json/params_system_1.json +6 -0
  32. data/test/resources/json/params_system_2.json +29 -0
  33. data/test/resources/json/params_unix_eol.json +6 -0
  34. data/test/resources/json/params_user_1.json +6 -0
  35. data/test/resources/json/params_user_2.json +29 -0
  36. data/test/resources/json/params_windows_eol.json +6 -0
  37. data/test/resources/yml/params_combinations.yml +72 -0
  38. data/test/resources/yml/params_defaults_at_root.yml +1 -0
  39. data/test/resources/yml/params_defaults_not_at_root.yml +2 -0
  40. data/test/resources/yml/params_project_1.yml +4 -0
  41. data/test/resources/yml/params_project_2.yml +23 -0
  42. data/test/resources/yml/params_simple.yml +1 -0
  43. data/test/resources/yml/params_system_1.yml +4 -0
  44. data/test/resources/yml/params_system_2.yml +25 -0
  45. data/test/resources/yml/params_unix_eol.yml +3 -0
  46. data/test/resources/yml/params_user_1.yml +4 -0
  47. data/test/resources/yml/params_user_2.yml +23 -0
  48. data/test/resources/yml/params_windows_eol.yml +3 -0
  49. metadata +175 -0
@@ -0,0 +1,104 @@
1
+ require 'deep_merge'
2
+ require 'json'
3
+ require 'ostruct'
4
+ require 'yaml'
5
+
6
+ module Nugrant
7
+ class Parameters < Nugrant::ParameterBag
8
+ def initialize(config = nil)
9
+ if config == nil
10
+ config = Nugrant::Config.new()
11
+ end
12
+
13
+ @config = config
14
+ @parameters = load_parameters()
15
+
16
+ super(@parameters)
17
+ end
18
+
19
+ def get_bag()
20
+ @bag
21
+ end
22
+
23
+ def get_params()
24
+ return @parameters
25
+ end
26
+
27
+ def get_project_params()
28
+ return @project_parameters
29
+ end
30
+
31
+ def get_user_params()
32
+ return @user_parameters
33
+ end
34
+
35
+ def get_system_params()
36
+ return @system_parameters
37
+ end
38
+
39
+ def load_parameters()
40
+ @project_parameters = load_parameters_file(@config.project_params_path)
41
+ @user_parameters = load_parameters_file(@config.user_params_path)
42
+ @system_parameters = load_parameters_file(@config.system_params_path)
43
+
44
+ parameters = Hash.new()
45
+
46
+ if @project_parameters == nil and @user_parameters == nil and @system_parameters == nil
47
+ return parameters
48
+ end
49
+
50
+ parameters.deep_merge!(@system_parameters) if @system_parameters != nil
51
+ parameters.deep_merge!(@user_parameters) if @user_parameters != nil
52
+ parameters.deep_merge!(@project_parameters) if @project_parameters != nil
53
+
54
+ return parameters
55
+ end
56
+
57
+ def load_parameters_file(file_path)
58
+ if not File.exists?(file_path)
59
+ return nil
60
+ end
61
+
62
+ begin
63
+ File.open(file_path, "rb") do |file|
64
+ parsing_method = "parse_#{@config.params_filetype}"
65
+ result = send(parsing_method, file.read)
66
+
67
+ restricted_key = has_restricted_keys?(result)
68
+ if restricted_key
69
+ raise ArgumentError, "The key '#{restricted_key}' has restricted usage and cannot be defined"
70
+ end
71
+
72
+ return result
73
+ end
74
+ rescue => error
75
+ raise RuntimeError, "Could not parse the user #{@config.params_filetype} parameters file '#{file_path}': #{error}"
76
+ end
77
+ end
78
+
79
+ def parse_json(data_string)
80
+ JSON.parse(data_string)
81
+ end
82
+
83
+ def parse_yml(data_string)
84
+ YAML::ENGINE.yamler= 'syck' if defined?(YAML::ENGINE)
85
+
86
+ YAML.load(data_string)
87
+ end
88
+
89
+ def has_restricted_keys?(parameters)
90
+ parameters.each do |key, value|
91
+ if key == "defaults"
92
+ return "defaults"
93
+ end
94
+
95
+ if value.is_a?(Hash)
96
+ result = has_restricted_keys?(value)
97
+ return result if result
98
+ end
99
+ end
100
+
101
+ return false
102
+ end
103
+ end
104
+ end
@@ -0,0 +1,86 @@
1
+ require 'nugrant'
2
+
3
+ module Nugrant
4
+ module Vagrant
5
+ class UserCommand < ::Vagrant::Command::Base
6
+ HANDLED_SUBCOMMANDS = [
7
+ "parameters",
8
+ ]
9
+
10
+ def initialize(arguments, environment)
11
+ super(arguments, environment)
12
+
13
+ @parameters = Nugrant::Parameters.new()
14
+ @options_parser = initialize_parsers()
15
+
16
+ @print_help = false
17
+ end
18
+
19
+ def initialize_parsers()
20
+ parser = OptionParser.new
21
+
22
+ parser.banner = "Usage: vagrant user subcommand [options]"
23
+
24
+ parser.on("-h", "--help", "Print this help") do
25
+ @print_help
26
+ end
27
+
28
+ parser.separator ""
29
+ parser.separator "Available subcommands:"
30
+
31
+ HANDLED_SUBCOMMANDS.each do |subcommand|
32
+ parser.separator " #{subcommand}"
33
+ end
34
+
35
+ return parser
36
+ end
37
+
38
+ def execute()
39
+ subcommands = parse_options(@options_parser)
40
+ if not subcommands
41
+ subcommands = ["parameters"]
42
+ end
43
+
44
+ subcommands = validate_subcommands(subcommands)
45
+ if not subcommands
46
+ safe_puts(@options_parser.help())
47
+ return
48
+ end
49
+
50
+ # At this point, subcommands are valid
51
+ subcommands.each do |subcommand|
52
+ execute_subcommand(subcommand)
53
+ end
54
+ end
55
+
56
+ def execute_subcommand(subcommand)
57
+ send(:parameters)
58
+ end
59
+
60
+ def parameters()
61
+ parameters = {
62
+ 'config' => {
63
+ 'user' => @parameters.get_params()
64
+ }
65
+ }
66
+
67
+ safe_puts(parameters.to_yaml(:Separator => ""))
68
+ end
69
+
70
+ def handled_subcommand?(subcommand)
71
+ return HANDLED_SUBCOMMANDS.find_index(subcommand)
72
+ end
73
+
74
+ def validate_subcommands(subcommands)
75
+ subcommands.each do |subcommand|
76
+ if not handled_subcommand?(subcommand)
77
+ puts "Unknown subcommand [#{subcommand}]."
78
+ return nil
79
+ end
80
+ end
81
+
82
+ return subcommands
83
+ end
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,27 @@
1
+ require 'nugrant'
2
+
3
+ module Nugrant
4
+ module Vagrant
5
+ class UserParameters < ::Vagrant::Config::Base
6
+ def initialize()
7
+ @parameters = Nugrant::Parameters.new()
8
+ end
9
+
10
+ def [](param_name)
11
+ return @parameters[param_name]
12
+ end
13
+
14
+ def method_missing(method, *args, &block)
15
+ return @parameters.method_missing(method, *args, &block)
16
+ end
17
+
18
+ def defaults(parameters)
19
+ @parameters.defaults(parameters)
20
+ end
21
+
22
+ def defaults=(parameters)
23
+ @parameters.defaults=(parameters)
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,3 @@
1
+ module Nugrant
2
+ VERSION = "0.0.11"
3
+ end
@@ -0,0 +1,7 @@
1
+ # This module will initialize the vagrant plugin
2
+ require 'nugrant/vagrant/user_command'
3
+ require 'nugrant/vagrant/user_parameters'
4
+
5
+ # Plugin bootstrap
6
+ Vagrant.commands.register(:user) { Nugrant::Vagrant::UserCommand }
7
+ Vagrant.config_keys.register(:user) { Nugrant::Vagrant::UserParameters }
data/nugrant.gemspec ADDED
@@ -0,0 +1,31 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ require 'nugrant/version'
6
+
7
+ Gem::Specification.new do |gem|
8
+ gem.name = "nugrant"
9
+ gem.version = Nugrant::VERSION
10
+ gem.authors = ["Matthieu Vachon"]
11
+ gem.email = ["matthieu.ouellette-vachon@nuecho.com"]
12
+ gem.homepage = "https://github.com/nuecho/nugrant"
13
+ gem.summary = "Vagrant plugin to enable user specific configuration parameters."
14
+ gem.description = <<-EOF
15
+ This gem is in fact a Vagrant pluging. By installing this gem, it will be
16
+ possible to define user specific configuration files that will be merge
17
+ directly into the Vagrant configuration. This is usefull if you need to
18
+ share a Vagrantfile to multiple developers but would like to customize
19
+ some parameters for each users differently.
20
+ EOF
21
+
22
+ gem.files = `git ls-files`.split($/)
23
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
24
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
25
+ gem.require_paths = ["lib"]
26
+
27
+ gem.add_dependency "deep_merge", "~> 1.0.0"
28
+
29
+ gem.add_development_dependency "rake", "~> 0.9.0"
30
+ gem.add_development_dependency "vagrant", "~> 1.0.5"
31
+ end
@@ -0,0 +1,125 @@
1
+ require 'nugrant/config'
2
+ require 'test/unit'
3
+ require 'tmpdir'
4
+
5
+ class Nugrant::TestConfig < Test::Unit::TestCase
6
+ def setup
7
+ @default_param_filename = Nugrant::Config::DEFAULT_PARAMS_FILENAME
8
+
9
+ @old_working_dir = Dir.getwd()
10
+ @user_dir = Nugrant::Config.user_base_path()
11
+ @system_dir = Nugrant::Config.system_base_path()
12
+
13
+ Dir.chdir(Dir.tmpdir())
14
+
15
+ @project_dir = Dir.getwd()
16
+ end
17
+
18
+ def teardown
19
+ Dir.chdir(@old_working_dir)
20
+
21
+ @old_working_dir = nil
22
+ @project_dir = nil
23
+ @user_dir = nil
24
+ @system_dir = nil
25
+ end
26
+
27
+ def test_default_values
28
+ config = Nugrant::Config.new
29
+
30
+ assert_equal(@default_param_filename, config.params_filename())
31
+ assert_equal("#{@project_dir}/#{@default_param_filename}", config.project_params_path())
32
+ assert_equal("#{@user_dir}/#{@default_param_filename}", config.user_params_path())
33
+ assert_equal("#{@system_dir}/#{@default_param_filename}", config.system_params_path())
34
+ end
35
+
36
+ def test_custom_params_filename
37
+ config = Nugrant::Config.new({:params_filename => ".customparams"})
38
+
39
+ assert_equal(".customparams", config.params_filename())
40
+ assert_equal("#{@project_dir}/.customparams", config.project_params_path())
41
+ assert_equal("#{@user_dir}/.customparams", config.user_params_path())
42
+ assert_equal("#{@system_dir}/.customparams", config.system_params_path())
43
+ end
44
+
45
+ def test_custom_params_filename_after_creation
46
+ config = Nugrant::Config.new({:params_filename => ".vagrantuser"})
47
+
48
+ config.params_filename = ".customparams"
49
+
50
+ assert_equal(".customparams", config.params_filename())
51
+ assert_equal("#{@project_dir}/.customparams", config.project_params_path())
52
+ assert_equal("#{@user_dir}/.customparams", config.user_params_path())
53
+ assert_equal("#{@system_dir}/.customparams", config.system_params_path())
54
+ end
55
+
56
+ def test_custom_project_params_path
57
+ config = Nugrant::Config.new({
58
+ :params_filename => ".customparams",
59
+ :project_params_path => "#{@user_dir}/.projectcustomparams"
60
+ })
61
+
62
+ assert_equal(".customparams", config.params_filename())
63
+ assert_equal("#{@user_dir}/.projectcustomparams", config.project_params_path())
64
+ end
65
+
66
+ def test_custom_user_params_path
67
+ config = Nugrant::Config.new({
68
+ :params_filename => ".customparams",
69
+ :user_params_path => "#{@system_dir}/.usercustomparams"
70
+ })
71
+
72
+ assert_equal(".customparams", config.params_filename())
73
+ assert_equal("#{@system_dir}/.usercustomparams", config.user_params_path()) end
74
+
75
+ def test_custom_system_params_path
76
+ config = Nugrant::Config.new({
77
+ :params_filename => ".customparams",
78
+ :system_params_path => "#{@project_dir}/.systemcustomparams"
79
+ })
80
+
81
+ assert_equal(".customparams", config.params_filename())
82
+ assert_equal("#{@project_dir}/.systemcustomparams", config.system_params_path())
83
+ end
84
+
85
+ def test_custom_all
86
+ config = Nugrant::Config.new({
87
+ :params_filename => ".customparams",
88
+ :project_params_path => "#{@user_dir}/.projectcustomparams",
89
+ :user_params_path => "#{@system_dir}/.usercustomparams",
90
+ :system_params_path => "#{@project_dir}/.systemcustomparams"
91
+ })
92
+
93
+ assert_equal(".customparams", config.params_filename())
94
+ assert_equal("#{@user_dir}/.projectcustomparams", config.project_params_path())
95
+ assert_equal("#{@system_dir}/.usercustomparams", config.user_params_path())
96
+ assert_equal("#{@project_dir}/.systemcustomparams", config.system_params_path())
97
+ end
98
+
99
+ def test_nil_project
100
+ config = Nugrant::Config.new({
101
+ :params_filename => ".customparams",
102
+ :project_params_path => nil,
103
+ })
104
+
105
+ assert_not_nil("#{@project_dir}/.customparams", config.project_params_path())
106
+ end
107
+
108
+ def test_nil_user
109
+ config = Nugrant::Config.new({
110
+ :params_filename => ".customparams",
111
+ :user_params_path => nil,
112
+ })
113
+
114
+ assert_equal("#{@user_dir}/.customparams", config.user_params_path())
115
+ end
116
+
117
+ def test_nil_system
118
+ config = Nugrant::Config.new({
119
+ :params_filename => ".customparams",
120
+ :system_params_path => nil,
121
+ })
122
+
123
+ assert_equal("#{@system_dir}/.customparams", config.system_params_path())
124
+ end
125
+ end
@@ -0,0 +1,205 @@
1
+ require 'nugrant'
2
+ require 'nugrant/parameters'
3
+ require 'test/unit'
4
+
5
+ class Nugrant::TestParameters < Test::Unit::TestCase
6
+
7
+ @@PARAMS_FILETYPES = ["json", "yml"]
8
+ @@INVALID_PATH = "impossible_file_path.yml.impossible"
9
+
10
+ def create_parameters(params_filetype, project_params_filename, user_params_filename, system_params_filename)
11
+ resource_path = File.expand_path("#{File.dirname(__FILE__)}/../../resources/#{params_filetype}")
12
+
13
+ project_params_path = "#{resource_path}/#{project_params_filename}.#{params_filetype}" if project_params_filename
14
+ user_params_path = "#{resource_path}/#{user_params_filename}.#{params_filetype}" if user_params_filename
15
+ system_params_path = "#{resource_path}/#{system_params_filename}.#{params_filetype}" if system_params_filename
16
+
17
+ return Nugrant::create_parameters({
18
+ :params_filetype => params_filetype,
19
+ :project_params_path => project_params_path,
20
+ :user_params_path => user_params_path,
21
+ :system_params_path => system_params_path
22
+ })
23
+ end
24
+
25
+ def assert_level(parameters, results)
26
+ results.each do |key, value|
27
+ assert_equal(value, parameters.send(key), "method(#{key})")
28
+ assert_equal(value, parameters[key], "array[#{key}]")
29
+ end
30
+
31
+ assert_equal(false, parameters.has_param?("0.0.0"))
32
+ end
33
+
34
+ def test_params_level_1()
35
+ filetypes.each do |params_filetype|
36
+ parameters = create_parameters(params_filetype, "params_project_1", "params_user_1", "params_system_1")
37
+
38
+ assert_level(parameters, {
39
+ "1.1.1" => "project",
40
+ "1.1.0" => "project",
41
+ "1.0.1" => "project",
42
+ "0.1.1" => "user",
43
+ "1.0.0" => "project",
44
+ "0.1.0" => "user",
45
+ "0.0.1" => "system",
46
+ })
47
+ end
48
+ end
49
+
50
+ def test_params_level_2()
51
+ filetypes.each do |params_filetype|
52
+ parameters = create_parameters(params_filetype, "params_project_2", "params_user_2", "params_system_2")
53
+
54
+ run_second_level(parameters, "1.1.1", {
55
+ "1.1.1" => "project",
56
+ "1.1.0" => "project",
57
+ "1.0.1" => "project",
58
+ "0.1.1" => "user",
59
+ "1.0.0" => "project",
60
+ "0.1.0" => "user",
61
+ "0.0.1" => "system",
62
+ })
63
+
64
+ run_second_level(parameters, "1.1.0", {
65
+ "1.1.1" => "project",
66
+ "1.1.0" => "project",
67
+ "1.0.1" => "project",
68
+ "0.1.1" => "user",
69
+ "1.0.0" => "project",
70
+ "0.1.0" => "user",
71
+ })
72
+
73
+ run_second_level(parameters, "1.0.1", {
74
+ "1.1.1" => "project",
75
+ "1.1.0" => "project",
76
+ "1.0.1" => "project",
77
+ "0.1.1" => "system",
78
+ "1.0.0" => "project",
79
+ "0.0.1" => "system",
80
+ })
81
+
82
+ run_second_level(parameters, "0.1.1", {
83
+ "1.1.1" => "user",
84
+ "1.1.0" => "user",
85
+ "1.0.1" => "system",
86
+ "0.1.1" => "user",
87
+ "0.1.0" => "user",
88
+ "0.0.1" => "system",
89
+ })
90
+
91
+ run_second_level(parameters, "1.0.0", {
92
+ "1.1.1" => "project",
93
+ "1.1.0" => "project",
94
+ "1.0.1" => "project",
95
+ "1.0.0" => "project",
96
+ })
97
+
98
+ run_second_level(parameters, "0.1.0", {
99
+ "1.1.1" => "user",
100
+ "1.1.0" => "user",
101
+ "0.1.1" => "user",
102
+ "0.1.0" => "user",
103
+ })
104
+
105
+ run_second_level(parameters, "0.0.1", {
106
+ "1.1.1" => "system",
107
+ "1.0.1" => "system",
108
+ "0.1.1" => "system",
109
+ "0.0.1" => "system",
110
+ })
111
+
112
+ assert_equal(false, parameters.has_param?("0.0.0"))
113
+ end
114
+ end
115
+
116
+ def run_second_level(parameters, key, results)
117
+ assert_level(parameters.send(key), results)
118
+ assert_level(parameters[key], results)
119
+
120
+ assert_equal(false, parameters.has_param?("0.0.0"))
121
+ end
122
+
123
+ def test_file_nil()
124
+ run_test_file_invalid(nil)
125
+ end
126
+
127
+ def test_file_does_not_exist()
128
+ run_test_file_invalid("impossible_file_path.yml.impossible")
129
+ end
130
+
131
+ def run_test_file_invalid(invalid_value)
132
+ filetypes.each do |params_filetype|
133
+ parameters = create_parameters(params_filetype, "params_simple", invalid_path, invalid_path)
134
+ assert_equal("value", parameters.test)
135
+ assert_equal("value", parameters["test"])
136
+
137
+ parameters = create_parameters(params_filetype, invalid_path, "params_simple", invalid_path)
138
+ assert_equal("value", parameters.test)
139
+ assert_equal("value", parameters["test"])
140
+
141
+ parameters = create_parameters(params_filetype, invalid_path, invalid_path, "params_simple")
142
+ assert_equal("value", parameters.test)
143
+ assert_equal("value", parameters["test"])
144
+
145
+ parameters = create_parameters(params_filetype, invalid_path, invalid_path, invalid_path)
146
+ assert_not_nil(parameters)
147
+ end
148
+ end
149
+
150
+ def test_params_windows_eol()
151
+ run_test_params_eol("params_windows_eol")
152
+ end
153
+
154
+ def test_params_unix_eol()
155
+ run_test_params_eol("params_unix_eol")
156
+ end
157
+
158
+ def run_test_params_eol(file_path)
159
+ filetypes.each do |params_filetype|
160
+ parameters = create_parameters(params_filetype, file_path, invalid_path, invalid_path)
161
+
162
+ assert_equal("value1", parameters.level1)
163
+ assert_equal("value2", parameters.level2.first)
164
+ end
165
+ end
166
+
167
+ def test_restricted_defaults_usage()
168
+ filetypes.each do |params_filetype|
169
+ assert_raise(RuntimeError) do
170
+ results = create_parameters(params_filetype, "params_defaults_at_root", invalid_path, invalid_path)
171
+ puts("Results: #{results.inspect} (Should have thrown!)")
172
+ end
173
+ end
174
+
175
+ filetypes.each do |params_filetype|
176
+ assert_raise(RuntimeError) do
177
+ results = create_parameters(params_filetype, "params_defaults_not_at_root", invalid_path, invalid_path)
178
+ puts("Results: #{results.inspect} (Should have thrown!)")
179
+ end
180
+ end
181
+ end
182
+
183
+ def test_defaults()
184
+ filetypes.each do |params_filetype|
185
+ parameters = create_parameters(params_filetype, "params_simple", invalid_path, invalid_path)
186
+ parameters.defaults({"test" => "override", "level" => "new"})
187
+
188
+ assert_equal("value", parameters.test)
189
+ assert_equal("new", parameters.level)
190
+
191
+ parameters.defaults = {"test" => "override1", "level" => "new1"}
192
+
193
+ assert_equal("value", parameters.test)
194
+ assert_equal("new1", parameters.level)
195
+ end
196
+ end
197
+
198
+ def filetypes()
199
+ @@PARAMS_FILETYPES
200
+ end
201
+
202
+ def invalid_path()
203
+ @@INVALID_PATH
204
+ end
205
+ end