13647: Move ...Controller.from_config_or_db to KeepService.all.
[arvados.git] / services / api / app / models / keep_service.rb
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 class KeepService < ArvadosModel
6   include HasUuid
7   include KindAndEtag
8   include CommonApiTemplate
9   extend DbCurrentTime
10
11   SERVER_START_TIME = db_current_time
12
13   api_accessible :user, extend: :common do |t|
14     t.add  :service_host
15     t.add  :service_port
16     t.add  :service_ssl_flag
17     t.add  :service_type
18     t.add  :read_only
19   end
20   api_accessible :superuser, :extend => :user do |t|
21   end
22
23   # return the set of keep services from the database (if this is an
24   # older installation or test system where entries have been added
25   # manually) or, preferably, the cluster config file.
26   def self.all *args
27     if super.count == 0
28       from_config
29     else
30       super
31     end
32   end
33
34   def self.where *args
35     all.where *args
36   end
37
38   protected
39
40   def permission_to_create
41     current_user.andand.is_admin
42   end
43
44   def permission_to_update
45     current_user.andand.is_admin
46   end
47
48   def self.from_config
49     config_time = connection.quote(SERVER_START_TIME)
50     owner = connection.quote(system_user_uuid)
51     values = []
52     id = 1
53     Rails.configuration.Services.Keepstore.InternalURLs.each do |url, info|
54       values << "(#{id}, " + quoted_column_values_from_url(url: url.to_s, info: info).join(", ") + ", 'disk', 'f'::bool, #{config_time}, #{config_time}, #{owner}, #{owner}, null)"
55       id += 1
56     end
57     Rails.configuration.Services.Keepproxy.InternalURLs.each do |url, info|
58       values << "(#{id}, " + quoted_column_values_from_url(url: url.to_s, info: info).join(", ") + ", 'proxy', 'f'::bool, #{config_time}, #{config_time}, #{owner}, #{owner}, null)"
59       id += 1
60     end
61     if values.length == 0
62       # return empty set as AR relation
63       return unscoped.where('1=0')
64     else
65       sql = "(values #{values.join(", ")}) as keep_services (id, uuid, service_host, service_port, service_ssl_flag, service_type, read_only, created_at, modified_at, owner_uuid, modified_by_user_uuid, modified_by_client_uuid)"
66       return unscoped.from(sql)
67     end
68   end
69
70   private
71
72   def self.quoted_column_values_from_url(url:, info:)
73     rvz = info.Rendezvous
74     rvz = url if rvz.blank?
75     if /^[a-zA-Z0-9]{15}$/ !~ rvz
76       # If rvz is an URL (either the real service URL, or an alternate
77       # one specified in config in order to preserve rendezvous order
78       # when changing hosts/ports), hash it to get 15 alphanums.
79       rvz = Digest::MD5.hexdigest(rvz)[0..15]
80     end
81     uuid = Rails.configuration.ClusterID + "-bi6l4-" + rvz
82     uri = URI::parse(url)
83     [uuid, uri.host, uri.port].map { |x| connection.quote(x) } + [(uri.scheme == 'https' ? "'t'::bool" : "'f'::bool")]
84   end
85
86 end