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, :presence => true
17 validate :validate_change
19 has_many :container_requests, :foreign_key => :container_uuid, :class_name => 'ContainerRequest', :primary_key => :uuid
21 api_accessible :user, extend: :common do |t|
23 t.add :container_image
33 t.add :runtime_constraints
38 # Supported states for a container
42 (Running = 'Running'),
43 (Complete = 'Complete'),
44 (Cancelled = 'Cancelled')
49 def fill_field_defaults
51 self.environment ||= {}
52 self.runtime_constraints ||= {}
58 def permission_to_create
59 current_user.andand.is_admin
62 def permission_to_update
63 current_user.andand.is_admin
67 if self.state_changed? and self.state == Running
68 self.started_at ||= db_current_time
71 if self.state_changed? and [Complete, Cancelled].include? self.state
72 self.finished_at ||= db_current_time
80 permitted.push :owner_uuid, :command, :container_image, :cwd, :environment,
81 :mounts, :output_path, :priority, :runtime_constraints, :state
86 # permit priority change only.
87 permitted.push :priority
89 if self.state_changed? and not self.state_was.nil?
90 errors.add :state, "Can only go to Queued from nil"
94 if self.state_changed?
95 # At point of state change, can only set state and started_at
96 if self.state_was == Queued
97 permitted.push :state, :started_at
99 errors.add :state, "Can only go from Queued to Running"
102 # While running, can update priority and progress.
103 permitted.push :priority, :progress
107 if self.state_changed?
108 if self.state_was == Running
109 permitted.push :state, :finished_at, :output, :log
111 errors.add :state, "Cannot go from #{self.state_was} to #{self.state}"
114 errors.add :state, "Cannot update record in Complete state"
118 if self.state_changed?
119 if self.state_was == Running
120 permitted.push :state, :finished_at, :output, :log
121 elsif self.state_was == Queued
122 permitted.push :state, :finished_at
124 errors.add :state, "Cannot go from #{self.state_was} to #{self.state}"
127 errors.add :state, "Cannot update record in Cancelled state"
131 errors.add :state, "Invalid state #{self.state}"
134 check_update_whitelist permitted