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
27 t.add :container_count_max
28 t.add :container_image
40 t.add :requesting_container_uuid
41 t.add :runtime_constraints
45 # Supported states for a container request
48 (Uncommitted = 'Uncommitted'),
49 (Committed = 'Committed'),
54 nil => [Uncommitted, Committed],
55 Uncommitted => [Committed],
63 def skip_uuid_read_permission_check
64 # XXX temporary until permissions are sorted out.
65 %w(modified_by_client_uuid container_uuid requesting_container_uuid)
68 # Finalize the container request after the container has
70 def container_completed!
71 update_attributes!(state: ContainerRequest::Final)
72 c = Container.find_by_uuid(container_uuid)
73 ['output', 'log'].each do |out_type|
74 pdh = c.send(out_type)
76 manifest = Collection.where(portable_data_hash: pdh).first.manifest_text
77 Collection.create!(owner_uuid: owner_uuid,
78 manifest_text: manifest,
79 portable_data_hash: pdh,
80 name: "Container #{out_type} for request #{uuid}",
83 'container_request' => uuid,
90 def fill_field_defaults
91 self.state ||= Uncommitted
92 self.environment ||= {}
93 self.runtime_constraints ||= {}
96 self.container_count_max ||= Rails.configuration.container_count_max
99 # Create a new container (or find an existing one) to satisfy this
102 c_mounts = mounts_for_container
103 c_runtime_constraints = runtime_constraints_for_container
104 c_container_image = container_image_for_container
105 c = act_as_system_user do
106 c_attrs = {command: self.command,
108 environment: self.environment,
109 output_path: self.output_path,
110 container_image: c_container_image,
112 runtime_constraints: c_runtime_constraints}
113 reusable = Container.find_reusable(c_attrs)
117 Container.create!(c_attrs)
120 self.container_uuid = c.uuid
123 # Return a runtime_constraints hash that complies with
124 # self.runtime_constraints but is suitable for saving in a container
125 # record, i.e., has specific values instead of ranges.
127 # Doing this as a step separate from other resolutions, like "git
128 # revision range to commit hash", makes sense only when there is no
129 # opportunity to reuse an existing container (e.g., container reuse
130 # is not implemented yet, or we have already found that no existing
131 # containers are suitable).
132 def runtime_constraints_for_container
134 runtime_constraints.each do |k, v|
144 # Return a mounts hash suitable for a Container, i.e., with every
145 # readonly collection UUID resolved to a PDH.
146 def mounts_for_container
148 mounts.each do |k, mount|
151 if mount['kind'] != 'collection'
154 if (uuid = mount.delete 'uuid')
156 readable_by(current_user).
158 select(:portable_data_hash).
161 raise ArvadosModel::UnresolvableContainerError.new "cannot mount collection #{uuid.inspect}: not found"
163 if mount['portable_data_hash'].nil?
164 # PDH not supplied by client
165 mount['portable_data_hash'] = c.portable_data_hash
166 elsif mount['portable_data_hash'] != c.portable_data_hash
167 # UUID and PDH supplied by client, but they don't agree
168 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"
175 # Return a container_image PDH suitable for a Container.
176 def container_image_for_container
177 coll = Collection.for_latest_docker_image(container_image)
179 raise ArvadosModel::UnresolvableContainerError.new "docker image #{container_image.inspect} not found"
181 return coll.portable_data_hash
185 if (container_uuid_changed? and
186 not current_user.andand.is_admin and
187 not container_uuid.nil?)
188 errors.add :container_uuid, "can only be updated to nil."
191 if state_changed? and state == Committed and container_uuid.nil?
194 if self.container_uuid != self.container_uuid_was
195 if self.container_count_changed?
196 errors.add :container_count, "cannot be updated directly."
199 self.container_count += 1
204 def validate_runtime_constraints
207 ['vcpus', 'ram'].each do |k|
208 if not (runtime_constraints.include? k and
209 runtime_constraints[k].is_a? Integer and
210 runtime_constraints[k] > 0)
211 errors.add :runtime_constraints, "#{k} must be a positive integer"
218 permitted = [:owner_uuid]
222 # Permit updating most fields
223 permitted.push :command, :container_count_max,
224 :container_image, :cwd, :description, :environment,
225 :filters, :mounts, :name, :output_path, :priority,
226 :properties, :requesting_container_uuid, :runtime_constraints,
227 :state, :container_uuid
230 if container_uuid.nil?
231 errors.add :container_uuid, "has not been resolved to a container."
235 errors.add :priority, "cannot be nil"
238 # Can update priority, container count, name and description
239 permitted.push :priority, :container_count, :container_count_max, :container_uuid, :name, :description
241 if self.state_changed?
242 # Allow create-and-commit in a single operation.
243 permitted.push :command, :container_image, :cwd, :description, :environment,
244 :filters, :mounts, :name, :output_path, :properties,
245 :requesting_container_uuid, :runtime_constraints,
246 :state, :container_uuid
250 if not current_user.andand.is_admin and not (self.name_changed? || self.description_changed?)
251 errors.add :state, "of container request can only be set to Final by system."
254 if self.state_changed? || self.name_changed? || self.description_changed?
255 permitted.push :state, :name, :description
257 errors.add :state, "does not allow updates"
261 errors.add :state, "invalid value"
264 check_update_whitelist permitted
268 if self.state_changed? or
269 self.priority_changed? or
270 self.container_uuid_changed?
271 act_as_system_user do
274 [self.container_uuid_was, self.container_uuid].compact).
275 map(&:update_priority!)
280 def set_requesting_container_uuid
281 return !new_record? if self.requesting_container_uuid # already set
283 token_uuid = current_api_client_authorization.andand.uuid
284 container = Container.where('auth_uuid=?', token_uuid).order('created_at desc').first
285 self.requesting_container_uuid = container.uuid if container