From 21db4470a0c5344933ffa42285fa1588f5676944 Mon Sep 17 00:00:00 2001 From: Fabio Rapposelli Date: Mon, 20 Apr 2015 10:13:21 -0700 Subject: [PATCH 1/2] Add Guest support for VMware Photon. Signed-off-by: Fabio Rapposelli --- plugins/guests/photon/cap/change_host_name.rb | 15 ++++++++++ .../guests/photon/cap/configure_networks.rb | 27 +++++++++++++++++ plugins/guests/photon/cap/docker.rb | 11 +++++++ plugins/guests/photon/guest.rb | 9 ++++++ plugins/guests/photon/plugin.rb | 30 +++++++++++++++++++ 5 files changed, 92 insertions(+) create mode 100644 plugins/guests/photon/cap/change_host_name.rb create mode 100644 plugins/guests/photon/cap/configure_networks.rb create mode 100644 plugins/guests/photon/cap/docker.rb create mode 100644 plugins/guests/photon/guest.rb create mode 100644 plugins/guests/photon/plugin.rb diff --git a/plugins/guests/photon/cap/change_host_name.rb b/plugins/guests/photon/cap/change_host_name.rb new file mode 100644 index 000000000..5249dadba --- /dev/null +++ b/plugins/guests/photon/cap/change_host_name.rb @@ -0,0 +1,15 @@ +module VagrantPlugins + module GuestPhoton + module Cap + class ChangeHostName + def self.change_host_name(machine, name) + machine.communicate.tap do |comm| + unless comm.test("sudo hostname --fqdn | grep '#{name}'") + comm.sudo("hostname #{name.split('.')[0]}") + end + end + end + end + end + end +end diff --git a/plugins/guests/photon/cap/configure_networks.rb b/plugins/guests/photon/cap/configure_networks.rb new file mode 100644 index 000000000..5e06f00eb --- /dev/null +++ b/plugins/guests/photon/cap/configure_networks.rb @@ -0,0 +1,27 @@ +require 'tempfile' +require 'vagrant/util/template_renderer' + +module VagrantPlugins + module GuestPhoton + module Cap + class ConfigureNetworks + include Vagrant::Util + + def self.configure_networks(machine, networks) + machine.communicate.tap do |comm| + # Read network interface names + interfaces = [] + comm.sudo("ifconfig | grep 'eth' | cut -f1 -d' '") do |_, result| + interfaces = result.split("\n") + end + + # Configure interfaces + networks.each do |network| + comm.sudo("ifconfig #{interfaces[network[:interface].to_i]} #{network[:ip]} netmask #{network[:netmask]}") + end + end + end + end + end + end +end diff --git a/plugins/guests/photon/cap/docker.rb b/plugins/guests/photon/cap/docker.rb new file mode 100644 index 000000000..954777908 --- /dev/null +++ b/plugins/guests/photon/cap/docker.rb @@ -0,0 +1,11 @@ +module VagrantPlugins + module GuestPhoton + module Cap + module Docker + def self.docker_daemon_running(machine) + machine.communicate.test('test -S /run/docker.sock') + end + end + end + end +end diff --git a/plugins/guests/photon/guest.rb b/plugins/guests/photon/guest.rb new file mode 100644 index 000000000..c33a9f8a2 --- /dev/null +++ b/plugins/guests/photon/guest.rb @@ -0,0 +1,9 @@ +module VagrantPlugins + module GuestPhoton + class Guest < Vagrant.plugin('2', :guest) + def detect?(machine) + machine.communicate.test("cat /etc/photon-release | grep 'VMware Photon Linux'") + end + end + end +end diff --git a/plugins/guests/photon/plugin.rb b/plugins/guests/photon/plugin.rb new file mode 100644 index 000000000..a56a0f44f --- /dev/null +++ b/plugins/guests/photon/plugin.rb @@ -0,0 +1,30 @@ +require 'vagrant' + +module VagrantPlugins + module GuestPhoton + class Plugin < Vagrant.plugin('2') + name 'VMware Photon guest' + description 'VMware Photon guest support.' + + guest('photon', 'linux') do + require File.expand_path("../guest", __FILE__) + Guest + end + + guest_capability('photon', 'change_host_name') do + require_relative 'cap/change_host_name' + Cap::ChangeHostName + end + + guest_capability('photon', 'configure_networks') do + require_relative 'cap/configure_networks' + Cap::ConfigureNetworks + end + + guest_capability('photon', 'docker_daemon_running') do + require_relative 'cap/docker' + Cap::Docker + end + end + end +end From 90bb760b3bef596e74a81e1dbe30966dd37bfc0d Mon Sep 17 00:00:00 2001 From: Fabio Rapposelli Date: Mon, 20 Apr 2015 14:56:26 -0700 Subject: [PATCH 2/2] Added tests Signed-off-by: Fabio Rapposelli --- .../guests/photon/cap/configure_networks.rb | 15 +++++++ .../photon/cap/change_host_name_test.rb | 34 ++++++++++++++++ .../photon/cap/configure_networks_test.rb | 40 +++++++++++++++++++ .../plugins/guests/photon/cap/docker_test.rb | 26 ++++++++++++ 4 files changed, 115 insertions(+) create mode 100644 test/unit/plugins/guests/photon/cap/change_host_name_test.rb create mode 100644 test/unit/plugins/guests/photon/cap/configure_networks_test.rb create mode 100644 test/unit/plugins/guests/photon/cap/docker_test.rb diff --git a/plugins/guests/photon/cap/configure_networks.rb b/plugins/guests/photon/cap/configure_networks.rb index 5e06f00eb..50f4737b8 100644 --- a/plugins/guests/photon/cap/configure_networks.rb +++ b/plugins/guests/photon/cap/configure_networks.rb @@ -19,6 +19,21 @@ module VagrantPlugins networks.each do |network| comm.sudo("ifconfig #{interfaces[network[:interface].to_i]} #{network[:ip]} netmask #{network[:netmask]}") end + + primary_machine_config = machine.env.active_machines.first + primary_machine = machine.env.machine(*primary_machine_config, true) + + get_ip = lambda do |machine| + ip = nil + machine.config.vm.networks.each do |type, opts| + if type == :private_network && opts[:ip] + ip = opts[:ip] + break + end + end + + ip + end end end end diff --git a/test/unit/plugins/guests/photon/cap/change_host_name_test.rb b/test/unit/plugins/guests/photon/cap/change_host_name_test.rb new file mode 100644 index 000000000..5e9e9e9e1 --- /dev/null +++ b/test/unit/plugins/guests/photon/cap/change_host_name_test.rb @@ -0,0 +1,34 @@ +# encoding: UTF-8 +# Copyright (c) 2015 VMware, Inc. All Rights Reserved. + +require File.expand_path("../../../../../base", __FILE__) + +describe "VagrantPlugins::GuestPhoton::Cap::ChangeHostName" do + let(:described_class) do + VagrantPlugins::GuestPhoton::Plugin.components.guest_capabilities[:photon].get(:change_host_name) + end + let(:machine) { double("machine") } + let(:communicator) { VagrantTests::DummyCommunicator::Communicator.new(machine) } + + before do + allow(machine).to receive(:communicate).and_return(communicator) + end + + after do + communicator.verify_expectations! + end + + it 'should change hostname when hostname is differ from current' do + hostname = 'vagrant-photon' + expect(communicator).to receive(:test).with("sudo hostname --fqdn | grep 'vagrant-photon'") + communicator.should_receive(:sudo).with("hostname #{hostname.split('.')[0]}") + described_class.change_host_name(machine, hostname) + end + + it 'should not change hostname when hostname equals current' do + hostname = 'vagrant-photon' + communicator.stub(:test).and_return(true) + communicator.should_not_receive(:sudo) + described_class.change_host_name(machine, hostname) + end +end diff --git a/test/unit/plugins/guests/photon/cap/configure_networks_test.rb b/test/unit/plugins/guests/photon/cap/configure_networks_test.rb new file mode 100644 index 000000000..dc6e9aaf1 --- /dev/null +++ b/test/unit/plugins/guests/photon/cap/configure_networks_test.rb @@ -0,0 +1,40 @@ +# encoding: UTF-8 +# Copyright (c) 2015 VMware, Inc. All Rights Reserved. + +require File.expand_path("../../../../../base", __FILE__) + +describe "VagrantPlugins::GuestPhoton::Cap::ConfigureNetworks" do + let(:described_class) do + VagrantPlugins::GuestPhoton::Plugin.components.guest_capabilities[:photon].get(:configure_networks) + end + let(:machine) { double("machine") } + let(:communicator) { VagrantTests::DummyCommunicator::Communicator.new(machine) } + + before do + allow(machine).to receive(:communicate).and_return(communicator) + end + + after do + communicator.verify_expectations! + end + + it 'should configure networks' do + networks = [ + { :type => :static, :ip => '192.168.10.10', :netmask => '255.255.255.0', :interface => 1, :name => 'eth0' }, + { :type => :dhcp, :interface => 2, :name => 'eth1' }, + { :type => :static, :ip => '10.168.10.10', :netmask => '255.255.0.0', :interface => 3, :name => 'docker0' } + ] + communicator.should_receive(:sudo).with("ifconfig | grep 'eth' | cut -f1 -d' '") + communicator.should_receive(:sudo).with('ifconfig 192.168.10.10 netmask 255.255.255.0') + communicator.should_receive(:sudo).with('ifconfig netmask ') + communicator.should_receive(:sudo).with('ifconfig 10.168.10.10 netmask 255.255.0.0') + + allow_message_expectations_on_nil + machine.should_receive(:env).at_least(5).times + machine.env.should_receive(:active_machines).at_least(:twice) + machine.env.active_machines.should_receive(:first) + machine.env.should_receive(:machine) + + described_class.configure_networks(machine, networks) + end +end diff --git a/test/unit/plugins/guests/photon/cap/docker_test.rb b/test/unit/plugins/guests/photon/cap/docker_test.rb new file mode 100644 index 000000000..6a1c726ff --- /dev/null +++ b/test/unit/plugins/guests/photon/cap/docker_test.rb @@ -0,0 +1,26 @@ +# encoding: UTF-8 +# Copyright (c) 2015 VMware, Inc. All Rights Reserved. + +require File.expand_path("../../../../../base", __FILE__) + +describe "VagrantPlugins::GuestPhoton::Cap::Docker" do + let(:described_class) do + VagrantPlugins::GuestPhoton::Plugin.components.guest_capabilities[:photon].get(:docker_daemon_running) + end + let(:machine) { double("machine") } + let(:communicator) { VagrantTests::DummyCommunicator::Communicator.new(machine) } + let(:old_hostname) { 'oldhostname.olddomain.tld' } + + before do + allow(machine).to receive(:communicate).and_return(communicator) + end + + after do + communicator.verify_expectations! + end + + it 'should check docker' do + expect(communicator).to receive(:test).with('test -S /run/docker.sock') + described_class.docker_daemon_running(machine) + end +end