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)
67 def fill_field_defaults
68 self.state ||= Uncommitted
69 self.environment ||= {}
70 self.runtime_constraints ||= {}
76 # Turn a container request into a container.
78 # In the future this will do things like resolve symbolic git and keep
79 # references to content addresses.
80 Container.create!({ :command => self.command,
81 :container_image => self.container_image,
83 :environment => self.environment,
84 :mounts => self.mounts,
85 :output_path => self.output_path,
86 :runtime_constraints => self.runtime_constraints })
90 if self.container_uuid_changed?
91 if not current_user.andand.is_admin and not self.container_uuid.nil?
92 errors.add :container_uuid, "can only be updated to nil."
95 if self.state_changed?
96 if self.state == Committed and (self.state_was == Uncommitted or self.state_was.nil?)
98 self.container_uuid = self.resolve.andand.uuid
106 permitted = [:owner_uuid]
110 # Permit updating most fields
111 permitted.push :command, :container_count_max,
112 :container_image, :cwd, :description, :environment,
113 :filters, :mounts, :name, :output_path, :priority,
114 :properties, :requesting_container_uuid, :runtime_constraints,
115 :state, :container_uuid
118 if container_uuid.nil?
119 errors.add :container_uuid, "has not been resolved to a container."
122 # Can update priority, container count.
123 permitted.push :priority, :container_count_max, :container_uuid
125 if self.state_changed?
126 # Allow create-and-commit in a single operation.
127 permitted.push :command, :container_image, :cwd, :description, :environment,
128 :filters, :mounts, :name, :output_path, :properties,
129 :requesting_container_uuid, :runtime_constraints,
130 :state, :container_uuid
134 if self.state_changed?
135 permitted.push :state
137 errors.add :state, "does not allow updates"
141 errors.add :state, "invalid value"
144 check_update_whitelist permitted
148 if [Committed, Final].include? self.state and (self.state_changed? or
149 self.priority_changed? or
150 self.container_uuid_changed?)
151 [self.container_uuid_was, self.container_uuid].each do |cuuid|
153 c = Container.find_by_uuid cuuid
154 act_as_system_user do