Fix 2.4.2 upgrade notes formatting refs #19330
[arvados.git] / services / api / app / controllers / arvados / v1 / nodes_controller.rb
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 class Arvados::V1::NodesController < ApplicationController
6   skip_before_action :require_auth_scope, :only => :ping
7   skip_before_action :find_object_by_uuid, :only => :ping
8   skip_before_action :render_404_if_no_object, :only => :ping
9
10   include DbCurrentTime
11
12   def self._ping_requires_parameters
13     { ping_secret: {required: true} }
14   end
15
16   def self._create_requires_parameters
17     super.merge(
18       { assign_slot: {required: false, type: 'boolean', description: 'assign slot and hostname'} })
19   end
20
21   def self._update_requires_parameters
22     super.merge(
23       { assign_slot: {required: false, type: 'boolean', description: 'assign slot and hostname'} })
24   end
25
26   def create
27     @object = model_class.new(resource_attrs)
28     @object.assign_slot if params[:assign_slot]
29     @object.save!
30     show
31   end
32
33   def update
34     if resource_attrs[:job_uuid].is_a? String
35       @object.job_readable = readable_job_uuids([resource_attrs[:job_uuid]]).any?
36     end
37     attrs_to_update = resource_attrs.reject { |k,v|
38       [:kind, :etag, :href].index k
39     }
40     @object.update_attributes!(attrs_to_update)
41     @object.assign_slot if params[:assign_slot]
42     @object.save!
43     show
44   end
45
46   def ping
47     act_as_system_user do
48       @object = Node.where(uuid: (params[:id] || params[:uuid])).first
49       if !@object
50         return render_not_found
51       end
52       ping_data = {
53         ip: params[:local_ipv4] || request.remote_ip,
54         ec2_instance_id: params[:instance_id]
55       }
56       [:ping_secret, :total_cpu_cores, :total_ram_mb, :total_scratch_mb]
57         .each do |key|
58         ping_data[key] = params[key] if params[key]
59       end
60       @object.ping(ping_data)
61       if @object.info['ping_secret'] == params[:ping_secret]
62         send_json @object.as_api_response(:superuser)
63       else
64         raise "Invalid ping_secret after ping"
65       end
66     end
67   end
68
69   def find_objects_for_index
70     if !current_user.andand.is_admin && current_user.andand.is_active
71       # active non-admin users can list nodes that are (or were
72       # recently) working
73       @objects = model_class.where('last_ping_at >= ?', db_current_time - 1.hours)
74     end
75     super
76     if @select.nil? or @select.include? 'job_uuid'
77       job_uuids = @objects.map { |n| n[:job_uuid] }.compact
78       assoc_jobs = readable_job_uuids(job_uuids)
79       @objects.each do |node|
80         node.job_readable = assoc_jobs.include?(node[:job_uuid])
81       end
82     end
83   end
84
85   protected
86
87   def readable_job_uuids(uuids)
88     Job.readable_by(*@read_users).select(:uuid).where(uuid: uuids).map(&:uuid)
89   end
90 end