Merge branch 'master' into 9372-container-display
[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 symbolic git and keep references to content
86     # addresses.
87     c = act_as_system_user do
88       Container.create!(command: self.command,
89                         container_image: self.container_image,
90                         cwd: self.cwd,
91                         environment: self.environment,
92                         mounts: self.mounts,
93                         output_path: self.output_path,
94                         runtime_constraints: self.runtime_constraints)
95     end
96     self.container_uuid = c.uuid
97   end
98
99   def set_container
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."
104       return false
105     end
106     if state_changed? and state == Committed and container_uuid.nil?
107       resolve
108     end
109   end
110
111   def validate_change
112     permitted = [:owner_uuid]
113
114     case self.state
115     when Uncommitted
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
122
123     when Committed
124       if container_uuid.nil?
125         errors.add :container_uuid, "has not been resolved to a container."
126       end
127
128       if priority.nil?
129         errors.add :priority, "cannot be nil"
130       end
131
132       # Can update priority, container count, name and description
133       permitted.push :priority, :container_count_max, :container_uuid, :name, :description
134
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
141       end
142
143     when Final
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."
146       end
147
148       if self.state_changed? || self.name_changed? || self.description_changed?
149           permitted.push :state, :name, :description
150       else
151         errors.add :state, "does not allow updates"
152       end
153
154     else
155       errors.add :state, "invalid value"
156     end
157
158     check_update_whitelist permitted
159   end
160
161   def update_priority
162     if self.state_changed? or
163         self.priority_changed? or
164         self.container_uuid_changed?
165       act_as_system_user do
166         Container.
167           where('uuid in (?)',
168                 [self.container_uuid_was, self.container_uuid].compact).
169           map(&:update_priority!)
170       end
171     end
172   end
173
174   def set_requesting_container_uuid
175     return true if self.requesting_container_uuid   # already set
176
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
180     true
181   end
182 end