-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrun.rb
400 lines (349 loc) · 13.7 KB
/
run.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
398
399
400
require "sinatra"
require "yaml"
require "erb"
require "pstore"
require "./lib/index"
require "./lib/form"
require "./lib/history"
require "./lib/scheduler"
set :environment, :production
#set :environment, :development
set :erb, trim: "-"
# Internal Constants
VERSION = "1.2.0"
SCHEDULERS_DIR_PATH = "./lib/schedulers"
HISTORY_ROWS = 10
JOB_STATUS = { "queued" => "QUEUED", "running" => "RUNNING", "completed" => "COMPLETED" }
JOB_ID = "id"
JOB_APP_NAME = "appName"
JOB_APP_PATH = "appPath"
JOB_STATUS_ID = "status"
HEADER_SCRIPT_LOCATION = "_script_location"
HEADER_SCRIPT_NAME = "_script_1"
HEADER_JOB_NAME = "_script_2"
SCRIPT_CONTENT = "_script_content"
SUBMIT_BUTTON = "_submitButton"
JOB_NAME = "Job Name"
JOB_SUBMISSION_TIME = "Submission Time"
JOB_PARTITION = "Partition"
JOB_KEYS = "job_keys"
# Structure of manifest
Manifest = Struct.new(:dirname, :name, :category, :description, :icon, :related_app)
# Create a YAML or ERB file object. Give priority to ERB.
# If the file does not exist or is not valid, return nil.
def read_yaml(yml_path)
erb_path = yml_path + ".erb"
if File.exist?(erb_path)
return YAML.load(ERB.new(File.read(erb_path), trim_mode: "-").result(binding))
elsif File.exist?(yml_path)
return YAML.load_file(yml_path)
else
return nil
end
end
# Create a configuration object.
# Defaults are applied for any missing values.
def create_conf
conf = read_yaml("./conf.yml")
# Check required values
["scheduler", "apps_dir"].each do |key|
halt 500, "In conf.yml, \"#{key}:\" must be defined." if conf[key].nil?
end
conf["login_node"] ||= nil
conf["data_dir"] ||= ENV["HOME"] + "/composer"
conf["bin"] ||= nil
conf["bin_overrides"] ||= nil
conf["ssh_wrapper"] ||= nil
conf["footer"] ||= " "
conf["thumbnail_width"] ||= "100"
conf["navbar_color"] ||= "#3D3B40"
conf["dropdown_color"] ||= conf["navbar_color"]
conf["footer_color"] ||= conf["navbar_color"]
conf["category_color"] ||= "#5522BB"
conf["description_color"] ||= conf["category_color"]
conf["form_color"] ||= "#BFCFE7"
# Set special environment variables for Grid Engine
ENV['SGE_ROOT'] ||= conf["sge_root"]
conf["history_db"] = File.join(conf["data_dir"], conf["scheduler"] + ".db")
return conf
end
# Create a manifest object in a specified application.
# If the name is not defined, the directory name is used.
def create_manifest(directory_path)
manifest = read_yaml(File.join(directory_path, "manifest.yml"))
dirname = File.basename(directory_path)
return Manifest.new(dirname, dirname, nil, nil, nil, nil) if manifest.nil?
manifest["name"] ||= dirname
related_app = Array(manifest["related_app"]) # If manifest["related_app"] is nil, it will be an empty array.
return Manifest.new(dirname, manifest["name"], manifest["category"], manifest["description"], manifest["icon"], related_app)
end
# Create an array of manifest objects for all applications.
def create_all_manifests(apps_dir)
Dir.children(apps_dir).each_with_object([]) do |dir, manifests|
next if dir.start_with?(".") # Skip hidden files and directories
directory_path = File.join(apps_dir, dir)
if ["form.yml", "form.yml.erb"].any? { |file| File.exist?(File.join(directory_path, file)) }
manifests << create_manifest(directory_path)
end
end
end
# Replace with cached value.
def replace_with_cache(form, cache)
form.each do |key, value|
value["value"] = case value["widget"]
when "number", "text", "email"
if value.key?("size")
value["size"].times.map { |i| cache["#{key}_#{i+1}"] }
else
cache[key]
end
when "select", "radio"
cache[key]
when "multi_select"
length = cache["#{key}_length"].to_i
length.times.map { |i| cache["#{key}_#{i+1}"] }
when "checkbox"
value["options"].size.times.map { |i| cache["#{key}_#{i+1}"] }
when "path"
cache["#{key}"]
end
end
end
# Create a scheduler object.
def create_scheduler(scheduler_name)
schedulers = Dir.glob(SCHEDULERS_DIR_PATH + "/*.rb").map { |file| File.basename(file, ".rb") }
halt 500, "No such scheduler_name (#{scheduler_name}) found." unless schedulers.include?(scheduler_name)
require SCHEDULERS_DIR_PATH + "/" + scheduler_name + ".rb"
return Object.const_get(scheduler_name.capitalize).new
end
# Create a website of Top, Application, and History.
def show_website(job_id = nil, scheduler = nil, error_msg = nil, error_params = nil)
@conf = create_conf
@apps_dir = @conf["apps_dir"]
@version = VERSION
@my_ood_url = request.base_url
@script_name = request.script_name
@path_info = request.path_info
@current_path = File.join(@script_name, @path_info)
@manifests = create_all_manifests(@apps_dir).sort_by { |m| [(m.category || "").downcase, m.name.downcase] }
@manifests_w_category, @manifests_wo_category = @manifests.partition(&:category)
case @path_info
when "/"
@name = "Top"
erb :index
when "/history"
@name = "History"
@login_node = @conf["login_node"]
@scheduler = scheduler || create_scheduler(@conf["scheduler"])
@bin = @conf["bin"]
@bin_overrides = @conf["bin_overrides"]
@ssh_wrapper = @conf["ssh_wrapper"]
@status = params["status"] || "all"
@filter = params["filter"]
@jobs_size = get_job_size()
@rows = [[(params["rows"] || HISTORY_ROWS).to_i, 1].max, @jobs_size].min
@page_size = (@rows == 0) ? 1 : ((@jobs_size - 1) / @rows) + 1
@current_page = (params["p"] || 1).to_i
@start_index = (@jobs_size == 0) ? 0 : (@current_page - 1) * @rows
@end_index = (@jobs_size == 0) ? 0 : [@current_page * @rows, @jobs_size].min - 1
@jobs, @error_msg = get_job_history(@status, @start_index, @end_index, @filter)
if !@error_msg.nil?
erb :error
else
@error_msg = error_msg
erb :history
end
else
@manifest = @manifests.find { |m| "/#{m.dirname}" == @path_info }
if !@manifest.nil?
@name = @manifest["name"]
@body = read_yaml(File.join(@apps_dir, @path_info, "form.yml"))
@header = if @body.key?("header")
@body["header"]
else
read_yaml("./lib/header.yml")["header"]
end
# Since the widget name is used as a variable in Ruby, it should consist of only
# alphanumeric characters and underscores, and numbers should not be used at the
# beginning of the name. Furthermore, underscores are also prohibited at the
# beginning of the name to avoid conflicts with Open Composer's internal variables.
if @body&.dig("form")
invalid_keys = @body["form"].each_key.reject { |key| key.match?(/^[a-zA-Z][a-zA-Z0-9_]*$/) }
halt 500, "Widget name(s) (#{invalid_keys.join(', ')}) cannot be used.\n" unless invalid_keys.empty?
end
# Load cache
@script_content = nil
if params["jobId"] || job_id
history_db = @conf["history_db"]
halt 404, "#{history_db} is not found." unless File.exist?(history_db)
db = PStore.new(history_db)
cache = ""
db.transaction(true) do
id = if params["jobId"]
params["jobId"]
else
job_id.is_a?(Array) ? job_id[0].to_s : job_id.to_s
end
cache = db[id]
halt 404, "Specified Job ID (#{id}) is not found." if cache.nil?
end
replace_with_cache(@header, cache)
replace_with_cache(@body["form"], cache)
@script_content = cache[SCRIPT_CONTENT]
elsif !error_msg.nil?
replace_with_cache(@header, error_params)
replace_with_cache(@body["form"], error_params)
@script_content = error_params[SCRIPT_CONTENT]
end
@script_label = @body["script"].is_a?(Hash) ? @body["script"]["label"] : "Script Content"
if @body["script"].is_a?(Hash) && @body["script"].key?("content")
@body["script"] = @body["script"]["content"]
end
@table_index = 1
@job_id = job_id.is_a?(Array) ? job_id.join(", ") : job_id
@error_msg = error_msg
erb :form
else
@error_msg = "#{request.url} is not found."
erb :error
end
end
end
# Send an application icon.
get "/:apps_dir/:folder/:icon" do
icon_path = File.join(create_conf["apps_dir"], params[:folder], params[:icon])
send_file(icon_path) if File.exist?(icon_path)
end
# Return a list of files and/or directories in JSON format.
get "/_files" do
path = params[:path] || "."
path = File.dirname(path) if File.file?(path)
content_type :json
if File.exist?(path)
entries = Dir.children(path).map do |entry|
full_path = File.join(path, entry)
{ name: entry, path: full_path, type: File.directory?(full_path) ? "directory" : "file" }
end.sort_by { |entry| entry[:name].downcase }
else
# When a non-existent directory is specified using the set-value statement of the dynamic form widget.
entries = ""
end
{ files: entries }.to_json
end
# Return whether the specified PATH is a file or a directory.
get "/_file_or_directory" do
path = params[:path] || "."
content_type :json
if File.file?(path)
{ type: "file" }.to_json
else
{ type: "directory" }.to_json
end
end
get "/*" do
show_website
end
post "/*" do
conf = create_conf
bin = conf["bin"]
bin_overrides = conf["bin_overrides"]
ssh_wrapper = conf["ssh_wrapper"]
data_dir = conf["data_dir"]
history_db = conf["history_db"]
scheduler = create_scheduler(conf["scheduler"])
if request.path_info == "/history"
job_ids = params["JobIds"].split(",")
error_msg = nil
case params["action"]
when "cancel"
error_msg = scheduler.cancel(job_ids, bin, bin_overrides, ssh_wrapper)
when "delete"
if File.exist?(history_db)
db = PStore.new(history_db)
db.transaction do
job_ids.each do |job_id|
db.delete(job_id)
end
end
end
end
show_website(nil, scheduler, error_msg)
else
app_path = File.join(conf["apps_dir"], request.path_info)
script_location = params[HEADER_SCRIPT_LOCATION]
script_name = params[HEADER_SCRIPT_NAME]
job_name = params[HEADER_JOB_NAME]
halt 500, "#{HEADER_SCRIPT_LOCATION} is not defined in #{app_path}/form.yml[.erb]." if script_location.nil?
halt 500, "#{HEADER_SCRIPT_NAME} is not defined in #{app_path}/form.yml[.erb]." if script_name.nil?
halt 500, "#{HEADER_JOB_NAME} is not defined in #{app_path}/form.yml[.erb]." if job_name.nil?
script_path = File.join(script_location, script_name)
script_content = params[SCRIPT_CONTENT].gsub("\r\n", "\n")
job_id = nil
error_msg = nil
form = read_yaml(File.join(app_path, "form.yml"))
submit_options = nil
# Run commands in check block
check = form["check"]
unless check.nil?
params.each do |key, value|
next if ['splat', SCRIPT_CONTENT].include?(key)
suffix = case key
when JOB_APP_NAME then "OC_APP_NAME"
when JOB_APP_PATH then "OC_APP_PATH"
when HEADER_SCRIPT_LOCATION then "OC_SCRIPT_LOCATION"
when HEADER_SCRIPT_NAME then "OC_SCRIPT_NAME"
when HEADER_JOB_NAME then "OC_JOB_NAME"
else key
end
instance_variable_set("@#{suffix}", value)
end
eval(check)
end
# Run commands in submit block
submit = form["submit"]
unless submit.nil?
replacements = params.each_with_object({}) do |(key, value), env|
next if ['splat', SCRIPT_CONTENT].include?(key)
suffix = case key
when JOB_APP_NAME then "OC_APP_NAME"
when JOB_APP_PATH then "OC_APP_PATH"
when HEADER_SCRIPT_LOCATION then "OC_SCRIPT_LOCATION"
when HEADER_SCRIPT_NAME then "OC_SCRIPT_NAME"
when HEADER_JOB_NAME then "OC_JOB_NAME"
else key
end
env[suffix] = value
end
replacements.each do |key, value|
submit.gsub!(/\#\{#{key}\}/, value.to_s)
end
submit_with_echo = <<~BASH
#{submit}
if [ -n "$OC_SUBMIT_OPTIONS" ]; then
echo "$OC_SUBMIT_OPTIONS"
else
echo "__UNDEFINED__"
fi
BASH
submit_options = `bash -c '#{submit_with_echo}' | tail -n 1`.strip
submit_options = nil if submit_options == "__UNDEFINED__"
end
# Save a job script
FileUtils.mkdir_p(script_location)
File.open(script_path, "w") { |file| file.write(script_content) }
# Submit a job script
Dir.chdir(File.dirname(script_path)) do
job_id, error_msg = scheduler.submit(script_path, job_name.strip, submit_options, bin, bin_overrides, ssh_wrapper)
params[JOB_SUBMISSION_TIME] = Time.now.strftime("%Y-%m-%d %H:%M:%S")
end
# Save a job history
FileUtils.mkdir_p(data_dir)
db = PStore.new(history_db)
db.transaction do
Array(job_id).each do |id|
db[id] = params
end
end
show_website(job_id, scheduler, error_msg, params)
end
end