This repository has been archived by the owner on Nov 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoverride-plugin.rb
117 lines (97 loc) · 2.98 KB
/
override-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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# -*- 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 Vagrant.source_root.join("plugins/guests/coreos/cap/configure_networks.rb")
BASE_CLOUD_CONFIG = <<EOF
#cloud-config
coreos:
units:
- name: coreos-cloudinit-vagrant-user.path
command: start
runtime: no
content: |
[Path]
PathExists=/var/lib/coreos-vagrant/vagrantfile-user-data
- name: coreos-cloudinit-vagrant-user.service
runtime: no
content: |
[Unit]
ConditionFileNotEmpty=/var/lib/coreos-vagrant/vagrantfile-user-data
[Service]
Type=oneshot
EnvironmentFile=/etc/environment
ExecStart=/usr/bin/coreos-cloudinit --from-file /var/lib/coreos-vagrant/vagrantfile-user-data
RemainAfterExit=yes
EOF
NETWORK_UNIT = <<EOF
- name: %s
runtime: no
content: |
[Match]
Name=%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
include Vagrant::Util
def self.configure_networks(machine, networks)
cfg = BASE_CLOUD_CONFIG
machine.communicate.tap do |comm|
# Read network interface names
interfaces = []
comm.sudo("ifconfig | grep enp0 | cut -f1 -d:") do |_, result|
interfaces = result.split("\n")
end
ip = ""
# Configure interfaces
# FIXME: fix matching of interfaces with IP adresses
networks.each do |network|
iface_num = network[:interface].to_i
iface_name = interfaces[iface_num]
cidr = IPAddr.new('255.255.255.0').to_cidr
address = "%s/%s" % [network[:ip], cidr]
unit_name = "50-%s.network" % [iface_name]
unit = NETWORK_UNIT % [unit_name, iface_name, address]
cfg = "#{cfg}#{unit}"
ip = network[:ip]
end
cfg = <<EOF
#{cfg}
write_files:
- path: /etc/environment
content: |
COREOS_PUBLIC_IPV4=#{ip}
COREOS_PRIVATE_IPV4=#{ip}
hostname: #{machine.name}
EOF
temp = Tempfile.new("coreos-vagrant")
temp.binmode
temp.write(cfg)
temp.close
comm.upload(temp.path, "/tmp/user-data")
comm.sudo("mkdir -p /var/lib/coreos-vagrant")
comm.sudo("mv /tmp/user-data /var/lib/coreos-vagrant/")
end
end
end
class ChangeHostName
def self.change_host_name(machine, name)
# This is handled in configure_networks
end
end
end
end
end