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
|
# File 'lib/cf/cli/line.rb', line 147
def create
line_source = Dir.pwd
yaml_source = "#{line_source}/line.yml"
unless File.exist?(yaml_source)
say "The line.yml file does not exist in this directory", :red
return
end
errors = validate(yaml_source)
if errors.present?
say("Invalid line.yml file. Correct its structure as per the errors shown below.", :red)
errors.each {|error| say(" #{error}", :cyan)}
exit(1)
end
set_target_uri(false)
set_api_key(yaml_source)
CF.account_name = CF::Account.info['name']
line_dump = YAML::load(File.read(yaml_source).strip)
line_title = line_dump['title'].parameterize
line_description = line_dump['description']
line_department = line_dump['department']
line_public = line_dump['public']
line_priority = line_dump['priority']
line_worker_pool = line_dump['worker_pool']
line_prefetch_task = line_dump['prefetch_task']
line = CF::Line.info(line_title)
if line['error'].blank? && options.force?
rollback(line.title)
elsif line['error'].blank?
say("This line already exist.", :yellow)
override = agree("Do you want to override? [y/n] ")
if override
self.delete
say("Deleting the line forcefuly..", :yellow)
rollback(line['title'])
else
say("Line creation aborted!!", :yellow) and exit(1)
end
end
line = CF::Line.new(line_title, line_department, {:description => line_description, :public => line_public, :priority => line_priority, :worker_pool => line_worker_pool, :prefetch_task => line_prefetch_task})
say "Creating new assembly line: #{line.title}", :green
say("Error: #{line.errors}", :red) and exit(1) if line.errors.present?
say "Adding InputFormats", :green
input_formats = line_dump['input_formats']
input_formats.each_with_index do |input_format, index|
if input_format['valid_type']
@attrs = {
:name => input_format['name'],
:required => input_format['required'],
:valid_type => input_format['valid_type']
}
elsif input_format['valid_type'].nil?
@attrs = {
:name => input_format['name'],
:required => input_format['required'],
:valid_type => input_format['valid_type']
}
end
input_format_for_line = CF::InputFormat.new(@attrs)
input_format = line.input_formats input_format_for_line
say_status "input", "#{@attrs[:name]}"
display_error(line_title, "#{line.input_formats[index].errors}") if line.input_formats[index].errors.present?
end
stations = line_dump['stations']
stations.each_with_index do |station_file, s_index|
type = station_file['station']['station_type']
index = station_file['station']['station_index']
input_formats_for_station = station_file['station']['input_formats']
batch_size = station_file['station']['batch_size']
gold_fields = station_file['station']['gold_fields']
if type == "tournament"
jury_worker = station_file['station']['jury_worker']
auto_judge = station_file['station']['auto_judge']
acceptance_ratio = station_file['station']['acceptance_ratio']
station_params = {:line => line, :type => type, :jury_worker => jury_worker, :auto_judge => auto_judge, :input_formats => input_formats_for_station, :batch_size => batch_size, :acceptance_ratio => acceptance_ratio, :gold_fields => gold_fields}
else
station_params = {:line => line, :type => type, :input_formats => input_formats_for_station, :batch_size => batch_size, :gold_fields => gold_fields}
end
station = CF::Station.create(station_params) do |s|
say "Adding Station #{index}: #{s.type}", :green
display_error(line_title, "#{s.errors}") if s.errors.present?
worker = station_file['station']['worker']
number = worker['num_workers']
reward = worker['reward']
worker_type = worker['worker_type']
if worker_type == "human"
badges = station_file['station']['badges']
stat_badge = worker['stat_badge']
human_worker = CF::HumanWorker.new({:station => s, :number => number, :reward => reward, :badge => badges, :stat_badge => stat_badge})
say_status "worker", "#{number} Cloud #{pluralize(number, "Worker")} with reward of #{reward} #{pluralize(reward, "cent")}"
display_error(line_title, "#{human_worker.errors}") if human_worker.errors.present?
elsif worker_type =~ /robot/
settings = worker['settings']
robot_worker = CF::RobotWorker.create({:station => s, :type => worker_type, :settings => settings})
say_status "robot", "Robot worker: #{worker_type}"
display_error(line_title, "#{robot_worker.errors}") if robot_worker.errors.present?
else
display_error(line_title, "Invalid worker type: #{worker_type}")
end
if station_file['station']['task_form'].present?
title = station_file['station']['task_form']['form_title']
instruction = station_file['station']['task_form']['instruction']
form = CF::TaskForm.create({:station => s, :title => title, :instruction => instruction}) do |f|
say_status "form", "TaskForm '#{f.title}'"
display_error(line_title, "#{f.errors}") if f.errors.present?
station_file['station']['task_form']['form_fields'].each do |form_field|
form_field_params = form_field.merge(:form => f)
field = CF::FormField.new(form_field_params.symbolize_keys)
say_status "form_field", "FormField '#{field.form_field_params}'"
display_error(line_title, field.errors) if field.errors.present?
end
end
elsif station_file['station']['custom_task_form'].present?
title = station_file['station']['custom_task_form']['form_title']
instruction = station_file['station']['custom_task_form']['instruction']
html_file = station_file['station']['custom_task_form']['html']
html = File.read("#{line_source}/station#{station_file['station']['station_index']}.html")
form = CF::CustomTaskForm.create({:station => s, :title => title, :instruction => instruction, :raw_html => html})
say_status "form", "CustomTaskForm '#{form.title}'"
display_error(line_title, "#{form.errors}") if form.errors.present?
end
if badges
say "Adding Badges For Station#{index}", :green
badges.each do |name|
say_status "-", "#{name}"
end
end
gold_standards = station_file['station']['gold_standards']
if gold_standards
say "Adding Gold Standards to stations", :green
if gold_standards.class == Hash
if gold_standards["template_file"]
gold_template_source = gold_standards["template_file"]
if File.exist?(gold_template_source)
gold_template = YAML::load(File.read(gold_template_source).strip)
gold_standards.merge!("template" => gold_template)
end
end
end
if gold_standards.class == Hash gold_standard = CF::GoldStandard.new(gold_standards.merge!({:line => line,:station => s }))
say_status "GoldStandard from file", "'#{gold_standards[:file]}'"
display_error(line_title, "#{gold_standard.errors}") if gold_standard.errors.present?
else
gold_standards.each do |gold_options|
gold_standard = CF::GoldStandard.new(gold_options.merge({:line => line,:station => s }))
say_status "GoldStandard", "'#{gold_options["name"]}'"
display_error(line_title, "#{gold_standard.errors}") if gold_standard.errors.present?
end
end
end
end
end
output_formats = line_dump['output_formats'].presence
if output_formats
output_format = CF::OutputFormat.new(output_formats.merge(:line => line))
say "Adding Output Format #{output_formats}", :green
display_error(line_title, "#{output_format.errors}") if output_format.errors.present?
end
gold_standards = line_dump['gold_standards'].presence
if gold_standards
say "Adding GoldStandards to Line", :green
if gold_standards.class == Hash
if gold_standards["template_file"]
gold_template_source = gold_standards["template_file"]
if File.exist?(gold_template_source)
gold_template = YAML::load(File.read(gold_template_source).strip)
gold_standards.merge!("template" => gold_template)
end
end
end
if gold_standards.class == Hash gold_standard = CF::GoldStandard.new(gold_standards.merge!({:line => line}))
say_status "GoldStandard from file", "'#{gold_standards[:file]}'"
display_error(line_title, "#{gold_standard.errors}") if gold_standard.errors.present?
else
gold_standards.each do |gold_options|
gold_standard = CF::GoldStandard.new(gold_options.merge(:line => line))
say_status "GoldStandard", "'#{gold_options["name"]}'"
display_error(line_title, "#{gold_standard.errors}") if gold_standard.errors.present?
end
end
end
say " ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁", :white
say "Line was successfully created.", :green
say "View your line at http://#{CF.account_name}.#{CF.api_url.split("/")[-2]}/lines/#{CF.account_name}/#{line.title}", :yellow
say "\nNow you can do production runs with: cf production start <your-run-title>", :green
end
|