-
Notifications
You must be signed in to change notification settings - Fork 0
/
panucci.rb
397 lines (346 loc) · 11.8 KB
/
panucci.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
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
require 'sinatra' #creates web interface
require 'tempfile' #allows use of tempfiles
require 'dotenv/load' #used to allow configuration used environment variables or .env file in project folder
require 'yaml'
require 'find'
require 'pathname'
require 'timeout'
require 'unirest'
require 'time'
load 'panucciLibs.rb'
LOGSERVER = "[email protected]"
GRADESERVER = "http://10.0.2.232:3000/machines"
MEMTESTPCT = 50
STARTTIME = Time.now
labelPrinted = false
load 'class_extensions.rb'
imageStarted = false
modelMatch = false
procMatch = false
$ffRegex = /MT|DT|SFF|USFF|USDT|Laptop|Workstation/
$modelRegex = Regexp.union($ffRegex, /[0-9]/)
$orderTable = {}
$orderData = {}
$ordernumber = 0
def populateOrderTable(sku)
$orderTable['sku'] = sku
$orderTable['desc'] = $orderData[sku]['description']
$orderTable['ram'] = $orderData[sku]['spec']['ram']
$orderTable['hdd'] = $orderData[sku]['spec']['hdd']
splitDesc = $orderTable['desc'].split(" ")
puts splitDesc.select{|x| $ffRegex.match(x)}.to_s
ffIndex = splitDesc.index(splitDesc.select{|x| $ffRegex.match(x)}[0])
puts ffIndex
$orderTable['model'] = splitDesc[0..ffIndex].select{|x| $modelRegex.match(x)}
puts $orderTable
osIndex = splitDesc.index(splitDesc.select{|x| /Win[dows]?/.match(x)}[0])
osString = splitDesc[osIndex, 3]
puts osString
osString[0] = osString[0].slice(0..2)
case osString[2]
when "Professional"
osString[2] = "Pro"
when "Home"
osString[2] = "HmPrem"
when "Hm"
osString[2] = "HmPrem"
end
$orderTable['os'] = osString.join('')
procEndIndex = splitDesc.index{|x| /[0-9]{1,2}GB/.match(x)}-1
procArray = splitDesc[ffIndex+1..procEndIndex]
procArray.delete(procArray.find{|x| /GHz/.match(x)})
$orderTable['proc'] = procArray.find{|x| /[0-9]{3,}/.match(x)}
end
# Create a new scrubdeku SCClient
scclient = Scrub::SCClient.new(SCSERVER, SCUSER, SCPASSWORD)
SIZES = [80, 120, 160, 250, 320, 256, 500, 1000].freeze
def findImagesFor(manufacturer, folder, hash)
dirHash = hash.clone
path = "#{manufacturer}/#{folder}"
locationArray = path.split('/')
puts locationArray
locationArray.each do |dir|
dirHash = dirHash.select { |x| x['text'] == dir }[0][:children]
end
dirHash
end
#Create directory hash through iteration
def directory_hash(path, name = nil, exclude = [])
exclude.concat(['..', '.', '.git', '__MACOSX', '.DS_Store', '._.DS_Store', 'All'])
data = { 'image' => 'false', 'text' => (name || path) }
data[:children] = children = []
Dir.foreach(path) do |entry|
next if exclude.include?(entry)
full_path = File.join(path, entry)
children << if File.directory?(full_path)
if Dir[full_path + '/sda-pt.sf'].empty?
directory_hash(full_path, entry)
else
{ 'image' => 'true', 'text' => entry }
end
end
end
data
end
def findImagesInFolder(folder)
Dir[ENV['IMAGES_DIR'] + '/' + folder + '/*'].select { |entry| File.directory?(entry) }
end
if ENV['IMAGES_DIR']
manufacturers = Dir.entries(ENV['IMAGES_DIR']).select { |entry| File.directory?(File.join(ENV['IMAGES_DIR'], entry)) && !(entry == '.' || entry == '..') }
manufacturers.concat ['All']
manufacturers.sort_by!(&:downcase)
globalImageList = Dir[ENV['IMAGES_DIR'] + '/**/*'].select { |entry| File.directory?(entry) && !Dir[entry + '/sda-pt.sf'].empty? }
end
def getSysInfo
sysInfo = {}
sysInfo[:serial] = `sudo dmidecode --type 1 | grep Serial | sed 's/\tSerial Number: //'`.chomp
sysInfo[:mfr] = `sudo dmidecode --type 1 | grep Manufacturer | sed 's/\tManufacturer: //'`.chomp
sysInfo[:model] = `sudo dmidecode --type 1 | grep "Product Name:" | sed 's/\tProduct Name: //'`.chomp
sysInfo[:version] = `sudo dmidecode --type 1 | grep "Version:" | sed 's/\tVersion: //'`.chomp
sysInfo[:proc] = `lscpu | grep "Model name:" | sed 's/Model name: *//'`.chomp
sysInfo[:isHDD] = `cat /sys/block/sda/queue/rotational`.chomp
sysInfo
end
def getFreeMemory
freeMem = `cat /proc/meminfo | grep MemFree`
freeMem = freeMem.scan(/\d*/).join('').to_i / 1024.0
freeMem
end
memTestAmt = (getFreeMemory * 0.01).floor
# enable SMART on drive
smartSupport = system('sudo smartctl --smart=on /dev/sda')
# run Conveyance SMART test
if !ENV['DEBUG']
memTestAmt = (getFreeMemory * (MEMTESTPCT/100.0)).floor
totalRam = `cat /proc/meminfo | grep MemTotal | sed 's/MemTotal: *//' | sed 's/ kB//'`.chomp.to_i / 1000.0 / 1000
totalRam = totalRam.round
memoryStatus = "Testing In Progress"
memTest = Thread.fork do
status = system("sudo memtester #{memTestAmt} 1").passfail
memoryStatus = status
end
if system('lsblk | grep "sda "')
driveSize = `lsblk -b | grep "sda " | grep -oE '[0-9]{3,}'`.chomp.to_i
humanReadableSize = driveSize / 1000.0 / 1000 / 1000
humanReadableSize = SIZES.map { |x| [x, (x - humanReadableSize).abs] }.to_h.min_by { |_size, distance| distance }[0]
hddStatus = "Testing In Progress"
if smartSupport == true
hddTest = Thread.fork do
hddTestStatus = true.passfail
waitForShortTest = `sudo smartctl -t short /dev/sda | grep Please | sed 's/Please wait //' | sed 's/ minutes for test to complete.//'`.chomp.to_i
sleep(150)
smartShortStatus = `sudo smartctl -l selftest /dev/sda | grep Short | grep "# 1 "`
smartShortPass = smartShortStatus.include? 'Completed without error'
if smartShortPass == false
puts 'FAILED AT SHORT SELFTEST'
hddTestStatus = false.passfail
end
selfHealthTest = `sudo smartctl -H /dev/sda | grep overall | sed 's/.*: //'`.chomp
if selfHealthTest != 'PASSED'
puts 'FAILED AT HEALTH CHECK'
hddTestStatus = false.passfail
end
unless system('lsblk | grep -E "sda[1234]"')
begin
Timeout::timeout(60) {
writeStatus = system("sudo dd if=/dev/zero of=/dev/sda bs=64M count=16")
}
rescue Timeout::Error
writeStatus = false
end
begin
Timeout::timeout(60) {
readStatus = system("sudo dd if=/dev/sda of=/dev/null bs=64M count=24")
}
rescue Timeout::Error
readStatus = false
end
if writeStatus == false
hddTestStatus = false.passfail
end
if readStatus == false
hddTestStatus = false.passfail
end
end
seekTestResults = system('sudo seeker /dev/sda')
if seekTestResults == false
hddTestStatus = false.passfail
end
hddStatus = hddTestStatus
end
else
hddStatus = ('ERROR: SMART Not Supported by Drive')
end
else
hddStatus = ("ERROR: No HDD Detected")
humanReadableSize = 0
end
else
driveSize = ENV["SIZE"]
memoryStatus = ('PASS')
hddStatus = ('PASS')
end
sysInfo = getSysInfo
get '/orderrequest' do
erb :orderreq
end
post '/ordersubmit' do
ordernumber = params[:orderno]
begin
order = Scrub::Order.new(scclient.order_data(ordernumber.to_i))
rescue Net::OpenTimeout
retry
end
$ordernumber = ordernumber
$orderData = order.computer_kit_listing
puts $orderData
case
when $orderData.length == 1
sku = $orderData.keys[0]
populateOrderTable(sku)
redirect '/'
when $orderData.length > 1
redirect '/orderselect'
end
end
get '/orderselect' do
erb :selectorder, locals: {
orderData: $orderData
}
end
get '/parseImage' do
sku = params[:sku]
populateOrderTable(sku)
redirect '/'
end
get '/' do
hddStatusVar = "#{hddStatus}"
memStatusVar = "#{memoryStatus}"
puts "Test"
puts memoryStatus
puts hddStatus
unless $orderTable == {}
if ($orderTable['model'])
unless $orderTable['model'].include?("Laptop")
if $orderTable['model'].all? {|x| sysInfo[:model].include?(x)}
modelMatch = true
end
else
if ($orderTable['model']-["Laptop"]).all? {|x| sysInfo[:model].include?(x)}
modelMatch = true
end
end
else
modelMatch = true
end
if sysInfo[:proc].include?($orderTable['proc'])
procMatch = true
end
end
erb :test, locals: {
totalRam: totalRam,
memoryStatus: memStatusVar,
hddStatus: hddStatusVar,
sysInfo: sysInfo,
humanReadableSize: humanReadableSize,
orderTable: $orderTable,
modelMatch: modelMatch,
procMatch: procMatch,
didSearch: false,
ordernumber: $ordernumber,
gradeServer: GRADESERVER
}
end
get '/clone' do
erb :clone, locals: {
sysInfo: sysInfo,
imaging: imageStarted
}
end
get '/images' do
puts ENV['IMAGES_DIR']
erb :images, locals: {
sysInfo: sysInfo,
globalImageList: globalImageList,
dir_listing: directory_hash(ENV['IMAGES_DIR'], nil, [])[:children],
size: humanReadableSize
}
end
get '/smug' do
erb :smug
end
get '/smugYes' do
$orderTable = {}
redirect '/'
end
get '/clearModel' do
$orderTable = $orderTable.tap{|x| x.delete("model")}
redirect '/'
end
get '/startClone' do
image = params[:image]
if imageStarted == false
#To print labels.
system("i3-msg layout splitv")
imageStarted = true
system("xterm -e \"sudo ocs-sr -g auto -e1 auto -e2 -r -j2 -scr -icds -p reboot restoredisk #{image} sda\"")
end
erb :clone, locals: {
sysInfo: nil,
imaging: true
}
end
get '/logdb' do
response = Unirest.post GRADESERVER, parameters:{ :machine => {:unit_serial_number => sysInfo[:serial], :unit_model => sysInfo[:model], :unit_mfr => sysInfo[:mfr], :ram_size => totalRam, :hdd_size => humanReadableSize, :ram_pass => memoryStatus, :hdd_pass => hddStatus, :order_number => $ordernumber, :cpu_model => sysInfo[:proc]}}
end
get '/timer' do
content_type :json
{'time' => Time.now - STARTTIME }
end
get '/status.json' do
content_type :json
if $ordernumber != 0
{ 'hddStatus' => hddStatus, 'memoryStatus' => memoryStatus, 'modelMatch' => modelMatch, 'procMatch' => procMatch}.to_json
else
{ 'hddStatus' => hddStatus, 'memoryStatus' => memoryStatus}.to_json
end
end
get '/printlabel' do
hddStatusVar = "#{hddStatus}"
memStatusVar = "#{memoryStatus}"
if ["PASS", "FAIL"].include?(memStatusVar)
if ["PASS", "FAIL", "ERROR: SMART Not Supported by Drive"].include?(hddStatusVar)
puts "K, it's going..."
if ["PASS"].include?(memStatusVar)
memPass = true
else
memPass = false
end
if ["PASS"].include?(hddStatusVar)
hddPass = true
else
hddPass =false
end
puts "Printing Label"
label = ""
label << " Date: #{Date.today.to_s}\n"
label << " HDD: #{hddStatus}\n"
label << " RAM: #{memoryStatus}\n"
label << " Mfr: #{sysInfo[:mfr]}\n"
label << " Model: #{sysInfo[:model]} #{sysInfo[:version]}\n"
label << " CPU: #{sysInfo[:proc]}\n"
label << " HDD Size: #{humanReadableSize}GB\n"
label << " RAM Size: #{totalRam}GB\n"
if hddPass && memPass
label << " Tested for Full Function, R2/Reuse"
end
if $ordernumber != 0
label << " Order Number: #{$ordernumber}"
end
puts label
labelPrinted = system("ssh #{LOGSERVER} \'printf \"#{label}\" | tee imageLogs/#{sysInfo[:serial]} | enscript -b #{sysInfo[:serial]} -FCourier10 -fCourier8 imageLogs/#{sysInfo[:serial]} -M Stage2 -d Stage2\'")
else
end
else
end
end