1 require 'whitelist_update'
3 class Container < ArvadosModel
6 include CommonApiTemplate
7 include WhitelistUpdate
9 serialize :environment, Hash
10 serialize :mounts, Hash
11 serialize :runtime_constraints, Hash
12 serialize :command, Array
14 before_validation :fill_field_defaults, :if => :new_record?
15 before_validation :set_timestamps
16 validates :command, :container_image, :output_path, :cwd, :priority, :presence => true
17 validate :validate_state_change
18 validate :validate_change
19 validate :validate_lock
20 after_validation :assign_auth
21 after_save :handle_completed
23 has_many :container_requests, :foreign_key => :container_uuid, :class_name => 'ContainerRequest', :primary_key => :uuid
24 belongs_to :auth, :class_name => 'ApiClientAuthorization', :foreign_key => :auth_uuid, :primary_key => :uuid
26 api_accessible :user, extend: :common do |t|
28 t.add :container_image
40 t.add :runtime_constraints
46 # Supported states for a container
51 (Running = 'Running'),
52 (Complete = 'Complete'),
53 (Cancelled = 'Cancelled')
58 Queued => [Locked, Cancelled],
59 Locked => [Queued, Running, Cancelled],
60 Running => [Complete, Cancelled]
68 if [Queued, Locked, Running].include? self.state
69 # Update the priority of this container to the maximum priority of any of
70 # its committed container requests and save the record.
71 self.priority = ContainerRequest.
72 where(container_uuid: uuid,
73 state: ContainerRequest::Committed).
79 def self.readable_by *users_list
80 if users_list.select { |u| u.is_admin }.any?
83 user_uuids = users_list.map { |u| u.uuid }
84 uuid_list = user_uuids + users_list.flat_map { |u| u.groups_i_can(:read) }
85 permitted_uuids = "(SELECT head_uuid FROM links WHERE link_class='permission' AND tail_uuid IN (?))"
86 joins(:container_requests).where("container_requests.uuid IN #{permitted_uuids} OR container_requests.owner_uuid IN (?)",
92 def fill_field_defaults
94 self.environment ||= {}
95 self.runtime_constraints ||= {}
101 def permission_to_create
102 current_user.andand.is_admin
105 def permission_to_update
106 current_user.andand.is_admin
110 if self.state_changed? and self.state == Running
111 self.started_at ||= db_current_time
114 if self.state_changed? and [Complete, Cancelled].include? self.state
115 self.finished_at ||= db_current_time
123 permitted.push(:owner_uuid, :command, :container_image, :cwd,
124 :environment, :mounts, :output_path, :priority,
125 :runtime_constraints)
130 permitted.push :priority
133 permitted.push :priority, :progress
134 if self.state_changed?
135 permitted.push :started_at
139 if self.state_was == Running
140 permitted.push :finished_at, :output, :log, :exit_code
146 permitted.push :finished_at, :output, :log
148 permitted.push :finished_at
152 # The state_transitions check will add an error message for this
156 check_update_whitelist permitted
160 # If the Container is already locked by someone other than the
161 # current api_client_auth, disallow all changes -- except
162 # priority, which needs to change to reflect max(priority) of
163 # relevant ContainerRequests.
164 if locked_by_uuid_was
165 if locked_by_uuid_was != Thread.current[:api_client_authorization].uuid
166 check_update_whitelist [:priority]
170 if [Locked, Running].include? self.state
171 # If the Container was already locked, locked_by_uuid must not
172 # changes. Otherwise, the current auth gets the lock.
173 need_lock = locked_by_uuid_was || Thread.current[:api_client_authorization].uuid
178 # The caller can provide a new value for locked_by_uuid, but only
179 # if it's exactly what we expect. This allows a caller to perform
180 # an update like {"state":"Unlocked","locked_by_uuid":null}.
181 if self.locked_by_uuid_changed?
182 if self.locked_by_uuid != need_lock
183 return errors.add :locked_by_uuid, "can only change to #{need_lock}"
186 self.locked_by_uuid = need_lock
190 if self.auth_uuid_changed?
191 return errors.add :auth_uuid, 'is readonly'
193 if not [Locked, Running].include? self.state
195 self.auth.andand.update_attributes(expires_at: db_current_time)
202 cr = ContainerRequest.
203 where('container_uuid=? and priority>0', self.uuid).
204 order('priority desc').
207 return errors.add :auth_uuid, "cannot be assigned because priority <= 0"
209 self.auth = ApiClientAuthorization.
210 create!(user_id: User.find_by_uuid(cr.modified_by_user_uuid).id,
215 # This container is finished so finalize any associated container requests
216 # that are associated with this container.
217 if self.state_changed? and [Complete, Cancelled].include? self.state
218 act_as_system_user do
219 # Notify container requests associated with this container
220 ContainerRequest.where(container_uuid: uuid,
221 :state => ContainerRequest::Committed).each do |cr|
222 cr.container_completed!
225 # Try to cancel any outstanding container requests made by this container.
226 ContainerRequest.where(requesting_container_uuid: uuid,
227 :state => ContainerRequest::Committed).each do |cr|