Merge branch 'crunch-job_finds_newer_docker_hashes' of https://github.com/tmooney...
[arvados.git] / sdk / cli / bin / arv-run-pipeline-instance
1 #!/usr/bin/env ruby
2
3 class WhRunPipelineInstance
4 end
5
6 if RUBY_VERSION < '1.9.3' then
7   abort <<-EOS
8 #{$0.gsub(/^\.\//,'')} requires Ruby version 1.9.3 or higher.
9   EOS
10 end
11
12 begin
13   require 'arvados'
14   require 'rubygems'
15   require 'json'
16   require 'pp'
17   require 'trollop'
18   require 'google/api_client'
19 rescue LoadError => l
20   $stderr.puts $:
21   abort <<-EOS
22 #{$0}: fatal: #{l.message}
23 Some runtime dependencies may be missing.
24 Try: gem install arvados pp google-api-client json trollop
25   EOS
26 end
27
28 def debuglog(message, verbosity=1)
29   $stderr.puts "#{File.split($0).last} #{$$}: #{message}" if $debuglevel >= verbosity
30 end
31
32 # Parse command line options (the kind that control the behavior of
33 # this program, that is, not the pipeline component parameters).
34
35 p = Trollop::Parser.new do
36   version __FILE__
37   banner(<<EOF)
38
39 Usage:
40   arv-run-pipeline-instance --template TEMPLATE_UUID [options] [--] [parameters]
41   arv-run-pipeline-instance --instance INSTANCE_UUID [options] [--] [parameters]
42
43 Parameters:
44   param_name=param_value
45   param_name param_value
46                          Set (or override) the default value for every
47                          pipeline component parameter with the given
48                          name.
49
50   component_name::param_name=param_value
51   component_name::param_name param_value
52   --component_name::param_name=param_value
53   --component_name::param_name param_value
54                          Set the value of a parameter for a single
55                          pipeline component.
56
57 Options:
58 EOF
59   opt(:dry_run,
60       "Do not start any new jobs or wait for existing jobs to finish. Just find out whether jobs are finished, queued, or running for each component.",
61       :type => :boolean,
62       :short => :n)
63   opt(:status_text,
64       "Store plain text status in given file.",
65       :short => :none,
66       :type => :string,
67       :default => '/dev/stdout')
68   opt(:status_json,
69       "Store json-formatted pipeline in given file.",
70       :short => :none,
71       :type => :string,
72       :default => '/dev/null')
73   opt(:no_wait,
74       "Do not wait for jobs to finish. Just look up status, submit new jobs if needed, and exit.",
75       :short => :none,
76       :type => :boolean)
77   opt(:no_reuse,
78       "Do not reuse existing jobs to satisfy pipeline components. Submit a new job for every component.",
79       :short => :none,
80       :type => :boolean)
81   opt(:debug,
82       "Print extra debugging information on stderr.",
83       :type => :boolean)
84   opt(:debug_level,
85       "Set debug verbosity level.",
86       :short => :none,
87       :type => :integer)
88   opt(:template,
89       "UUID of pipeline template, or path to local pipeline template file.",
90       :short => :none,
91       :type => :string)
92   opt(:instance,
93       "UUID of pipeline instance.",
94       :short => :none,
95       :type => :string)
96   opt(:submit,
97       "Submit the pipeline instance to the server, and exit. Let the Crunch dispatch service satisfy the components by finding/running jobs.",
98       :short => :none,
99       :type => :boolean)
100   opt(:run_pipeline_here,
101       "Manage the pipeline instance in-process. Submit jobs to Crunch as needed. Do not exit until the pipeline finishes (or fails).",
102       :short => :none,
103       :type => :boolean)
104   opt(:run_jobs_here,
105       "Run jobs in the local terminal session instead of submitting them to Crunch. Implies --run-pipeline-here. Note: this results in a significantly different job execution environment, and some Crunch features are not supported. It can be necessary to modify a pipeline in order to make it run this way.",
106       :short => :none,
107       :type => :boolean)
108   opt(:run_here,
109       "Synonym for --run-jobs-here.",
110       :short => :none,
111       :type => :boolean)
112   opt(:description,
113       "Description for the pipeline instance.",
114       :short => :none,
115       :type => :string)
116   opt(:project_uuid,
117       "UUID of the project for the pipeline instance.",
118       short: :none,
119       type: :string)
120   stop_on [:'--']
121 end
122 $options = Trollop::with_standard_exception_handling p do
123   p.parse ARGV
124 end
125 $debuglevel = $options[:debug_level] || ($options[:debug] && 1) || 0
126
127 $options[:run_jobs_here] ||= $options[:run_here] # old flag name
128 $options[:run_pipeline_here] ||= $options[:run_jobs_here] # B requires A
129
130 if $options[:instance]
131   if $options[:template] or $options[:submit]
132     abort "#{$0}: syntax error: --instance cannot be combined with --template or --submit."
133   end
134 elsif not $options[:template]
135   $stderr.puts "error: you must supply a --template or --instance."
136   p.educate
137   abort
138 end
139
140 if $options[:run_pipeline_here] == $options[:submit]
141   abort "#{$0}: error: you must supply --run-pipeline-here, --run-jobs-here, or --submit."
142 end
143
144 # Set up the API client.
145
146 $arv = Arvados.new api_version: 'v1'
147 $client = $arv.client
148 $arvados = $arv.arvados_api
149
150 class PipelineInstance
151   def self.find(uuid)
152     result = $client.execute(:api_method => $arvados.pipeline_instances.get,
153                              :parameters => {
154                                :uuid => uuid
155                              },
156                              :authenticated => false,
157                              :headers => {
158                                authorization: 'OAuth2 '+$arv.config['ARVADOS_API_TOKEN']
159                              })
160     j = JSON.parse result.body, :symbolize_names => true
161     unless j.is_a? Hash and j[:uuid]
162       debuglog "Failed to get pipeline_instance: #{j[:errors] rescue nil}", 0
163       nil
164     else
165       debuglog "Retrieved pipeline_instance #{j[:uuid]}"
166       self.new(j)
167     end
168   end
169   def self.create(attributes)
170     result = $client.execute(:api_method => $arvados.pipeline_instances.create,
171                              :body_object => {
172                                :pipeline_instance => attributes
173                              },
174                              :authenticated => false,
175                              :headers => {
176                                authorization: 'OAuth2 '+$arv.config['ARVADOS_API_TOKEN']
177                              })
178     j = JSON.parse result.body, :symbolize_names => true
179     unless j.is_a? Hash and j[:uuid]
180       abort "\n#{Time.now} -- pipeline_template #{@template[:uuid]}\nFailed to create pipeline_instance: #{j[:errors] rescue nil} #{j.inspect}"
181     end
182     debuglog "Created pipeline instance: #{j[:uuid]}"
183     self.new(j)
184   end
185   def save
186     result = $client.execute(:api_method => $arvados.pipeline_instances.update,
187                              :parameters => {
188                                :uuid => @pi[:uuid]
189                              },
190                              :body_object => {
191                                :pipeline_instance => @attributes_to_update
192                              },
193                              :authenticated => false,
194                              :headers => {
195                                authorization: 'OAuth2 '+$arv.config['ARVADOS_API_TOKEN']
196                              })
197     j = JSON.parse result.body, :symbolize_names => true
198     unless j.is_a? Hash and j[:uuid]
199       debuglog "Failed to save pipeline_instance: #{j[:errors] rescue nil}", 0
200       nil
201     else
202       @attributes_to_update = {}
203       @pi = j
204     end
205   end
206   def []=(x,y)
207     @attributes_to_update[x] = y
208     @pi[x] = y
209   end
210   def [](x)
211     @pi[x]
212   end
213
214   def log_stderr(msg)
215     $arv.log.create log: {
216       event_type: 'stderr',
217       object_uuid: self[:uuid],
218       owner_uuid: self[:owner_uuid],
219       properties: {"text" => msg},
220     }
221   end
222
223   protected
224   def initialize(j)
225     @attributes_to_update = {}
226     @pi = j
227   end
228 end
229
230 class JobCache
231   def self.get(uuid)
232     @cache ||= {}
233     result = $client.execute(:api_method => $arvados.jobs.get,
234                              :parameters => {
235                                :uuid => uuid
236                              },
237                              :authenticated => false,
238                              :headers => {
239                                authorization: 'OAuth2 '+$arv.config['ARVADOS_API_TOKEN']
240                              })
241     @cache[uuid] = JSON.parse result.body, :symbolize_names => true
242   end
243   def self.where(conditions)
244     result = $client.execute(:api_method => $arvados.jobs.list,
245                              :parameters => {
246                                :limit => 10000,
247                                :where => conditions.to_json
248                              },
249                              :authenticated => false,
250                              :headers => {
251                                authorization: 'OAuth2 '+$arv.config['ARVADOS_API_TOKEN']
252                              })
253     list = JSON.parse result.body, :symbolize_names => true
254     if list and list[:items].is_a? Array
255       list[:items]
256     else
257       []
258     end
259   end
260   def self.create(pipeline, component, job, create_params)
261     @cache ||= {}
262
263     body = {job: no_nil_values(job)}.merge(no_nil_values(create_params))
264
265     result = $client.execute(:api_method => $arvados.jobs.create,
266                              :body_object => body,
267                              :authenticated => false,
268                              :headers => {
269                                authorization: 'OAuth2 '+$arv.config['ARVADOS_API_TOKEN']
270                              })
271     j = JSON.parse result.body, :symbolize_names => true
272     if j.is_a? Hash and j[:uuid]
273       @cache[j[:uuid]] = j
274     else
275       debuglog "create job: #{j[:errors] rescue nil} with attributes #{body}", 0
276
277       msg = ""
278       j[:errors].each do |err|
279         msg += "Error creating job for component #{component}: #{err}\n"
280       end
281       msg += "Job submission was: #{body.to_json}"
282
283       pipeline.log_stderr(msg)
284       nil
285     end
286   end
287
288   protected
289
290   def self.no_nil_values(hash)
291     hash.reject { |key, value| value.nil? }
292   end
293 end
294
295 class WhRunPipelineInstance
296   attr_reader :instance
297
298   def initialize(_options)
299     @options = _options
300   end
301
302   def fetch_template(template)
303     if template.match /[^-0-9a-z]/
304       # Doesn't look like a uuid -- use it as a filename.
305       @template = JSON.parse File.read(template), :symbolize_names => true
306     else
307       result = $client.execute(:api_method => $arvados.pipeline_templates.get,
308                                :parameters => {
309                                  :uuid => template
310                                },
311                                :authenticated => false,
312                                :headers => {
313                                  authorization: 'OAuth2 '+$arv.config['ARVADOS_API_TOKEN']
314                                })
315       @template = JSON.parse result.body, :symbolize_names => true
316       if !@template[:uuid]
317         abort "#{$0}: fatal: failed to retrieve pipeline template #{template} #{@template[:errors].inspect rescue nil}"
318       end
319     end
320     self
321   end
322
323   def fetch_instance(instance_uuid)
324     @instance = PipelineInstance.find(instance_uuid)
325     @template = @instance
326     self
327   end
328
329   def apply_parameters(params_args)
330     params_args.shift if params_args[0] == '--'
331     params = {}
332     while !params_args.empty?
333       if (re = params_args[0].match /^(--)?([^-].*?)=(.+)/)
334         params[re[2]] = re[3]
335         params_args.shift
336       elsif params_args.size > 1
337         param = params_args.shift.sub /^--/, ''
338         params[param] = params_args.shift
339       else
340         abort "\n#{Time.now} -- pipeline_template #{@template[:uuid]}\nSyntax error: I do not know what to do with arg \"#{params_args[0]}\""
341       end
342     end
343
344     if not @template[:components].is_a?(Hash)
345       abort "\n#{Time.now} -- pipeline_template #{@template[:uuid]}\nSyntax error: Template missing \"components\" hash"
346     end
347     @components = @template[:components].dup
348
349     bad_components = @components.each_pair.select do |cname, cspec|
350       not cspec.is_a?(Hash)
351     end
352     if bad_components.any?
353       abort "\n#{Time.now} -- pipeline_template #{@template[:uuid]}\nSyntax error: Components not specified with hashes: #{bad_components.map(&:first).join(', ')}"
354     end
355
356     bad_components = @components.each_pair.select do |cname, cspec|
357       not cspec[:script_parameters].is_a?(Hash)
358     end
359     if bad_components.any?
360       abort "\n#{Time.now} -- pipeline_template #{@template[:uuid]}\nSyntax error: Components missing \"script_parameters\" hashes: #{bad_components.map(&:first).join(', ')}"
361     end
362
363     errors = []
364     @components.each do |componentname, component|
365       component[:script_parameters].each do |parametername, parameter|
366         parameter = { :value => parameter } unless parameter.is_a? Hash
367         if params.has_key?("#{componentname}::#{parametername}")
368           value = params["#{componentname}::#{parametername}"]
369         elsif parameter.has_key?(:value)
370           value = parameter[:value]
371         elsif parameter.has_key?(:output_of)
372           if !@components[parameter[:output_of].intern]
373             errors << [componentname, parametername, "output_of refers to nonexistent component '#{parameter[:output_of]}'"]
374           else
375             # value will be filled in later when the upstream
376             # component's output becomes known
377           end
378           next
379         elsif params.has_key?(parametername.to_s)
380           value = params[parametername.to_s]
381         elsif parameter.has_key?(:default)
382           value = parameter[:default]
383         else
384           errors << [componentname, parametername, "required parameter is missing"]
385           next
386         end
387         debuglog "parameter #{componentname}::#{parametername} == #{value}"
388
389         component[:script_parameters][parametername] =
390           parameter.dup.merge(value: value)
391       end
392     end
393     if !errors.empty?
394       abort "\n#{Time.now} -- pipeline_template #{@template[:uuid]}\nErrors:\n#{errors.collect { |c,p,e| "#{c}::#{p} - #{e}\n" }.join ""}"
395     end
396     debuglog "options=" + @options.pretty_inspect
397     self
398   end
399
400   def setup_instance
401     if @instance
402       @instance[:properties][:run_options] ||= {}
403       if @options[:no_reuse]
404         # override properties of existing instance
405         @instance[:properties][:run_options][:enable_job_reuse] = false
406       else
407         # Default to "enable reuse" if not specified. (This code path
408         # can go away when old clients go away.)
409         if @instance[:properties][:run_options][:enable_job_reuse].nil?
410           @instance[:properties][:run_options][:enable_job_reuse] = true
411         end
412       end
413     else
414       description = $options[:description] ||
415                     ("Created at #{Time.now.localtime}" + (@template[:name].andand.size.andand>0 ? " using the pipeline template *#{@template[:name]}*" : ""))
416       instance_body = {
417         components: @components,
418         properties: {
419           run_options: {
420             enable_job_reuse: !@options[:no_reuse]
421           }
422         },
423         pipeline_template_uuid: @template[:uuid],
424         description: description,
425         state: ($options[:submit] ? 'RunningOnServer' : 'RunningOnClient')
426       }
427       if @options[:project_uuid]
428         instance_body[:owner_uuid] = @options[:project_uuid]
429       end
430       @instance = PipelineInstance.create(instance_body)
431     end
432     self
433   end
434
435   def run
436     moretodo = true
437     interrupted = false
438
439     if @instance[:started_at].nil?
440       @instance[:started_at] = Time.now
441     end
442
443     job_creation_failed = 0
444     while moretodo
445       moretodo = false
446       @components.each do |cname, c|
447         job = nil
448         owner_uuid = @instance[:owner_uuid]
449         # Is the job satisfying this component already known to be
450         # finished? (Already meaning "before we query API server about
451         # the job's current state")
452         c_already_finished = (c[:job] &&
453                               c[:job][:uuid] &&
454                               ["Complete", "Failed", "Cancelled"].include?(c[:job][:state]))
455         if !c[:job] and
456             c[:script_parameters].select { |pname, p| p.is_a? Hash and p[:output_of]}.empty?
457           # No job yet associated with this component and is component inputs
458           # are fully specified (any output_of script_parameters are resolved
459           # to real value)
460           my_submit_id = "instance #{@instance[:uuid]} rand #{rand(2**64).to_s(36)}"
461           job = JobCache.create(@instance, cname, {
462             :script => c[:script],
463             :script_parameters => Hash[c[:script_parameters].map do |key, spec|
464                                          [key, spec[:value]]
465                                        end],
466             :script_version => c[:script_version],
467             :repository => c[:repository],
468             :nondeterministic => c[:nondeterministic],
469             :runtime_constraints => c[:runtime_constraints],
470             :owner_uuid => owner_uuid,
471             :is_locked_by_uuid => (@options[:run_jobs_here] ? owner_uuid : nil),
472             :submit_id => my_submit_id,
473             :state => (if @options[:run_jobs_here] then "Running" else "Queued" end)
474           }, {
475             # This is the right place to put these attributes when
476             # dealing with new API servers.
477             :minimum_script_version => c[:minimum_script_version],
478             :exclude_script_versions => c[:exclude_minimum_script_versions],
479             :find_or_create => (@instance[:properties][:run_options].andand[:enable_job_reuse] &&
480                                 !c[:nondeterministic]),
481             :filters => c[:filters]
482           })
483           if job
484             debuglog "component #{cname} new job #{job[:uuid]}"
485             c[:job] = job
486             c[:run_in_process] = (@options[:run_jobs_here] and
487                                   job[:submit_id] == my_submit_id)
488           else
489             debuglog "component #{cname} new job failed", 0
490             job_creation_failed += 1
491           end
492         end
493
494         if c[:job] and c[:run_in_process] and not ["Complete", "Failed", "Cancelled"].include? c[:job][:state]
495           report_status
496           begin
497             require 'open3'
498             Open3.popen3("arv-crunch-job", "--force-unlock",
499                          "--job", c[:job][:uuid]) do |stdin, stdout, stderr, wait_thr|
500               debuglog "arv-crunch-job pid #{wait_thr.pid} started", 0
501               stdin.close
502               while true
503                 rready, wready, = IO.select([stdout, stderr], [])
504                 break if !rready[0]
505                 begin
506                   buf = rready[0].read_nonblock(2**20)
507                 rescue EOFError
508                   break
509                 end
510                 (rready[0] == stdout ? $stdout : $stderr).write(buf)
511               end
512               stdout.close
513               stderr.close
514               debuglog "arv-crunch-job pid #{wait_thr.pid} exit #{wait_thr.value.to_i}", 0
515             end
516             if not $arv.job.get(uuid: c[:job][:uuid])[:finished_at]
517               raise Exception.new("arv-crunch-job did not set finished_at.")
518             end
519           rescue Exception => e
520             debuglog "Interrupted (#{e}). Failing job.", 0
521             $arv.job.update(uuid: c[:job][:uuid],
522                             job: {
523                               state: "Failed"
524                             })
525           end
526         end
527
528         if c[:job] and c[:job][:uuid]
529           if ["Running", "Queued"].include?(c[:job][:state])
530             # Job is running (or may be soon) so update copy of job record
531             c[:job] = JobCache.get(c[:job][:uuid])
532           end
533
534           if c[:job][:state] == "Complete"
535             # Populate script_parameters of other components waiting for
536             # this job
537             @components.each do |c2name, c2|
538               c2[:script_parameters].each do |pname, p|
539                 if p.is_a? Hash and p[:output_of] == cname.to_s
540                   debuglog "parameter #{c2name}::#{pname} == #{c[:job][:output]}"
541                   c2[:script_parameters][pname] = {value: c[:job][:output]}
542                   moretodo = true
543                 end
544               end
545             end
546             unless c_already_finished
547               # This is my first time discovering that the job
548               # succeeded. (At the top of this loop, I was still
549               # waiting for it to finish.)
550
551               if @instance[:name].andand.length.andand > 0
552                 pipeline_name = @instance[:name]
553               elsif @template.andand[:name].andand.length.andand > 0
554                 pipeline_name = @template[:name]
555               else
556                 pipeline_name = @instance[:uuid]
557               end
558               if c[:output_name] != false
559                 # Create a collection located in the same project as the pipeline with the contents of the output.
560                 portable_data_hash = c[:job][:output]
561                 collections = $arv.collection.list(limit: 1,
562                                                    filters: [['portable_data_hash', '=', portable_data_hash]],
563                                                    select: ["portable_data_hash", "manifest_text"]
564                                                    )[:items]
565                 if collections.any?
566                   name = c[:output_name] || "Output #{portable_data_hash[0..7]} of #{cname} of #{pipeline_name}"
567
568                   # check if there is a name collision.
569                   name_collisions = $arv.collection.list(filters: [["owner_uuid", "=", owner_uuid],
570                                                                    ["name", "=", name]])[:items]
571
572                   newcollection_actual = nil
573                   if name_collisions.any? and name_collisions.first[:portable_data_hash] == portable_data_hash
574                     # There is already a collection with the same name and the
575                     # same contents, so just point to that.
576                     newcollection_actual = name_collisions.first
577                   end
578
579                   if newcollection_actual.nil?
580                     # Did not find a collection with the same name (or the
581                     # collection has a different portable data hash) so create
582                     # a new collection with ensure_unique_name: true.
583                     newcollection = {
584                       owner_uuid: owner_uuid,
585                       name: name,
586                       portable_data_hash: collections.first[:portable_data_hash],
587                       manifest_text: collections.first[:manifest_text]
588                     }
589                     debuglog "Creating collection #{newcollection}", 0
590                     newcollection_actual = $arv.collection.create collection: newcollection, ensure_unique_name: true
591                   end
592
593                   c[:output_uuid] = newcollection_actual[:uuid]
594                 else
595                   debuglog "Could not find a collection with portable data hash #{portable_data_hash}", 0
596                 end
597               end
598             end
599           elsif ["Queued", "Running"].include? c[:job][:state]
600             # Job is running or queued to run, so indicate that pipeline
601             # should continue to run
602             moretodo = true
603           elsif c[:job][:state] == "Cancelled"
604             debuglog "component #{cname} job #{c[:job][:uuid]} cancelled."
605             moretodo = false
606           elsif c[:job][:state] == "Failed"
607             moretodo = false
608           end
609         end
610       end
611       @instance[:components] = @components
612       report_status
613
614       if @options[:no_wait]
615         moretodo = false
616       end
617
618       # If job creation fails, just give up on this pipeline instance.
619       if job_creation_failed > 0
620         moretodo = false
621       end
622
623       if moretodo
624         begin
625           sleep 10
626         rescue Interrupt
627           debuglog "interrupt", 0
628           interrupted = true
629           break
630         end
631       end
632     end
633
634     c_in_state = @components.values.group_by { |c|
635       c[:job] and c[:job][:state]
636     }
637     succeeded = c_in_state["Complete"].andand.count || 0
638     failed = (c_in_state["Failed"].andand.count || 0) + (c_in_state["Cancelled"].andand.count || 0)
639     ended = succeeded + failed
640
641     success = (succeeded == @components.length)
642
643     # A job create call failed. Just give up.
644     if job_creation_failed > 0
645       debuglog "job creation failed - giving up on this pipeline instance", 0
646       success = false
647       failed += 1
648     end
649
650     if interrupted
651      if success
652         @instance[:state] = 'Complete'
653      else
654         @instance[:state] = 'Paused'
655       end
656     else
657       if ended == @components.length or failed > 0
658         @instance[:state] = success ? 'Complete' : 'Failed'
659       end
660     end
661
662     if @instance[:finished_at].nil? and ['Complete', 'Failed'].include? @instance[:state]
663       @instance[:finished_at] = Time.now
664     end
665
666     debuglog "pipeline instance state is #{@instance[:state]}"
667
668     # set components_summary
669     components_summary = {"todo" => @components.length - ended, "done" => succeeded, "failed" => failed}
670     @instance[:components_summary] = components_summary
671
672     @instance.save
673   end
674
675   def cleanup
676     if @instance and @instance[:state] == 'RunningOnClient'
677       @instance[:state] = 'Paused'
678       @instance.save
679     end
680   end
681
682   def uuid
683     @instance[:uuid]
684   end
685
686   protected
687
688   def report_status
689     @instance.save
690
691     if @options[:status_json] != '/dev/null'
692       File.open(@options[:status_json], 'w') do |f|
693         f.puts @components.pretty_inspect
694       end
695     end
696
697     if @options[:status_text] != '/dev/null'
698       File.open(@options[:status_text], 'w') do |f|
699         f.puts ""
700         f.puts "#{Time.now} -- pipeline_instance #{@instance[:uuid]}"
701         namewidth = @components.collect { |cname, c| cname.size }.max
702         @components.each do |cname, c|
703           jstatus = if !c[:job]
704                       "-"
705                     else case c[:job][:state]
706                          when "Running"
707                            "#{c[:job][:tasks_summary].inspect}"
708                          when "Complete"
709                            c[:job][:output]
710                          when "Cancelled"
711                            "cancelled #{c[:job][:cancelled_at]}"
712                          when "Failed"
713                            "failed #{c[:job][:finished_at]}"
714                          when "Queued"
715                            "queued #{c[:job][:created_at]}"
716                          end
717                     end
718           f.puts "#{cname.to_s.ljust namewidth} #{c[:job] ? c[:job][:uuid] : '-'.ljust(27)} #{jstatus}"
719         end
720       end
721     end
722   end
723
724   def abort(msg)
725     if @instance
726       if ["New", "Ready", "RunningOnClient",
727           "RunningOnServer"].include?(@instance[:state])
728         @instance[:state] = "Failed"
729         @instance[:finished_at] = Time.now
730         @instance.save
731       end
732       @instance.log_stderr(msg)
733     end
734     Kernel::abort(msg)
735   end
736 end
737
738 runner = WhRunPipelineInstance.new($options)
739 begin
740   if $options[:template]
741     runner.fetch_template($options[:template])
742   else
743     runner.fetch_instance($options[:instance])
744   end
745   runner.apply_parameters(p.leftovers)
746   runner.setup_instance
747   if $options[:submit]
748     runner.instance.save
749     puts runner.instance[:uuid]
750   else
751     runner.run
752   end
753 rescue Exception => e
754   runner.cleanup
755   raise e
756 end