Skip to content

Commit

Permalink
Added configuration:check rake task that checks a configuration (#4785)
Browse files Browse the repository at this point in the history
* Added configuration:check rake task that checks a configuration

* CR
  • Loading branch information
farhatahmad authored Feb 14, 2023
1 parent ac2655c commit d8cc171
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 7 deletions.
6 changes: 3 additions & 3 deletions config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@
user_name: ENV.fetch('SMTP_USERNAME', nil),
password: ENV.fetch('SMTP_PASSWORD', nil),
authentication: ENV.fetch('SMTP_AUTH', nil),
enable_starttls_auto: ENV.fetch('SMTP_STARTTLS_AUTO', true),
enable_starttls: ENV.fetch('SMTP_STARTTLS', false),
tls: ENV.fetch('SMTP_TLS', 'false') != 'false',
enable_starttls_auto: ActiveModel::Type::Boolean.new.cast(ENV.fetch('SMTP_STARTTLS_AUTO', 'true')),
enable_starttls: ActiveModel::Type::Boolean.new.cast(ENV.fetch('SMTP_STARTTLS', 'false')),
tls: ActiveModel::Type::Boolean.new.cast(ENV.fetch('SMTP_TLS', 'false')),
openssl_verify_mode: ENV.fetch('SMTP_SSL_VERIFY', 'true') == 'false' ? OpenSSL::SSL::VERIFY_NONE : OpenSSL::SSL::VERIFY_PEER
}
config.action_mailer.default_options = {
Expand Down
8 changes: 4 additions & 4 deletions config/environments/production.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@
user_name: ENV.fetch('SMTP_USERNAME'),
password: ENV.fetch('SMTP_PASSWORD'),
authentication: ENV.fetch('SMTP_AUTH'),
enable_starttls_auto: ENV.fetch('SMTP_STARTTLS_AUTO', true),
enable_starttls: ENV.fetch('SMTP_STARTTLS', false),
tls: ENV.fetch('SMTP_TLS', 'false') != 'false',
enable_starttls_auto: ActiveModel::Type::Boolean.new.cast(ENV.fetch('SMTP_STARTTLS_AUTO', 'true')),
enable_starttls: ActiveModel::Type::Boolean.new.cast(ENV.fetch('SMTP_STARTTLS', 'false')),
tls: ActiveModel::Type::Boolean.new.cast(ENV.fetch('SMTP_TLS', 'false')),
openssl_verify_mode: ENV.fetch('SMTP_SSL_VERIFY', 'true') == 'false' ? OpenSSL::SSL::VERIFY_NONE : OpenSSL::SSL::VERIFY_PEER
}
config.action_mailer.default_options = {
Expand All @@ -70,7 +70,7 @@
config.action_mailer.perform_deliveries = false
end

config.action_mailer.raise_delivery_errors = false
config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_caching = false
config.action_mailer.deliver_later_queue_name = 'mailing'

Expand Down
72 changes: 72 additions & 0 deletions lib/tasks/configuration.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# frozen_string_literal: true

require_relative 'task_helpers'

namespace :configuration do
desc 'Checks that the application was configured correctly'
task check: :environment do
required_env_vars = %w[SECRET_KEY_BASE BIGBLUEBUTTON_ENDPOINT BIGBLUEBUTTON_SECRET DATABASE_URL REDIS_URL].freeze

# Initial check that variables are set
info 'Checking required environment variables:'
required_env_vars.each do |var|
failed("#{var} not set correctly") if ENV[var].blank?
end
passed

info 'Checking connection to Postgres Database:'
begin
ActiveRecord::Base.establish_connection # Establishes connection
ActiveRecord::Base.connection # Calls connection object
failed('Unable to connect to Database') unless ActiveRecord::Base.connected?
rescue StandardError => e
failed("Unable to connect to Database - #{e}")
end
passed

info 'Checking connection to Redis Cache:'
begin
Redis.new.ping
rescue StandardError => e
failed("Unable to connect to Redis - #{e}")
end
passed

info 'Checking connection to BigBlueButton:'
test_request(Rails.configuration.bigbluebutton_endpoint)
checksum = Digest::SHA1.hexdigest("getMeetings#{Rails.configuration.bigbluebutton_secret}")
test_request("#{Rails.configuration.bigbluebutton_endpoint}getMeetings?checksum=#{checksum}")
passed

if ENV['SMTP_SERVER'].present?
info 'Checking connection to SMTP Server'
begin
UserMailer.with(to: ENV.fetch('SMTP_SENDER_EMAIL', nil), subject: ENV.fetch('SMTP_SENDER_EMAIL', nil)).test_email.deliver_now
rescue StandardError => e
failed("Unable to connect to SMTP Server - #{e}")
end
passed
end

exit 0
end
end

# Takes the full URL including the protocol
def test_request(url)
uri = URI(url)
res = Net::HTTP.get(uri)

doc = Nokogiri::XML(res)
failed("Could not get a valid response from BigBlueButton server - #{res}") if doc.css('returncode').text != 'SUCCESS'
rescue StandardError => e
failed("Error connecting to BigBlueButton server - #{e}")
end

def passed
success 'Passed'
end

def failed(msg)
err "Failed - #{msg}"
end

0 comments on commit d8cc171

Please sign in to comment.