Merge pull request #5612 from frapposelli/photon-guest-support

Add Guest support for VMware Photon.
This commit is contained in:
Seth Vargo 2015-04-20 18:17:27 -04:00
commit 2c247824e1
8 changed files with 207 additions and 0 deletions

View File

@ -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

View File

@ -0,0 +1,42 @@
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
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
end
end
end

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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