Refactoring: DRYed up .gitlab-ci.yml.

This commit is contained in:
Thorsten Eckel 2018-10-01 19:41:17 +02:00
parent 97b17caea4
commit e4d7dc449f
27 changed files with 909 additions and 793 deletions

File diff suppressed because it is too large Load diff

View file

@ -1,54 +0,0 @@
module BootstrapRakeHelper
APP_CACHE = Dir.glob(Rails.root.join('tmp', 'cache*'))
SERVER_LOG = Rails.root.join('log', "#{Rails.env}.log")
AUTO_WIZARD = { source: Rails.root.join('contrib', 'auto_wizard_test.json'),
dest: Rails.root.join('auto_wizard.json') }.freeze
DB_CONFIG = { source: Rails.root.join('config', 'database', 'database.yml'),
dest: Rails.root.join('config', 'database.yml') }.freeze
def flush_cache_and_logs
FileUtils.rm_rf(APP_CACHE)
File.write(SERVER_LOG, '')
end
def run_auto_wizard
FileUtils.ln(AUTO_WIZARD[:source], AUTO_WIZARD[:dest], force: true)
AutoWizard.setup
# set system init to done
UserInfo.current_user_id = 1
Setting.set('system_init_done', true)
end
def add_database_config
raise Errno::ENOENT, 'config/database.yml not found' unless File.exist?(DB_CONFIG[:source])
if File.exist?(DB_CONFIG[:dest])
return if FileUtils.identical?(DB_CONFIG[:source], DB_CONFIG[:dest])
printf 'config/database.yml: File exists. Overwrite? [y/N] '
return if STDIN.gets.chomp.downcase != 'y'
end
FileUtils.cp(DB_CONFIG[:source], DB_CONFIG[:dest])
end
end
namespace :bs do
desc 'Bootstrap the application'
task :init => %i[db_config db:create db:migrate db:seed] do
include BootstrapRakeHelper
run_auto_wizard
end
desc 'Reset the application to its initial state'
task :reset => %i[db:reset] do
include BootstrapRakeHelper
run_auto_wizard
flush_cache_and_logs
end
task :db_config do
include BootstrapRakeHelper
add_database_config
end
end

View file

@ -0,0 +1,12 @@
namespace :zammad do
namespace :bootstrap do
desc 'Initializes a Zammad for the first time'
task init: %i[
zammad:setup:db_config
zammad:db:init
zammad:setup:auto_wizard
]
end
end

View file

@ -0,0 +1,13 @@
namespace :zammad do
namespace :bootstrap do
desc 'Resets a Zammad and reinitializes it'
task reset: %i[
db:drop
zammad:db:init
zammad:setup:auto_wizard
zammad:flush
]
end
end

View file

@ -0,0 +1,14 @@
namespace :zammad do
namespace :ci do
namespace :app do
desc 'Restarts the application'
task restart: %i[
zammad:ci:app:stop
zammad:ci:app:start
]
end
end
end

View file

@ -0,0 +1,15 @@
namespace :zammad do
namespace :ci do
namespace :app do
desc 'Starts the application and uses BROWSER_PORT, RAILS_ENV and WS_PORT ENVs'
task :start do
Rake::Task['zammad:ci:service:puma:start'].invoke(ENV['BROWSER_PORT'], ENV['RAILS_ENV'])
Rake::Task['zammad:ci:service:websocket:start'].invoke(ENV['WS_PORT'])
Rake::Task['zammad:ci:service:scheduler:start'].invoke
end
end
end
end

View file

@ -0,0 +1,15 @@
namespace :zammad do
namespace :ci do
namespace :app do
desc 'Stops the application'
task stop: %i[
zammad:ci:service:scheduler:stop
zammad:ci:service:websocket:stop
zammad:ci:service:puma:stop
]
end
end
end

View file

@ -0,0 +1,36 @@
namespace :zammad do
namespace :ci do
namespace :service do
namespace :puma do
desc 'Starts the puma application webserver'
task :start, [:port, :env] do |_task, args|
port = args.fetch(:port, '3000')
env = args.fetch(:env, 'production')
command = [
'bundle',
'exec',
'puma',
'--pidfile',
'tmp/pids/server.pid',
'-d',
'-p',
port,
'-e',
env
]
stdout, stderr, status = Open3.capture3(*command)
next if status.success?
abort("Error while starting Puma - error status #{status.exitstatus}: #{stderr}")
end
end
end
end
end

