Top Level Namespace

Defined Under Namespace

Modules: Put, SubutaiConfig, SubutaiDisk, SubutaiValidation, VagrantSubutai

Constant Summary collapse

PROVIDERS =

Most hypervisors uses a mac prefix: we cannot just generate a random mac

%i[:virtualbox :libvirt :vmware_fusion :vmware :parallels :hyper_v]
PROVIDER_MAC_PREFIXES =
{
    :virtualbox       => '080027',
    :libvirt          => '525400',
    :vmware_fusion    => '______',
    :vmware           => '005056',
    :parallels        => '001c42',
    :hyper_v          => '0003ff'
}

Instance Method Summary collapse

Instance Method Details

#arp_tableObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/vagrant-subutai/packer/subutai_net.rb', line 72

def arp_table
  broadcast_ping(2)
  arp_table = {}

  if RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin|darwin/
    `arp -a`.split("\n").each do |line|
      matches = /.*(\d+\.\d+\.\d+.\d+)[[:space:]]((([a-f]|[0-9]){1,2}:){5}([a-f]|[0-9]){1,2})[[:space:]].*/.match(line) \
      if (RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/)
      matches = /.*\((\d+\.\d+\.\d+.\d+)\) at ((([a-f]|[0-9]){1,2}:){5}([a-f]|[0-9]){1,2}) .*/.match(line) \
      if (RbConfig::CONFIG['host_os'] =~ /darwin/)

      if ! matches.nil? && ! matches[2].nil?
        key_mac = zero_pad_mac(matches[2])
        value_ip = matches[1]
        arp_table.store(key_mac, value_ip)
      end
    end
  elsif RbConfig::CONFIG['host_os'] =~ /linux|bsd/
    `ip n | awk '{print $1,$5}'`.split("\n").each do |line|
      ip, mac = line.split(' ')

      if ! line.nil? && ! mac.nil?

        key_mac = zero_pad_mac(mac)
        value_ip = ip
        arp_table.store(key_mac, value_ip)
      end
    end
  end

  arp_table
end

#broadcast_addrObject



45
46
47
48
49
50
51
52
53
54
# File 'lib/vagrant-subutai/packer/subutai_net.rb', line 45

def broadcast_addr
  octets = `route print 0.0.0.0 | findstr 0.0.0.0`.split(' ').map(&:strip)[2].split('.')    \
    if (RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/)
  octets = `ip route show | grep default | awk '{print $3}'`.gsub(/\s+/, "").split(".")     \
    if (RbConfig::CONFIG['host_os'] =~ /linux/)
  octets = `route -n get default | grep gateway | awk '{print $2}'`.gsub(/\s+/, "").split(".") \
    if (RbConfig::CONFIG['host_os'] =~ /darwin/)
  octets[3] = 255
  octets.join('.')
end

#broadcast_ping(count) ⇒ Object



56
57
58
59
60
61
# File 'lib/vagrant-subutai/packer/subutai_net.rb', line 56

def broadcast_ping(count)
  system("ping -n #{count} #{broadcast_addr} >/dev/null")   \
    if (RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/)
  system("ping -c #{count} #{broadcast_addr} >/dev/null")   \
    if (RbConfig::CONFIG['host_os'] =~ /darwin|linux/)
end

#find_mac(provider) ⇒ Object



110
111
112
113
114
115
116
# File 'lib/vagrant-subutai/packer/subutai_net.rb', line 110

def find_mac(provider)
  mac = random_mac_addr(provider)
  until mac_uniq?(mac) do
    mac = random_mac_addr(provider)
  end
  mac
end

#find_port(port) ⇒ Object

Finds free port for console and puts in wormstore




24
25
26
27
# File 'lib/vagrant-subutai/packer/subutai_net.rb', line 24

def find_port(port)
  port += 1 while port_bound?('127.0.0.1', port)
  port
end

#hook_handler(hook) ⇒ Object

Handle hook scripts or normal files: returns path to file to provision




15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/vagrant-subutai/packer/subutai_hooks.rb', line 15

