configure_environment.rb 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #!/usr/bin/env ruby
  2. # Copyright (C) 2012-2024 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. true
  20. ENV_FILE_CONTENT
  21. DB_SETTINGS_MAP = {
  22. 'postgresql' => {
  23. 'adapter' => 'postgresql',
  24. 'username' => 'zammad',
  25. 'password' => 'zammad',
  26. 'host' => 'postgresql', # db alias from gitlab-ci.yml
  27. },
  28. 'mysql' => {
  29. 'adapter' => 'mysql2',
  30. 'username' => 'root',
  31. 'password' => 'zammad',
  32. 'host' => 'mysql', # db alias from gitlab-ci.yml
  33. 'flags' => [
  34. # See https://github.com/brianmario/mysql2#flags-option-parsing
  35. # This should hopefully reduce mysql crashes in CI.
  36. '-COMPRESS',
  37. '-MULTI_STATEMENTS',
  38. ]
  39. }
  40. }.freeze
  41. # Detect service availability based on host presence in network.
  42. def self.network_host_exists?(hostname)
  43. # GitLab used the /etc/hosts file if FF_NETWORK_PER_BUILD is not set.
  44. return true if File.foreach('/etc/hosts').any? { |l| l[hostname] }
  45. # Fall back to DNS lookup, also for GitHub
  46. !!Resolv::DNS.new.tap { |dns| dns.timeouts = 3 }.getaddress(hostname)
  47. rescue Resolv::ResolvError
  48. false
  49. end
  50. def self.configure_database
  51. if File.exist? File.join(__dir__, '../config/database.yml')
  52. puts "'config/database.yml' already exists and will not be changed."
  53. return
  54. end
  55. # Ruby 3.1 uses Psych 4 which made aliases support optional
  56. cnf = YAML.load_file(File.join(__dir__, '../config/database/database.yml'), aliases: true)
  57. cnf.delete('default')
  58. database = ENV['ENFORCE_DB_SERVICE'] || %w[postgresql mysql].shuffle.find do |db|
  59. network_host_exists?(db)
  60. end
  61. raise "Can't find any supported database." if database.nil?
  62. puts "Using #{database} as database service."
  63. # fetch DB settings from settings map and fallback to postgresql
  64. db_settings = DB_SETTINGS_MAP.fetch(database) { DB_SETTINGS_MAP['postgresql'] }
  65. %w[development test production].each do |environment|
  66. cnf[environment].merge!(db_settings)
  67. end
  68. File.write(File.join(__dir__, '../config/database.yml'), Psych.dump(cnf))
  69. end
  70. def self.configure_redis
  71. has_redis = network_host_exists?('redis')
  72. needs_redis = !%w[1 true].include?(ENV['ZAMMAD_SAFE_MODE']) # rubocop:disable Rails/NegateInclude
  73. if needs_redis && !has_redis
  74. raise 'Redis was not found, but is required for ActionCable.'
  75. end
  76. if has_redis && [true, needs_redis].sample
  77. puts 'Using Redis as adapter for ActionCable.'
  78. @env_file_content += "export REDIS_URL='redis://redis:6379'\n"
  79. if [true, false].sample
  80. puts 'Using FS as web socket session store.'
  81. @env_file_content += "export ZAMMAD_WEBSOCKET_SESSION_STORE_FORCE_FS_BACKEND='true'\n"
  82. else
  83. puts 'Using Redis as web socket session store.'
  84. end
  85. return
  86. end
  87. puts 'Not using Redis.'
  88. @env_file_content += "unset REDIS_URL\n"
  89. end
  90. def self.configure_memcached
  91. if network_host_exists?('memcached') && [true, false].sample
  92. puts 'Using memcached as Rails cache store.'
  93. @env_file_content += "export MEMCACHE_SERVERS='memcached'\n"
  94. return
  95. end
  96. puts "Using Zammad's file store as Rails cache store."
  97. @env_file_content += "unset MEMCACHE_SERVERS\n"
  98. end
  99. def self.configure_elasticsearch
  100. if network_host_exists?('elasticsearch')
  101. puts 'Activating support for Elasticsearch.'
  102. @env_file_content += "export ES_URL='http://elasticsearch:9200'\n"
  103. return
  104. end
  105. puts 'Not using Elasticsearch.'
  106. @env_file_content += "unset ES_URL\n"
  107. end
  108. # Since configure_database skips if database.yml already exists, check the
  109. # content of that file to reliably determine the database type in all cases.
  110. def self.database_type
  111. database = File.read(File.join(__dir__, '../config/database.yml')).match(%r{^\s*adapter:\s*(mysql|postgresql)})[1]
  112. if !database
  113. raise 'Could not determine database type, cannot setup cable.yml'
  114. end
  115. database
  116. end
  117. def self.write_env_file
  118. File.write(File.join(__dir__, 'environment.env'), @env_file_content)
  119. end
  120. def self.run
  121. configure_database
  122. configure_redis
  123. configure_memcached
  124. configure_elasticsearch
  125. write_env_file
  126. end
  127. end
  128. ConfigureEnvironment.run