-
Notifications
You must be signed in to change notification settings - Fork 1
/
helpers.rb
67 lines (56 loc) · 1.49 KB
/
helpers.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
module Tory
module TaskHelpers
def parse_params
@mac = params[:mac] || params[:mac_address]
@image = params[:image]
@pxe = settings.pxe_server
@storage = settings.storage_server
@app = settings.app_server
if @mac.nil? || @image.nil?
errors = []
errors << 'missing mac_address' if @mac.nil?
errors << 'missing image' if @image.nil?
json_response(422, 'missing required parameters', errors)
end
end
def pxe_mac(mac)
flat = normalize_mac(mac)
"01-" << flat.scan(/\w{2}/).join('-').downcase
end
def normalize_mac(mac)
if mac.size > 17
mac = mac[2..-1]
end
mac.downcase.gsub(/\:|\-/, '')
end
def write_pxe_file(data)
File.open("#{settings.task_path}/#{pxe_mac(@mac)}", 'w') do |f|
f << data
end
end
def affirm_task(mac)
if File.exists? "#{settings.task_path}/#{pxe_mac(mac)}"
json_response(200, 'task active')
else
errors = [mac]
json_response(404, 'no task for that address', errors)
end
end
def deploy
data = erb :deploy
write_pxe_file(data)
end
def upload
data = erb :upload
write_pxe_file(data)
end
def json_response(status_code, message, errors = [])
status status_code
resp = {message: message}
if errors.any?
resp[:errors] = errors
end
body resp.to_json
end
end
end