vagrant-guests-photon 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bd4c53f5252dcca41018c65180fdcd71c60a57ca
4
- data.tar.gz: 41f24c6616adfccca687a5725efc1a3b8ca799d8
3
+ metadata.gz: 78a2f47f2f4da91108b4e1a202da0f3006f6f08b
4
+ data.tar.gz: 0085fd4d34bdda428f9129adc791b2af45dee6a6
5
5
  SHA512:
6
- metadata.gz: b2bd905bcc1d184e23ad3cfee9ba9c4899637d3129fe3413383e9070b65c4cd80ae16e6db3a6b3d98f2a288a8b26e09fe4f0aaf062e479f82557de780f515428
7
- data.tar.gz: faf4b0a58521adb7a4a9e1dca8c6d4884f6efb692f071b3b52839b2161ac901b89f41002bc04184f6d289932622ee930624c6f66f8206534c5a30c8954b8a6e9
6
+ metadata.gz: d628f32f1edb4319b9246090a1e1c7590b6188a1faab21bf83e9327779846424dfdabfcbea4c2e743ae6b0e91bbe56b0c8e69469e6c4d6ec5aa942d2b617e82f
7
+ data.tar.gz: 721336c56c357163a9ca5b7a05af9d31def65f32057561f2ec5ca127e1fe780c3fbebea760d7adcb89193f68658d99a21ecfc2926ff1ea39ca2570b1b4db2dd5
data/README.md CHANGED
@@ -14,11 +14,11 @@ To build and install the plugin directly from this repo:
14
14
 
15
15
  ```
16
16
  $ rake build
17
- $ vagrant plugin install pkg/vagrant-guests-photon-1.0.0.gem
17
+ $ vagrant plugin install pkg/vagrant-guests-photon-1.0.1.gem
18
18
  ```
19
19
 
20
20
  You can run RSpec with:
21
21
 
