1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: AGPL-3.0
5 class KeepService < ArvadosModel
8 include CommonApiTemplate
10 extend CurrentApiClient
12 SERVER_START_TIME = db_current_time
14 api_accessible :user, extend: :common do |t|
17 t.add :service_ssl_flag
21 api_accessible :superuser, :extend => :user do |t|
24 # return the set of keep services from the database (if this is an
25 # older installation or test system where entries have been added
26 # manually) or, preferably, the cluster config file.
41 def permission_to_create
42 current_user.andand.is_admin
45 def permission_to_update
46 current_user.andand.is_admin
50 config_time = connection.quote(SERVER_START_TIME)
51 owner = connection.quote(system_user_uuid)
54 Rails.configuration.Services.Keepstore.InternalURLs.each do |url, info|
55 values << "(#{id}, " + quoted_column_values_from_url(url: url.to_s, rendezvous: info.Rendezvous).join(", ") + ", 'disk', 'f'::bool, #{config_time}, #{config_time}, #{owner}, #{owner}, null)"
58 url = Rails.configuration.Services.Keepproxy.ExternalURL.to_s
60 values << "(#{id}, " + quoted_column_values_from_url(url: url, rendezvous: "").join(", ") + ", 'proxy', 'f'::bool, #{config_time}, #{config_time}, #{owner}, #{owner}, null)"
64 # return empty set as AR relation
65 return unscoped.where('1=0')
67 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)"
68 return unscoped.from(sql)
74 def self.quoted_column_values_from_url(url:, rendezvous:)
76 rvz = url if rvz.blank?
77 if /^[a-zA-Z0-9]{15}$/ !~ rvz
78 # If rvz is an URL (either the real service URL, or an alternate
79 # one specified in config in order to preserve rendezvous order
80 # when changing hosts/ports), hash it to get 15 alphanums.
81 rvz = Digest::MD5.hexdigest(rvz)[0..15]
83 uuid = Rails.configuration.ClusterID + "-bi6l4-" + rvz
85 [uuid, uri.host, uri.port].map { |x| connection.quote(x) } + [(uri.scheme == 'https' ? "'t'::bool" : "'f'::bool")]