13594: Remove table lock.
[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 class Arvados::V1::ContainersController < ApplicationController
6   accept_attribute_as_json :environment, Hash
7   accept_attribute_as_json :mounts, Hash
8   accept_attribute_as_json :runtime_constraints, Hash
9   accept_attribute_as_json :command, Array
10   accept_attribute_as_json :scheduling_parameters, Hash
11
12   skip_before_filter :find_object_by_uuid, only: [:current]
13   skip_before_filter :render_404_if_no_object, only: [:current]
14
15   def auth
16     if @object.locked_by_uuid != Thread.current[:api_client_authorization].uuid
17       raise ArvadosModel::PermissionDeniedError.new("Not locked by your token")
18     end
19     @object = @object.auth
20     show
21   end
22
23   def update
24     @object.with_lock do
25       @object.reload
26       super
27     end
28   end
29
30   def find_objects_for_index
31     super
32     if action_name == 'lock' || action_name == 'unlock'
33       # Avoid loading more fields than we need
34       @objects = @objects.select(:id, :uuid, :state, :priority, :auth_uuid, :locked_by_uuid)
35       @select = %w(uuid state priority auth_uuid locked_by_uuid)
36     end
37   end
38
39   def lock
40     @object.lock
41     show
42   end
43
44   def unlock
45     @object.unlock
46     show
47   end
48
49   def current
50     if Thread.current[:api_client_authorization].nil?
51       send_error("Not logged in", status: 401)
52     else
53       c = Container.where(auth_uuid: Thread.current[:api_client_authorization].uuid).first
54       if c.nil?
55         send_error("Token is not associated with a container.", status: 404)
56       else
57         @object = c
58         show
59       end
60     end
61   end
62
63   def secret_mounts
64     if @object &&
65        @object.auth_uuid &&
66        @object.auth_uuid == Thread.current[:api_client_authorization].uuid
67       send_json({"secret_mounts" => @object.secret_mounts})
68     else
69       send_error("Token is not associated with this container.", status: 403)
70     end
71   end
72 end