Class: VagrantTun::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-tun/command.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ Command

Returns a new instance of Command.



4
5
6
7
# File 'lib/vagrant-tun/command.rb', line 4

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

Instance Method Details

#call(env) ⇒ Object



9
10
11
12
13
14
# File 'lib/vagrant-tun/command.rb', line 9

def call(env)
  @app.call(env)
  if env[:machine].config.tun.enabled
    ensure_tun_available(env)
  end
end

#ensure_tun_available(env) ⇒ Object



77
78
79
80
# File 'lib/vagrant-tun/command.rb', line 77

def ensure_tun_available(env)
  success = iter_verify_adapter(env)
  log_success_or_fail_message(env, success)
end

#iter_verify_adapter(env) ⇒ Object

Try all strategies to get the TUN adapter in an available state



60
61
62
63
64
65
66
67
# File 'lib/vagrant-tun/command.rb', line 60

def iter_verify_adapter(env)
  success = verify_adapter(env, true)
  if ! success
    reboot(env)
    success = verify_adapter(env, true)
  end
  return success
end

#log_success_or_fail_message(env, success) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/vagrant-tun/command.rb', line 69

def log_success_or_fail_message(env, success)
  if success
    env[:ui].info("Ensured the TUN module is loaded into the kernel.")
  else
    env[:ui].error("Failed to load the TUN/TAP adapter. If you are running a custom kernel make sure you have the tun module enabled.")
  end
end

#reboot(env) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/vagrant-tun/command.rb', line 82

def reboot(env)
  env[:ui].info("Rebooting because we couldn't load the tun module. Maybe the kernel was updated?")
  env[:machine].action(:reload, :provision_enabled => false)
  begin
    sleep 1
  end until env[:machine].communicate.ready?
end

#try_create_adapter(env) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/vagrant-tun/command.rb', line 23

def try_create_adapter(env)
  env[:ui].info("Trying to create TUN adapter")
  result = false
  create_adapter_command = '(mkdir -p /dev/net && '
  create_adapter_command << 'mknod /dev/net/tun c 10 200 && '
  create_adapter_command << 'chmod 0666 /dev/net/tun) 2>/dev/null'
  env[:machine].communicate.sudo(create_adapter_command, {:error_check => false}) do |type, data|
    result = verify_adapter(env, false)
    if ! result
      result = try_load_tun_kernel_module(env)
    end
  end
  return result
end

#try_load_tun_kernel_module(env) ⇒ Object



16
17
18
19
20
21
# File 'lib/vagrant-tun/command.rb', line 16

def try_load_tun_kernel_module(env)
  load_tun_module_command = "modprobe tun"
  env[:machine].communicate.sudo(load_tun_module_command, {:error_check => false}) do |type, data|
    return verify_adapter(env, false)
  end
end

#verify_adapter(env, try_create) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/vagrant-tun/command.rb', line 38

def verify_adapter(env, try_create)
  env[:ui].info("Verifying TUN adapter..")
  result = false
  env[:machine].communicate.sudo('cat /dev/net/tun', {:error_check => false}) do |type, data|
    adapter_state = data.to_s.strip
    case adapter_state
    when /\bFile descriptor in bad state\b/
      env[:ui].info("TUN adapter OK!")
      result = true
    when /\bNo such file or directory\b/
      if try_create
        env[:ui].info("TUN adapter not OK :(")
        result = try_create_adapter(env)
      else
        result = false
      end
    end
  end
  return result
end