debian-mirror-gitlab/lib/gitlab/mail_room.rb

96 lines
2.5 KiB
Ruby
Raw Normal View History

2018-12-13 13:39:08 +05:30
# frozen_string_literal: true
2016-09-13 17:45:13 +05:30
require 'yaml'
require 'json'
2020-03-09 13:42:32 +05:30
require 'pathname'
2017-09-10 17:25:29 +05:30
require_relative 'redis/queues' unless defined?(Gitlab::Redis::Queues)
2016-09-13 17:45:13 +05:30
2020-01-01 13:55:28 +05:30
# This service is run independently of the main Rails process,
# therefore the `Rails` class and its methods are unavailable.
2016-09-13 17:45:13 +05:30
module Gitlab
module MailRoom
2020-01-01 13:55:28 +05:30
RAILS_ROOT_DIR = Pathname.new('../..').expand_path(__dir__).freeze
2018-03-17 18:26:18 +05:30
DEFAULT_CONFIG = {
enabled: false,
port: 143,
ssl: false,
start_tls: false,
mailbox: 'inbox',
2020-01-01 13:55:28 +05:30
idle_timeout: 60,
log_path: RAILS_ROOT_DIR.join('log', 'mail_room_json.log')
2018-03-17 18:26:18 +05:30
}.freeze
2020-03-09 13:42:32 +05:30
# Email specific configuration which is merged with configuration
# fetched from YML config file.
ADDRESS_SPECIFIC_CONFIG = {
incoming_email: {
queue: 'email_receiver',
worker: 'EmailReceiverWorker'
},
service_desk_email: {
queue: 'service_desk_email_receiver',
worker: 'ServiceDeskEmailReceiverWorker'
}
}.freeze
2016-09-13 17:45:13 +05:30
class << self
2020-03-09 13:42:32 +05:30
def enabled_configs
@enabled_configs ||= configs.select { |config| enabled?(config) }
2016-09-13 17:45:13 +05:30
end
2020-03-09 13:42:32 +05:30
private
2016-09-13 17:45:13 +05:30
2020-03-09 13:42:32 +05:30
def enabled?(config)
config[:enabled] && !config[:address].to_s.empty?
2016-09-13 17:45:13 +05:30
end
2020-03-09 13:42:32 +05:30
def configs
ADDRESS_SPECIFIC_CONFIG.keys.map { |key| fetch_config(key) }
end
2016-09-13 17:45:13 +05:30
2020-03-09 13:42:32 +05:30
def fetch_config(config_key)
2016-09-13 17:45:13 +05:30
return {} unless File.exist?(config_file)
2020-03-09 13:42:32 +05:30
config = merged_configs(config_key)
config.merge!(redis_config) if enabled?(config)
config[:log_path] = File.expand_path(config[:log_path], RAILS_ROOT_DIR)
config
end
def merged_configs(config_key)
yml_config = load_yaml.fetch(config_key, {})
specific_config = ADDRESS_SPECIFIC_CONFIG.fetch(config_key, {})
DEFAULT_CONFIG.merge(specific_config, yml_config) do |_key, oldval, newval|
2018-03-17 18:26:18 +05:30
newval.nil? ? oldval : newval
end
2020-03-09 13:42:32 +05:30
end
2016-09-13 17:45:13 +05:30
2020-03-09 13:42:32 +05:30
def redis_config
gitlab_redis_queues = Gitlab::Redis::Queues.new(rails_env)
config = { redis_url: gitlab_redis_queues.url }
2017-08-17 22:00:37 +05:30
2020-03-09 13:42:32 +05:30
if gitlab_redis_queues.sentinels?
config[:sentinels] = gitlab_redis_queues.sentinels
2016-09-13 17:45:13 +05:30
end
config
end
2018-03-17 18:26:18 +05:30
def rails_env
@rails_env ||= ENV['RAILS_ENV'] || ENV['RACK_ENV'] || 'development'
end
2016-09-13 17:45:13 +05:30
def config_file
2018-11-18 11:00:15 +05:30
ENV['MAIL_ROOM_GITLAB_CONFIG_FILE'] || File.expand_path('../../config/gitlab.yml', __dir__)
2016-09-13 17:45:13 +05:30
end
2020-01-01 13:55:28 +05:30
2020-03-09 13:42:32 +05:30
def load_yaml
@yaml ||= YAML.load_file(config_file)[rails_env].deep_symbolize_keys
2020-01-01 13:55:28 +05:30
end
2016-09-13 17:45:13 +05:30
end
end
end