1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: AGPL-3.0
5 require 'whitelist_update'
7 class ContainerRequest < ArvadosModel
10 include CommonApiTemplate
11 include WhitelistUpdate
13 belongs_to :container, foreign_key: :container_uuid, primary_key: :uuid
15 serialize :properties, Hash
16 serialize :environment, Hash
17 serialize :mounts, Hash
18 serialize :runtime_constraints, Hash
19 serialize :command, Array
20 serialize :scheduling_parameters, Hash
21 serialize :secret_mounts, Hash
23 before_validation :fill_field_defaults, :if => :new_record?
24 before_validation :validate_runtime_constraints
25 before_validation :validate_scheduling_parameters
26 before_validation :set_container
27 validates :command, :container_image, :output_path, :cwd, :presence => true
28 validates :output_ttl, numericality: { only_integer: true, greater_than_or_equal_to: 0 }
29 validates :priority, numericality: { only_integer: true, greater_than_or_equal_to: 0, less_than_or_equal_to: 1000 }
30 validate :validate_state_change
31 validate :check_update_whitelist
32 validate :secret_mounts_key_conflict
33 before_save :scrub_secret_mounts
34 after_save :update_priority
35 after_save :finalize_if_needed
36 before_create :set_requesting_container_uuid
37 before_destroy :set_priority_zero
39 api_accessible :user, extend: :common do |t|
41 t.add :container_count
42 t.add :container_count_max
43 t.add :container_image
59 t.add :requesting_container_uuid
60 t.add :runtime_constraints
61 t.add :scheduling_parameters
66 # Supported states for a container request
69 (Uncommitted = 'Uncommitted'),
70 (Committed = 'Committed'),
75 nil => [Uncommitted, Committed],
76 Uncommitted => [Committed],
80 AttrsPermittedAlways = [:owner_uuid, :state, :name, :description]
81 AttrsPermittedBeforeCommit = [:command, :container_count_max,
82 :container_image, :cwd, :environment, :filters, :mounts,
83 :output_path, :priority, :properties, :requesting_container_uuid,
84 :runtime_constraints, :state, :container_uuid, :use_existing,
85 :scheduling_parameters, :secret_mounts, :output_name, :output_ttl]
87 def self.limit_index_columns_read
92 super.except('secret_mounts')
99 def skip_uuid_read_permission_check
100 # XXX temporary until permissions are sorted out.
101 %w(modified_by_client_uuid container_uuid requesting_container_uuid)
104 def finalize_if_needed
105 if state == Committed && Container.find_by_uuid(container_uuid).final?
107 act_as_system_user do
113 # Finalize the container request after the container has
114 # finished/cancelled.
118 c = Container.find_by_uuid(container_uuid)
119 ['output', 'log'].each do |out_type|
120 pdh = c.send(out_type)
122 coll_name = "Container #{out_type} for request #{uuid}"
124 if out_type == 'output'
126 coll_name = self.output_name
128 if self.output_ttl > 0
129 trash_at = db_current_time + self.output_ttl
132 manifest = Collection.where(portable_data_hash: pdh).first.manifest_text
134 coll = Collection.new(owner_uuid: owner_uuid,
135 manifest_text: manifest,
136 portable_data_hash: pdh,
142 'container_request' => uuid,
144 coll.save_with_unique_name!
145 if out_type == 'output'
151 update_attributes!(state: Final, output_uuid: out_coll, log_uuid: log_coll)
154 def self.full_text_searchable_columns
155 super - ["mounts", "secret_mounts", "secret_mounts_md5"]
160 def fill_field_defaults
161 self.state ||= Uncommitted
162 self.environment ||= {}
163 self.runtime_constraints ||= {}
166 self.container_count_max ||= Rails.configuration.container_count_max
167 self.scheduling_parameters ||= {}
168 self.output_ttl ||= 0
173 if (container_uuid_changed? and
174 not current_user.andand.is_admin and
175 not container_uuid.nil?)
176 errors.add :container_uuid, "can only be updated to nil."
179 if state_changed? and state == Committed and container_uuid.nil?
180 self.container_uuid = Container.resolve(self).uuid
182 if self.container_uuid != self.container_uuid_was
183 if self.container_count_changed?
184 errors.add :container_count, "cannot be updated directly."
187 self.container_count += 1
192 def validate_runtime_constraints
197 ['keep_cache_ram', false]].each do |k, required|
198 if !required && !runtime_constraints.include?(k)
201 v = runtime_constraints[k]
202 unless (v.is_a?(Integer) && v > 0)
203 errors.add(:runtime_constraints,
204 "[#{k}]=#{v.inspect} must be a positive integer")
210 def validate_scheduling_parameters
211 if self.state == Committed
212 if scheduling_parameters.include? 'partitions' and
213 (!scheduling_parameters['partitions'].is_a?(Array) ||
214 scheduling_parameters['partitions'].reject{|x| !x.is_a?(String)}.size !=
215 scheduling_parameters['partitions'].size)
216 errors.add :scheduling_parameters, "partitions must be an array of strings"
221 def check_update_whitelist
222 permitted = AttrsPermittedAlways.dup
224 if self.new_record? || self.state_was == Uncommitted
225 # Allow create-and-commit in a single operation.
226 permitted.push(*AttrsPermittedBeforeCommit)
231 permitted.push :priority, :container_count_max, :container_uuid
233 if self.container_uuid.nil?
234 self.errors.add :container_uuid, "has not been resolved to a container."
237 if self.priority.nil?
238 self.errors.add :priority, "cannot be nil"
241 # Allow container count to increment by 1
242 if (self.container_uuid &&
243 self.container_uuid != self.container_uuid_was &&
244 self.container_count == 1 + (self.container_count_was || 0))
245 permitted.push :container_count
249 if self.state_was == Committed
250 # "Cancel" means setting priority=0, state=Committed
251 permitted.push :priority
253 if current_user.andand.is_admin
254 permitted.push :output_uuid, :log_uuid
263 def secret_mounts_key_conflict
264 secret_mounts.each do |k, v|
265 if mounts.has_key?(k)
266 errors.add(:secret_mounts, 'conflict with non-secret mounts')
272 def scrub_secret_mounts
273 if self.state == Final
274 self.secret_mounts = {}
279 if self.state_changed? or
280 self.priority_changed? or
281 self.container_uuid_changed?
282 act_as_system_user do
285 [self.container_uuid_was, self.container_uuid].compact).
286 map(&:update_priority!)
291 def set_priority_zero
292 self.update_attributes!(priority: 0) if self.state != Final
295 def set_requesting_container_uuid
296 return !new_record? if self.requesting_container_uuid # already set
298 token_uuid = current_api_client_authorization.andand.uuid
299 container = Container.where('auth_uuid=?', token_uuid).order('created_at desc').first
301 self.requesting_container_uuid = container.uuid
302 self.priority = container.priority