Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
YungSang committed Apr 29, 2014
1 parent 27c3445 commit 0d5e3b6
Show file tree
Hide file tree
Showing 6 changed files with 226 additions and 20 deletions.
14 changes: 9 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ box: coreos.box

disk: tmp/CoreOS.vmdk

coreos.box: tmp/CoreOS.vmdk box/override-plugin.rb box/vagrantfile.tpl
coreos.box: tmp/CoreOS.vmdk box/change_host_name.rb box/configure_networks.rb box/vagrantfile.tpl
vagrant halt -f
#
# Clone
Expand All @@ -28,7 +28,7 @@ coreos.box: tmp/CoreOS.vmdk box/override-plugin.rb box/vagrantfile.tpl
#
rm -f coreos.box
cd box; \
vagrant package --base "${BOX_NAME}" --output ../coreos.box --include override-plugin.rb --vagrantfile vagrantfile.tpl
vagrant package --base "${BOX_NAME}" --output ../coreos.box --include change_host_name.rb,configure_networks.rb --vagrantfile vagrantfile.tpl

tmp/CoreOS.vmdk: tmp/coreos-install oem/coreos-setup-environment oem/cloud-config.yml
vagrant destroy -f
Expand All @@ -38,7 +38,7 @@ tmp/CoreOS.vmdk: tmp/coreos-install oem/coreos-setup-environment oem/cloud-confi

parallels: coreos-parallels.box

coreos-parallels.box: tmp/CoreOS.vmdk parallels/metadata.json parallels/override-plugin.rb parallels/Vagrantfile
coreos-parallels.box: tmp/CoreOS.vmdk parallels/metadata.json parallels/change_host_name.rb parallels/configure_networks.rb parallels/Vagrantfile
vagrant halt -f
#
# Convert VMDK to HDD
Expand Down Expand Up @@ -76,9 +76,13 @@ parallels/metadata.json:
mkdir -p parallels
echo '{"provider": "parallels"}' > parallels/metadata.json

parallels/override-plugin.rb: box/override-plugin.rb
parallels/change_host_name.rb: box/change_host_name.rb
mkdir -p parallels
cp box/override-plugin.rb parallels/override-plugin.rb
cp box/change_host_name.rb parallels/change_host_name.rb

parallels/configure_networks.rb: box/configure_networks.rb
mkdir -p parallels
cp box/configure_networks.rb parallels/configure_networks.rb

parallels/Vagrantfile: box/vagrantfile.tpl
mkdir -p parallels
Expand Down
37 changes: 37 additions & 0 deletions box/change_host_name.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# -*- mode: ruby -*-
# # vi: set ft=ruby :

# NOTE: This monkey-patching of the coreos guest plugin is a terrible
# hack that needs to be removed once the upstream plugin works with
# alpha CoreOS images.

require 'tempfile'
require Vagrant.source_root.join("plugins/guests/coreos/cap/change_host_name.rb")

CLOUD_CONFIG = <<EOF
#cloud-config
hostname: %s
EOF

module VagrantPlugins
module GuestCoreOS
module Cap
class ChangeHostName
def self.change_host_name(machine, name)
machine.communicate.tap do |comm|
temp = Tempfile.new("coreos-vagrant")
temp.binmode
temp.write(CLOUD_CONFIG % [name])
temp.close

path = "/var/tmp/hostname.yml"
path_esc = path.gsub("/", "-")
comm.upload(temp.path, path)
comm.sudo("systemctl start system-cloudinit@#{path_esc}.service")
end
end
end
end
end
end
142 changes: 142 additions & 0 deletions box/configure_networks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
# -*- mode: ruby -*-
# # vi: set ft=ruby :

# NOTE: This monkey-patching of the coreos guest plugin is a terrible
# hack that needs to be removed once the upstream plugin works with
# alpha CoreOS images.

require 'tempfile'
require 'ipaddr'
require 'log4r'
require Vagrant.source_root.join("plugins/guests/coreos/cap/configure_networks.rb")

BASE_CLOUD_CONFIG = <<EOF
#cloud-config
write_files:
- path: /etc/environment
content: |
COREOS_PUBLIC_IPV4=%s
COREOS_PRIVATE_IPV4=%s
coreos:
units:
EOF

NETWORK_UNIT = <<EOF
- name: %s
runtime: no
content: |
[Match]
%s
[Network]
Address=%s
EOF

# Borrowed 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 GuestCoreOS
module Cap
class ConfigureNetworks
@@logger = Log4r::Logger.new("vagrant::guest::coreos::configure_networks")

def self.configure_networks(machine, networks)
public_ipv4, private_ipv4 = get_environment_ips(machine, "127.0.0.1")
cfg = BASE_CLOUD_CONFIG % [public_ipv4, private_ipv4]

# Define network units by mac address if possible.
match_rules = {}
if false
#if machine.provider.capability?(:nic_mac_addresses)
# untested, required feature hasn't made it into a release yet
match_rules = match_by_mac(machine)
else
match_rules = match_by_name(machine)
end

@@logger.debug("Networks: #{networks.inspect}")
@@logger.debug("Interfaces: #{match_rules.inspect}")

# Generate any static networks, let DHCP handle the rest
networks.each do |network|
next if network[:type].to_sym != :static
interface = network[:interface].to_i
unit_name = "50-vagrant%d.network" % [interface]

