# frozen_string_literal: true
namespace :docker do
namespace :app do task :start do on roles(:app), in: :parallel do env_vars = fetch(:environment_variables).map do |key| value = ENV.fetch(key) { nil } ['-e', "#{key}='#{value}'"] if value end.compact.flatten.join(' ') network_list = [fetch(:docker_network, nil)].compact.flatten networks = network_list.map do |network| ['--network', network] end.compact.flatten.join(' ') labels = fetch(:docker_labels).map do |label| ['--label', label] end.compact.flatten.join(' ') execute 'docker', 'run', '-d', '--restart', 'always', '--name', fetch(:slug), fetch(:docker_options, nil), '-e', "RAILS_ENV=#{fetch(:stage)}", redact(env_vars), '-e', "DATABASE_URL=#{fetch(:database_url)}", networks, labels, fetch(:exposed_ports).map { |pgp| ['-p', pgp] }.flatten .join(' '), "#{fetch(:docker_registry_image)}:#{fetch(:docker_image_tag)}" end end task :stop do on roles(:app), in: :parallel do %w[stop rm].each do |command| execute 'docker', command, fetch(:slug), '||', 'true' end end end task :terminate do invoke 'docker:app:stop' invoke 'docker:app:clean' end task :clean do on roles(:app) do execute 'docker', 'rmi', fetch(:docker_registry_image), '||', 'true' end end end
end