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
15 before_validation :fill_field_defaults, :if => :new_record?
16 before_validation :validate_runtime_constraints
17 before_validation :set_container
18 validates :command, :container_image, :output_path, :cwd, :presence => true
19 validate :validate_state_change
20 validate :validate_change
21 after_save :update_priority
22 after_save :finalize_if_needed
23 before_create :set_requesting_container_uuid
25 api_accessible :user, extend: :common do |t|
27 t.add :container_count
28 t.add :container_count_max
29 t.add :container_image
41 t.add :requesting_container_uuid
42 t.add :runtime_constraints
47 # Supported states for a container request
50 (Uncommitted = 'Uncommitted'),
51 (Committed = 'Committed'),
56 nil => [Uncommitted, Committed],
57 Uncommitted => [Committed],
65 def skip_uuid_read_permission_check
66 # XXX temporary until permissions are sorted out.
67 %w(modified_by_client_uuid container_uuid requesting_container_uuid)
70 def finalize_if_needed
71 if state == Committed && Container.find_by_uuid(container_uuid).final?
79 # Finalize the container request after the container has
82 update_attributes!(state: Final)
83 c = Container.find_by_uuid(container_uuid)
84 ['output', 'log'].each do |out_type|
85 pdh = c.send(out_type)
87 manifest = Collection.where(portable_data_hash: pdh).first.manifest_text
88 Collection.create!(owner_uuid: owner_uuid,
89 manifest_text: manifest,
90 portable_data_hash: pdh,
91 name: "Container #{out_type} for request #{uuid}",
94 'container_request' => uuid,
101 def fill_field_defaults
102 self.state ||= Uncommitted
103 self.environment ||= {}
104 self.runtime_constraints ||= {}
107 self.container_count_max ||= Rails.configuration.container_count_max
110 # Create a new container (or find an existing one) to satisfy this
113 c_mounts = mounts_for_container
114 c_runtime_constraints = runtime_constraints_for_container
115 c_container_image = container_image_for_container
116 c = act_as_system_user do
117 c_attrs = {command: self.command,
119 environment: self.environment,
120 output_path: self.output_path,
121 container_image: c_container_image,
123 runtime_constraints: c_runtime_constraints}
125 reusable = self.use_existing ? Container.find_reusable(c_attrs) : nil
129 Container.create!(c_attrs)
132 self.container_uuid = c.uuid
135 # Return a runtime_constraints hash that complies with
136 # self.runtime_constraints but is suitable for saving in a container
137 # record, i.e., has specific values instead of ranges.
139 # Doing this as a step separate from other resolutions, like "git
140 # revision range to commit hash", makes sense only when there is no
141 # opportunity to reuse an existing container (e.g., container reuse
142 # is not implemented yet, or we have already found that no existing
143 # containers are suitable).
144 def runtime_constraints_for_container
146 runtime_constraints.each do |k, v|
156 # Return a mounts hash suitable for a Container, i.e., with every
157 # readonly collection UUID resolved to a PDH.
158 def mounts_for_container
160 mounts.each do |k, mount|
163 if mount['kind'] != 'collection'
166 if (uuid = mount.delete 'uuid')
168 readable_by(current_user).
170 select(:portable_data_hash).
173 raise ArvadosModel::UnresolvableContainerError.new "cannot mount collection #{uuid.inspect}: not found"
175 if mount['portable_data_hash'].nil?
176 # PDH not supplied by client
177 mount['portable_data_hash'] = c.portable_data_hash
178 elsif mount['portable_data_hash'] != c.portable_data_hash
179 # UUID and PDH supplied by client, but they don't agree
180 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"
187 # Return a container_image PDH suitable for a Container.
188 def container_image_for_container
189 coll = Collection.for_latest_docker_image(container_image)
191 raise ArvadosModel::UnresolvableContainerError.new "docker image #{container_image.inspect} not found"
193 return coll.portable_data_hash
197 if (container_uuid_changed? and
198 not current_user.andand.is_admin and
199 not container_uuid.nil?)
200 errors.add :container_uuid, "can only be updated to nil."
203 if state_changed? and state == Committed and container_uuid.nil?
206 if self.container_uuid != self.container_uuid_was
207 if self.container_count_changed?
208 errors.add :container_count, "cannot be updated directly."
211 self.container_count += 1
216 def validate_runtime_constraints
219 ['vcpus', 'ram'].each do |k|
220 if not (runtime_constraints.include? k and
221 runtime_constraints[k].is_a? Integer and
222 runtime_constraints[k] > 0)
223 errors.add :runtime_constraints, "#{k} must be a positive integer"
227 if runtime_constraints.include? 'keep_cache_ram' and
228 (!runtime_constraints['keep_cache_ram'].is_a?(Integer) or
229 runtime_constraints['keep_cache_ram'] <= 0)
230 errors.add :runtime_constraints, "keep_cache_ram must be a positive integer"
231 elsif !runtime_constraints.include? 'keep_cache_ram'
232 runtime_constraints['keep_cache_ram'] = Rails.configuration.container_default_keep_cache_ram
238 permitted = [:owner_uuid]
242 # Permit updating most fields
243 permitted.push :command, :container_count_max,
244 :container_image, :cwd, :description, :environment,
245 :filters, :mounts, :name, :output_path, :priority,
246 :properties, :requesting_container_uuid, :runtime_constraints,
247 :state, :container_uuid, :use_existing
250 if container_uuid.nil?
251 errors.add :container_uuid, "has not been resolved to a container."
255 errors.add :priority, "cannot be nil"
258 # Can update priority, container count, name and description
259 permitted.push :priority, :container_count, :container_count_max, :container_uuid, :name, :description
261 if self.state_changed?
262 # Allow create-and-commit in a single operation.
263 permitted.push :command, :container_image, :cwd, :description, :environment,
264 :filters, :mounts, :name, :output_path, :properties,
265 :requesting_container_uuid, :runtime_constraints,
266 :state, :container_uuid
270 if not current_user.andand.is_admin and not (self.name_changed? || self.description_changed?)
271 errors.add :state, "of container request can only be set to Final by system."
274 if self.state_changed? || self.name_changed? || self.description_changed?
275 permitted.push :state, :name, :description
277 errors.add :state, "does not allow updates"
281 errors.add :state, "invalid value"
284 check_update_whitelist permitted
288 if self.state_changed? or
289 self.priority_changed? or
290 self.container_uuid_changed?
291 act_as_system_user do
294 [self.container_uuid_was, self.container_uuid].compact).
295 map(&:update_priority!)
300 def set_requesting_container_uuid
301 return !new_record? if self.requesting_container_uuid # already set
303 token_uuid = current_api_client_authorization.andand.uuid
304 container = Container.where('auth_uuid=?', token_uuid).order('created_at desc').first
305 self.requesting_container_uuid = container.uuid if container