Merge branch '17286-print-urls' refs #17286
[arvados.git] / services / api / app / models / container.rb
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 require 'log_reuse_info'
6 require 'whitelist_update'
7 require 'safe_json'
8 require 'update_priority'
9
10 class Container < ArvadosModel
11   include ArvadosModelUpdates
12   include HasUuid
13   include KindAndEtag
14   include CommonApiTemplate
15   include WhitelistUpdate
16   extend CurrentApiClient
17   extend DbCurrentTime
18   extend LogReuseInfo
19
20   # Posgresql JSONB columns should NOT be declared as serialized, Rails 5
21   # already know how to properly treat them.
22   attribute :secret_mounts, :jsonbHash, default: {}
23   attribute :runtime_status, :jsonbHash, default: {}
24   attribute :runtime_auth_scopes, :jsonbHash, default: {}
25
26   serialize :environment, Hash
27   serialize :mounts, Hash
28   serialize :runtime_constraints, Hash
29   serialize :command, Array
30   serialize :scheduling_parameters, Hash
31
32   after_find :fill_container_defaults_after_find
33   before_validation :fill_field_defaults, :if => :new_record?
34   before_validation :set_timestamps
35   before_validation :check_lock
36   before_validation :check_unlock
37   validates :command, :container_image, :output_path, :cwd, :priority, { presence: true }
38   validates :priority, numericality: { only_integer: true, greater_than_or_equal_to: 0 }
39   validate :validate_runtime_status
40   validate :validate_state_change
41   validate :validate_change
42   validate :validate_lock
43   validate :validate_output
44   after_validation :assign_auth
45   before_save :sort_serialized_attrs
46   before_save :update_secret_mounts_md5
47   before_save :scrub_secrets
48   before_save :clear_runtime_status_when_queued
49   after_save :update_cr_logs
50   after_save :handle_completed
51   after_save :propagate_priority
52   after_commit { UpdatePriority.run_update_thread }
53
54   has_many :container_requests, :foreign_key => :container_uuid, :class_name => 'ContainerRequest', :primary_key => :uuid
55   belongs_to :auth, :class_name => 'ApiClientAuthorization', :foreign_key => :auth_uuid, :primary_key => :uuid
56
57   api_accessible :user, extend: :common do |t|
58     t.add :command
59     t.add :container_image
60     t.add :cwd
61     t.add :environment
62     t.add :exit_code
63     t.add :finished_at
64     t.add :locked_by_uuid
65     t.add :log
66     t.add :mounts
67     t.add :output
68     t.add :output_path
69     t.add :priority
70     t.add :progress
71     t.add :runtime_constraints
72     t.add :runtime_status
73     t.add :started_at
74     t.add :state
75     t.add :auth_uuid
76     t.add :scheduling_parameters
77     t.add :runtime_user_uuid
78     t.add :runtime_auth_scopes
79     t.add :lock_count
80   end
81
82   # Supported states for a container
83   States =
84     [
85      (Queued = 'Queued'),
86      (Locked = 'Locked'),
87      (Running = 'Running'),
88      (Complete = 'Complete'),
89      (Cancelled = 'Cancelled')
90     ]
91
92   State_transitions = {
93     nil => [Queued],
94     Queued => [Locked, Cancelled],
95     Locked => [Queued, Running, Cancelled],
96     Running => [Complete, Cancelled],
97     Complete => [Cancelled]
98   }
99
100   def self.limit_index_columns_read
101     ["mounts"]
102   end
103
104   def self.full_text_searchable_columns
105     super - ["secret_mounts", "secret_mounts_md5", "runtime_token"]
106   end
107
108   def self.searchable_columns *args
109     super - ["secret_mounts_md5", "runtime_token"]
110   end
111
112   def logged_attributes
113     super.except('secret_mounts', 'runtime_token')
114   end
115
116   def state_transitions
117     State_transitions
118   end
119
120   # Container priority is the highest "computed priority" of any
121   # matching request. The computed priority of a container-submitted
122   # request is the priority of the submitting container. The computed
123   # priority of a user-submitted request is a function of
124   # user-assigned priority and request creation time.
125   def update_priority!
126     return if ![Queued, Locked, Running].include?(state)
127     p = ContainerRequest.
128         where('container_uuid=? and priority>0', uuid).
129         includes(:requesting_container).
130         lock(true).
131         map do |cr|
132       if cr.requesting_container
133         cr.requesting_container.priority
134       else
135         (cr.priority << 50) - (cr.created_at.to_time.to_f * 1000).to_i
136       end
137     end.max || 0
138     update_attributes!(priority: p)
139   end
140
141   def propagate_priority
142     return true unless saved_change_to_priority?
143     act_as_system_user do
144       # Update the priority of child container requests to match new
145       # priority of the parent container (ignoring requests with no
146       # container assigned, because their priority doesn't matter).
147       ContainerRequest.
148         where(requesting_container_uuid: self.uuid,
149               state: ContainerRequest::Committed).
150         where('container_uuid is not null').
151         includes(:container).
152         map(&:container).
153         map(&:update_priority!)
154     end
155   end
156
157   # Create a new container (or find an existing one) to satisfy the
158   # given container request.
159   def self.resolve(req)
160     if req.runtime_token.nil?
161       runtime_user = if req.modified_by_user_uuid.nil?
162                        current_user
163                      else
164                        User.find_by_uuid(req.modified_by_user_uuid)
165                      end
166       runtime_auth_scopes = ["all"]
167     else
168       auth = ApiClientAuthorization.validate(token: req.runtime_token)
169       if auth.nil?
170         raise ArgumentError.new "Invalid runtime token"
171       end
172       runtime_user = User.find_by_id(auth.user_id)
173       runtime_auth_scopes = auth.scopes
174     end
175     c_attrs = act_as_user runtime_user do
176       {
177         command: req.command,
178         cwd: req.cwd,
179         environment: req.environment,
180         output_path: req.output_path,
181         container_image: resolve_container_image(req.container_image),
182         mounts: resolve_mounts(req.mounts),
183         runtime_constraints: resolve_runtime_constraints(req.runtime_constraints),
184         scheduling_parameters: req.scheduling_parameters,
185         secret_mounts: req.secret_mounts,
186         runtime_token: req.runtime_token,
187         runtime_user_uuid: runtime_user.uuid,
188         runtime_auth_scopes: runtime_auth_scopes
189       }
190     end
191     act_as_system_user do
192       if req.use_existing && (reusable = find_reusable(c_attrs))
193         reusable
194       else
195         Container.create!(c_attrs)
196       end
197     end
198   end
199
200   # Return a runtime_constraints hash that complies with requested but
201   # is suitable for saving in a container record, i.e., has specific
202   # values instead of ranges.
203   #
204   # Doing this as a step separate from other resolutions, like "git
205   # revision range to commit hash", makes sense only when there is no
206   # opportunity to reuse an existing container (e.g., container reuse
207   # is not implemented yet, or we have already found that no existing
208   # containers are suitable).
209   def self.resolve_runtime_constraints(runtime_constraints)
210     rc = {}
211     runtime_constraints.each do |k, v|
212       if v.is_a? Array
213         rc[k] = v[0]
214       else
215         rc[k] = v
216       end
217     end
218     if rc['keep_cache_ram'] == 0
219       rc['keep_cache_ram'] = Rails.configuration.Containers.DefaultKeepCacheRAM
220     end
221     rc
222   end
223
224   # Return a mounts hash suitable for a Container, i.e., with every
225   # readonly collection UUID resolved to a PDH.
226   def self.resolve_mounts(mounts)
227     c_mounts = {}
228     mounts.each do |k, mount|
229       mount = mount.dup
230       c_mounts[k] = mount
231       if mount['kind'] != 'collection'
232         next
233       end
234
235       uuid = mount.delete 'uuid'
236
237       if mount['portable_data_hash'].nil? and !uuid.nil?
238         # PDH not supplied, try by UUID
239         c = Collection.
240           readable_by(current_user).
241           where(uuid: uuid).
242           select(:portable_data_hash).
243           first
244         if !c
245           raise ArvadosModel::UnresolvableContainerError.new "cannot mount collection #{uuid.inspect}: not found"
246         end
247         mount['portable_data_hash'] = c.portable_data_hash
248       end
249     end
250     return c_mounts
251   end
252
253   # Return a container_image PDH suitable for a Container.
254   def self.resolve_container_image(container_image)
255     coll = Collection.for_latest_docker_image(container_image)
256     if !coll
257       raise ArvadosModel::UnresolvableContainerError.new "docker image #{container_image.inspect} not found"
258     end
259     coll.portable_data_hash
260   end
261
262   def self.find_reusable(attrs)
263     log_reuse_info { "starting with #{Container.all.count} container records in database" }
264     candidates = Container.where_serialized(:command, attrs[:command], md5: true)
265     log_reuse_info(candidates) { "after filtering on command #{attrs[:command].inspect}" }
266
267     candidates = candidates.where('cwd = ?', attrs[:cwd])
268     log_reuse_info(candidates) { "after filtering on cwd #{attrs[:cwd].inspect}" }
269
270     candidates = candidates.where_serialized(:environment, attrs[:environment], md5: true)
271     log_reuse_info(candidates) { "after filtering on environment #{attrs[:environment].inspect}" }
272
273     candidates = candidates.where('output_path = ?', attrs[:output_path])
274     log_reuse_info(candidates) { "after filtering on output_path #{attrs[:output_path].inspect}" }
275
276     image = resolve_container_image(attrs[:container_image])
277     candidates = candidates.where('container_image = ?', image)
278     log_reuse_info(candidates) { "after filtering on container_image #{image.inspect} (resolved from #{attrs[:container_image].inspect})" }
279
280     candidates = candidates.where_serialized(:mounts, resolve_mounts(attrs[:mounts]), md5: true)
281     log_reuse_info(candidates) { "after filtering on mounts #{attrs[:mounts].inspect}" }
282
283     secret_mounts_md5 = Digest::MD5.hexdigest(SafeJSON.dump(self.deep_sort_hash(attrs[:secret_mounts])))
284     candidates = candidates.where('secret_mounts_md5 = ?', secret_mounts_md5)
285     log_reuse_info(candidates) { "after filtering on secret_mounts_md5 #{secret_mounts_md5.inspect}" }
286
287     candidates = candidates.where_serialized(:runtime_constraints, resolve_runtime_constraints(attrs[:runtime_constraints]), md5: true)
288     log_reuse_info(candidates) { "after filtering on runtime_constraints #{attrs[:runtime_constraints].inspect}" }
289
290     log_reuse_info { "checking for state=Complete with readable output and log..." }
291
292     select_readable_pdh = Collection.
293       readable_by(current_user).
294       select(:portable_data_hash).
295       to_sql
296
297     usable = candidates.where(state: Complete, exit_code: 0)
298     log_reuse_info(usable) { "with state=Complete, exit_code=0" }
299
300     usable = usable.where("log IN (#{select_readable_pdh})")
301     log_reuse_info(usable) { "with readable log" }
302
303     usable = usable.where("output IN (#{select_readable_pdh})")
304     log_reuse_info(usable) { "with readable output" }
305
306     usable = usable.order('finished_at ASC').limit(1).first
307     if usable
308       log_reuse_info { "done, reusing container #{usable.uuid} with state=Complete" }
309       return usable
310     end
311
312     # Check for non-failing Running candidates and return the most likely to finish sooner.
313     log_reuse_info { "checking for state=Running..." }
314     running = candidates.where(state: Running).
315               where("(runtime_status->'error') is null").
316               order('progress desc, started_at asc').
317               limit(1).first
318     if running
319       log_reuse_info { "done, reusing container #{running.uuid} with state=Running" }
320       return running
321     else
322       log_reuse_info { "have no containers in Running state" }
323     end
324
325     # Check for Locked or Queued ones and return the most likely to start first.
326     locked_or_queued = candidates.
327                        where("state IN (?)", [Locked, Queued]).
328                        order('state asc, priority desc, created_at asc').
329                        limit(1).first
330     if locked_or_queued
331       log_reuse_info { "done, reusing container #{locked_or_queued.uuid} with state=#{locked_or_queued.state}" }
332       return locked_or_queued
333     else
334       log_reuse_info { "have no containers in Locked or Queued state" }
335     end
336
337     log_reuse_info { "done, no reusable container found" }
338     nil
339   end
340
341   def lock
342     self.with_lock do
343       if self.state != Queued
344         raise LockFailedError.new("cannot lock when #{self.state}")
345       end
346       self.update_attributes!(state: Locked)
347     end
348   end
349
350   def check_lock
351     if state_was == Queued and state == Locked
352       if self.priority <= 0
353         raise LockFailedError.new("cannot lock when priority<=0")
354       end
355       self.lock_count = self.lock_count+1
356     end
357   end
358
359   def unlock
360     self.with_lock do
361       if self.state != Locked
362         raise InvalidStateTransitionError.new("cannot unlock when #{self.state}")
363       end
364       self.update_attributes!(state: Queued)
365     end
366   end
367
368   def check_unlock
369     if state_was == Locked and state == Queued
370       if self.locked_by_uuid != current_api_client_authorization.uuid
371         raise ArvadosModel::PermissionDeniedError.new("locked by a different token")
372       end
373       if self.lock_count >= Rails.configuration.Containers.MaxDispatchAttempts
374         self.state = Cancelled
375         self.runtime_status = {error: "Failed to start container.  Cancelled after exceeding 'Containers.MaxDispatchAttempts' (lock_count=#{self.lock_count})"}
376       end
377     end
378   end
379
380   def self.readable_by(*users_list)
381     # Load optional keyword arguments, if they exist.
382     if users_list.last.is_a? Hash
383       kwargs = users_list.pop
384     else
385       kwargs = {}
386     end
387     if users_list.select { |u| u.is_admin }.any?
388       return super
389     end
390     Container.where(ContainerRequest.readable_by(*users_list).where("containers.uuid = container_requests.container_uuid").arel.exists)
391   end
392
393   def final?
394     [Complete, Cancelled].include?(self.state)
395   end
396
397   def self.for_current_token
398     return if !current_api_client_authorization
399     _, _, _, container_uuid = Thread.current[:token].split('/')
400     if container_uuid.nil?
401       Container.where(auth_uuid: current_api_client_authorization.uuid).first
402     else
403       Container.where('auth_uuid=? or (uuid=? and runtime_token=?)',
404                       current_api_client_authorization.uuid,
405                       container_uuid,
406                       current_api_client_authorization.token).first
407     end
408   end
409
410   protected
411
412   def fill_field_defaults
413     self.state ||= Queued
414     self.environment ||= {}
415     self.runtime_constraints ||= {}
416     self.mounts ||= {}
417     self.cwd ||= "."
418     self.priority ||= 0
419     self.scheduling_parameters ||= {}
420   end
421
422   def permission_to_create
423     current_user.andand.is_admin
424   end
425
426   def permission_to_destroy
427     current_user.andand.is_admin
428   end
429
430   def ensure_owner_uuid_is_permitted
431     # validate_change ensures owner_uuid can't be changed at all --
432     # except during create, which requires admin privileges. Checking
433     # permission here would be superfluous.
434     true
435   end
436
437   def set_timestamps
438     if self.state_changed? and self.state == Running
439       self.started_at ||= db_current_time
440     end
441
442     if self.state_changed? and [Complete, Cancelled].include? self.state
443       self.finished_at ||= db_current_time
444     end
445   end
446
447   # Check that well-known runtime status keys have desired data types
448   def validate_runtime_status
449     [
450       'error', 'errorDetail', 'warning', 'warningDetail', 'activity'
451     ].each do |k|
452       if self.runtime_status.andand.include?(k) && !self.runtime_status[k].is_a?(String)
453         errors.add(:runtime_status, "'#{k}' value must be a string")
454       end
455     end
456   end
457
458   def validate_change
459     permitted = [:state]
460     progress_attrs = [:progress, :runtime_status, :log, :output]
461     final_attrs = [:exit_code, :finished_at]
462
463     if self.new_record?
464       permitted.push(:owner_uuid, :command, :container_image, :cwd,
465                      :environment, :mounts, :output_path, :priority,
466                      :runtime_constraints, :scheduling_parameters,
467                      :secret_mounts, :runtime_token,
468                      :runtime_user_uuid, :runtime_auth_scopes)
469     end
470
471     case self.state
472     when Locked
473       permitted.push :priority, :runtime_status, :log, :lock_count
474
475     when Queued
476       permitted.push :priority
477
478     when Running
479       permitted.push :priority, *progress_attrs
480       if self.state_changed?
481         permitted.push :started_at
482       end
483
484     when Complete
485       if self.state_was == Running
486         permitted.push *final_attrs, *progress_attrs
487       end
488
489     when Cancelled
490       case self.state_was
491       when Running
492         permitted.push :finished_at, *progress_attrs
493       when Queued, Locked
494         permitted.push :finished_at, :log, :runtime_status
495       end
496
497     else
498       # The state_transitions check will add an error message for this
499       return false
500     end
501
502     if self.state_was == Running &&
503        !current_api_client_authorization.nil? &&
504        (current_api_client_authorization.uuid == self.auth_uuid ||
505         current_api_client_authorization.token == self.runtime_token)
506       # The contained process itself can write final attrs but can't
507       # change priority or log.
508       permitted.push *final_attrs
509       permitted = permitted - [:log, :priority]
510     elsif !current_user.andand.is_admin
511       raise PermissionDeniedError
512     elsif self.locked_by_uuid && self.locked_by_uuid != current_api_client_authorization.andand.uuid
513       # When locked, progress fields cannot be updated by the wrong
514       # dispatcher, even though it has admin privileges.
515       permitted = permitted - progress_attrs
516     end
517     check_update_whitelist permitted
518   end
519
520   def validate_lock
521     if [Locked, Running].include? self.state
522       # If the Container was already locked, locked_by_uuid must not
523       # changes. Otherwise, the current auth gets the lock.
524       need_lock = locked_by_uuid_was || current_api_client_authorization.andand.uuid
525     else
526       need_lock = nil
527     end
528
529     # The caller can provide a new value for locked_by_uuid, but only
530     # if it's exactly what we expect. This allows a caller to perform
531     # an update like {"state":"Unlocked","locked_by_uuid":null}.
532     if self.locked_by_uuid_changed?
533       if self.locked_by_uuid != need_lock
534         return errors.add :locked_by_uuid, "can only change to #{need_lock}"
535       end
536     end
537     self.locked_by_uuid = need_lock
538   end
539
540   def validate_output
541     # Output must exist and be readable by the current user.  This is so
542     # that a container cannot "claim" a collection that it doesn't otherwise
543     # have access to just by setting the output field to the collection PDH.
544     if output_changed?
545       c = Collection.
546             readable_by(current_user, {include_trash: true}).
547             where(portable_data_hash: self.output).
548             first
549       if !c
550         errors.add :output, "collection must exist and be readable by current user."
551       end
552     end
553   end
554
555   def update_cr_logs
556     # If self.final?, this update is superfluous: the final log/output
557     # update will be done when handle_completed calls finalize! on
558     # each requesting CR.
559     return if self.final? || !saved_change_to_log?
560     leave_modified_by_user_alone do
561       ContainerRequest.where(container_uuid: self.uuid).each do |cr|
562         cr.update_collections(container: self, collections: ['log'])
563         cr.save!
564       end
565     end
566   end
567
568   def assign_auth
569     if self.auth_uuid_changed?
570          return errors.add :auth_uuid, 'is readonly'
571     end
572     if not [Locked, Running].include? self.state
573       # Don't need one. If auth already exists, expire it.
574       #
575       # We use db_transaction_time here (not db_current_time) to
576       # ensure the token doesn't validate later in the same
577       # transaction (e.g., in a test case) by satisfying expires_at >
578       # transaction timestamp.
579       self.auth.andand.update_attributes(expires_at: db_transaction_time)
580       self.auth = nil
581       return
582     elsif self.auth
583       # already have one
584       return
585     end
586     if self.runtime_token.nil?
587       if self.runtime_user_uuid.nil?
588         # legacy behavior, we don't have a runtime_user_uuid so get
589         # the user from the highest priority container request, needed
590         # when performing an upgrade and there are queued containers,
591         # and some tests.
592         cr = ContainerRequest.
593                where('container_uuid=? and priority>0', self.uuid).
594                order('priority desc').
595                first
596         if !cr
597           return errors.add :auth_uuid, "cannot be assigned because priority <= 0"
598         end
599         self.runtime_user_uuid = cr.modified_by_user_uuid
600         self.runtime_auth_scopes = ["all"]
601       end
602
603       # generate a new token
604       self.auth = ApiClientAuthorization.
605                     create!(user_id: User.find_by_uuid(self.runtime_user_uuid).id,
606                             api_client_id: 0,
607                             scopes: self.runtime_auth_scopes)
608     end
609   end
610
611   def sort_serialized_attrs
612     if self.environment_changed?
613       self.environment = self.class.deep_sort_hash(self.environment)
614     end
615     if self.mounts_changed?
616       self.mounts = self.class.deep_sort_hash(self.mounts)
617     end
618     if self.runtime_constraints_changed?
619       self.runtime_constraints = self.class.deep_sort_hash(self.runtime_constraints)
620     end
621     if self.scheduling_parameters_changed?
622       self.scheduling_parameters = self.class.deep_sort_hash(self.scheduling_parameters)
623     end
624     if self.runtime_auth_scopes_changed?
625       self.runtime_auth_scopes = self.runtime_auth_scopes.sort
626     end
627   end
628
629   def update_secret_mounts_md5
630     if self.secret_mounts_changed?
631       self.secret_mounts_md5 = Digest::MD5.hexdigest(
632         SafeJSON.dump(self.class.deep_sort_hash(self.secret_mounts)))
633     end
634   end
635
636   def scrub_secrets
637     # this runs after update_secret_mounts_md5, so the
638     # secret_mounts_md5 will still reflect the secrets that are being
639     # scrubbed here.
640     if self.state_changed? && self.final?
641       self.secret_mounts = {}
642       self.runtime_token = nil
643     end
644   end
645
646   def clear_runtime_status_when_queued
647     # Avoid leaking status messages between different dispatch attempts
648     if self.state_was == Locked && self.state == Queued
649       self.runtime_status = {}
650     end
651   end
652
653   def handle_completed
654     # This container is finished so finalize any associated container requests
655     # that are associated with this container.
656     if saved_change_to_state? and self.final?
657       # These get wiped out by with_lock (which reloads the record),
658       # so record them now in case we need to schedule a retry.
659       prev_secret_mounts = secret_mounts_before_last_save
660       prev_runtime_token = runtime_token_before_last_save
661
662       # Need to take a lock on the container to ensure that any
663       # concurrent container requests that might try to reuse this
664       # container will block until the container completion
665       # transaction finishes.  This ensure that concurrent container
666       # requests that try to reuse this container are finalized (on
667       # Complete) or don't reuse it (on Cancelled).
668       self.with_lock do
669         act_as_system_user do
670           if self.state == Cancelled
671             retryable_requests = ContainerRequest.where("container_uuid = ? and priority > 0 and state = 'Committed' and container_count < container_count_max", uuid)
672           else
673             retryable_requests = []
674           end
675
676           if retryable_requests.any?
677             c_attrs = {
678               command: self.command,
679               cwd: self.cwd,
680               environment: self.environment,
681               output_path: self.output_path,
682               container_image: self.container_image,
683               mounts: self.mounts,
684               runtime_constraints: self.runtime_constraints,
685               scheduling_parameters: self.scheduling_parameters,
686               secret_mounts: prev_secret_mounts,
687               runtime_token: prev_runtime_token,
688               runtime_user_uuid: self.runtime_user_uuid,
689               runtime_auth_scopes: self.runtime_auth_scopes
690             }
691             c = Container.create! c_attrs
692             retryable_requests.each do |cr|
693               cr.with_lock do
694                 leave_modified_by_user_alone do
695                   # Use row locking because this increments container_count
696                   cr.container_uuid = c.uuid
697                   cr.save!
698                 end
699               end
700             end
701           end
702
703           # Notify container requests associated with this container
704           ContainerRequest.where(container_uuid: uuid,
705                                  state: ContainerRequest::Committed).each do |cr|
706             leave_modified_by_user_alone do
707               cr.finalize!
708             end
709           end
710
711           # Cancel outstanding container requests made by this container.
712           ContainerRequest.
713             includes(:container).
714             where(requesting_container_uuid: uuid,
715                   state: ContainerRequest::Committed).each do |cr|
716             leave_modified_by_user_alone do
717               cr.update_attributes!(priority: 0)
718               cr.container.reload
719               if cr.container.state == Container::Queued || cr.container.state == Container::Locked
720                 # If the child container hasn't started yet, finalize the
721                 # child CR now instead of leaving it "on hold", i.e.,
722                 # Queued with priority 0.  (OTOH, if the child is already
723                 # running, leave it alone so it can get cancelled the
724                 # usual way, get a copy of the log collection, etc.)
725                 cr.update_attributes!(state: ContainerRequest::Final)
726               end
727             end
728           end
729         end
730       end
731     end
732   end
733 end