View file

@ -0,0 +1,26 @@
namespace :zammad do
namespace :ci do
namespace :service do
namespace :puma do
desc 'Stops the puma application webserver'
task :stop do
file = Rails.root.join('tmp', 'pids', 'server.pid')
pid = File.read(file).to_i
Process.kill('SIGTERM', pid)
sleep 5
next if !File.exist?(file)
Process.kill('SIGKILL', pid)
end
end
end
end
end

View file

@ -0,0 +1,28 @@
namespace :zammad do
namespace :ci do
namespace :service do
namespace :scheduler do
desc 'Starts the scheduler'
task :start do
command = [
'bundle',
'exec',
'script/scheduler.rb',
'start',
]
stdout, stderr, status = Open3.capture3(*command)
next if status.success?
abort("Error while starting scheduler - error status #{status.exitstatus}: #{stderr}")
end
end
end
end
end

View file

@ -0,0 +1,28 @@
namespace :zammad do
namespace :ci do
namespace :service do
namespace :scheduler do
desc 'Stops the scheduler'
task :stop do
command = [
'bundle',
'exec',
'script/scheduler.rb',
'stop',
]
stdout, stderr, status = Open3.capture3(*command)
next if status.success?
abort("Error while stopping scheduler - error status #{status.exitstatus}: #{stderr}")
end
end
end
end
end

View file

@ -0,0 +1,32 @@
namespace :zammad do
namespace :ci do
namespace :service do
namespace :websocket do
desc 'Starts the websocket server'
task :start, [:port] do |_task, args|
port = args.fetch(:port, '6042')
command = [
'bundle',
'exec',
'script/websocket-server.rb',
'start',
'-d',
'-p',
port
]
stdout, stderr, status = Open3.capture3(*command)
next if status.success?
abort("Error while starting websocket server - error status #{status.exitstatus}: #{stderr}")
end
end
end
end
end

View file

@ -0,0 +1,28 @@
namespace :zammad do
namespace :ci do
namespace :service do
namespace :websocket do
desc 'Stops the websocket server'
task :stop do
command = [
'bundle',
'exec',
'script/websocket-server.rb',
'stop',
]
stdout, stderr, status = Open3.capture3(*command)
next if status.success?
abort("Error while stopping websocket server - error status #{status.exitstatus}: #{stderr}")
end
end
end
end
end

View file

@ -0,0 +1,20 @@
namespace :zammad do
namespace :ci do
desc 'Sets all required Settings for performing tests in the CI environment'
task :settings, [:elasticsearch] => :environment do |_task, args|
Setting.set('developer_mode', true)
Setting.set('websocket_port', ENV['WS_PORT'])
Setting.set('fqdn', "#{ENV['IP']}:#{ENV['BROWSER_PORT']}")
Setting.set('chat_agent_idle_timeout', '45')
next if args[:elasticsearch] != 'with_elasticsearch'
Setting.set('es_url', 'http://localhost:9200')
Setting.set('es_index', "browser_test_#{ENV['CI_BUILD_ID']}")
Rake::Task['searchindex:rebuild'].invoke
end
end
end

View file

@ -0,0 +1,13 @@
namespace :zammad do
namespace :ci do
namespace :test do
desc 'Stops all of Zammads services and exists the rake task with exit code 1'
task fail: %i[zammad:ci:test:stop] do
abort('Abort further test processing')
end
end
end
end

View file

@ -0,0 +1,24 @@
namespace :zammad do
namespace :ci do
namespace :test do
desc 'Starts all of Zammads services for CI test'
task :start, [:elasticsearch] do |_task, args|
ENV['RAILS_ENV'] ||= 'production'
ENV['DISABLE_DATABASE_ENVIRONMENT_CHECK'] = 'true'
# we have to enforce the env
# otherwise it will fallback to default (develop)
Rails.env = ENV['RAILS_ENV']
Rake::Task['zammad:flush:cache'].invoke
Rake::Task['zammad:db:init'].invoke
Rake::Task['zammad:ci:settings'].invoke(args[:elasticsearch])
Rake::Task['zammad:ci:app:start'].invoke
end
end
end
end