match = match_rules[interface]
if match.nil?
@@logger.warn("Could not find match rule for network #{network.inspect}")
next
end

cidr = IPAddr.new(network[:netmask]).to_cidr
address = "%s/%s" % [network[:ip], cidr]
cfg << NETWORK_UNIT % [unit_name, match, address]
end

machine.communicate.tap do |comm|
temp = Tempfile.new("coreos-vagrant")
temp.binmode
temp.write(cfg)
temp.close

path = "/var/tmp/networks.yml"
path_esc = path.gsub("/", "-")
comm.upload(temp.path, path)
comm.sudo("systemctl start system-cloudinit@#{path_esc}.service")
end
end

# Find IP addresses to export in /etc/environment. This only works
# for static addresses defined in the user's Vagrantfile.
def self.get_environment_ips(machine, default)
public_ipv4 = nil
private_ipv4 = nil

machine.config.vm.networks.each do |type, options|
next if !options[:ip]
if type == :public_network
public_ipv4 = options[:ip]
elsif type == :private_network
private_ipv4 = options[:ip]
end
end

# Fall back to localhost if no static networks are configured.
private_ipv4 ||= default
public_ipv4 ||= private_ipv4
return [public_ipv4, private_ipv4]
end

def self.match_by_name(machine)
match = {}
machine.communicate.tap do |comm|
comm.sudo("ifconfig -a | grep ^en | cut -f1 -d:") do |_, result|
result.split("\n").each_with_index do |name, interface|
match[interface] = "Name=#{name}"
end
end
end
match
end

def self.match_by_mac(machine)
match = {}
macs = machine.provider.capability(:nic_mac_addresses)
macs.each do |adapter, address|
# The adapter list from VirtualBox is 1 indexed instead of 0
interface = adapter.to_i - 1
match[interface] = "MACAddress=#{address}"
end
match
end
end
end
end
end
10 changes: 9 additions & 1 deletion box/vagrantfile.tpl
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
require_relative "override-plugin.rb"
# -*- mode: ruby -*-
# # vi: set ft=ruby :

if Vagrant::VERSION < "1.5.4"
raise "Need at least Vagrant version 1.5.4, please update"
end

require_relative "change_host_name.rb"
require_relative "configure_networks.rb"

Vagrant.configure("2") do |config|
# SSH in as the default 'core' user, it has the vagrant ssh key.
Expand Down
16 changes: 12 additions & 4 deletions oem/cloud-config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,21 @@ coreos:
bug-report-url: https://github.com/YungSang/coreos-packer

units:
- name: coreos-cloudinit-vagrant.path
- name: coreos-cloudinit-vagrant-mkdir.service
command: start
runtime: no
runtime: yes
content: |
[Service]
Type=oneshot
ExecStart=/bin/mkdir -p /var/lib/coreos-vagrant
- name: coreos-cloudinit-vagrant-user.path
command: start
runtime: yes
content: |
[Path]
PathExists=/var/lib/coreos-vagrant/user-data
Unit=system-cloudinit@var-lib-coreos\x2dvagrant-user\x2ddata.service
PathExists=/var/lib/coreos-vagrant/vagrantfile-user-data
Unit=user-cloudinit@var-lib-coreos\x2dvagrant-vagrantfile\x2duser\x2ddata.service
- name: docker-tcp.socket
command: start
Expand Down
27 changes: 17 additions & 10 deletions oem/coreos-setup-environment
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,24 @@ done

ifaces=($ifaces)
if [ -z "${ifaces[1]}" ]; then
exit 0
echo "No network configuration provided by Vagrant!" >&2
echo "Using localhost, for default public and private IPs" >&2
echo "COREOS_PUBLIC_IPV4=127.0.0.1" >> "$ENV"
echo "COREOS_PRIVATE_IPV4=127.0.0.1" >> "$ENV"
exit
fi

# just block until the second interface is ready
ipaddr=`ifconfig ${ifaces[1]} | grep 'inet ' | sed 's/^ *//' | cut -f 2 -d ' '`
while [ -z "$ipaddr" ]; do
now=$(date +%s)
timeout=$(( now + 60 ))

# just block until cloudinit updates environment
while ! grep -qs ^COREOS_PUBLIC_IPV4 "$ENV"; do
if [[ $timeout -lt $(date +%s) ]]; then
echo "No network configuration provided by Vagrant!" >&2
echo "Using localhost, for default public and private IPs" >&2
echo "COREOS_PUBLIC_IPV4=127.0.0.1" >> "$ENV"
echo "COREOS_PRIVATE_IPV4=127.0.0.1" >> "$ENV"
exit
fi
sleep 0.1
ifaces=`ifconfig | grep enp0 | cut -f1 -d: | tr "\n" " "`
ifaces=($ifaces)
ipaddr=`ifconfig ${ifaces[1]} | grep 'inet ' | sed 's/^ *//' | cut -f 2 -d ' '`
done

echo "COREOS_PUBLIC_IPV4=${ipaddr}" >> $ENV
echo "COREOS_PRIVATE_IPV4=${ipaddr}" >> $ENV

0 comments on commit 0d5e3b6

Please sign in to comment.