make rails 7 and ruby 2.7 run in Docker (and on mac)
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
FGU 2022-12-12 14:21:25 +01:00
parent ff41025584
commit 38c27a23f5
9 changed files with 3 additions and 1 deletions

0
lib/tasks/.gitkeep Normal file
View file

87
lib/tasks/foodsoft.rake Normal file
View file

@ -0,0 +1,87 @@
# put in here all foodsoft tasks
# => :environment loads the environment an gives easy access to the application
namespace :foodsoft do
desc "Finish ended orders"
task :finish_ended_orders => :environment do
Order.finish_ended!
end
desc "Notify users of upcoming tasks"
task :notify_upcoming_tasks => :environment do
tasks = Task.where(done: false, due_date: 1.day.from_now.to_date)
for task in tasks
rake_say "Send notifications for #{task.name} to .."
for user in task.users
if user.settings.notify['upcoming_tasks']
Mailer.deliver_now_with_user_locale user do
Mailer.upcoming_tasks(user, task)
end
end
end
end
end
desc "Notify workgroup of upcoming weekly task"
task :notify_users_of_weekly_task => :environment do
tasks = Task.where(done: false, due_date: 7.day.from_now.to_date)
for task in tasks
unless task.enough_users_assigned?
workgroup = task.workgroup
if workgroup
rake_say "Notify workgroup: #{workgroup.name} for task #{task.name}"
for user in workgroup.users
if user.receive_email?
Mailer.deliver_now_with_user_locale user do
Mailer.not_enough_users_assigned(task, user)
end
end
end
end
end
end
end
desc "Create upcoming periodic tasks"
task :create_upcoming_periodic_tasks => :environment do
for tg in PeriodicTaskGroup.all
created_until = tg.create_tasks_for_upfront_days
rake_say "created until #{created_until}"
end
end
desc "Parse incoming email on stdin (options: RECIPIENT=foodcoop.handling)"
task :parse_reply_email => :environment do
FoodsoftMailReceiver.received ENV.fetch('RECIPIENT', nil), STDIN.read
end
desc "Start STMP server for incoming email (options: SMTP_SERVER_PORT=2525, SMTP_SERVER_HOST=0.0.0.0)"
task :reply_email_smtp_server => :environment do
port = ENV['SMTP_SERVER_PORT'].present? ? ENV['SMTP_SERVER_PORT'].to_i : 2525
host = ENV.fetch('SMTP_SERVER_HOST', nil)
rake_say "Started SMTP server for incoming email on port #{port}."
server = FoodsoftMailReceiver.new(ports: port, hosts: host, max_processings: 1, logger: Rails.logger)
server.start
server.join
end
desc "Import and assign bank transactions"
task :import_and_assign_bank_transactions => :environment do
BankAccount.find_each do |ba|
importer = ba.find_connector
next unless importer
importer.load nil
ok = importer.import nil
next unless ok
importer.finish
assign_count = ba.assign_unlinked_transactions
rake_say "#{ba.name}: imported #{importer.count}, assigned #{assign_count}"
end
end
end
# Helper
def rake_say(message)
puts message unless Rake.application.options.silent
end

View file

