Switch to host naming

Servers concept will encompass custom cmd and labels. Host is just the IP address.
This commit is contained in:
David Heinemeier Hansson
2023-01-10 14:15:16 +01:00
parent 5ca6f32ee7
commit 78a5d08d3f
6 changed files with 25 additions and 42 deletions

View File

@@ -17,8 +17,9 @@ class Mrsk::Configuration
ensure_required_keys_present ensure_required_keys_present
end end
def servers def hosts
ENV["SERVERS"] || config.servers ENV["HOSTS"] || config.servers
end
end end
def version def version

View File

@@ -14,12 +14,12 @@ namespace :mrsk do
desc "Pull app image from the registry onto servers" desc "Pull app image from the registry onto servers"
task :pull do task :pull do
on(MRSK_CONFIG.servers) { execute *app.pull } on(MRSK_CONFIG.hosts) { execute *app.pull }
end end
desc "Run app on servers (or start them if they've already been run)" desc "Run app on servers (or start them if they've already been run)"
task :run do task :run do
on(MRSK_CONFIG.servers) do |host| on(MRSK_CONFIG.hosts) do |host|
begin begin
execute *app.run execute *app.run
rescue SSHKit::Command::Failed => e rescue SSHKit::Command::Failed => e
@@ -33,32 +33,14 @@ namespace :mrsk do
end end
end end
desc "Run app on servers (or start them if they've already been run)"
task :run do
MRSK_CONFIG.roles.each do |role|
on(MRSK_CONFIG.servers(role: role)) do |host|
begin
execute *app.run(role: role)
rescue SSHKit::Command::Failed => e
if e.message =~ /already in use/
puts "Container with same version already deployed on #{host}, starting that instead"
execute *app.start, host: host
else
raise
end
end
end
end
end
desc "Start existing app on servers" desc "Start existing app on servers"
task :start do task :start do
on(MRSK_CONFIG.servers) { execute *app.start, raise_on_non_zero_exit: false } on(MRSK_CONFIG.hosts) { execute *app.start, raise_on_non_zero_exit: false }
end end
desc "Stop app on servers" desc "Stop app on servers"
task :stop do task :stop do
on(MRSK_CONFIG.servers) { execute *app.stop, raise_on_non_zero_exit: false } on(MRSK_CONFIG.hosts) { execute *app.stop, raise_on_non_zero_exit: false }
end end
desc "Start app on servers (use VERSION=<git-hash> to designate which version)" desc "Start app on servers (use VERSION=<git-hash> to designate which version)"
@@ -66,46 +48,46 @@ namespace :mrsk do
desc "Display information about app containers" desc "Display information about app containers"
task :info do task :info do
on(MRSK_CONFIG.servers) { |host| puts "App Host: #{host}\n" + capture(*app.info) + "\n\n" } on(MRSK_CONFIG.hosts) { |host| puts "App Host: #{host}\n" + capture(*app.info) + "\n\n" }
end end
desc "Execute a custom task on servers passed in as CMD='bin/rake some:task'" desc "Execute a custom task on servers passed in as CMD='bin/rake some:task'"
task :exec do task :exec do
on(MRSK_CONFIG.servers) { |host| puts "App Host: #{host}\n" + capture(*app.exec(ENV["CMD"])) + "\n\n" } on(MRSK_CONFIG.hosts) { |host| puts "App Host: #{host}\n" + capture(*app.exec(ENV["CMD"])) + "\n\n" }
end end
namespace :exec do namespace :exec do
desc "Execute Rails command on servers, like CMD='runner \"puts %(Hello World)\"" desc "Execute Rails command on servers, like CMD='runner \"puts %(Hello World)\""
task :rails do task :rails do
on(MRSK_CONFIG.servers) { |host| puts "App Host: #{host}\n" + capture(*app.exec("bin/rails", ENV["CMD"])) + "\n\n" } on(MRSK_CONFIG.hosts) { |host| puts "App Host: #{host}\n" + capture(*app.exec("bin/rails", ENV["CMD"])) + "\n\n" }
end end
desc "Execute a custom task on the first defined server" desc "Execute a custom task on the first defined server"
task :once do task :once do
on(MRSK_CONFIG.servers.first) { |host| puts capture(*app.exec(ENV["CMD"])) } on(MRSK_CONFIG.hosts.first) { |host| puts capture(*app.exec(ENV["CMD"])) }
end end
namespace :once do namespace :once do
desc "Execute Rails command on the first defined server, like CMD='runner \"puts %(Hello World)\"" desc "Execute Rails command on the first defined server, like CMD='runner \"puts %(Hello World)\""
task :rails do task :rails do
on(MRSK_CONFIG.servers.first) { puts capture(*app.exec("bin/rails", ENV["CMD"])) } on(MRSK_CONFIG.hosts.first) { puts capture(*app.exec("bin/rails", ENV["CMD"])) }
end end
end end
end end
desc "List all the app containers currently on servers" desc "List all the app containers currently on servers"
task :containers do task :containers do
on(MRSK_CONFIG.servers) { |host| puts "App Host: #{host}\n" + capture(*app.list_containers) + "\n\n" } on(MRSK_CONFIG.hosts) { |host| puts "App Host: #{host}\n" + capture(*app.list_containers) + "\n\n" }
end end
desc "Tail logs from app containers" desc "Tail logs from app containers"
task :logs do task :logs do
on(MRSK_CONFIG.servers) { execute *app.logs } on(MRSK_CONFIG.hosts) { execute *app.logs }
end end
desc "Remove app containers and images from servers" desc "Remove app containers and images from servers"
task remove: %i[ stop ] do task remove: %i[ stop ] do
on(MRSK_CONFIG.servers) do on(MRSK_CONFIG.hosts) do
execute *app.remove_containers execute *app.remove_containers
execute *app.remove_images execute *app.remove_images
end end

