21700: Install Bundler system-wide in Rails postinst
[arvados.git] / services / api / app / controllers / arvados / v1 / containers_controller.rb
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 require 'update_priorities'
6
7 class Arvados::V1::ContainersController < ApplicationController
8   accept_attribute_as_json :environment, Hash
9   accept_attribute_as_json :mounts, Hash
10   accept_attribute_as_json :runtime_constraints, Hash
11   accept_attribute_as_json :runtime_status, Hash
12   accept_attribute_as_json :command, Array
13   accept_attribute_as_json :scheduling_parameters, Hash
14
15   skip_before_action :find_object_by_uuid, only: [:current]
16   skip_before_action :render_404_if_no_object, only: [:current]
17
18   def auth
19     if @object.locked_by_uuid != Thread.current[:api_client_authorization].uuid
20       raise ArvadosModel::PermissionDeniedError.new("Not locked by your token")
21     end
22     if @object.runtime_token.nil?
23       @object = @object.auth
24     else
25       @object = ApiClientAuthorization.validate(token: @object.runtime_token)
26       if @object.nil?
27         raise ArvadosModel::PermissionDeniedError.new("Invalid runtime_token")
28       end
29     end
30     show
31   end
32
33   def update
34     if (resource_attrs.keys.map(&:to_sym) - [:cost, :gateway_address, :output_properties, :progress, :runtime_status]).empty?
35       # If no attributes are being updated besides these, there are no
36       # cascading changes to other rows/tables, the only lock will the
37       # single row lock on SQL UPDATE.
38       super
39     else
40       Container.transaction do
41         # Get locks ahead of time to avoid deadlock in cascading priority
42         # update
43         row_lock_for_priority_update @object.uuid
44         super
45       end
46     end
47   end
48
49   def find_objects_for_index
50     super
51     if action_name == 'lock' || action_name == 'unlock'
52       # Avoid loading more fields than we need
53       @objects = @objects.select(:id, :uuid, :state, :priority, :auth_uuid, :locked_by_uuid, :lock_count)
54       # This gets called from within find_object_by_uuid.
55       # find_object_by_uuid stores the original value of @select in
56       # @preserve_select, edits the value of @select, calls
57       # find_objects_for_index, then restores @select from the value
58       # of @preserve_select.  So if we want our updated value of
59       # @select here to stick, we have to set @preserve_select.
60       @select = @preserve_select = %w(uuid state priority auth_uuid locked_by_uuid)
61     elsif action_name == 'update_priority'
62       # We're going to reload in update_priority!, which will select
63       # all attributes, but will fail if we don't select :id now.
64       @objects = @objects.select(:id, :uuid)
65     end
66   end
67
68   def lock
69     @object.lock
70     show
71   end
72
73   def unlock
74     @object.unlock
75     show
76   end
77
78   def update_priority
79     @object.update_priority!
80     show
81   end
82
83   def current
84     if Thread.current[:api_client_authorization].nil?
85       send_error("Not logged in", status: 401)
86     else
87       @object = Container.for_current_token
88       if @object.nil?
89         send_error("Token is not associated with a container.", status: 404)
90       else
91         show
92       end
93     end
94   end
95
96   def secret_mounts
97     c = Container.for_current_token
98     if @object && c && @object.uuid == c.uuid
99       send_json({"secret_mounts" => @object.secret_mounts})
100     else
101       send_error("Token is not associated with this container.", status: 403)
102     end
103   end
104 end