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 :set_container
17 validates :command, :container_image, :output_path, :cwd, :presence => true
18 validate :validate_state_change
19 validate :validate_change
20 validate :validate_runtime_constraints
21 after_save :update_priority
22 before_create :set_requesting_container_uuid
24 api_accessible :user, extend: :common do |t|
26 t.add :container_count_max
27 t.add :container_image
39 t.add :requesting_container_uuid
40 t.add :runtime_constraints
44 # Supported states for a container request
47 (Uncommitted = 'Uncommitted'),
48 (Committed = 'Committed'),
53 nil => [Uncommitted, Committed],
54 Uncommitted => [Committed],
62 def skip_uuid_read_permission_check
63 # XXX temporary until permissions are sorted out.
64 %w(modified_by_client_uuid container_uuid requesting_container_uuid)
67 # Finalize the container request after the container has
69 def container_completed!
70 update_attributes!(state: ContainerRequest::Final)
71 c = Container.find_by_uuid(container_uuid)
72 ['output', 'log'].each do |out_type|
73 pdh = c.send(out_type)
75 manifest = Collection.where(portable_data_hash: pdh).first.manifest_text
76 Collection.create!(owner_uuid: owner_uuid,
77 manifest_text: manifest,
78 portable_data_hash: pdh,
79 name: "Container #{out_type} for request #{uuid}",
82 'container_request' => uuid,
89 def fill_field_defaults
90 self.state ||= Uncommitted
91 self.environment ||= {}
92 self.runtime_constraints ||= {}
97 # Create a new container (or find an existing one) to satisfy this
100 c_mounts = mounts_for_container
101 c_runtime_constraints = runtime_constraints_for_container
102 c_container_image = container_image_for_container
103 c = act_as_system_user do
104 c_attrs = {command: self.command,
106 environment: self.environment,
107 output_path: self.output_path,
108 container_image: c_container_image,
110 runtime_constraints: c_runtime_constraints}
111 reusable = Container.find_reusable(c_attrs)
115 Container.create!(c_attrs)
118 self.container_uuid = c.uuid
121 # Return a runtime_constraints hash that complies with
122 # self.runtime_constraints but is suitable for saving in a container
123 # record, i.e., has specific values instead of ranges.
125 # Doing this as a step separate from other resolutions, like "git
126 # revision range to commit hash", makes sense only when there is no
127 # opportunity to reuse an existing container (e.g., container reuse
128 # is not implemented yet, or we have already found that no existing
129 # containers are suitable).
130 def runtime_constraints_for_container
132 runtime_constraints.each do |k, v|
142 # Return a mounts hash suitable for a Container, i.e., with every
143 # readonly collection UUID resolved to a PDH.
144 def mounts_for_container
146 mounts.each do |k, mount|
149 if mount['kind'] != 'collection'
152 if (uuid = mount.delete 'uuid')
154 readable_by(current_user).
156 select(:portable_data_hash).
159 raise ArvadosModel::UnresolvableContainerError.new "cannot mount collection #{uuid.inspect}: not found"
161 if mount['portable_data_hash'].nil?
162 # PDH not supplied by client
163 mount['portable_data_hash'] = c.portable_data_hash
164 elsif mount['portable_data_hash'] != c.portable_data_hash
165 # UUID and PDH supplied by client, but they don't agree
166 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"
173 # Return a container_image PDH suitable for a Container.
174 def container_image_for_container
175 coll = Collection.for_latest_docker_image(container_image)
177 raise ArvadosModel::UnresolvableContainerError.new "docker image #{container_image.inspect} not found"
179 return coll.portable_data_hash
183 if (container_uuid_changed? and
184 not current_user.andand.is_admin and
185 not container_uuid.nil?)
186 errors.add :container_uuid, "can only be updated to nil."
189 if state_changed? and state == Committed and container_uuid.nil?
194 def validate_runtime_constraints
197 ['vcpus', 'ram'].each do |k|
198 if not (runtime_constraints.include? k and
199 runtime_constraints[k].is_a? Integer and
200 runtime_constraints[k] > 0)
201 errors.add :runtime_constraints, "#{k} must be a positive integer"
208 permitted = [:owner_uuid]
212 # Permit updating most fields
213 permitted.push :command, :container_count_max,
214 :container_image, :cwd, :description, :environment,
215 :filters, :mounts, :name, :output_path, :priority,
216 :properties, :requesting_container_uuid, :runtime_constraints,
217 :state, :container_uuid
220 if container_uuid.nil?
221 errors.add :container_uuid, "has not been resolved to a container."
225 errors.add :priority, "cannot be nil"
228 # Can update priority, container count, name and description
229 permitted.push :priority, :container_count_max, :container_uuid, :name, :description
231 if self.state_changed?
232 # Allow create-and-commit in a single operation.
233 permitted.push :command, :container_image, :cwd, :description, :environment,
234 :filters, :mounts, :name, :output_path, :properties,
235 :requesting_container_uuid, :runtime_constraints,
236 :state, :container_uuid
240 if not current_user.andand.is_admin and not (self.name_changed? || self.description_changed?)
241 errors.add :state, "of container request can only be set to Final by system."
244 if self.state_changed? || self.name_changed? || self.description_changed?
245 permitted.push :state, :name, :description
247 errors.add :state, "does not allow updates"
251 errors.add :state, "invalid value"
254 check_update_whitelist permitted
258 if self.state_changed? or
259 self.priority_changed? or
260 self.container_uuid_changed?
261 act_as_system_user do
264 [self.container_uuid_was, self.container_uuid].compact).
265 map(&:update_priority!)
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
275 self.requesting_container_uuid = container.uuid if container