1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: AGPL-3.0
5 class KeepService < ArvadosModel
8 include CommonApiTemplate
11 SERVER_START_TIME = db_current_time
13 api_accessible :user, extend: :common do |t|
16 t.add :service_ssl_flag
20 api_accessible :superuser, :extend => :user do |t|
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.
40 def permission_to_create
41 current_user.andand.is_admin
44 def permission_to_update
45 current_user.andand.is_admin
49 config_time = connection.quote(SERVER_START_TIME)
50 owner = connection.quote(system_user_uuid)
53 Rails.configuration.Services.Keepstore.InternalURLs.each do |url, info|
54 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)"
57 url = Rails.configuration.Services.Keepproxy.ExternalURL.to_s
59 values << "(#{id}, " + quoted_column_values_from_url(url: url, rendezvous: "").join(", ") + ", 'proxy', 'f'::bool, #{config_time}, #{config_time}, #{owner}, #{owner}, null)"
63 # return empty set as AR relation
64 return unscoped.where('1=0')
66 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)"
67 return unscoped.from(sql)
73 def self.quoted_column_values_from_url(url:, rendezvous:)
75 rvz = url if rvz.blank?
76 if /^[a-zA-Z0-9]{15}$/ !~ rvz
77 # If rvz is an URL (either the real service URL, or an alternate
78 # one specified in config in order to preserve rendezvous order
79 # when changing hosts/ports), hash it to get 15 alphanums.
80 rvz = Digest::MD5.hexdigest(rvz)[0..15]
82 uuid = Rails.configuration.ClusterID + "-bi6l4-" + rvz
84 [uuid, uri.host, uri.port].map { |x| connection.quote(x) } + [(uri.scheme == 'https' ? "'t'::bool" : "'f'::bool")]