Merge branch '6520-nodemanager-crunchv2' refs #6520
[arvados.git] / services / api / app / models / collection.rb
index 93d46209276c95a2cc3544ee0c82f437aec6943c..9b081dbd2e6586a0e3822f796c2eebfc7bfb5e6d 100644 (file)
@@ -2,6 +2,7 @@ require 'arvados/keep'
 require 'sweep_trashed_collections'
 
 class Collection < ArvadosModel
+  extend CurrentApiClient
   extend DbCurrentTime
   include HasUuid
   include KindAndEtag
@@ -31,6 +32,7 @@ class Collection < ArvadosModel
     t.add :properties
     t.add :portable_data_hash
     t.add :signed_manifest_text, as: :manifest_text
+    t.add :manifest_text, as: :unsigned_manifest_text
     t.add :replication_desired
     t.add :replication_confirmed
     t.add :replication_confirmed_at
@@ -55,6 +57,7 @@ class Collection < ArvadosModel
                 # We need trash_at and is_trashed to determine the
                 # correct timestamp in signed_manifest_text.
                 'manifest_text' => ['manifest_text', 'trash_at', 'is_trashed'],
+                'unsigned_manifest_text' => ['manifest_text'],
                 )
   end
 
@@ -370,6 +373,23 @@ class Collection < ArvadosModel
     find_all_for_docker_image(search_term, search_tag, readers).first
   end
 
+  # If the given pdh is an old-format docker image, old-format images
+  # aren't supported by the compute nodes according to site config,
+  # and a migrated new-format image is available, return the migrated
+  # image's pdh. Otherwise, just return pdh.
+  def self.docker_migration_pdh(read_users, pdh)
+    if Rails.configuration.docker_image_formats.include?('v1')
+      return pdh
+    end
+    Collection.readable_by(*read_users).
+      joins('INNER JOIN links ON head_uuid=portable_data_hash').
+      where('tail_uuid=? AND link_class=? AND links.owner_uuid=?',
+            pdh, 'docker_image_migration', system_user_uuid).
+      order('links.created_at desc').
+      select('portable_data_hash').
+      first.andand.portable_data_hash || pdh
+  end
+
   def self.searchable_columns operator
     super - ["manifest_text"]
   end
@@ -454,16 +474,18 @@ class Collection < ArvadosModel
   # If trash_at is updated without touching delete_at, automatically
   # update delete_at to a sensible value.
   def default_trash_interval
-    if trash_at && trash_at_changed? && !delete_at_changed?
-      self.delete_at = trash_at + Rails.configuration.default_trash_lifetime.seconds
-    elsif trash_at.nil? && trash_at_changed? && !delete_at_changed?
-      self.delete_at = nil
+    if trash_at_changed? && !delete_at_changed?
+      if trash_at.nil?
+        self.delete_at = nil
+      else
+        self.delete_at = trash_at + Rails.configuration.default_trash_lifetime.seconds
+      end
     end
   end
 
   def validate_trash_and_delete_timing
     if trash_at.nil? != delete_at.nil?
-      errors.add :delete_at, "must be nil if and only if trash_at is nil"
+      errors.add :delete_at, "must be set if trash_at is set, and must be nil otherwise"
     end
 
     earliest_delete = ([@validation_timestamp, trash_at_was].compact.min +