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 after_save :update_priority
21 before_create :set_requesting_container_uuid
23 api_accessible :user, extend: :common do |t|
25 t.add :container_count_max
26 t.add :container_image
38 t.add :requesting_container_uuid
39 t.add :runtime_constraints
43 # Supported states for a container request
46 (Uncommitted = 'Uncommitted'),
47 (Committed = 'Committed'),
52 nil => [Uncommitted, Committed],
53 Uncommitted => [Committed],
61 def skip_uuid_read_permission_check
62 # XXX temporary until permissions are sorted out.
63 %w(modified_by_client_uuid container_uuid requesting_container_uuid)
66 def container_completed!
67 # may implement retry logic here in the future.
68 self.state = ContainerRequest::Final
74 def fill_field_defaults
75 self.state ||= Uncommitted
76 self.environment ||= {}
77 self.runtime_constraints ||= {}
82 # Create a new container (or find an existing one) to satisfy this
85 # TODO: resolve symbolic git and keep references to content
87 c = act_as_system_user do
88 Container.create!(command: self.command,
89 container_image: self.container_image,
91 environment: self.environment,
93 output_path: self.output_path,
94 runtime_constraints: self.runtime_constraints)
96 self.container_uuid = c.uuid
100 if (container_uuid_changed? and
101 not current_user.andand.is_admin and
102 not container_uuid.nil?)
103 errors.add :container_uuid, "can only be updated to nil."
106 if state_changed? and state == Committed and container_uuid.nil?
112 permitted = [:owner_uuid]
116 # Permit updating most fields
117 permitted.push :command, :container_count_max,
118 :container_image, :cwd, :description, :environment,
119 :filters, :mounts, :name, :output_path, :priority,
120 :properties, :requesting_container_uuid, :runtime_constraints,
121 :state, :container_uuid
124 if container_uuid.nil?
125 errors.add :container_uuid, "has not been resolved to a container."
129 errors.add :priority, "cannot be nil"
132 # Can update priority, container count, name and description
133 permitted.push :priority, :container_count_max, :container_uuid, :name, :description
135 if self.state_changed?
136 # Allow create-and-commit in a single operation.
137 permitted.push :command, :container_image, :cwd, :description, :environment,
138 :filters, :mounts, :name, :output_path, :properties,
139 :requesting_container_uuid, :runtime_constraints,
140 :state, :container_uuid
144 if not current_user.andand.is_admin and not (self.name_changed? || self.description_changed?)
145 errors.add :state, "of container request can only be set to Final by system."
148 if self.state_changed? || self.name_changed? || self.description_changed?
149 permitted.push :state, :name, :description
151 errors.add :state, "does not allow updates"
155 errors.add :state, "invalid value"
158 check_update_whitelist permitted
162 if self.state_changed? or
163 self.priority_changed? or
164 self.container_uuid_changed?
165 act_as_system_user do
168 [self.container_uuid_was, self.container_uuid].compact).
169 map(&:update_priority!)
174 def set_requesting_container_uuid
175 return true if self.requesting_container_uuid # already set
177 token_uuid = current_api_client_authorization.andand.uuid
178 container = Container.where('auth_uuid=?', token_uuid).order('created_at desc').first
179 self.requesting_container_uuid = container.uuid if container