This repository has been archived by the owner on Sep 7, 2021. It is now read-only.
/
box.rb
117 lines (89 loc) · 3.33 KB
/
box.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
require 'fileutils'
module Vagabond
class Box
attr_reader :name, :build_path, :env
attr_accessor :template, :settings
def initialize(name, env)
@name = name
@env = env
@build_path = File.join(env.builds_path, name)
end
def self.find(name, env = Environment.new)
box = Box.new(name, env)
if(box.created?)
#box.settings = Vagabond::BoxSettings.new File.join(env.builds_path, name, settings)
return box
else
return nil
end
end
def self.create(name, options = {:template => 'ubuntu-12.04-server-amd64'}, env)
puts "creating box #{name} from #{options[:template]}"
box = Box.new(name, env)
raise Exception.new("Box #{name} already exist!") if(box.created?)
template_path = File.join(env.template_path, options[:template])
raise Exception.new("Template #{options[:template]} does not exist at #{template_path}!") unless Dir.exists?(template_path)
box.template = options[:template]
settings = Vagabond::BoxSettings.new(File.join(template_path, 'settings.rb'))
# clean out empty options
box.settings = settings.merge(options.delete_if {|key,value| value.nil?})
box
end
def build
iso_file = File.join("iso", @settings[:iso_file])
if(!File.exists?(iso_file) && @settings[:iso_src])
puts "#{File.expand_path(iso_file)} not found!"
puts "Please wait while the file is downloaded..."
start_time = Time.now
Vagabond::Web.download(@settings[:iso_src], iso_file)
puts "Download completed in #{Time.at(Time.now - start_time).gmtime.strftime("%H:%M:%S")}"
end
raise "Please download #{@settings[:iso_file]} and place it at #{File.expand_path(iso_file)}" if(!File.exists? iso_file)
Vagabond::VM::Commands.create(name, @settings)
sata_name = Vagabond::VM::Commands.create_sata_controller(name, @settings)
disk_name = Vagabond::VM::Commands.create_disk(name, name, @settings)
Vagabond::VM::Commands.attach_disk(name, sata_name, disk_name)
Vagabond::VM::Commands.attach_iso(name, sata_name, iso_file)
Vagabond::VM::Commands.set_boot_order(name)
Vagabond::VM::Commands.create_ssh_mapping(name)
Vagabond::VM::Commands.start(name)
puts "Waiting for #{name} to boot up..."
sleep @settings[:boot_wait]
if(@settings[:boot_cmd_sequence])
puts "Sending boot parameters..."
@settings[:boot_cmd_sequence].each { |s|
s.gsub!(/%IP%/, @env.host);
s.gsub!(/%PORT%/, @env.port.to_s);
s.gsub!(/%NAME%/, name);
Vagabond::VM::Commands.send_sequence(name,s)
}
end
if(@settings[:install_files])
@settings[:install_files].each do |file|
Vagabond::Web.wait_for_request({
:filename => file,
:web_dir => File.join(env.template_path, @template)},
self
)
end
end
end
def start
Vagabond::VM::Commands.start(name)
end
def created?
return Dir.exists?(File.join(@env.builds_path, @name))
end
def destroy
if(created?)
Vagabond::VM::Commands.destroy(name)
end
if(Dir.exists? build_path)
FileUtils.remove_dir(build_path)
end
end
def get_binding
binding
end
end
end