22
22
  ```
23
23
  $ rake
24
- ```
24
+ ```
@@ -2,41 +2,80 @@
2
2
  # Copyright (c) 2015 VMware, Inc. All Rights Reserved.
3
3
 
4
4
  require 'tempfile'
5
- require 'vagrant/util/template_renderer'
5
+ require 'ipaddr'
6
+ require 'log4r'
7
+
8
+ # Borrowed from http://stackoverflow.com/questions/1825928/netmask-to-cidr-in-ruby
9
+ IPAddr.class_eval do
10
+ def to_cidr
11
+ self.to_i.to_s(2).count("1")
12
+ end
13
+ end
14
+
15
+ STATIC_NETWORK = <<EOF
16
+ [Match]
17
+ Name=%s
18
+
19
+ [Network]
20
+ Address=%s
21
+ EOF
22
+
23
+ DHCP_NETWORK = <<EOF
24
+ [Match]
25
+ Name=%s
26
+
27
+ [Network]
28
+ DHCP=yes
29
+ EOF
6
30
 
7
31
  module VagrantPlugins
8
32
  module GuestPhoton
9
33
  module Cap
10
34
  class ConfigureNetworks
11
- include Vagrant::Util
35
+ @@logger = Log4r::Logger.new("vagrant::guest::photon::configure_networks")
12
36
 
13
37
  def self.configure_networks(machine, networks)
14
38
  machine.communicate.tap do |comm|
39
+ comm.sudo("rm -f /etc/systemd/network/50-vagrant-*.network")
40
+
15
41
  # Read network interface names
16
42
  interfaces = []
17
- comm.sudo("ifconfig | grep 'eth' | cut -f1 -d' '") do |_, result|
43
+ comm.sudo("ifconfig -a | grep '^eth' | cut -f1 -d' '") do |_, result|
18
44
  interfaces = result.split("\n")
19
45
  end
20
46
 
21
47
  # Configure interfaces
22
48
  networks.each do |network|
23
- comm.sudo("ifconfig #{interfaces[network[:interface].to_i]} #{network[:ip]} netmask #{network[:netmask]}")
24
- end
49
+ interface = network[:interface].to_i
50
+
51
+ iface = interfaces[interface]
52
+ if iface.nil?
53
+ @@logger.warn("Could not find match rule for network #{network.inspect}")
54
+ next
55
+ end
25
56
 
26
- primary_machine_config = machine.env.active_machines.first
27
- primary_machine = machine.env.machine(*primary_machine_config, true)
57
+ unit_name = "50-vagrant-%s.network" % [iface]
28
58
 
29
- get_ip = lambda do |machine|
30
- ip = nil
31
- machine.config.vm.networks.each do |type, opts|
32
- if type == :private_network && opts[:ip]
33
- ip = opts[:ip]
34
- break
35
- end
59
+ if network[:type] == :static
60
+ cidr = IPAddr.new(network[:netmask]).to_cidr
61
+ address = "%s/%s" % [network[:ip], cidr]
62
+ unit_file = STATIC_NETWORK % [iface, address]
63
+ elsif network[:type] == :dhcp
64
+ unit_file = DHCP_NETWORK % [iface]
36
65
  end
37
66
 
38
- ip
67
+ temp = Tempfile.new("vagrant")
68
+ temp.binmode
69
+ temp.write(unit_file)
70
+ temp.close
71
+
72
+ comm.upload(temp.path, "/tmp/#{unit_name}")
73
+ comm.sudo("mv /tmp/#{unit_name} /etc/systemd/network/")
74
+ comm.sudo("chown root:root /etc/systemd/network/#{unit_name}")
75
+ comm.sudo("chmod +r /etc/systemd/network/#{unit_name}")
39
76
  end
77
+
78
+ comm.sudo("systemctl restart systemd-networkd.service")
40
79
  end
41
80
  end
42
81
  end
@@ -4,6 +4,6 @@
4
4
  module VagrantPlugins
5
5
  # Set version for vagrant-guests-photon gem.
6
6
  module GuestPhoton
7
- VERSION = '1.0.0'
7
+ VERSION = '1.0.1'
8
8
  end
9
9
  end
@@ -7,23 +7,171 @@ require 'spec_helper'
7
7
  describe VagrantPlugins::GuestPhoton::Cap::ConfigureNetworks do
8
8
  include_context 'machine'
9
9
 
10
- it 'should configure networks' do
10
+ context 'configure 4 kinds of networks' do
11
11
  networks = [
12
- { :type => :static, :ip => '192.168.10.10', :netmask => '255.255.255.0', :interface => 1, :name => 'eth0' },
13
- { :type => :dhcp, :interface => 2, :name => 'eth1' },
14
- { :type => :static, :ip => '10.168.10.10', :netmask => '255.255.0.0', :interface => 3, :name => 'docker0' }
12
+ # config.vm.network :private_network, ip: "192.168.33.10"
13
+ {
14
+ :type => :static,
15
+ :adapter_ip => '192.168.10.1',
16
+ :ip => '192.168.10.10',
17
+ :netmask => '255.255.255.0',
18
+ :auto_config => true,
19
+ :interface => 1
20
+ },
21
+ # config.vm.network :private_network, type: "dhcp"
22
+ {
23
+ :type => :dhcp,
24
+ :adapter_ip => "172.28.128.1",
25
+ :ip => "172.28.128.1",
26
+ :netmask => "255.255.255.0",
27
+ :auto_config => true,
28
+ :interface => 2
29
+ },
30
+ # config.vm.network :public_network, bridge: "en0: Wi-Fi (AirPort)"
31
+ {
32
+ :type => :dhcp,
33
+ :use_dhcp_assigned_default_route => false,
34
+ :auto_config => true,
35
+ :interface => 3
36
+ },
37
+ # config.vm.network :public_network, bridge: "en0: Wi-Fi (AirPort)", ip: "192.168.1.201"
38
+ {
39
+ :type=>:static,
40
+ :bridge => "en0: Wi-Fi (AirPort)",
41
+ :ip => "192.168.1.201",
42
+ :netmask => "255.255.255.0",
43
+ :use_dhcp_assigned_default_route => false,
44
+ :auto_config => true,
45
+ :interface => 4
46
+ }
15
47
  ]
16
- communicate.should_receive(:sudo).with("ifconfig | grep 'eth' | cut -f1 -d' '")
17
- communicate.should_receive(:sudo).with('ifconfig 192.168.10.10 netmask 255.255.255.0')
18
- communicate.should_receive(:sudo).with('ifconfig netmask ')
19
- communicate.should_receive(:sudo).with('ifconfig 10.168.10.10 netmask 255.255.0.0')
20
-
21
- allow_message_expectations_on_nil
22
- machine.should_receive(:env).at_least(5).times
23
- machine.env.should_receive(:active_machines).at_least(:twice)
24
- machine.env.active_machines.should_receive(:first)
25
- machine.env.should_receive(:machine)
26
-
27
- described_class.configure_networks(machine, networks)
48
+
49
+ interfaces = "eth0\neth1\neth2\neth3\neth4\n"
50
+
51
+ before do
52
+ communicate.stub(:sudo).with("ifconfig -a | grep '^eth' | cut -f1 -d' '")
53
+ .and_yield(nil, interfaces)
54
+ end
55
+
56
+ it 'should configure networks' do
57
+ communicate.should_receive(:sudo).with("rm -f /etc/systemd/network/50-vagrant-*.network")
58
+ communicate.should_receive(:sudo).with("ifconfig -a | grep '^eth' | cut -f1 -d' '")
59
+
60
+ # eth1
61
+ communicate.should_receive(:upload) do |src, dst|
62
+ contents = (File.readlines src).join("")
63
+ contents.should eq "[Match]\nName=eth1\n\n[Network]\nAddress=192.168.10.10/24\n"
64
+ dst.should eq "/tmp/50-vagrant-eth1.network"
65
+ end
66
+ communicate.should_receive(:sudo)
67
+ .with("mv /tmp/50-vagrant-eth1.network /etc/systemd/network/")
68
+ communicate.should_receive(:sudo)
69
+ .with("chown root:root /etc/systemd/network/50-vagrant-eth1.network")
70
+ communicate.should_receive(:sudo)
71
+ .with("chmod +r /etc/systemd/network/50-vagrant-eth1.network")
72
+
73
+ # eth2
74
+ communicate.should_receive(:upload) do |src, dst|
75
+ contents = (File.readlines src).join("")
76
+ contents.should eq "[Match]\nName=eth2\n\n[Network]\nDHCP=yes\n"
77
+ dst.should eq "/tmp/50-vagrant-eth2.network"
78
+ end
79
+ communicate.should_receive(:sudo)
80
+ .with("mv /tmp/50-vagrant-eth2.network /etc/systemd/network/")
81
+ communicate.should_receive(:sudo)
82
+ .with("chown root:root /etc/systemd/network/50-vagrant-eth2.network")
83
+ communicate.should_receive(:sudo)
84
+ .with("chmod +r /etc/systemd/network/50-vagrant-eth2.network")
85
+
86
+ # eth3
87
+ communicate.should_receive(:upload) do |src, dst|
88
+ contents = (File.readlines src).join("")
89
+ contents.should eq "[Match]\nName=eth3\n\n[Network]\nDHCP=yes\n"
90
+ dst.should eq "/tmp/50-vagrant-eth3.network"
91
+ end
92
+ communicate.should_receive(:sudo)
93
+ .with("mv /tmp/50-vagrant-eth3.network /etc/systemd/network/")
94
+ communicate.should_receive(:sudo)
95
+ .with("chown root:root /etc/systemd/network/50-vagrant-eth3.network")
96
+ communicate.should_receive(:sudo)
97
+ .with("chmod +r /etc/systemd/network/50-vagrant-eth3.network")
98
+
99
+ # eth4
100
+ communicate.should_receive(:upload) do |src, dst|
101
+ contents = (File.readlines src).join("")
102
+ contents.should eq "[Match]\nName=eth4\n\n[Network]\nAddress=192.168.1.201/24\n"
103
+ dst.should eq "/tmp/50-vagrant-eth4.network"
104
+ end
105
+ communicate.should_receive(:sudo)
106
+ .with("mv /tmp/50-vagrant-eth4.network /etc/systemd/network/")
107
+ communicate.should_receive(:sudo)
108
+ .with("chown root:root /etc/systemd/network/50-vagrant-eth4.network")
109
+ communicate.should_receive(:sudo)
110
+ .with("chmod +r /etc/systemd/network/50-vagrant-eth4.network")
111
+
112
+ communicate.should_receive(:sudo).with("systemctl restart systemd-networkd.service")
113
+
114
+ described_class.configure_networks(machine, networks)
115
+ end
116
+ end
117
+
118
+ context 'configure 2 kinds of networks without eth2' do
119
+ networks = [
120
+ # config.vm.network :private_network, ip: "192.168.33.10"
121
+ {
122
+ :type => :static,
123
+ :adapter_ip => '192.168.10.1',
124
+ :ip => '192.168.10.10',
125
+ :netmask => '255.255.255.0',
126
+ :auto_config => true,
127
+ :interface => 1
128
+ },
129
+ # config.vm.network :private_network, type: "dhcp"
130
+ {
131
+ :type => :dhcp,
132
+ :adapter_ip => "172.28.128.1",
133
+ :ip => "172.28.128.1",
134
+ :netmask => "255.255.255.0",
135
+ :auto_config => true,
136
+ :interface => 2
137
+ }
138
+ ]
139
+
140
+ interfaces = "eth0\neth1\n"
141
+
142
+ before do
143
+ communicate.stub(:sudo).with("ifconfig -a | grep '^eth' | cut -f1 -d' '")
144
+ .and_yield(nil, interfaces)
145
+ @@logger = Log4r::Logger.new("vagrant::guest::photon::configure_networks")
146
+ end
147
+
148
+ it 'should configure networks without eth2' do
149
+ communicate.should_receive(:sudo).with("rm -f /etc/systemd/network/50-vagrant-*.network")
150
+ communicate.should_receive(:sudo).with("ifconfig -a | grep '^eth' | cut -f1 -d' '")
151
+
152
+ # eth1
153
+ communicate.should_receive(:upload) do |src, dst|
154
+ contents = (File.readlines src).join("")
155
+ contents.should eq "[Match]\nName=eth1\n\n[Network]\nAddress=192.168.10.10/24\n"
156
+ dst.should eq "/tmp/50-vagrant-eth1.network"
157
+ end
158
+ communicate.should_receive(:sudo)
159
+ .with("mv /tmp/50-vagrant-eth1.network /etc/systemd/network/")
160
+ communicate.should_receive(:sudo)
161
+ .with("chown root:root /etc/systemd/network/50-vagrant-eth1.network")
162
+ communicate.should_receive(:sudo)
163
+ .with("chmod +r /etc/systemd/network/50-vagrant-eth1.network")
164
+
165
+ # eth2
166
+ @@logger.should_receive(:warn).with(
167
+ "Could not find match rule for network " +
168
+ "{:type=>:dhcp, :adapter_ip=>\"172.28.128.1\", :ip=>\"172.28.128.1\", " +
169
+ ":netmask=>\"255.255.255.0\", :auto_config=>true, :interface=>2}"
170
+ )
171
+
172
+ communicate.should_receive(:sudo).with("systemctl restart systemd-networkd.service")
173
+
174
+ described_class.configure_networks(machine, networks)
175
+ end
28
176
  end
29
177
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vagrant-guests-photon
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Fabio Rapposelli
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-20 00:00:00.000000000 Z
11
+ date: 2015-05-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler