vagrant-port-range 0.1.3 → 0.1.4

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fbb290ba667171dfe779bd0bd7c0256f8f86bbcc
4
- data.tar.gz: 171cdf86d4169bf466fd15c043f226c9143ff566
3
+ metadata.gz: 5ea234f007fbdd768c4d153ed0a384183d5c9dc1
4
+ data.tar.gz: 0eee4fa26cd279dd3976297cd2c8eece9f1c1f4b
5
5
  SHA512:
6
- metadata.gz: 2ad740655459d9fc65c97b441a9e199c8c6b8a6d2d59b93b1f5b79d854d479718ac73da7f52dcb8130e67d727c7984d0ebc4f3a86683b6e174a784d25c44e7cc
7
- data.tar.gz: d0144c2850a0af1608b7ad62efe12c198046914975df4c3bc39df5b737832c2160ad54f46f11a30abf6034b676337eec9460bb8c2ca187ac9b85ce57e1d5486f
6
+ metadata.gz: 5f72ed60d29cd2c577abd091d89adcce449a45d02e04b0daa94794731c0cef3c9e9a9d97610fc373cad47f4adc29e2d704edee76b60da400cdc632ddbedb51a7
7
+ data.tar.gz: '0328eebddead9d8a5fe67d514630e9de2c99b893738e24994f590d411828a9eb85cb61d6ee379fb1ce2e86cea43785f67941efa6a858e8d4a7282f9bee6b2292'
@@ -4,11 +4,8 @@ require 'vagrant-port-range/plugin'
4
4
  module VagrantPlugins
5
5
  module PortRange
6
6
  lib_path = Pathname.new(File.expand_path('../vagrant-port-range', __FILE__))
7
- autoload :Action, lib_path.join('action')
8
7
 
9
- # This returns the path to the source of this plugin
10
- #
11
- # @return [Pathname]
8
+ # Returns the path to the source of this plugins
12
9
  def self.source_root
13
10
  @source_root ||= Pathname.new(File.expand_path('../../', __FILE__))
14
11
  end
@@ -3,7 +3,6 @@ require 'socket'
3
3
  module VagrantPlugins
4
4
  module PortRange
5
5
  module Action
6
-
7
6
  class SetupPorts
8
7
  def initialize(app, env)
9
8
  @app = app
@@ -17,8 +16,19 @@ module VagrantPlugins
17
16
  # setup forwarded ports
18
17
  forwarded_ports = @machine.config.portrange.forwarded_ports
19
18
  forwarded_ports.each do |id, options|
19
+ if !options[:host_range]
20
+ @ui.error "[vagrant-port-range] Range of host ports not specified ('host_range' option)"
21
+ setupped = false
22
+ break
23
+ end
24
+ if !options[:attempts]
25
+ @ui.error "[vagrant-port-range] Number of attempts not specified ('attempts' option)"
26
+ setupped = false
27
+ break
28
+ end
29
+
20
30
  # get free port and add to host
21
- host_port = get_free_port(options[:host_range])
31
+ host_port = try_find_free_port(options[:host_range], options[:attempts])
22
32
 
23
33
  if host_port == -1
24
34
  setupped = false
@@ -26,10 +36,10 @@ module VagrantPlugins
26
36
  end
27
37
 
28
38
  options[:host] = host_port
29
- @ui.info("[vagrant-port-range] get free port #{host_port}")
39
+ @ui.info("[vagrant-port-range] Free port found: #{host_port}")
30
40
 
31
- # delete port range option
32
- options.tap { |op| op.delete(:host_range) }
41
+ # delete our options
42
+ options.tap { |op| op.delete(:host_range); op.delete(:attempts); op.delete(:id); }
33
43
 
34
44
  # standart way to add forwarded_port
35
45
  @machine.config.vm.network "forwarded_port", options
@@ -41,12 +51,19 @@ module VagrantPlugins
41
51
  end
42
52
  end
43
53
 
44
- def get_free_port(port_range)
54
+ # Trys to find free port in specified range
55
+ # Params:
56
+ # port_range - range of ports (bounds included)
57
+ # max_attempts - maximum number of attempts to try
58
+ # Returns:
59
+ # [positive number] - free port
60
+ # -1 - maximum number of attempts ended unsuccessfully
61
+ def try_find_free_port(port_range, max_attempts)
45
62
  min = port_range[0]
46
63
  max = port_range[1]
47
64
 
48
65
  if min >= max
49
- @ui.error("[vagrant-port-range] Incorrect host_range option #{port_range.inspect}")
66
+ @ui.error("[vagrant-port-range] Incorrect range of host ports #{port_range.inspect}")
50
67
  return -1
51
68
  end
52
69
 
@@ -55,8 +72,8 @@ module VagrantPlugins
55
72
 
56
73
  begin
57
74
  attempts = attempts + 1
58
- if attempts > 10
59
- @ui.error "[vagrant-port-range] Can't get free port from range #{port_range.inspect}"
75
+ if attempts > max_attempts
76
+ @ui.error "[vagrant-port-range] Free port from range #{port_range.inspect} not found"
60
77
  return -1
61
78
  end
62
79
 
@@ -68,9 +85,7 @@ module VagrantPlugins
68
85
  retry
69
86
  end
70
87
  end
71
-
72
88
  end
73
-
74
89
  end
75
90
  end
76
91
  end
@@ -4,14 +4,6 @@ rescue LoadError
4
4
  raise 'This plugin must run within Vagrant.'
5
5
  end
6
6
 
7
- # This is a sanity check to make sure no one is attempting to install
8
- # this into an early Vagrant version.
9
- if Vagrant::VERSION < '1.2.0'
10
- raise 'The vagrant-port-range plugin is only compatible with Vagrant 1.2+'
11
- end
12
-
13
- require_relative 'action/port_range'
14
-
15
7
  module VagrantPlugins
16
8
  module PortRange
17
9
  class Plugin < Vagrant.plugin('2')
@@ -28,6 +20,7 @@ module VagrantPlugins
28
20
  end
29
21
 
30
22
  action_hook(:portrange, :machine_action_up) do |hook|
23
+ require_relative 'action/port_range'
31
24
  hook.prepend(Action::SetupPorts)
32
25
  end
33
26
  end
@@ -1,5 +1,5 @@
1
1
  module VagrantPlugins
2
2
  module PortRange
3
- VERSION = '0.1.3'
3
+ VERSION = '0.1.4'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vagrant-port-range
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexey Chirukhin