Class: Engineyard::Local::Middleware::Network

Inherits:
Object
  • Object
show all
Includes:
Helpers::Network
Defined in:
lib/engineyard-local/middleware/network.rb

Instance Method Summary collapse

Methods included from Helpers::Network

#networks, #proposed_ip

Constructor Details

#initialize(app, env, options) ⇒ Network

Returns a new instance of Network.



7
8
9
# File 'lib/engineyard-local/middleware/network.rb', line 7

def initialize(app, env, options)
  @app, @env, @options = app, env, options
end

Instance Method Details

#call(env) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/engineyard-local/middleware/network.rb', line 11

def call(env)
  Virtualbox.uuid_map.each do |name, uuid|
    # skip the current vm
    next if uuid == env[:vm].uuid

    # get the configured ip from the tag data stored by ey-local
    existing_ip = Virtualbox.extra_data(uuid, Local.config[:network_ip_key])

    # if the proposed ip matches a different vms tagged ip
    # let the user decide if this is intended or needs to be fixed
    if(!@options[:silent] && existing_ip == proposed_ip)
      while !["Y","y","N","n"].include?(result ||= nil)
        result = check_with_user(name)
      end

      if result.upcase == "N"
        raise(Errors::PossibleIPCollision,
              :proposed_ip => proposed_ip,
              :vm_name => name)
      end

      if result.upcase == "Y"
        break
      end
    end
  end

  @app.call(env)
end

#check_with_user(name) ⇒ Object



41
42
43
# File 'lib/engineyard-local/middleware/network.rb', line 41

def check_with_user(name)
   @env[:ui].ask(prompt(name), :color => Thor::Shell::Color::YELLOW)
end

#prompt(name) ⇒ Object



45
46
47
48
49
# File 'lib/engineyard-local/middleware/network.rb', line 45

def prompt(name)
  @prompt ||= I18n.t("eylocal.actions.network.prompt",
                     :vm_name => name,
                     :proposed_ip => proposed_ip)
end

#recover(env) ⇒ Object

taken directly from Vagrant source



52
53
54
55
56
57
58
59
60
# File 'lib/engineyard-local/middleware/network.rb', line 52

def recover(env)
  if env[:vm].created? && env["vagrant.error"].is_a?(Errors::PossibleIPCollision)
    # Interrupted, destroy the VM. We note that we don't want to
    # validate the configuration here.
    destroy_env = env.clone
    destroy_env[:validate] = false
    env[:action_runner].run(:destroy, destroy_env)
  end
end