6429: Committing container request creates container
[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_change
19   after_save :update_priority
20
21   api_accessible :user, extend: :common do |t|
22     t.add :command
23     t.add :container_count_max
24     t.add :container_image
25     t.add :container_uuid
26     t.add :cwd
27     t.add :description
28     t.add :environment
29     t.add :expires_at
30     t.add :filters
31     t.add :mounts
32     t.add :name
33     t.add :output_path
34     t.add :priority
35     t.add :properties
36     t.add :requesting_container_uuid
37     t.add :runtime_constraints
38     t.add :state
39   end
40
41   # Supported states for a container request
42   States =
43     [
44      (Uncommitted = 'Uncommitted'),
45      (Committed = 'Committed'),
46      (Final = 'Final'),
47     ]
48
49   protected
50
51   def fill_field_defaults
52     self.state ||= Uncommitted
53     self.environment ||= {}
54     self.runtime_constraints ||= {}
55     self.mounts ||= {}
56     self.cwd ||= "."
57     self.priority ||= 1
58   end
59
60   def set_container
61     if self.state_changed?
62       if self.state == Committed and (self.state_was == Uncommitted or self.state_was.nil?)
63         act_as_system_user do
64           self.container_uuid = Container.resolve(self).andand.uuid
65           Link.create!(head_uuid: self.container_uuid,
66                        tail_uuid: self.owner_uuid,
67                        link_class: "permission",
68                        name: "can_read")
69         end
70       end
71     end
72   end
73
74   def validate_change
75     permitted = [:owner_uuid]
76
77     case self.state
78     when Uncommitted
79       # Permit updating most fields
80       permitted.push :command, :container_count_max,
81                      :container_image, :cwd, :description, :environment,
82                      :filters, :mounts, :name, :output_path, :priority,
83                      :properties, :requesting_container_uuid, :runtime_constraints,
84                      :state, :container_uuid
85
86     when Committed
87       if container_uuid.nil?
88         errors.add :container_uuid, "Has not been resolved to a container."
89       end
90
91       # Can update priority, container count.
92       permitted.push :priority, :container_count_max
93
94       if self.state_changed?
95         if self.state_was == Uncommitted or self.state_was.nil?
96           # Allow create-and-commit in a single operation.
97           permitted.push :command, :container_count_max,
98                          :container_image, :cwd, :description, :environment,
99                          :filters, :mounts, :name, :output_path, :priority,
100                          :properties, :requesting_container_uuid, :runtime_constraints,
101                          :state, :container_uuid
102         else
103           errors.add :state, "Can only go from Uncommitted to Committed"
104         end
105       end
106
107     when Final
108       if self.state_changed?
109         if self.state_was == Committed
110           permitted.push :state
111         else
112           errors.add :state, "Can only go from Committed to Final"
113         end
114       else
115         errors.add "Cannot update record in Final state"
116       end
117
118     else
119       errors.add :state, "Invalid state #{self.state}"
120     end
121
122     check_update_whitelist permitted
123   end
124
125   def update_priority
126     if self.state == Committed and self.priority_changed?
127       c = Container.find_by_uuid self.container_uuid
128       c.update_priority!
129     end
130   end
131
132 end