View File

@@ -7,12 +7,12 @@ namespace :mrsk do
namespace :prune do namespace :prune do
desc "Prune unused images older than 30 days" desc "Prune unused images older than 30 days"
task :images do task :images do
on(MRSK_CONFIG.servers) { execute "docker image prune -f --filter 'until=720h'" } on(MRSK_CONFIG.hosts) { execute "docker image prune -f --filter 'until=720h'" }
end end
desc "Prune stopped containers for the service older than 3 days" desc "Prune stopped containers for the service older than 3 days"
task :containers do task :containers do
on(MRSK_CONFIG.servers) { execute "docker container prune -f --filter label=service=#{MRSK_CONFIG.service} --filter 'until=72h'" } on(MRSK_CONFIG.hosts) { execute "docker container prune -f --filter label=service=#{MRSK_CONFIG.service} --filter 'until=72h'" }
end end
end end
end end

View File

@@ -7,12 +7,12 @@ namespace :mrsk do
desc "Login to the registry locally and remotely" desc "Login to the registry locally and remotely"
task :login do task :login do
run_locally { execute *registry.login } run_locally { execute *registry.login }
on(MRSK_CONFIG.servers) { execute *registry.login } on(MRSK_CONFIG.hosts) { execute *registry.login }
end end
desc "Logout of the registry remotely" desc "Logout of the registry remotely"
task :logout do task :logout do
on(MRSK_CONFIG.servers) { execute *registry.logout } on(MRSK_CONFIG.hosts) { execute *registry.logout }
end end
end end
end end

View File

@@ -5,7 +5,7 @@ namespace :mrsk do
desc "Setup Docker on the remote servers" desc "Setup Docker on the remote servers"
task :bootstrap do task :bootstrap do
# FIXME: Detect when apt-get is not available and use the appropriate alternative # FIXME: Detect when apt-get is not available and use the appropriate alternative
on(MRSK_CONFIG.servers) { execute "apt-get install docker.io -y" } on(MRSK_CONFIG.hosts) { execute "apt-get install docker.io -y" }
end end
end end
end end

View File

@@ -6,17 +6,17 @@ namespace :mrsk do
namespace :traefik do namespace :traefik do
desc "Run Traefik on servers" desc "Run Traefik on servers"
task :run do task :run do
on(MRSK_CONFIG.servers) { execute *traefik.run, raise_on_non_zero_exit: false } on(MRSK_CONFIG.hosts) { execute *traefik.run, raise_on_non_zero_exit: false }
end end
desc "Start existing Traefik on servers" desc "Start existing Traefik on servers"
task :start do task :start do
on(MRSK_CONFIG.servers) { execute *traefik.start, raise_on_non_zero_exit: false } on(MRSK_CONFIG.hosts) { execute *traefik.start, raise_on_non_zero_exit: false }
end end
desc "Stop Traefik on servers" desc "Stop Traefik on servers"
task :stop do task :stop do
on(MRSK_CONFIG.servers) { execute *traefik.stop, raise_on_non_zero_exit: false } on(MRSK_CONFIG.hosts) { execute *traefik.stop, raise_on_non_zero_exit: false }
end end
desc "Restart Traefik on servers" desc "Restart Traefik on servers"
@@ -24,12 +24,12 @@ namespace :mrsk do
desc "Display information about Traefik containers from servers" desc "Display information about Traefik containers from servers"
task :info do task :info do
on(MRSK_CONFIG.servers) { |host| puts "Traefik Host: #{host}\n" + capture(*traefik.info) + "\n\n" } on(MRSK_CONFIG.hosts) { |host| puts "Traefik Host: #{host}\n" + capture(*traefik.info) + "\n\n" }
end end
desc "Remove Traefik container and image from servers" desc "Remove Traefik container and image from servers"
task remove: %i[ stop ] do task remove: %i[ stop ] do
on(MRSK_CONFIG.servers) do on(MRSK_CONFIG.hosts) do
execute *traefik.remove_container execute *traefik.remove_container
execute *traefik.remove_image execute *traefik.remove_image
end end