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 before_save :sort_serialized_attrs
22 after_save :handle_completed
24 has_many :container_requests, :foreign_key => :container_uuid, :class_name => 'ContainerRequest', :primary_key => :uuid
25 belongs_to :auth, :class_name => 'ApiClientAuthorization', :foreign_key => :auth_uuid, :primary_key => :uuid
27 api_accessible :user, extend: :common do |t|
29 t.add :container_image
41 t.add :runtime_constraints
47 # Supported states for a container
52 (Running = 'Running'),
53 (Complete = 'Complete'),
54 (Cancelled = 'Cancelled')
59 Queued => [Locked, Cancelled],
60 Locked => [Queued, Running, Cancelled],
61 Running => [Complete, Cancelled]
69 if [Queued, Locked, Running].include? self.state
70 # Update the priority of this container to the maximum priority of any of
71 # its committed container requests and save the record.
72 self.priority = ContainerRequest.
73 where(container_uuid: uuid,
74 state: ContainerRequest::Committed).
82 def self.deep_sort_hash(x)
84 x.sort.collect do |k, v|
85 [k, deep_sort_hash(v)]
88 x.collect { |v| deep_sort_hash(v) }
94 def fill_field_defaults
96 self.environment ||= {}
97 self.runtime_constraints ||= {}
103 def permission_to_create
104 current_user.andand.is_admin
107 def permission_to_update
108 current_user.andand.is_admin
112 if self.state_changed? and self.state == Running
113 self.started_at ||= db_current_time
116 if self.state_changed? and [Complete, Cancelled].include? self.state
117 self.finished_at ||= db_current_time
125 permitted.push(:owner_uuid, :command, :container_image, :cwd,
126 :environment, :mounts, :output_path, :priority,
127 :runtime_constraints)
132 permitted.push :priority
135 permitted.push :priority, :progress
136 if self.state_changed?
137 permitted.push :started_at
141 if self.state_was == Running
142 permitted.push :finished_at, :output, :log, :exit_code
148 permitted.push :finished_at, :output, :log
150 permitted.push :finished_at
154 # The state_transitions check will add an error message for this
158 check_update_whitelist permitted
162 # If the Container is already locked by someone other than the
163 # current api_client_auth, disallow all changes -- except
164 # priority, which needs to change to reflect max(priority) of
165 # relevant ContainerRequests.
166 if locked_by_uuid_was
167 if locked_by_uuid_was != Thread.current[:api_client_authorization].uuid
168 check_update_whitelist [:priority]
172 if [Locked, Running].include? self.state
173 # If the Container was already locked, locked_by_uuid must not
174 # changes. Otherwise, the current auth gets the lock.
175 need_lock = locked_by_uuid_was || Thread.current[:api_client_authorization].uuid
180 # The caller can provide a new value for locked_by_uuid, but only
181 # if it's exactly what we expect. This allows a caller to perform
182 # an update like {"state":"Unlocked","locked_by_uuid":null}.
183 if self.locked_by_uuid_changed?
184 if self.locked_by_uuid != need_lock
185 return errors.add :locked_by_uuid, "can only change to #{need_lock}"
188 self.locked_by_uuid = need_lock
192 if self.auth_uuid_changed?
193 return errors.add :auth_uuid, 'is readonly'
195 if not [Locked, Running].include? self.state
197 self.auth.andand.update_attributes(expires_at: db_current_time)
204 cr = ContainerRequest.
205 where('container_uuid=? and priority>0', self.uuid).
206 order('priority desc').
209 return errors.add :auth_uuid, "cannot be assigned because priority <= 0"
211 self.auth = ApiClientAuthorization.
212 create!(user_id: User.find_by_uuid(cr.modified_by_user_uuid).id,
216 def sort_serialized_attrs
217 self.environment = self.class.deep_sort_hash(self.environment)
218 self.mounts = self.class.deep_sort_hash(self.mounts)
219 self.runtime_constraints = self.class.deep_sort_hash(self.runtime_constraints)
223 # This container is finished so finalize any associated container requests
224 # that are associated with this container.
225 if self.state_changed? and [Complete, Cancelled].include? self.state
226 act_as_system_user do
227 # Notify container requests associated with this container
228 ContainerRequest.where(container_uuid: uuid,
229 :state => ContainerRequest::Committed).each do |cr|
230 cr.container_completed!
233 # Try to cancel any outstanding container requests made by this container.
234 ContainerRequest.where(requesting_container_uuid: uuid,
235 :state => ContainerRequest::Committed).each do |cr|