configure_environment.rb 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #!/usr/bin/env ruby
  2. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  3. require 'yaml'
  4. require 'resolv'
  5. require 'fileutils'
  6. #
  7. # Configures the CI system
  8. # - (randomly) mysql or postgresql, if available
  9. # - (randomly) Redis or File as web socket session back end, if Redis is available
  10. # - (randomly) Memcached or File as Rails cache store, if Memcached is available
  11. # - Elasticsearch support, if available
  12. #
  13. # Database config happens directly in config/database.yml, other settings are written to
  14. # .gitlab/environment.env which must be sourced in the CI configuration.
  15. #
  16. class ConfigureEnvironment
  17. @env_file_content = <<~ENV_FILE_CONTENT
  18. #!/bin/bash
  19. FRESHENVFILE=fresh.env && test -f $FRESHENVFILE && source $FRESHENVFILE
  20. true
  21. ENV_FILE_CONTENT
  22. DB_SETTINGS_MAP = {
  23. 'postgresql' => {
  24. 'adapter' => 'postgresql',
  25. 'username' => 'zammad',
  26. 'password' => 'zammad',
  27. 'host' => 'postgresql', # db alias from gitlab-ci.yml
  28. },
  29. 'mysql' => {
  30. 'adapter' => 'mysql2',
  31. 'username' => 'root',
  32. 'password' => 'zammad',
  33. 'host' => 'mysql', # db alias from gitlab-ci.yml
  34. }
  35. }.freeze
  36. # Detect service availability based on host presence in network.
  37. def self.network_host_exists?(hostname)
  38. # GitLab used the /etc/hosts file if FF_NETWORK_PER_BUILD is not set.
  39. return true if File.foreach('/etc/hosts').any? { |l| l[hostname] }
  40. # Fall back to DNS lookup, also for GitHub
  41. !!Resolv::DNS.new.tap { |dns| dns.timeouts = 3 }.getaddress(hostname)
  42. rescue Resolv::ResolvError
  43. false
  44. end
  45. def self.configure_database # rubocop:disable Metrics/AbcSize
  46. if File.exist? File.join(__dir__, '../config/database.yml')
  47. puts "'config/database.yml' already exists and will not be changed."
  48. return
  49. end
  50. cnf = YAML.load_file(File.join(__dir__, '../config/database/database.yml'))
  51. cnf.delete('default')
  52. database = ENV['ENFORCE_DB_SERVICE'] || %w[postgresql mysql].shuffle.find do |db|
  53. network_host_exists?(db)
  54. end
  55. raise "Can't find any supported database." if database.nil?
  56. puts "Using #{database} as database service."
  57. # fetch DB settings from settings map and fallback to postgresql
  58. db_settings = DB_SETTINGS_MAP.fetch(database) { DB_SETTINGS_MAP['postgresql'] }
  59. %w[development test production].each do |environment|
  60. cnf[environment].merge!(db_settings)
  61. end
  62. File.write(File.join(__dir__, '../config/database.yml'), Psych.dump(cnf))
  63. end
  64. def self.configure_redis
  65. has_redis = network_host_exists?('redis')
  66. needs_redis = database_type == 'mysql' && ENV['ENABLE_EXPERIMENTAL_MOBILE_FRONTEND'] == 'true'
  67. if needs_redis && !has_redis
  68. raise 'Redis was not found, but is required for ActionCable on MySQL based systems.'
  69. end
  70. if has_redis && [true, needs_redis].sample
  71. puts 'Using Redis as web socket session store and as adapter for ActionCable.'
  72. @env_file_content += "export REDIS_URL='redis://redis:6379'\n"
  73. return
  74. end
  75. puts 'Using File as web socket session store and the PostgreSQL adapter for ActionCable.'
  76. @env_file_content += "unset REDIS_URL\n"
  77. end
  78. def self.configure_memcached
  79. if network_host_exists?('memcached') && [true, false].sample
  80. puts 'Using memcached as Rails cache store.'
  81. @env_file_content += "export MEMCACHE_SERVERS='memcached'\n"
  82. return
  83. end
  84. puts "Using Zammad's file store as Rails cache store."
  85. @env_file_content += "unset MEMCACHE_SERVERS\n"
  86. end
  87. def self.configure_elasticsearch
  88. if network_host_exists?('elasticsearch')
  89. puts 'Activating support for Elasticsearch.'
  90. @env_file_content += "export ES_URL='http://elasticsearch:9200'\n"
  91. return
  92. end
  93. puts 'Not using Elasticsearch.'
  94. @env_file_content += "unset ES_URL\n"
  95. end
  96. # Since configure_database skips if database.yml already exists, check the
  97. # content of that file to reliably determine the database type in all cases.
  98. def self.database_type
  99. database = File.read(File.join(__dir__, '../config/database.yml')).match(%r{^\s*adapter:\s*(mysql|postgresql)})[1]
  100. if !database
  101. raise 'Could not determine database type, cannot setup cable.yml'
  102. end
  103. database
  104. end
  105. def self.write_env_file
  106. File.write(File.join(__dir__, 'environment.env'), @env_file_content)
  107. end
  108. def self.run
  109. puts 'ENABLING THE NEW EXPERIMENTAL MOBILE FRONTEND.' if ENV['ENABLE_EXPERIMENTAL_MOBILE_FRONTEND'] == 'true'
  110. configure_database
  111. configure_redis
  112. configure_memcached
  113. configure_elasticsearch
  114. write_env_file
  115. end
  116. end
  117. ConfigureEnvironment.run