forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vagrant_ros_guest_plugin.rb
73 lines (63 loc) · 2.36 KB
/
vagrant_ros_guest_plugin.rb
1
2
3
4
5
6
7
8
9
10
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
require 'ipaddr'
## Hacking this until we get a real plugin
# Borrowing from http://stackoverflow.com/questions/1825928/netmask-to-cidr-in-ruby
IPAddr.class_eval do
def to_cidr
self.to_i.to_s(2).count("1")
end
end
module VagrantPlugins
module GuestLinux
class Plugin < Vagrant.plugin("2")
guest_capability("linux", "change_host_name") do
Cap::ChangeHostName
end
guest_capability("linux", "configure_networks") do
Cap::ConfigureNetworks
end
end
end
end
module VagrantPlugins
module GuestLinux
module Cap
class ConfigureNetworks
def self.configure_networks(machine, networks)
machine.communicate.tap do |comm|
interfaces = []
comm.sudo("ip link show|grep eth[1-9]|awk '{print $2}'|sed -e 's/:$//'") do |_, result|
interfaces = result.split("\n")
end
networks.each do |network|
dhcp = "true"
iface = interfaces[network[:interface].to_i - 1]
if network[:type] == :static
cidr = IPAddr.new(network[:netmask]).to_cidr
comm.sudo("ros config set rancher.network.interfaces.#{iface}.address #{network[:ip]}/#{cidr}")
comm.sudo("ros config set rancher.network.interfaces.#{iface}.match #{iface}")
dhcp = "false"
end
comm.sudo("ros config set rancher.network.interfaces.#{iface}.dhcp #{dhcp}")
end
comm.sudo("system-docker restart network")
end
end
end
end
end
end
module VagrantPlugins
module GuestLinux
module Cap
class ChangeHostName
def self.change_host_name(machine, name)
machine.communicate.tap do |comm|
if !comm.test("sudo hostname --fqdn | grep '#{name}'")
comm.sudo("hostname #{name.split('.')[0]}")
end
end
end
end
end
end
end