-
Notifications
You must be signed in to change notification settings - Fork 5
/
createimage
executable file
·229 lines (189 loc) · 6.28 KB
/
createimage
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
#!/usr/bin/env ruby
require 'methadone'
require 'time'
require 'erb'
require 'highline/import'
require 'json'
ft = HighLine::ColorScheme.new do |cs|
cs[:headline] = [ :bold, :red, :on_black ]
cs[:menu_item] = [ :green ]
end
HighLine.color_scheme = ft
include Methadone::Main
include Methadone::CLILogging
PACKER_ROOT = File.dirname(__FILE__)
imagesDir = "#{PACKER_ROOT}/src/os/"
main do |func,distro|
if distro == nil
distro = getDistro(imagesDir)
end
distroDir = "#{imagesDir}/" + distro
if func == 'build'
Dir.chdir("#{distroDir}") do
system('rm -rf http && mkdir http')
system('rm -rf output-vmware-iso')
system('rm -rf output-virtualbox-iso')
system('rm -rf packer_virtualbox-iso_virtualbox.box')
unless options[:password]
puts
options[:password] = ask("<%= color('Please set a Password: ', :headline)%>") { |q| q.echo = "x" }
puts
end
@password = options[:password]
template_string = File.read('template/ks-or-preseed.template')
template = ERB.new template_string
File.write('http/ks-or-preseed.cfg', template.result)
packerFile = File.read('./packer.json')
packerHash = JSON.parse(packerFile)
buildType = nil
formatResults = getFormat(options[:vagrant_only], options[:vmware_only], packerHash)
formatString = formatResults[0]
buildType = formatResults[1]
verifyBuild(packerHash, buildType)
command = "packer build #{formatString} -var 'ssh_root_password=#{options[:password]}' packer.json"
viscommand = "packer build #{formatString} -var 'ssh_root_password=****HIDDEN****' packer.json"
puts viscommand
system(command)
unless options[:vagrant_only]
system('sed -i.bak s/"nat"/"none"/ output-vmware-iso/packer-vmware-iso.vmx')
end
end
end
if func == 'upload'
Dir.chdir("#{distroDir}/output-vmware-iso") do
command = 'ovftool --vCloudTemplate --acceptAllEulas packer-vmware-iso.vmx '+
'"vcloud://'+options[:username]+'@api.vcd.portal.skyscapecloud.com:443?org='+options[:org]+
'&vappTemplate='+options[:template]+
'&catalog='+options[:catalog]+'"'
puts command
system(command)
end
end
end
version '0.0.1'
description 'Build and Upload templates'
arg :func, :required, "build or upload"
arg :distro, :optional
on("-p PASSWORD","--password","password") do |pass|
options[:password] = pass
end
on("-u USERNAME","--username","username") do |username|
options[:username] = username
end
on("-o ORG","--org","org") do |org|
options[:org] = org
end
on("-t TEMPLATE","--template","Template Name") do |template|
options[:template] = template + Time.now.utc.iso8601
end
on("-c CATALOG","--catalog","Catalog") do |catalog|
options[:catalog] = catalog
end
on("--vagrant-also", "Also build VirtualBox Vagrant Box") do
options[:vmware_only] = false
end
on("--vagrant-only", "Build VirtualBox Vagrant Box Only") do
options[:vagrant_only] = true
options[:vmware_only] = false
end
def getFormat(vagrant_only, vmware_only, packerHash)
if vagrant_only
formatString = ' -only virtualbox-iso '
elsif vmware_only
formatString = ' -only vmware-iso '
else
formatString = nil
buildType = nil
builderArr = []
i = 0
for type in packerHash['builders']
builderArr[i] = type['type']
i += 1
end
if i > 1
puts
say("<%= color('Which type of box should I build?', :headline)%>")
choose do |menu|
menu.index = :letter
menu.index_suffix = ") "
menu.prompt = "Choose one option:"
for type in packerHash['builders']
menu.choice(type['type']) do |theOp|
formatString = " -only #{theOp} "
buildType = theOp
end
end
end
else
formatString = "-only #{type['type']}"
buildType = type['type']
end
end
return formatString, buildType
end
def getDistro(imagesDir)
theDistro = nil
Dir.chdir(imagesDir) do
output = `ls -l`
distros = output.split("\n")
distros.shift
puts
say("<%= color('Which distro would you like to use?', :headline)%>")
choose do |menu|
menu.index = :letter
menu.index_suffix = ") "
menu.prompt = "Choose one option:"
for distro in distros;
name = distro.split.last
menu.choice(name) do |theChoice| theDistro = theChoice end
end
end
end
return theDistro
end
def verifyBuild(packerHash, buildType)
for builder in packerHash['builders']
unless builder['type'] == buildType
next
else
if builder['headless']
puts
say("<%= color('Your build is set to use headless mode, continue?', :headline)%>")
choose do |menu|
menu.index = :letter
menu.index_suffix = ") "
menu.prompt = "Choose one option:"
menu.choice(:Continue) do say("Continuing with build in headless mode.") end
menu.choice(:Quit) do say("<%= color('Cancelling the build, please fix the json build file and re-run createimage.', :headline)%>")
abort
end
end
else
for builder in packerHash['builders']
unless builder['type'] == buildType
next
else
puts
say("<%= color('Your build currently looks like this:', :headline)%>")
puts
for key, value in builder
say("<%= color('#{key}: #{value}', :menu_item)%>")
end
puts
say("<%= color('Continue with the build options shown above?', :headline)%>")
choose do |menu|
menu.index = :letter
menu.index_suffix = ") "
menu.prompt = "Choose one option:"
menu.choice(:Continue) do say("Continuing with build.") end
menu.choice(:Quit) do say("<%= color('Cancelling the build, please fix the json build file and re-run createimage.', :headline)%>")
abort
end
end
end
end
end
end
end
end
go!