vagrant-helpers 1.3.1 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/vagrant-helpers.rb +59 -40
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2e64852305521b99058dc5300e24e1dd43cdfaa8
4
- data.tar.gz: d4e5bb0d69e1db72beade42dca3980d849370172
3
+ metadata.gz: c65a2f2f9790f765452287bd0bbd9f77e2b02616
4
+ data.tar.gz: b458bee7e078148489bb796f10afd4c0340f799b
5
5
  SHA512:
6
- metadata.gz: 0f375f62fbc2ca760bdd5ac6a5be4ad88eb86806ad3915011df5f72a9b4f91f4d8f7ea6730b5574b0ed6a02d85e2dfd983ce6be93487b0416ab3c2a12a961d6b
7
- data.tar.gz: 35383e0f98a2441981c3bc90a305a0d54d6e2d8cf9bcbe018d1226426d55310d76059a39826f3484f59aa72a2ef04f7ec605a6a4b59824b2ec5e3f4976f2a289
6
+ metadata.gz: 4694e1dd2fb3b51bb6ff4ac1fd1e31a286c957013e427c35184bd090377cdc87c047008f3a60dfa063ca620c2c7d03ccf4d0455251c8dd1051dad2197d765bdf
7
+ data.tar.gz: 91f2dfd6bc80cdf25c1c66cb25de4c403508b837d246a62bec241994e8757d5f1493016ecc89aef95a180b18b47f21d56811d2f177717c002162126c085d5182
@@ -25,12 +25,18 @@ module VagrantPlugins
25
25
  end
26
26
  end
27
27
 
28
- class MissingVMNameOptionError < Vagrant::Errors::VagrantError
28
+ class MissingVMNameOptionError < ::Vagrant::Errors::VagrantError
29
29
  def error_message
30
30
  "Missing vm.name option in `opts.yaml` file!"
31
31
  end
32
32
  end
33
33
 
34
+ class AmbiguousConfigurationError < ::Vagrant::Errors::VagrantError
35
+ def error_message
36
+ "Ambiguous configuration found in `opts.yaml` file! Specify either `vm` or `vms` key, not both of them!"
37
+ end
38
+ end
39
+
34
40
  def self.get_opts(dir)
35
41
  filename = ::File.expand_path(ENV['VAGRANT_HELPERS_OPTS'] || 'opts.yaml', dir)
36
42
  if ::File.exists? filename
@@ -40,8 +46,7 @@ module VagrantPlugins
40
46
  end
41
47
  end
42
48
 
43
- def self.set_vm_box(config, opts)
44
- vm_box = opts.fetch('vm', {}).fetch('box', nil)
49
+ def self.set_vm_box(config, vm_box)
45
50
  if vm_box.nil?
46
51
  raise MissingVMBoxOptionError.new
47
52
  end
@@ -49,44 +54,35 @@ module VagrantPlugins
49
54
  config.vm.box = vm_box
50
55
  end
51
56
 
52
- def self.set_vm_name(config, opts)
53
- vm_name = opts.fetch('vm', {}).fetch('name', nil)
57
+ def self.set_vm_name(config, vm_name)
54
58
  if vm_name.nil?
55
59
  raise MissingVMNameOptionError.new
56
60
  end
57
61
 
58
62
  config.vm.provider :virtualbox do |v|
59
- v.name = vm_name
63
+ v.name = vm_name
60
64
  end
61
65
  end
62
66
 
63
- def self.set_vm_memory(config, opts)
64
- vm_memory = opts.fetch('vm', {}).fetch('memory', 512)
65
-
67
+ def self.set_vm_memory(config, vm_memory)
66
68
  config.vm.provider :virtualbox do |v|
67
69
  v.memory = vm_memory
68
70
  end
69
71
  end
70
72
 
71
- def self.set_vm_cpus(config, opts)
72
- vm_cpus = opts.fetch('vm', {}).fetch('cpus', 1)
73
-
73
+ def self.set_vm_cpus(config, vm_cpus)
74
74
  config.vm.provider :virtualbox do |v|
75
75
  v.cpus = vm_cpus
76
76
  end
77
77
  end
78
78
 
79
- def self.set_vm_hostname(config, opts)
80
- vm_hostname = opts.fetch('vm', {}).fetch('hostname', nil)
81
-
79
+ def self.set_vm_hostname(config, vm_hostname)
82
80
  unless vm_hostname.nil?
83
81
  config.vm.hostname = vm_hostname
84
82
  end
85
83
  end
86
84
 
87
- def self.set_vm_forwarded_ports(config, opts)
88
- vm_forwarded_ports = opts.fetch('vm', {}).fetch('network', {}).fetch('forwarded_ports', [])
89
-
85
+ def self.set_vm_forwarded_ports(config, vm_forwarded_ports)
90
86
  vm_forwarded_ports.each do |options|
91
87
  prepared_options = ::Hash[options.map { |(k, v)| [k.to_sym, v] }]
92
88
  config.vm.network :forwarded_port, **prepared_options
@@ -116,9 +112,7 @@ module VagrantPlugins
116
112
  get_host_networks.any? { |host_network| host_network.eql? network_addr }
