-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVagrantfile
More file actions
258 lines (230 loc) · 9.03 KB
/
Copy pathVagrantfile
File metadata and controls
258 lines (230 loc) · 9.03 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.require_version ">= 2.2.4"
require 'yaml'
require 'json'
require 'net/http'
require_relative "lib/expand_env.rb"
require_relative "lib/loader.rb"
require_relative "lib/roles.rb"
require_relative "lib/sharing.rb"
# minicoin command extensions
require_relative "lib/minicoin.rb"
$has_winrm = false
$HOME = ENV["HOME"]
$USER = ENV['USER']
if Vagrant::Util::Platform.windows?
$HOME = ENV["USERPROFILE"]
$USER = ENV['USERNAME']
end
$MINICOIN_USER_DIR = (ENV["MINICOIN_USER_DIR"] || File.join($HOME, "/minicoin")).gsub("\\", "/")
$MINICOIN_PROJECT_DIR = (ENV["MINICOIN_PROJECT_DIR"] || Dir.pwd).gsub("\\", "/")
$PWD = File.dirname(__FILE__) #
ENV["PWD"] = $PWD if ENV["PWD"].nil?
def check_for_updates()
return unless File.exist?("../.git")
if File.exist?("../.git/FETCH_HEAD")
days = (Time.now - File.stat("../.git/FETCH_HEAD").mtime).to_f / 86400.0
else
days = 100
end
if days > 6
old_newest=`git tag --list --sort=-taggerdate`.strip.split("\n").first
`git fetch --tags`
new_newest=`git tag --list --sort=-taggerdate`.strip.split("\n").first
if old_newest != new_newest
ui = Vagrant::UI::Colored.new
ui.warn "New minicoin version #{new_newest} available, run 'minicoin update' when ready!"
end
end
end
Vagrant.configure("2") do |config|
check_for_updates()
config.vagrant.sensitive = [ ENV["minicoin_key"] ]
Minicoin.machines = load_minicoin()
Minicoin.machines.each do |machine|
next if Minicoin.skip?(machine)
name = machine["name"]
ui = Vagrant::UI::Prefixed.new(Minicoin.ui, name)
machine_box = machine["box"]
machine_url = machine_box
if !machine_box.nil?
if machine_box.include?("$minicoin_key")
minicoin_key = ENV['minicoin_key'] || ""
if minicoin_key.empty?
machine[:disabled] = "Box is private, minicoin_key not set"
end
machine_url = machine_box.gsub("$minicoin_key", minicoin_key)
machine_box.gsub!("$minicoin_key/", "")
end
machine_box = expand_env(machine_box)
end
if machine_box.nil?
ui.warn "Machine has no box set"
next
end
next if ARGV[0] == "list" # shortcut
if machine[:disabled] && !Minicoin.requested_boxes.empty?
ui.warn "Machine is not available: #{machine[:disabled]}"
next
end
config.vm.define name, autostart: false do |box|
box.minicoin.machine = machine
box.minicoin.guest_user = machine["user"] || "vagrant"
# make sure the default provider used for the machine is the first provider
# for which there is an explicit configuration; otherwise respect vagrant defaults
first_provider = machine["provider"] || ENV["VAGRANT_DEFAULT_PROVIDER"] || :virtualbox
first_provider = machine["provider"].keys.first if first_provider.is_a?(Hash)
box.vm.provider first_provider if first_provider
next if machine[:disabled] # only declare disabled machines if we get here
is_windows = (machine["os"] =~ /windows/i) != nil
is_mac = (machine['os'] =~ /macos/i) != nil
sources = $urls[machine_box.split(/\//).first]
unless sources.nil?
box.vm.box_url = []
if sources.is_a?(String)
sources = [sources];
end
if File.file?("boxes/#{machine_box}.json")
box.vm.box_url = ["#{sources[0]}/#{machine_url}.json"]
else
sources.each do |source|
box.vm.box_url += ["#{source}/#{machine_url}.box"]
end
end
end
box.vm.box = machine_box
box.vm.box_version = machine["box_version"] unless machine["box_version"].nil?
hostname = name.gsub("_", "-")
# Platform specific configurations
if is_windows
box.vm.guest = :windows
hostname = hostname[0..14] # windows hostnames cannot be more than 15 characters long
box.minicoin.guest_homes = "C:\\Users"
# "Intel Pro/1000 MT Desktop" must be used on Windows
machine["nictype1"] = "82540EM"
# default to rdp forwarding, unless ports are set
machine["ports"] = [ { "id" => "rdp" } ] if machine["ports"].nil?
# Test if winrm is available
begin
$has_winrm = require 'winrm' unless $has_winrm
rescue LoadError => e
if ARGV[0] != "runinfo"
puts "==> #{name}: WinRM gem not installed, falling back to winssh!"
end
end
if $has_winrm
box.vm.communicator = :winrm
else
box.vm.communicator = :winssh
end
elsif is_mac
box.vm.guest = :darwin
box.minicoin.guest_homes = "/Users"
# "Intel Pro/1000 MT Server" must be used on Mac
machine["nictype1"] = "82545EM"
box.vm.communicator = :ssh
else
box.vm.guest = machine["os"] unless machine["os"].nil? || machine["os"] == "linux"
box.minicoin.guest_homes = "/home"
box.vm.communicator = :ssh
end
ENV["GUEST_HOMES"] = box.minicoin.guest_homes.to_s
# Allow overriding defaults
box.vm.communicator = machine["communicator"] unless machine["communicator"].nil?
if box.vm.communicator == :winssh
box.winssh.insert_key = false # Replacing insecure key on Windows fails or takes forever
end
config.ssh.shell = machine["shell"] unless machine["shell"].nil?
# port forwarding
box.vm.network "forwarded_port", id: "ssh", # id must be ssh
guest: 22,
host: 2200 + box.minicoin.hash,
auto_correct: true
ports = machine["ports"]
if !ports.nil?
begin
ports.each do |port|
attributes = {
:auto_correct => true
}
port.each do |key, value|
attributes[key.to_sym] = value
end
raise "No ID" if attributes[:id].nil?
if attributes[:guest].nil?
case attributes[:id]
when "rdp"
attributes[:guest] = 3389
else
raise "No guest port"
end
end
if attributes[:id] == "rdp"
# turn on local hypervisor's RDP server, if supported
machine["rdp"] = true
end
attributes[:host] = attributes[:guest] * 10 + box.minicoin.hash if attributes[:host].nil?
box.vm.network "forwarded_port", attributes
end
rescue
STDERR.puts "==> #{name}: 'ports' needs to be a list of mappings, and 'id' and 'guest' needs to be set"
end
end
# private network setup
box.vm.hostname = hostname unless machine["no-hostname"] == true
private_net = machine["private_net"]
if private_net.nil?
box.vm.network "private_network", type: "dhcp"
machine["nictype2"] = machine["nictype1"] if machine["nictype2"].nil? && !machine["nictype1"].nil?
elsif private_net != "disabled"
box.vm.network "private_network", ip: "#{private_net}"
machine["nictype2"] = machine["nictype1"] if machine["nictype2"].nil? && !machine["nictype1"].nil?
end
# storage setup, using the experimental 'disks' vagrant feature. It is not implemented
# for most providers and anyway won't do anything unless VAGRANT_EXPERIMENTAL="disks"
# is set, so the _setup methods that come next will implement the feature where we can.
diskname="data0"
storages = machine["storage"] || []
storages = [storages] unless storages.is_a?(Array)
storages.each do |storage|
storage = { "size" => storage } unless storage.is_a?(Hash)
box.vm.disk :disk, name: storage['name'] || diskname, size: "#{storage['size']}GB"
diskname.next!
end
# call provider specific plugin extensions to map general minicoin configs to
# providers, and to execute provider specific configurations
Vagrant::Plugin::V2::Plugin.manager.registered.each do |plugin|
if plugin.respond_to?(:minicoin_extension)
begin
plugin.minicoin_setup(box, machine)
rescue => e
puts "==> #{name}: Error with provider setup:"
puts " #{e}"
end
if machine["provider"].is_a?(Hash)
machine["provider"].each do |provider, settings|
klass = plugin.minicoin_extension(provider.to_sym)
unless klass.nil?
extension = klass.new
begin
extension.provision(box, name, settings, machine)
rescue => e
puts "==> #{name}: Error with provider configuration:"
puts " #{e}"
end
end
end
end
end
end
# folder sharing, taking guest OS and provider specific quirks into account
share_folders(box, machine, machine["shared_folders"])
# Definition of provisioning steps
roles = machine["roles"] || []
# some default provisioning at the very end
roles << "windows-setup" if box.vm.guest == :windows && machine["provider"] != "metal"
add_roles(box, roles, machine)
end
end
end