1 require 'whitelist_update'
3 class ContainerRequest < ArvadosModel
6 include CommonApiTemplate
7 include WhitelistUpdate
9 serialize :properties, Hash
10 serialize :environment, Hash
11 serialize :mounts, Hash
12 serialize :runtime_constraints, Hash
13 serialize :command, Array
14 serialize :scheduling_parameters, Hash
16 before_validation :fill_field_defaults, :if => :new_record?
17 before_validation :validate_runtime_constraints
18 before_validation :validate_scheduling_parameters
19 before_validation :set_container
20 validates :command, :container_image, :output_path, :cwd, :presence => true
21 validates :output_ttl, numericality: { only_integer: true, greater_than_or_equal_to: 0 }
22 validate :validate_state_change
23 validate :check_update_whitelist
24 after_save :update_priority
25 after_save :finalize_if_needed
26 before_create :set_requesting_container_uuid
28 api_accessible :user, extend: :common do |t|
30 t.add :container_count
31 t.add :container_count_max
32 t.add :container_image
48 t.add :requesting_container_uuid
49 t.add :runtime_constraints
50 t.add :scheduling_parameters
55 # Supported states for a container request
58 (Uncommitted = 'Uncommitted'),
59 (Committed = 'Committed'),
64 nil => [Uncommitted, Committed],
65 Uncommitted => [Committed],
69 AttrsPermittedAlways = [:owner_uuid, :state, :name, :description]
70 AttrsPermittedBeforeCommit = [:command, :container_count_max,
71 :container_image, :cwd, :environment, :filters, :mounts,
72 :output_path, :priority, :properties, :requesting_container_uuid,
73 :runtime_constraints, :state, :container_uuid, :use_existing,
74 :scheduling_parameters, :output_name, :output_ttl]
76 def self.limit_index_columns_read
84 def skip_uuid_read_permission_check
85 # XXX temporary until permissions are sorted out.
86 %w(modified_by_client_uuid container_uuid requesting_container_uuid)
89 def finalize_if_needed
90 if state == Committed && Container.find_by_uuid(container_uuid).final?
98 # Finalize the container request after the container has
103 c = Container.find_by_uuid(container_uuid)
104 ['output', 'log'].each do |out_type|
105 pdh = c.send(out_type)
107 coll_name = "Container #{out_type} for request #{uuid}"
109 if out_type == 'output'
111 coll_name = self.output_name
113 if self.output_ttl > 0
114 trash_at = db_current_time + self.output_ttl
117 manifest = Collection.unscoped do
118 Collection.where(portable_data_hash: pdh).first.manifest_text
121 coll = Collection.new(owner_uuid: owner_uuid,
122 manifest_text: manifest,
123 portable_data_hash: pdh,
129 'container_request' => uuid,
131 coll.save_with_unique_name!
132 if out_type == 'output'
138 update_attributes!(state: Final, output_uuid: out_coll, log_uuid: log_coll)
141 def self.full_text_searchable_columns
147 def fill_field_defaults
148 self.state ||= Uncommitted
149 self.environment ||= {}
150 self.runtime_constraints ||= {}
153 self.container_count_max ||= Rails.configuration.container_count_max
154 self.scheduling_parameters ||= {}
155 self.output_ttl ||= 0
159 if (container_uuid_changed? and
160 not current_user.andand.is_admin and
161 not container_uuid.nil?)
162 errors.add :container_uuid, "can only be updated to nil."
165 if state_changed? and state == Committed and container_uuid.nil?
166 self.container_uuid = Container.resolve(self).uuid
168 if self.container_uuid != self.container_uuid_was
169 if self.container_count_changed?
170 errors.add :container_count, "cannot be updated directly."
173 self.container_count += 1
178 def validate_runtime_constraints
183 ['keep_cache_ram', false]].each do |k, required|
184 if !required && !runtime_constraints.include?(k)
187 v = runtime_constraints[k]
188 unless (v.is_a?(Integer) && v > 0)
189 errors.add(:runtime_constraints,
190 "[#{k}]=#{v.inspect} must be a positive integer")
196 def validate_scheduling_parameters
197 if self.state == Committed
198 if scheduling_parameters.include? 'partitions' and
199 (!scheduling_parameters['partitions'].is_a?(Array) ||
200 scheduling_parameters['partitions'].reject{|x| !x.is_a?(String)}.size !=
201 scheduling_parameters['partitions'].size)
202 errors.add :scheduling_parameters, "partitions must be an array of strings"
207 def check_update_whitelist
208 permitted = AttrsPermittedAlways.dup
210 if self.new_record? || self.state_was == Uncommitted
211 # Allow create-and-commit in a single operation.
212 permitted.push *AttrsPermittedBeforeCommit
217 permitted.push :priority, :container_count_max, :container_uuid
219 if self.container_uuid.nil?
220 self.errors.add :container_uuid, "has not been resolved to a container."
223 if self.priority.nil?
224 self.errors.add :priority, "cannot be nil"
227 # Allow container count to increment by 1
228 if (self.container_uuid &&
229 self.container_uuid != self.container_uuid_was &&
230 self.container_count == 1 + (self.container_count_was || 0))
231 permitted.push :container_count
235 if self.state_changed? and not current_user.andand.is_admin
236 self.errors.add :state, "of container request can only be set to Final by system."
239 if self.state_was == Committed
240 permitted.push :output_uuid, :log_uuid
249 if self.state_changed? or
250 self.priority_changed? or
251 self.container_uuid_changed?
252 act_as_system_user do
255 [self.container_uuid_was, self.container_uuid].compact).
256 map(&:update_priority!)
261 def set_requesting_container_uuid
262 return !new_record? if self.requesting_container_uuid # already set
264 token_uuid = current_api_client_authorization.andand.uuid
265 container = Container.where('auth_uuid=?', token_uuid).order('created_at desc').first
266 self.requesting_container_uuid = container.uuid if container