Moved message handling out to a separate method for clarity in eventbus.
[arvados.git] / services / api / app / controllers / arvados / v1 / nodes_controller.rb
1 class Arvados::V1::NodesController < ApplicationController
2   skip_before_filter :require_auth_scope_all, :only => :ping
3   skip_before_filter :find_object_by_uuid, :only => :ping
4   skip_before_filter :render_404_if_no_object, :only => :ping
5
6   def create
7     @object = Node.new
8     @object.save!
9     @object.start!(lambda { |h| arvados_v1_ping_node_url(h) })
10     show
11   end
12
13   def self._ping_requires_parameters
14     { ping_secret: true }
15   end
16
17   def ping
18     act_as_system_user do
19       @object = Node.where(uuid: (params[:id] || params[:uuid])).first
20       if !@object
21         return render_not_found
22       end
23       @object.ping({ ip: params[:local_ipv4] || request.env['REMOTE_ADDR'],
24                      ping_secret: params[:ping_secret],
25                      ec2_instance_id: params[:instance_id] })
26       if @object.info[:ping_secret] == params[:ping_secret]
27         render json: @object.as_api_response(:superuser)
28       else
29         raise "Invalid ping_secret after ping"
30       end
31     end
32   end
33
34   def find_objects_for_index
35     if current_user.andand.is_admin || !current_user.andand.is_active
36       super
37     else
38       # active non-admin users can list nodes that are (or were
39       # recently) working
40       @objects = model_class.where('last_ping_at >= ?', Time.now - 1.hours)
41     end
42   end
43 end