13647: Update keepstore install docs, eliminate keep_services step.
[arvados.git] / services / api / app / models / keep_service.rb
index 3baf0983d935a387dfa30bad3aed1115c1fbf065..f9dd01837c988c35e8597cf3a96d45c255297cfe 100644 (file)
@@ -1,15 +1,71 @@
+# Copyright (C) The Arvados Authors. All rights reserved.
+#
+# SPDX-License-Identifier: AGPL-3.0
+
 class KeepService < ArvadosModel
   include HasUuid
   include KindAndEtag
   include CommonApiTemplate
+  extend DbCurrentTime
+
+  SERVER_START_TIME = db_current_time
 
   api_accessible :user, extend: :common do |t|
     t.add  :service_host
     t.add  :service_port
     t.add  :service_ssl_flag
     t.add  :service_type
+    t.add  :read_only
   end
   api_accessible :superuser, :extend => :user do |t|
   end
 
+  protected
+
+  def permission_to_create
+    current_user.andand.is_admin
+  end
+
+  def permission_to_update
+    current_user.andand.is_admin
+  end
+
+  def self.from_config
+    config_time = connection.quote(SERVER_START_TIME)
+    owner = connection.quote(system_user_uuid)
+    values = []
+    id = 1
+    Rails.configuration.Services.Keepstore.InternalURLs.each do |url, info|
+      values << "(#{id}, " + quoted_column_values_from_url(url: url.to_s, info: info).join(", ") + ", 'disk', 'f'::bool, #{config_time}, #{config_time}, #{owner}, #{owner}, null)"
+      id += 1
+    end
+    Rails.configuration.Services.Keepproxy.InternalURLs.each do |url, info|
+      values << "(#{id}, " + quoted_column_values_from_url(url: url.to_s, info: info).join(", ") + ", 'proxy', 'f'::bool, #{config_time}, #{config_time}, #{owner}, #{owner}, null)"
+      id += 1
+    end
+    if values.length == 0
+      # return empty set as AR relation
+      return where('1=0')
+    else
+      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)"
+      return KeepService.from(sql)
+    end
+  end
+
+  private
+
+  def self.quoted_column_values_from_url(url:, info:)
+    rvz = info.Rendezvous
+    rvz = url if rvz.blank?
+    if /^[a-zA-Z0-9]{15}$/ !~ rvz
+      # If rvz is an URL (either the real service URL, or an alternate
+      # one specified in config in order to preserve rendezvous order
+      # when changing hosts/ports), hash it to get 15 alphanums.
+      rvz = Digest::MD5.hexdigest(rvz)[0..15]
+    end
+    uuid = Rails.configuration.ClusterID + "-bi6l4-" + rvz
+    uri = URI::parse(url)
+    [uuid, uri.host, uri.port].map { |x| connection.quote(x) } + [(uri.scheme == 'https' ? "'t'::bool" : "'f'::bool")]
+  end
+
 end