117
113
  end
118
114
 
119
- def self.set_vm_public_networks(config, opts)
120
- vm_public_networks = opts.fetch('vm', {}).fetch('network', {}).fetch('public', [])
121
-
115
+ def self.set_vm_public_networks(config, vm_public_networks)
122
116
  network_list = []
123
117
 
124
118
  vm_public_networks.each do |options|
@@ -139,27 +133,21 @@ module VagrantPlugins
139
133
  end
140
134
  end
141
135
 
142
- def self.set_vm_private_networks(config, opts)
143
- vm_private_networks = opts.fetch('vm', {}).fetch('network', {}).fetch('private', [])
144
-
136
+ def self.set_vm_private_networks(config, vm_private_networks)
145
137
  vm_private_networks.each do |options|
146
138
  prepared_options = ::Hash[options.map { |(k, v)| [k.to_sym, v] }]
147
139
  config.vm.network :private_network, **prepared_options
148
140
  end
149
141
  end
150
142
 
151
- def self.set_vm_synced_folders(config, opts)
152
- vm_synced_folders = opts.fetch('vm', {}).fetch('synced_folders', [])
153
-
143
+ def self.set_vm_synced_folders(config, vm_synced_folders)
154
144
  vm_synced_folders.each do |entry|
155
145
  prepared_options = ::Hash[entry.fetch('opts', {}).map { |(k,v)| [k.to_sym, v] }]
156
146
  config.vm.synced_folder entry['host'], entry['guest'], **prepared_options
157
147
  end
158
148
  end
159
149
 
160
- def self.set_vm_extra_storage(config, opts)
161
- vm_storage_drives = opts.fetch('vm', {}).fetch('storage', [])
162
-
150
+ def self.set_vm_extra_storage(config, vm_storage_drives)
163
151
  config.vm.provider :virtualbox do |v|
164
152
  vm_storage_drives.each_with_index do |entry, ndx|
165
153
  unless ::File.exists? entry['filename']
@@ -192,6 +180,36 @@ module VagrantPlugins
192
180
  end
193
181
  end
194
182
 
183
+ def self.each_vm(opts)
184
+ if opts.has_key?('vm') and opts.has_key?('vms')
185
+ raise AmbiguousConfigurationError.new
186
+ end
187
+
188
+ if opts.has_key? 'vm'
189
+ vm_opts = opts['vm']
190
+ yield nil, vm_opts
191
+ end
192
+
193
+ if opts.has_key? 'vms'
194
+ opts['vms'].each do |name, vm_opts|
195
+ yield name, vm_opts
196
+ end
197
+ end
198
+ end
199
+
200
+ def self.setup_instance(config, vm_opts)
201
+ set_vm_box config, vm_opts.fetch('box', nil)
202
+ set_vm_name config, vm_opts.fetch('name', nil)
203
+ set_vm_memory config, vm_opts.fetch('memory', 512)
204
+ set_vm_cpus config, vm_opts.fetch('cpus', 1)
205
+ set_vm_hostname config, vm_opts.fetch('hostname', nil)
206
+ set_vm_forwarded_ports config, vm_opts.fetch('network', {}).fetch('forwarded_ports', [])
207
+ set_vm_public_networks config, vm_opts.fetch('network', {}).fetch('public', [])
208
+ set_vm_private_networks config, vm_opts.fetch('network', {}).fetch('private', [])
209
+ set_vm_synced_folders config, vm_opts.fetch('synced_folders', [])
210
+ set_vm_extra_storage config, vm_opts.fetch('storage', [])
211
+ end
212
+
195
213
  def self.setup(dir)
196
214
  dotenv_filename = ::File.join dir, '.env'
197
215
  ::Dotenv.load
@@ -199,16 +217,17 @@ module VagrantPlugins
199
217
  ::Vagrant.configure(2) do |config|
200
218
  opts = get_opts dir
201
219
 
202
- set_vm_box config, opts
203
- set_vm_name config, opts
204
- set_vm_memory config, opts
205
- set_vm_cpus config, opts
206
- set_vm_hostname config, opts
207
- set_vm_forwarded_ports config, opts
208
- set_vm_public_networks config, opts
209
- set_vm_private_networks config, opts
210
- set_vm_synced_folders config, opts
211
- set_vm_extra_storage config, opts
220
+ each_vm(opts) do |name, vm_opts|
221
+ if name.nil?
222
+ # there is one instance only
223
+ setup_instance config, vm_opts
224
+ else
225
+ # there are several instances
226
+ config.vm.define name do |instance_config|
227
+ setup_instance instance_config, vm_opts
228
+ end
229
+ end
230
+ end
212
231
  end
213
232
  end
214
233
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vagrant-helpers
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.1
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexander Pyatkin
@@ -93,7 +93,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
93
93
  version: '0'
94
94
  requirements: []
95
95
  rubyforge_project:
96
- rubygems_version: 2.4.5.1
96
+ rubygems_version: 2.5.1
97
97
  signing_key:
98
98
  specification_version: 4
99
99
  summary: Vagrant helpers