@ -0,0 +1,170 @@
require 'stringio'
# put in here all foodsoft tasks
# => :environment loads the environment an gives easy access to the application
module Colors
def colorize(text, color_code)
"\033[#{color_code}m#{text}\033[0m"
end
{
:black => 30,
:red => 31,
:green => 32,
:yellow => 33,
:blue => 34,
:magenta => 35,
:cyan => 36,
:white => 37
}.each do |key, color_code|
define_method key do |text|
colorize(text, color_code)
end
end
end
include Colors
namespace :foodsoft do
desc "Setup foodsoft"
task :setup_development do
puts yellow "This task will help you get your foodcoop running in development."
setup_bundler
setup_app_config
setup_development
setup_database
setup_storage
start_mailcatcher
puts yellow "All done! Your foodsoft setup should be running smoothly."
start_server
end
desc "Setup foodsoft"
task :setup_development_docker do
puts yellow "This task will help you get your foodcoop running in development via docker."
setup_app_config
setup_development
setup_storage
setup_run_rake_db_setup
puts yellow "All done! Your foodsoft setup should be running smoothly via docker."
end
namespace :setup do
desc "Initialize stock configuration"
task :stock_config do
setup_app_config
setup_development
end
end
end
def setup_bundler
puts yellow "Installing bundler if not installed..."
%x(if [ -z `which bundle` ]; then gem install bundler --no-rdoc --no-ri; fi)
puts yellow "Executing bundle install..."
%x(bundle install)
end
def setup_database
file = 'config/database.yml'
if ENV['DATABASE_URL']
puts blue "DATABASE_URL found, please remember to also set it when running Foodsoft"
return nil
end
return nil if skip?(file)
database = ask("What kind of database do you use?\nOptions:\n(1) MySQL\n(2) SQLite", ["1", "2"])
if database == "1"
puts yellow "Using MySQL..."
%x(cp -p #{Rails.root.join("#{file}.MySQL_SAMPLE")} #{Rails.root.join(file)})
elsif database == "2"
puts yellow "Using SQLite..."
%x(cp -p #{Rails.root.join("#{file}.SQLite_SAMPLE")} #{Rails.root.join(file)})
end
reminder(file)
puts blue "IMPORTANT: Edit (rake-generated) config/database.yml with valid username and password for EACH env before continuing!"
finished = ask("Finished?\nOptions:\n(y) Yes", ["y"])
setup_run_rake_db_setup if finished
end
def setup_run_rake_db_setup
Rake::Task["db:setup"].reenable
db_setup = capture_stdout { Rake::Task["db:setup"].invoke }
puts db_setup
end
def setup_app_config
file = 'config/app_config.yml'
sample = Rails.root.join("#{file}.SAMPLE")
return nil if skip?(file)
puts yellow "Copying #{file}..."
%x(cp -p #{sample} #{Rails.root.join(file)})
reminder(file)
end
def setup_development
file = 'config/environments/development.rb'
return nil if skip?(file)
puts yellow "Copying #{file}..."
%x(cp -p #{Rails.root.join("#{file}.SAMPLE")} #{Rails.root.join(file)})
reminder(file)
end
def setup_storage
file = 'config/storage.yml'
return nil if skip?(file)
puts yellow "Copying #{file}..."
%x(cp -p #{Rails.root.join("#{file}.SAMPLE")} #{Rails.root.join(file)})
reminder(file)
end
def start_mailcatcher
return nil if ENV['MAILCATCHER_PORT'] # skip when it has an existing Docker container
mailcatcher = ask("Do you want to start mailcatcher?\nOptions:\n(y) Yes\n(n) No", ["y", "n"])
if mailcatcher === "y"
puts yellow "Starting mailcatcher at http://localhost:1080..."
%x(mailcatcher)
end
end
def start_server
puts blue "Start your server running 'bundle exec rails s' and visit http://localhost:3000"
end
# Helper Methods
def ask(question, answers = false)
puts question
input = STDIN.gets.chomp
if input.blank? || (answers && !answers.include?(input))
puts red "Your Input is not valid. Try again!"
input = ask(question, answers)
end
input
end
def skip?(file)
output = false
skip = ask(cyan("We found #{file}!\nOptions:\n(1) Skip step\n(2) Force rewrite"), ["1", "2"]) if File.exists?(Rails.root.join(file))
output = true if skip == "1"
output
end
def reminder(file)
puts blue "don't forget to edit #{file}"
end
def capture_stdout
s = StringIO.new
$stdout = s
yield
s.string
ensure
$stdout = STDOUT
end

34
lib/tasks/multicoops.rake Normal file
View file

@ -0,0 +1,34 @@
# This namespace is used for a collection of tasks to maintain a hosting environment with multiple foodcoops
# This tasks are a kind of wrapper for other tasks. The wrapper makes sure, that the appropriate database and config
# for each foodcoop is used.
namespace :multicoops do
desc 'Runs a specific rake task for each registered foodcoop, use rake multicoops:run TASK=db:migrate'
task :run => :environment do
task_to_run = ENV.fetch('TASK', nil)
last_error = nil
FoodsoftConfig.each_coop do |coop|
begin
rake_say "Run '#{task_to_run}' for #{coop}"
Rake::Task[task_to_run].execute
rescue => error
last_error = error
ExceptionNotifier.notify_exception(error, data: { foodcoop: coop })
end
end
raise last_error if last_error
end
desc 'Runs a specific rake task for a single coop, use rake mutlicoops:run_single TASK=db:migrate FOODCOOP=demo'
task :run_single => :environment do
task_to_run = ENV.fetch('TASK', nil)
FoodsoftConfig.select_foodcoop ENV.fetch('FOODCOOP', nil)
rake_say "Run '#{task_to_run}' for #{ENV.fetch('FOODCOOP', nil)}"
Rake::Task[task_to_run].execute
end
end
# Helper
def rake_say(message)
puts message unless Rake.application.options.silent
end

41
lib/tasks/resque.rake Normal file
View file

@ -0,0 +1,41 @@
require "resque/tasks"
def run_worker(queue, count = 1)
puts "Starting #{count} worker(s) with QUEUE: #{queue}"
ops = { :pgroup => true, :err => ["log/resque_worker_foodsoft_notifier.log", "a"],
:out => ["log/resque_worker_foodsoft_notifier.log", "a"] }
env_vars = { "QUEUE" => queue.to_s, "PIDFILE" => "tmp/pids/resque_worker_foodsoft_notifier.pid" }
count.times {
## Using Kernel.spawn and Process.detach because regular system() call would
## cause the processes to quit when capistrano finishes
pid = spawn(env_vars, "bundle exec rake resque:work", ops)
Process.detach(pid)
}
end
namespace :resque do
task :setup => :environment
desc "Restart running workers"
task :restart_workers do
Rake::Task['resque:stop_workers'].invoke
Rake::Task['resque:start_workers'].invoke
end
desc "Quit running workers"
task :stop_workers do
pids = File.read('tmp/pids/resque_worker_foodsoft_notifier.pid').split("\n")
if pids.empty?
puts "No workers to kill"
else
syscmd = "kill -s QUIT #{pids.join(' ')}"
puts "Running syscmd: #{syscmd}"
system(syscmd)
end
end
desc "Start workers"
task :start_workers do
run_worker("foodsoft_notifier")
end
end

13
lib/tasks/rspec.rake Normal file
View file

@ -0,0 +1,13 @@
begin
require 'rspec/core/rake_task'
task(:spec).clear
RSpec::Core::RakeTask.new(:spec)
task :default => :spec
# Use `rspec` to run a single test. When a test fails in rake but not
# with rspec, you can use the following to run a single test using rake:
# RSpec::Core::RakeTask.new('spec:foo') do |t|
# t.pattern = "spec/integration/foo_spec.rb"
# end
rescue LoadError
end

16
lib/tasks/seeds.rake Normal file
View file

@ -0,0 +1,16 @@
# Allow loading multiple seeds
# Tried using seedbank, but that supposes all seeds are loaded, while
# we want to have different options to choose from (and they can't
# be loaded at the same time).
require 'pathname'
namespace :db do
namespace :seed do
Dir.glob(Rails.root.join('db/seeds/*.seeds.rb')).sort.each do |seedfile|
desc "Load the seed data from #{Pathname.new(seedfile).relative_path_from(Rails.root)}"
task File.basename(seedfile, '.seeds.rb') => :environment do
require_relative seedfile
end
end
end
end