View file

@ -0,0 +1,25 @@
namespace :zammad do
namespace :ci do
namespace :test do
desc 'Stop of all Zammad services and cleans up the database(s)'
task :stop do
ENV['RAILS_ENV'] ||= 'production'
ENV['DISABLE_DATABASE_ENVIRONMENT_CHECK'] = 'true'
# we have to enforce the env
# otherwise it will fallback to default (develop)
Rails.env = ENV['RAILS_ENV']
Rake::Task['zammad:ci:app:stop'].invoke
Rake::Task['db:drop:all'].invoke
next if !SearchIndexBackend.enabled?
Rake::Task['searchindex:drop'].invoke
end
end
end
end

View file

@ -0,0 +1,8 @@
namespace :zammad do
namespace :db do
desc 'Initializes (creates, migrates and seeds) the DB'
task init: %i[zammad:db:unseeded db:seed]
end
end

View file

@ -0,0 +1,8 @@
namespace :zammad do
namespace :db do
desc 'Creates and migrates the DB without seeding'
task unseeded: %i[db:create db:migrate]
end
end

View file

@ -0,0 +1,8 @@
namespace :zammad do
desc 'Flushes all logs and caches'
task flush: %i[
zammad:flush:logs
zammad:flush:cache
]
end

View file

@ -0,0 +1,10 @@
namespace :zammad do
namespace :flush do
desc 'Flushes all caches'
task :cache do
FileUtils.rm_rf(Rails.root.join('tmp', 'cache*'))
end
end
end

View file

@ -0,0 +1,15 @@
namespace :zammad do
namespace :flush do
namespace :log do
desc 'Flushes all scheduler log files'
task :scheduler do
%w[err out].each do |suffix|
File.write(Rails.root.join('log', "scheduler_#{suffix}.log"), '')
end
end
end
end
end

View file

@ -0,0 +1,14 @@
namespace :zammad do
namespace :flush do
namespace :log do
desc 'Flushes the log Rails file of the given or active environment'
task :rails, [:env] do |_task, args|
env = args.fetch(:env, Rails.env)
File.write(Rails.root.join('log', "#{env}.log"), '')
end
end
end
end

View file

@ -0,0 +1,15 @@
namespace :zammad do
namespace :flush do
namespace :log do
desc 'Flushes all websocket server log files'
task :websocket do
%w[err out].each do |suffix|
File.write(Rails.root.join('log', "websocket-server_#{suffix}.log"), '')
end
end
end
end
end

View file

@ -0,0 +1,12 @@
namespace :zammad do
namespace :flush do
desc 'Flushes all logs'
task logs: %i[
zammad:flush:log:rails
zammad:flush:log:scheduler
zammad:flush:log:websocket
]
end
end

View file

@ -0,0 +1,19 @@
namespace :zammad do
namespace :setup do
desc 'Initializes Zammad via a given auto_wizard JSON file or falls back to contrib/auto_wizard_test.json'
task :auto_wizard, [:source] => :environment do |_task, args|
root = Rails.root
source = args.fetch(:source, root.join('contrib', 'auto_wizard_test.json'))
FileUtils.ln(source, root.join('auto_wizard.json'), force: true)
AutoWizard.setup
# set system init to done
Setting.set('system_init_done', true)
end
end
end

View file

@ -0,0 +1,23 @@
namespace :zammad do
namespace :setup do
desc 'Copies the database config template file to config/database.yml'
task :db_config do
config_dir = Rails.root.join('config')
template = config_dir.join('database', 'database.yml')
destination = config_dir.join('database.yml')
raise Errno::ENOENT, "#{template} not found" unless File.exist?(template)
if File.exist?(destination)
next if FileUtils.identical?(template, destination)
printf 'config/database.yml: File exists. Overwrite? [y/N] '
next if STDIN.gets.chomp.downcase != 'y'
end
FileUtils.cp(template, destination)
end
end
end