def hook_handler(hook)
  # hook might be the upload file or an executable to give the path to the
  # file to be provisioned. Builders can use this to hook into the
  # provisioning process for new management templates.
  unless File.executable?(hook)
    puts '[WARNING] hook script ' + hook + ' not executable. Abandoning launch.'
    return nil
  end

  fext = File.extname(hook)
  if fext == '.sh' && shebang?(hook)
    `#{hook}`.strip # executes and returns output (should be file)
  # elsif fext == '.bat'
  #   return `#{hook}`.strip # executes and returns output (should be file)
  else
    # raise '[WARNING] hook script not valid bash .sh or .bat. Abandoning launch.'
    raise '[WARNING] hook script not valid bash file with .sh extension. Abandoning launch.'
  end
end

#mac_uniq?(mac) ⇒ Boolean

Returns:

  • (Boolean)


105
106
107
108
# File 'lib/vagrant-subutai/packer/subutai_net.rb', line 105

def mac_uniq?(mac)
  arptab = arp_table
  arptab[mac].nil?
end

#management_handler(hook) ⇒ Object

if arg is nil check for presence of management_hook.$ext




40
41
42
43
44
45
46
47
48
49
50
# File 'lib/vagrant-subutai/packer/subutai_hooks.rb', line 40

def management_handler(hook)
  if hook.nil?
    %w[.sh .bat].each do |e|
      if File.exist?('./management_hook' + e)
        return hook_handler('management_hook' + e)
      end
    end
    return nil
  end
  hook_handler(hook)
end

#port_bound?(host, port) ⇒ Boolean

Checks if a port is not available


Returns:

  • (Boolean)


10
11
12
13
14
15
16
17
18
19
20
# File 'lib/vagrant-subutai/packer/subutai_net.rb', line 10

def port_bound?(host, port)
  Timeout.timeout(1) do
    s = TCPSocket.new(host, port)
    s.close rescue nil
    return true
  end
rescue Timeout::Error, Errno::ECONNREFUSED, Errno::EHOSTUNREACH, \
       Errno::ENETUNREACH, Errno::EACCES, Errno::ENOTCONN, \
       Errno::EADDRNOTAVAIL
  return false
end

#random_mac_addr(provider) ⇒ Object

Generate a random mac address that works with the hypervisor of a provider



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/vagrant-subutai/packer/subutai_net.rb', line 119

def random_mac_addr(provider)
  symbol = provider.to_sym
  case symbol
    when :virtualbox
      PROVIDER_MAC_PREFIXES[:virtualbox] + 3.times.map { '%02x' % rand(0..255) }.join
    when :libvirt
      PROVIDER_MAC_PREFIXES[:libvirt] + 3.times.map { '%02x' % rand(0..255) }.join
    when :vmware_fusion
       PROVIDER_MAC_PREFIXES[:vmware] + 3.times.map { '%02x' % rand(0..255) }.join
    when :vmware
      PROVIDER_MAC_PREFIXES[:vmware] + 3.times.map { '%02x' % rand(0..255) }.join
    when :parallels
      PROVIDER_MAC_PREFIXES[:parallels] + 3.times.map { '%02x' % rand(0..255) }.join
    when :hyper_v
      PROVIDER_MAC_PREFIXES[:hyper_v] + 3.times.map { '%02x' % rand(0..255) }.join
    else
      raise "Unsupported provider #{provider}"
  end
end

#shebang?(bash_hook) ⇒ Boolean

True if script starts with #!/bin/bash


Returns:

  • (Boolean)


5
6
7
8
9
10
11
# File 'lib/vagrant-subutai/packer/subutai_hooks.rb', line 5

def shebang?(bash_hook)
  text = File.open(bash_hook).read
  text.gsub!(/\r\n?/, "\n")
  line = text.next
  return true if line.start_with? '#!/bin/bash'
  true if line.start_with? '#!/usr/bin/env bash'
end

#zero_pad_mac(mac) ⇒ Object



63
64
65
66
67
68
69
70
# File 'lib/vagrant-subutai/packer/subutai_net.rb', line 63

def zero_pad_mac(mac)
  reassembled = ''
  mac.split(':').each do |octet|
    octet = '0' + octet if octet.length == 1
    reassembled += octet
  end
  reassembled
end