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_change
19 after_save :update_priority
21 api_accessible :user, extend: :common do |t|
23 t.add :container_count_max
24 t.add :container_image
36 t.add :requesting_container_uuid
37 t.add :runtime_constraints
41 # Supported states for a container request
44 (Uncommitted = 'Uncommitted'),
45 (Committed = 'Committed'),
49 def skip_uuid_read_permission_check
50 # XXX temporary until permissions are sorted out.
51 %w(modified_by_client_uuid container_uuid requesting_container_uuid)
56 def fill_field_defaults
57 self.state ||= Uncommitted
58 self.environment ||= {}
59 self.runtime_constraints ||= {}
66 if self.container_uuid_changed?
67 if not current_user.andand.is_admin and not self.container_uuid.nil?
68 errors.add :container_uuid, "Cannot only update container_uuid to nil."
71 if self.state_changed?
72 if self.state == Committed and (self.state_was == Uncommitted or self.state_was.nil?)
74 self.container_uuid = Container.resolve(self).andand.uuid
82 permitted = [:owner_uuid]
86 # Permit updating most fields
87 permitted.push :command, :container_count_max,
88 :container_image, :cwd, :description, :environment,
89 :filters, :mounts, :name, :output_path, :priority,
90 :properties, :requesting_container_uuid, :runtime_constraints,
91 :state, :container_uuid
94 if container_uuid.nil?
95 errors.add :container_uuid, "Has not been resolved to a container."
98 # Can update priority, container count.
99 permitted.push :priority, :container_count_max, :container_uuid
101 if self.state_changed?
102 if self.state_was == Uncommitted or self.state_was.nil?
103 # Allow create-and-commit in a single operation.
104 permitted.push :command, :container_image, :cwd, :description, :environment,
105 :filters, :mounts, :name, :output_path, :properties,
106 :requesting_container_uuid, :runtime_constraints,
107 :state, :container_uuid
109 errors.add :state, "Can only go from Uncommitted to Committed"
114 if self.state_changed?
115 if self.state_was == Committed
116 permitted.push :state
118 errors.add :state, "Can only go from Committed to Final"
121 errors.add "Cannot update record in Final state"
125 errors.add :state, "Invalid state #{self.state}"
128 check_update_whitelist permitted
132 if self.state == Committed and (self.state_changed? or
133 self.priority_changed? or
134 self.container_uuid_changed?)
135 c = Container.find_by_uuid self.container_uuid
136 act_as_system_user do