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 serialize :properties, Hash
14 serialize :environment, Hash
15 serialize :mounts, Hash
16 serialize :runtime_constraints, Hash
17 serialize :command, Array
18 serialize :scheduling_parameters, Hash
20 before_validation :fill_field_defaults, :if => :new_record?
21 before_validation :validate_runtime_constraints
22 before_validation :validate_scheduling_parameters
23 before_validation :set_container
24 validates :command, :container_image, :output_path, :cwd, :presence => true
25 validates :output_ttl, numericality: { only_integer: true, greater_than_or_equal_to: 0 }
26 validates :priority, numericality: { only_integer: true, greater_than_or_equal_to: 0, less_than_or_equal_to: 1000 }
27 validate :validate_state_change
28 validate :check_update_whitelist
29 after_save :update_priority
30 after_save :finalize_if_needed
31 before_create :set_requesting_container_uuid
32 before_destroy :set_priority_zero
34 api_accessible :user, extend: :common do |t|
36 t.add :container_count
37 t.add :container_count_max
38 t.add :container_image
54 t.add :requesting_container_uuid
55 t.add :runtime_constraints
56 t.add :scheduling_parameters
61 # Supported states for a container request
64 (Uncommitted = 'Uncommitted'),
65 (Committed = 'Committed'),
70 nil => [Uncommitted, Committed],
71 Uncommitted => [Committed],
75 AttrsPermittedAlways = [:owner_uuid, :state, :name, :description]
76 AttrsPermittedBeforeCommit = [:command, :container_count_max,
77 :container_image, :cwd, :environment, :filters, :mounts,
78 :output_path, :priority, :properties, :requesting_container_uuid,
79 :runtime_constraints, :state, :container_uuid, :use_existing,
80 :scheduling_parameters, :output_name, :output_ttl]
82 def self.limit_index_columns_read
90 def skip_uuid_read_permission_check
91 # XXX temporary until permissions are sorted out.
92 %w(modified_by_client_uuid container_uuid requesting_container_uuid)
95 def finalize_if_needed
96 if state == Committed && Container.find_by_uuid(container_uuid).final?
104 # Finalize the container request after the container has
105 # finished/cancelled.
109 c = Container.find_by_uuid(container_uuid)
110 ['output', 'log'].each do |out_type|
111 pdh = c.send(out_type)
113 coll_name = "Container #{out_type} for request #{uuid}"
115 if out_type == 'output'
117 coll_name = self.output_name
119 if self.output_ttl > 0
120 trash_at = db_current_time + self.output_ttl
123 manifest = Collection.where(portable_data_hash: pdh).first.manifest_text
125 coll = Collection.new(owner_uuid: owner_uuid,
126 manifest_text: manifest,
127 portable_data_hash: pdh,
133 'container_request' => uuid,
135 coll.save_with_unique_name!
136 if out_type == 'output'
142 update_attributes!(state: Final, output_uuid: out_coll, log_uuid: log_coll)
145 def self.full_text_searchable_columns
151 def fill_field_defaults
152 self.state ||= Uncommitted
153 self.environment ||= {}
154 self.runtime_constraints ||= {}
157 self.container_count_max ||= Rails.configuration.container_count_max
158 self.scheduling_parameters ||= {}
159 self.output_ttl ||= 0
164 if (container_uuid_changed? and
165 not current_user.andand.is_admin and
166 not container_uuid.nil?)
167 errors.add :container_uuid, "can only be updated to nil."
170 if state_changed? and state == Committed and container_uuid.nil?
171 self.container_uuid = Container.resolve(self).uuid
173 if self.container_uuid != self.container_uuid_was
174 if self.container_count_changed?
175 errors.add :container_count, "cannot be updated directly."
178 self.container_count += 1
183 def validate_runtime_constraints
188 ['keep_cache_ram', false]].each do |k, required|
189 if !required && !runtime_constraints.include?(k)
192 v = runtime_constraints[k]
193 unless (v.is_a?(Integer) && v > 0)
194 errors.add(:runtime_constraints,
195 "[#{k}]=#{v.inspect} must be a positive integer")
201 def validate_scheduling_parameters
202 if self.state == Committed
203 if scheduling_parameters.include? 'partitions' and
204 (!scheduling_parameters['partitions'].is_a?(Array) ||
205 scheduling_parameters['partitions'].reject{|x| !x.is_a?(String)}.size !=
206 scheduling_parameters['partitions'].size)
207 errors.add :scheduling_parameters, "partitions must be an array of strings"
212 def check_update_whitelist
213 permitted = AttrsPermittedAlways.dup
215 if self.new_record? || self.state_was == Uncommitted
216 # Allow create-and-commit in a single operation.
217 permitted.push *AttrsPermittedBeforeCommit
222 permitted.push :priority, :container_count_max, :container_uuid
224 if self.container_uuid.nil?
225 self.errors.add :container_uuid, "has not been resolved to a container."
228 if self.priority.nil?
229 self.errors.add :priority, "cannot be nil"
232 # Allow container count to increment by 1
233 if (self.container_uuid &&
234 self.container_uuid != self.container_uuid_was &&
235 self.container_count == 1 + (self.container_count_was || 0))
236 permitted.push :container_count
240 if self.state_changed? and not current_user.andand.is_admin
241 self.errors.add :state, "of container request can only be set to Final by system."
244 if self.state_was == Committed
245 permitted.push :output_uuid, :log_uuid
254 if self.state_changed? or
255 self.priority_changed? or
256 self.container_uuid_changed?
257 act_as_system_user do
260 [self.container_uuid_was, self.container_uuid].compact).
261 map(&:update_priority!)
266 def set_priority_zero
267 self.update_attributes!(priority: 0) if self.state != Final
270 def set_requesting_container_uuid
271 return !new_record? if self.requesting_container_uuid # already set
273 token_uuid = current_api_client_authorization.andand.uuid
274 container = Container.where('auth_uuid=?', token_uuid).order('created_at desc').first
276 self.requesting_container_uuid = container.uuid
277 self.priority = container.priority