8470: Resolve runtime_constraints ranges to numbers when satisfying a Container Request.
[arvados.git] / services / api / app / models / container_request.rb
1 require 'whitelist_update'
2
3 class ContainerRequest < ArvadosModel
4   include HasUuid
5   include KindAndEtag
6   include CommonApiTemplate
7   include WhitelistUpdate
8
9   serialize :properties, Hash
10   serialize :environment, Hash
11   serialize :mounts, Hash
12   serialize :runtime_constraints, Hash
13   serialize :command, Array
14
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
22
23   api_accessible :user, extend: :common do |t|
24     t.add :command
25     t.add :container_count_max
26     t.add :container_image
27     t.add :container_uuid
28     t.add :cwd
29     t.add :description
30     t.add :environment
31     t.add :expires_at
32     t.add :filters
33     t.add :mounts
34     t.add :name
35     t.add :output_path
36     t.add :priority
37     t.add :properties
38     t.add :requesting_container_uuid
39     t.add :runtime_constraints
40     t.add :state
41   end
42
43   # Supported states for a container request
44   States =
45     [
46      (Uncommitted = 'Uncommitted'),
47      (Committed = 'Committed'),
48      (Final = 'Final'),
49     ]
50
51   State_transitions = {
52     nil => [Uncommitted, Committed],
53     Uncommitted => [Committed],
54     Committed => [Final]
55   }
56
57   def state_transitions
58     State_transitions
59   end
60
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)
64   end
65
66   def container_completed!
67     # may implement retry logic here in the future.
68     self.state = ContainerRequest::Final
69     self.save!
70   end
71
72   protected
73
74   def fill_field_defaults
75     self.state ||= Uncommitted
76     self.environment ||= {}
77     self.runtime_constraints ||= {}
78     self.mounts ||= {}
79     self.cwd ||= "."
80   end
81
82   # Create a new container (or find an existing one) to satisfy this
83   # request.
84   def resolve
85     # TODO: resolve mounts and container_image to content addresses.
86     c = act_as_system_user do
87       Container.create!(command: self.command,
88                         container_image: self.container_image,
89                         cwd: self.cwd,
90                         environment: self.environment,
91                         mounts: self.mounts,
92                         output_path: self.output_path,
93                         runtime_constraints: runtime_constraints_for_container)
94     end
95     self.container_uuid = c.uuid
96   end
97
98   # Return a runtime_constraints hash that complies with
99   # self.runtime_constraints but is suitable for saving in a container
100   # record, i.e., has specific values instead of ranges.
101   #
102   # Doing this as a step separate from other resolutions, like "git
103   # revision range to commit hash", makes sense only when there is no
104   # opportunity to reuse an existing container (e.g., container reuse
105   # is not implemented yet, or we have already found that no existing
106   # containers are suitable).
107   def runtime_constraints_for_container
108     rc = {}
109     runtime_constraints.each do |k, v|
110       if v.is_a? Array
111         rc[k] = v[0]
112       else
113         rc[k] = v
114       end
115     end
116     rc
117   end
118
119   def set_container
120     if (container_uuid_changed? and
121         not current_user.andand.is_admin and
122         not container_uuid.nil?)
123       errors.add :container_uuid, "can only be updated to nil."
124       return false
125     end
126     if state_changed? and state == Committed and container_uuid.nil?
127       resolve
128     end
129   end
130
131   def validate_change
132     permitted = [:owner_uuid]
133
134     case self.state
135     when Uncommitted
136       # Permit updating most fields
137       permitted.push :command, :container_count_max,
138                      :container_image, :cwd, :description, :environment,
139                      :filters, :mounts, :name, :output_path, :priority,
140                      :properties, :requesting_container_uuid, :runtime_constraints,
141                      :state, :container_uuid
142
143     when Committed
144       if container_uuid.nil?
145         errors.add :container_uuid, "has not been resolved to a container."
146       end
147
148       if priority.nil?
149         errors.add :priority, "cannot be nil"
150       end
151
152       # Can update priority, container count, name and description
153       permitted.push :priority, :container_count_max, :container_uuid, :name, :description
154
155       if self.state_changed?
156         # Allow create-and-commit in a single operation.
157         permitted.push :command, :container_image, :cwd, :description, :environment,
158                        :filters, :mounts, :name, :output_path, :properties,
159                        :requesting_container_uuid, :runtime_constraints,
160                        :state, :container_uuid
161       end
162
163     when Final
164       if not current_user.andand.is_admin and not (self.name_changed? || self.description_changed?)
165         errors.add :state, "of container request can only be set to Final by system."
166       end
167
168       if self.state_changed? || self.name_changed? || self.description_changed?
169           permitted.push :state, :name, :description
170       else
171         errors.add :state, "does not allow updates"
172       end
173
174     else
175       errors.add :state, "invalid value"
176     end
177
178     check_update_whitelist permitted
179   end
180
181   def update_priority
182     if self.state_changed? or
183         self.priority_changed? or
184         self.container_uuid_changed?
185       act_as_system_user do
186         Container.
187           where('uuid in (?)',
188                 [self.container_uuid_was, self.container_uuid].compact).
189           map(&:update_priority!)
190       end
191     end
192   end
193
194   def set_requesting_container_uuid
195     return true if self.requesting_container_uuid   # already set
196
197     token_uuid = current_api_client_authorization.andand.uuid
198     container = Container.where('auth_uuid=?', token_uuid).order('created_at desc').first
199     self.requesting_container_uuid = container.uuid if container
200     true
201   end
202 end