nugrant 0.0.11

Sign up to get free protection for your applications and to get access to all the features.
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
data/.buildpath ADDED
@@ -0,0 +1,6 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <buildpath>
3
+ <buildpathentry kind="src" path="test"/>
4
+ <buildpathentry kind="src" path="lib"/>
5
+ <buildpathentry kind="con" path="org.eclipse.dltk.launching.INTERPRETER_CONTAINER"/>
6
+ </buildpath>
data/.gitattributes ADDED
@@ -0,0 +1,33 @@
1
+ # Handle line endings automatically for files detected as text
2
+ # and leave all files detected as binary untouched.
3
+ * text=auto
4
+
5
+ #
6
+ # The above will handle all files NOT found below
7
+ #
8
+ # These files are text and should be normalized (Convert crlf => lf)
9
+ Gemfile
10
+ Rakefile
11
+ Vagrantfile
12
+ *.rb text
13
+ *.md text
14
+ *.gemspec text
15
+ *.lock text
16
+ *.txt text
17
+ *.yml text
18
+ *.yaml text
19
+ *.prefs text
20
+
21
+ # Eclipse specific files
22
+ *.project text
23
+ *.buildpath text
24
+
25
+ # Those have specific eol for testing purpose
26
+ test/resources/json/params_unix_eol.json eol=lf
27
+ test/resources/json/params_windows_eol.json eol=crlf
28
+ test/resources/yml/params_unix_eol.yml eol=lf
29
+ test/resources/yml/params_windows_eol.yml eol=crlf
30
+
31
+ # These files are binary and should be left untouched
32
+ # (binary is a macro for -text -diff)
33
+ *.gem binary
data/.gitignore ADDED
@@ -0,0 +1,19 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ .vagrant
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ lib/nugrant_debug.rb
13
+ lib/bundler/man
14
+ pkg
15
+ rdoc
16
+ spec/reports
17
+ test/tmp
18
+ test/version_tmp
19
+ tmp
data/.project ADDED
@@ -0,0 +1,17 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <projectDescription>
3
+ <name>nugrant</name>
4
+ <comment></comment>
5
+ <projects>
6
+ </projects>
7
+ <buildSpec>
8
+ <buildCommand>
9
+ <name>org.eclipse.dltk.core.scriptbuilder</name>
10
+ <arguments>
11
+ </arguments>
12
+ </buildCommand>
13
+ </buildSpec>
14
+ <natures>
15
+ <nature>org.eclipse.dltk.ruby.core.nature</nature>
16
+ </natures>
17
+ </projectDescription>
data/CHANGELOG.md ADDED
@@ -0,0 +1,35 @@
1
+ # 0.0.11
2
+
3
+ * Updated README file for installation via rubygems.org
4
+
5
+ # 0.0.10
6
+
7
+ * Added a subcommand `parameters` for vagrant command `user`.
8
+ * Added a vagrant command `vagrant user subcommand [options]`.
9
+
10
+ # 0.0.9
11
+
12
+ * Fixed a bug with the new default values implementation
13
+
14
+ # 0.0.8
15
+
16
+ * Introduced possibility to set default values
17
+ * Introduced restricted keys (For now, restricted keys are [`defaults`])
18
+ * Fixed a bug with system-wide parameters
19
+
20
+ # 0.0.7
21
+
22
+ * YAML is back as the default file format for parameters
23
+
24
+ # 0.0.6
25
+
26
+ * Fixed a bug on ruby 1.8.7 which doesn't have yaml included in its load path by default
27
+
28
+ # 0.0.5
29
+
30
+ * Introduced system-wide parameters file
31
+
32
+ # 0.0.4
33
+
34
+ * JSON is now the default file format for parameters (due to problem with YAML)
35
+ * It is now possible to store parameters in the JSON format
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in nugrant.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Matthieu Vachon
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,198 @@
1
+ # Nugrant
2
+
3
+ Vagrant plugin that brings user specific configuration
4
+ options. It will enable a `.vagrantuser` at different
5
+ location that will get imported into the main vagrant
6
+ config.
7
+
8
+ For example, let say the git repository you want to
9
+ expose is not located under the root folder of
10
+ your `Vagrantfile`. That means you will need to specify
11
+ an absolute host path to share the folder on the guest vm.
12
+
13
+ Your `Vagrantfile` would look like this:
14
+
15
+ Vagrant::Config.run do |config|
16
+ config.vm.share_folder "git", "/git", "/home/user/work/git"
17
+ end
18
+
19
+ However, what happens when multiple developers
20
+ need to share the same `Vagrantfile`? This is the main
21
+ use case this plugin address.
22
+
23
+ ## Installation
24
+
25
+ First, you need to have Vagrant installed for this gem
26
+ to work correctly.
27
+
28
+ There is two different way to install the gem. You can
29
+ install it via Vagrant or via the system gem containers.
30
+
31
+ When you install from Vagrant, the main benefit is that
32
+ it's decoupled from your other system gems. There is less
33
+ chance for this gem's dependencies, even if they are minimal,
34
+ to clash with gems already installed on your system. This is the
35
+ recommended installation method. To install, simply run:
36
+
37
+ > vagrant gem install nugrant
38
+
39
+ If you prefer to install the gem in a system wide matters,
40
+ please use this command instead:
41
+
42
+ > gem install nugrant
43
+
44
+ ## Usage
45
+
46
+ When Vagrant starts, via any of the `vagrant` commands,
47
+ it loads all vagrant plugins it founds under the `GEM_PATH`
48
+ variable. If you installed the plugin with one of the two
49
+ methods we listed above, you DO NOT need to setup this
50
+ environment variable.
51
+
52
+ To use the plugin, first create a yaml file named
53
+ `.vagrantuser` where your `Vagrantfile` is located. The file
54
+ must be a valid yaml file:
55
+
56
+ vm_port: 2223
57
+ repository:
58
+ project: "/home/user/work/git"
59
+
60
+ The configuration hierarchy you define in the `.vagrantuser` file
61
+ is imported into the `config` object of the `Vagrantfile`
62
+ under the key `user`. So, with the `.vagrantuser` file above, you
63
+ could have this `Vagrantfile` that abstract absolute paths.
64
+
65
+ Vagrant::Config.run do |config|
66
+ config.ssh.port config.user.vm_port
67
+
68
+ config.vm.share_folder "git", "/git", config.user.repository.project
69
+ end
70
+
71
+ This way, paths can be customized by every developer. They just
72
+ have to add a `.vagrantuser` file where user specific configuration
73
+ values can be specified. The `.vagrantuser` should be ignored by you
74
+ version control system so it is to committed with the project.
75
+
76
+ Additionally, you can also have a `.vagrantuser` under your user home
77
+ directory. This way, you can set parameters that will be
78
+ available to all your `Vagrantfile'. The project `.vagrantuser`
79
+ file will overrides parameters defined in the `.vagrantuser` file
80
+ defined in the user home directory
81
+
82
+ For example, you have `.vagrantuser` file located at `~/.vagrantuser`
83
+ that has the following content:
84
+
85
+ vm_port: 2223
86
+ repository:
87
+ project: "/home/user/work/git"
88
+
89
+ And another `.vagrantuser` at the root of your `Vagrantfile`:
90
+
91
+ vm_port: 3332
92
+ repository:
93
+ personal: "/home/user/personal/git"
94
+
95
+ Then, the `Vagrantfile` could be defined like this:
96
+
97
+ Vagrant::Config.run do |config|
98
+ config.ssh.port config.user.vm_port
99
+
100
+ config.vm.share_folder "git", "/git", config.user.repository.project
101
+ config.vm.share_folder "personal", "/personal", config.user.repository.personal
102
+ end
103
+
104
+ That would be equivalent to:
105
+
106
+ Vagrant::Config.run do |config|
107
+ config.ssh.port 3332
108
+
109
+ config.vm.share_folder "git", "/git", "/home/user/work/git"
110
+ config.vm.share_folder "personal", "/personal", "/home/user/personal/git"
111
+ end
112
+
113
+ As you can see, the parameters defined in the second `.vagrantuser` file
114
+ (the project one) overrides settings defined in the `.vagrantuser` found
115
+ in the home directory (the user one).
116
+
117
+ ### Parameters access
118
+
119
+ Parameters in the `Vagrantfile` can be retrieved via method call
120
+ of array access.
121
+
122
+ config.user['repository']['project'] # Array access
123
+ config.user.repository.project # Method access
124
+
125
+ You can even mix the two if you want, but we do not recommend
126
+ it since its always better to be consistent:
127
+
128
+ config.user['repository'].project # Mixed access
129
+ config.user.repository['project'] # Mixed access
130
+
131
+ Only the root key, i.e. `config.user`, cannot be access with
132
+ both syntax, only the method syntax can be used since this
133
+ is not provided by this plugin but by Vagrant itself.
134
+
135
+ ### Default values
136
+
137
+ When using parameters, it is often needed so set default values
138
+ for certain parameters so if the user does not define one, the
139
+ default value will be picked up.
140
+
141
+ For example, say you want a parameter that will hold the ssh
142
+ port of the vm. This parameter will be accessible via the
143
+ parameter `config.user.vm.ssh_port`.
144
+
145
+ You can use the following snippet directly within your Vagrantfile
146
+ to set a default value for this parameter:
147
+
148
+ Vagrant::Config.run do |config|
149
+ config.user.defaults = {
150
+ "vm" => {
151
+ "ssh_port" => "3335"
152
+ }
153
+ }
154
+
155
+ config.ssh.port config.user.vm.ssh_port
156
+ end
157
+
158
+ With this Vagrantfile, the parameter `config.user.vm.ssh_port`
159
+ will default to `3335` in cases where it is not defined by the
160
+ user.
161
+
162
+ If the user decides to change it, he just has to set it in his
163
+ own `.vagrantuser` and it will override the default value defined
164
+ in the Vagrantfile.
165
+
166
+ ### Vagrant commands
167
+
168
+ In this section, we describe the various vagrant commands defined
169
+ by this plugin that can be used to interact with it.
170
+
171
+ #### Parameters
172
+
173
+ This command will print the currently defined parameters at the
174
+ given location. All rules are respected when using this command.
175
+ It is usefull to see what parameters are available and what are
176
+ the current values of those parameters.
177
+
178
+ Usage:
179
+
180
+ > vagrant user parameters
181
+ ---
182
+ config:
183
+ user:
184
+ chef:
185
+ cookbooks_path: /Users/Chef/kitchen/cookbooks
186
+ nodes_path: /Users/Chef/kitchen/nodes
187
+ roles_path: /Users/Chef/kitchen/roles
188
+
189
+ ## Contributing
190
+
191
+ You can contribute by filling issues when something goes
192
+ wrong or was not what you expected. I will do my best
193
+ to fix the issue either in the code or in the documentation,
194
+ where applicable.
195
+
196
+ You can also send pull requests for any feature or improvement
197
+ you think should be included in this plugin. I will evaluate
198
+ each of them and merge them as fast as possible.
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new do |task|
5
+ task.test_files = FileList['test/**/test*.rb']
6
+ end
7
+
8
+ desc "Run tests"
9
+ task :default => :test
10
+
data/lib/nugrant.rb ADDED
@@ -0,0 +1,12 @@
1
+ require 'nugrant'
2
+ require 'nugrant/config'
3
+ require 'nugrant/parameter_bag'
4
+ require 'nugrant/parameters'
5
+
6
+ module Nugrant
7
+ def self.create_parameters(options)
8
+ config = Nugrant::Config.new(options)
9
+
10
+ return Nugrant::Parameters.new(config)
11
+ end
12
+ end
@@ -0,0 +1,51 @@
1
+ require 'rbconfig'
2
+
3
+ module Nugrant
4
+ class Config
5
+ DEFAULT_PARAMS_FILENAME = ".vagrantuser"
6
+ DEFAULT_PARAMS_FILETYPE = "yml"
7
+
8
+ attr :params_filename, true
9
+ attr :params_filetype, true
10
+
11
+ def self.user_base_path()
12
+ return File.expand_path("~")
13
+ end
14
+
15
+ def self.system_base_path()
16
+ # TODO: Fixme, find the right location to put system wide settings on windows...
17
+ if Config.on_windows?
18
+ return "C:/etc"
19
+ end
20
+
21
+ return "/etc"
22
+ end
23
+
24
+ def self.on_windows?()
25
+ return (RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/) != nil
26
+ end
27
+
28
+ def initialize(options = {})
29
+ options.delete_if { |key, value| value == nil }
30
+
31
+ @params_filename = options.fetch(:params_filename, DEFAULT_PARAMS_FILENAME)
32
+ @params_filetype = options.fetch(:params_filetype, DEFAULT_PARAMS_FILETYPE)
33
+
34
+ @project_params_path = options.fetch(:project_params_path, nil)
35
+ @user_params_path = options.fetch(:user_params_path, nil)
36
+ @system_params_path = options.fetch(:system_params_path, nil)
37
+ end
38
+
39
+ def project_params_path()
40
+ File.expand_path(@project_params_path || "./#{@params_filename}")
41
+ end
42
+
43
+ def user_params_path()
44
+ File.expand_path(@user_params_path || "#{Config.user_base_path()}/#{@params_filename}")
45
+ end
46
+
47
+ def system_params_path()
48
+ File.expand_path(@system_params_path || "#{Config.system_base_path()}/#{@params_filename}")
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,68 @@
1
+ module Nugrant
2
+ class ParameterBag
3
+ def initialize(parameters)
4
+ if parameters == nil
5
+ return
6
+ end
7
+
8
+ @bag = {}
9
+ @defaults = {}
10
+
11
+ parameters.each do |key, value|
12
+ if key == "defaults"
13
+ raise ArgumentError, "The key 'defaults' has restricted usage and cannot be defined"
14
+ end
15
+
16
+ if not value.is_a?(Hash)
17
+ @bag[key] = value
18
+ next
19
+ end
20
+
21
+ # It is a hash, transform it into a bag
22
+ @bag[key] = Nugrant::ParameterBag.new(value)
23
+ end
24
+ end
25
+
26
+ def [](param_name)
27
+ return get_param(param_name)
28
+ end
29
+
30
+ def method_missing(method, *args, &block)
31
+ return get_param(method.to_s)
32
+ end
33
+
34
+ def has_param?(param_name)
35
+ return @bag != nil && @bag.has_key?(param_name)
36
+ end
37
+
38
+ def get_param(param_name)
39
+ if not has_param?(param_name)
40
+ if @defaults[param_name]
41
+ return @defaults[param_name]
42
+ end
43
+
44
+ raise KeyError, "Undefined parameter '#{param_name}'"
45
+ end
46
+
47
+ return @bag[param_name]
48
+ end
49
+
50
+ def get_params()
51
+ return @bag
52
+ end
53
+
54
+ def defaults(parameters)
55
+ parameters.each do |key, value|
56
+ if value.is_a?(Hash) and has_param?(key)
57
+ @bag[key].defaults(value)
58
+ end
59
+ end
60
+
61
+ @defaults = Nugrant::ParameterBag.new(parameters)
62
+ end
63
+
64
+ def defaults=(parameters)
65
+ defaults(parameters)
66
+ end
67
+ end
68
+ end