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
22 api_accessible :user, extend: :common do |t|
24 t.add :container_count_max
25 t.add :container_image
37 t.add :requesting_container_uuid
38 t.add :runtime_constraints
42 # Supported states for a container request
45 (Uncommitted = 'Uncommitted'),
46 (Committed = 'Committed'),
51 nil => [Uncommitted, Committed],
52 Uncommitted => [Committed],
60 def skip_uuid_read_permission_check
61 # XXX temporary until permissions are sorted out.
62 %w(modified_by_client_uuid container_uuid requesting_container_uuid)
65 def container_completed!
66 # may implement retry logic here in the future.
67 self.state = ContainerRequest::Final
73 def fill_field_defaults
74 self.state ||= Uncommitted
75 self.environment ||= {}
76 self.runtime_constraints ||= {}
81 # Create a new container (or find an existing one) to satisfy this
84 # TODO: resolve symbolic git and keep references to content
86 c = act_as_system_user do
87 Container.create!(command: self.command,
88 container_image: self.container_image,
90 environment: self.environment,
92 output_path: self.output_path,
93 runtime_constraints: self.runtime_constraints)
95 self.container_uuid = c.uuid
99 if (container_uuid_changed? and
100 not current_user.andand.is_admin and
101 not container_uuid.nil?)
102 errors.add :container_uuid, "can only be updated to nil."
105 if state_changed? and state == Committed and container_uuid.nil?
111 permitted = [:owner_uuid]
115 # Permit updating most fields
116 permitted.push :command, :container_count_max,
117 :container_image, :cwd, :description, :environment,
118 :filters, :mounts, :name, :output_path, :priority,
119 :properties, :requesting_container_uuid, :runtime_constraints,
120 :state, :container_uuid
123 if container_uuid.nil?
124 errors.add :container_uuid, "has not been resolved to a container."
128 errors.add :priority, "cannot be nil"
131 # Can update priority, container count.
132 permitted.push :priority, :container_count_max, :container_uuid
134 if self.state_changed?
135 # Allow create-and-commit in a single operation.
136 permitted.push :command, :container_image, :cwd, :description, :environment,
137 :filters, :mounts, :name, :output_path, :properties,
138 :requesting_container_uuid, :runtime_constraints,
139 :state, :container_uuid
143 if not current_user.andand.is_admin
144 errors.add :state, "of container request can only be set to Final by system."
147 if self.state_changed?
148 permitted.push :state
150 errors.add :state, "does not allow updates"
154 errors.add :state, "invalid value"
157 check_update_whitelist permitted
161 if self.state_changed? or
162 self.priority_changed? or
163 self.container_uuid_changed?
164 act_as_system_user do
167 [self.container_uuid_was, self.container_uuid].compact).
168 map(&:update_priority!)