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 validate :validate_state_change
22 validate :validate_change
23 after_save :update_priority
24 after_save :finalize_if_needed
25 before_create :set_requesting_container_uuid
27 api_accessible :user, extend: :common do |t|
29 t.add :container_count
30 t.add :container_count_max
31 t.add :container_image
46 t.add :requesting_container_uuid
47 t.add :runtime_constraints
48 t.add :scheduling_parameters
53 # Supported states for a container request
56 (Uncommitted = 'Uncommitted'),
57 (Committed = 'Committed'),
62 nil => [Uncommitted, Committed],
63 Uncommitted => [Committed],
71 def skip_uuid_read_permission_check
72 # XXX temporary until permissions are sorted out.
73 %w(modified_by_client_uuid container_uuid requesting_container_uuid)
76 def finalize_if_needed
77 if state == Committed && Container.find_by_uuid(container_uuid).final?
85 # Finalize the container request after the container has
90 c = Container.find_by_uuid(container_uuid)
91 ['output', 'log'].each do |out_type|
92 pdh = c.send(out_type)
94 if self.output_name and out_type == 'output'
95 coll_name = self.output_name
97 coll_name = "Container #{out_type} for request #{uuid}"
99 manifest = Collection.where(portable_data_hash: pdh).first.manifest_text
101 coll = Collection.create!(owner_uuid: owner_uuid,
102 manifest_text: manifest,
103 portable_data_hash: pdh,
107 'container_request' => uuid,
109 rescue ActiveRecord::RecordNotUnique => rn
110 # In case this is executed as part of a transaction: When a Postgres exception happens,
111 # the following statements on the same transaction become invalid, so a rollback is
112 # needed. One example are Unit Tests, every test is enclosed inside a transaction so
113 # that the database can be reverted before every new test starts.
114 # See: http://api.rubyonrails.org/classes/ActiveRecord/Transactions/ClassMethods.html#module-ActiveRecord::Transactions::ClassMethods-label-Exception+handling+and+rolling+back
115 ActiveRecord::Base.connection.execute 'ROLLBACK'
116 raise unless out_type == 'output' and self.output_name
117 # Postgres specific unique name check. See ApplicationController#create for
118 # a detailed explanation.
119 raise unless rn.original_exception.is_a? PG::UniqueViolation
120 err = rn.original_exception
121 detail = err.result.error_field(PG::Result::PG_DIAG_MESSAGE_DETAIL)
122 raise unless /^Key \(owner_uuid, name\)=\([a-z0-9]{5}-[a-z0-9]{5}-[a-z0-9]{15}, .*?\) already exists\./.match detail
123 # Output collection name collision detected: append a timestamp.
124 coll_name = "#{self.output_name} #{Time.now.getgm.strftime('%FT%TZ')}"
127 if out_type == 'output'
133 update_attributes!(state: Final, output_uuid: out_coll, log_uuid: log_coll)
138 def fill_field_defaults
139 self.state ||= Uncommitted
140 self.environment ||= {}
141 self.runtime_constraints ||= {}
144 self.container_count_max ||= Rails.configuration.container_count_max
145 self.scheduling_parameters ||= {}
148 # Create a new container (or find an existing one) to satisfy this
151 c_mounts = mounts_for_container
152 c_runtime_constraints = runtime_constraints_for_container
153 c_container_image = container_image_for_container
154 c = act_as_system_user do
155 c_attrs = {command: self.command,
157 environment: self.environment,
158 output_path: self.output_path,
159 container_image: c_container_image,
161 runtime_constraints: c_runtime_constraints}
163 reusable = self.use_existing ? Container.find_reusable(c_attrs) : nil
167 c_attrs[:scheduling_parameters] = self.scheduling_parameters
168 Container.create!(c_attrs)
171 self.container_uuid = c.uuid
174 # Return a runtime_constraints hash that complies with
175 # self.runtime_constraints but is suitable for saving in a container
176 # record, i.e., has specific values instead of ranges.
178 # Doing this as a step separate from other resolutions, like "git
179 # revision range to commit hash", makes sense only when there is no
180 # opportunity to reuse an existing container (e.g., container reuse
181 # is not implemented yet, or we have already found that no existing
182 # containers are suitable).
183 def runtime_constraints_for_container
185 runtime_constraints.each do |k, v|
195 # Return a mounts hash suitable for a Container, i.e., with every
196 # readonly collection UUID resolved to a PDH.
197 def mounts_for_container
199 mounts.each do |k, mount|
202 if mount['kind'] != 'collection'
205 if (uuid = mount.delete 'uuid')
207 readable_by(current_user).
209 select(:portable_data_hash).
212 raise ArvadosModel::UnresolvableContainerError.new "cannot mount collection #{uuid.inspect}: not found"
214 if mount['portable_data_hash'].nil?
215 # PDH not supplied by client
216 mount['portable_data_hash'] = c.portable_data_hash
217 elsif mount['portable_data_hash'] != c.portable_data_hash
218 # UUID and PDH supplied by client, but they don't agree
219 raise ArgumentError.new "cannot mount collection #{uuid.inspect}: current portable_data_hash #{c.portable_data_hash.inspect} does not match #{c['portable_data_hash'].inspect} in request"
226 # Return a container_image PDH suitable for a Container.
227 def container_image_for_container
228 coll = Collection.for_latest_docker_image(container_image)
230 raise ArvadosModel::UnresolvableContainerError.new "docker image #{container_image.inspect} not found"
232 return coll.portable_data_hash
236 if (container_uuid_changed? and
237 not current_user.andand.is_admin and
238 not container_uuid.nil?)
239 errors.add :container_uuid, "can only be updated to nil."
242 if state_changed? and state == Committed and container_uuid.nil?
245 if self.container_uuid != self.container_uuid_was
246 if self.container_count_changed?
247 errors.add :container_count, "cannot be updated directly."
250 self.container_count += 1
255 def validate_runtime_constraints
258 ['vcpus', 'ram'].each do |k|
259 if not (runtime_constraints.include? k and
260 runtime_constraints[k].is_a? Integer and
261 runtime_constraints[k] > 0)
262 errors.add :runtime_constraints, "#{k} must be a positive integer"
266 if runtime_constraints.include? 'keep_cache_ram' and
267 (!runtime_constraints['keep_cache_ram'].is_a?(Integer) or
268 runtime_constraints['keep_cache_ram'] <= 0)
269 errors.add :runtime_constraints, "keep_cache_ram must be a positive integer"
270 elsif !runtime_constraints.include? 'keep_cache_ram'
271 runtime_constraints['keep_cache_ram'] = Rails.configuration.container_default_keep_cache_ram
276 def validate_scheduling_parameters
277 if self.state == Committed
278 if scheduling_parameters.include? 'partitions' and
279 (!scheduling_parameters['partitions'].is_a?(Array) ||
280 scheduling_parameters['partitions'].reject{|x| !x.is_a?(String)}.size !=
281 scheduling_parameters['partitions'].size)
282 errors.add :scheduling_parameters, "partitions must be an array of strings"
288 permitted = [:owner_uuid]
292 # Permit updating most fields
293 permitted.push :command, :container_count_max,
294 :container_image, :cwd, :description, :environment,
295 :filters, :mounts, :name, :output_path, :priority,
296 :properties, :requesting_container_uuid, :runtime_constraints,
297 :state, :container_uuid, :use_existing, :scheduling_parameters,
301 if container_uuid.nil?
302 errors.add :container_uuid, "has not been resolved to a container."
306 errors.add :priority, "cannot be nil"
309 # Can update priority, container count, name and description
310 permitted.push :priority, :container_count, :container_count_max, :container_uuid,
313 if self.state_changed?
314 # Allow create-and-commit in a single operation.
315 permitted.push :command, :container_image, :cwd, :description, :environment,
316 :filters, :mounts, :name, :output_path, :properties,
317 :requesting_container_uuid, :runtime_constraints,
318 :state, :container_uuid, :use_existing, :scheduling_parameters,
323 if not current_user.andand.is_admin and not (self.name_changed? || self.description_changed?)
324 errors.add :state, "of container request can only be set to Final by system."
327 if self.state_changed? || self.name_changed? || self.description_changed? || self.output_uuid_changed? || self.log_uuid_changed?
328 permitted.push :state, :name, :description, :output_uuid, :log_uuid
330 errors.add :state, "does not allow updates"
334 errors.add :state, "invalid value"
337 check_update_whitelist permitted
341 if self.state_changed? or
342 self.priority_changed? or
343 self.container_uuid_changed?
344 act_as_system_user do
347 [self.container_uuid_was, self.container_uuid].compact).
348 map(&:update_priority!)
353 def set_requesting_container_uuid
354 return !new_record? if self.requesting_container_uuid # already set
356 token_uuid = current_api_client_authorization.andand.uuid
357 container = Container.where('auth_uuid=?', token_uuid).order('created_at desc').first
358 self.requesting_container_uuid = container.uuid if container