Merge branch 'main' into add-support-for-volumes
This commit is contained in:
@@ -49,16 +49,16 @@ class Mrsk::Cli::App < Mrsk::Cli::Base
|
||||
desc "console", "Start Rails Console on primary host (or specific host set by --hosts)"
|
||||
def console
|
||||
run_locally do
|
||||
puts "Launching Rails console on #{MRSK.primary_host}..."
|
||||
exec MRSK.app.console(host: MRSK.primary_host)
|
||||
info "Launching Rails console on #{MRSK.primary_host}"
|
||||
exec MRSK.app.console(host: MRSK.primary_host)
|
||||
end
|
||||
end
|
||||
|
||||
desc "bash", "Start a bash session on primary host (or specific host set by --hosts)"
|
||||
def bash
|
||||
run_locally do
|
||||
puts "Launching bash session on #{MRSK.primary_host}..."
|
||||
exec MRSK.app.bash(host: MRSK.primary_host)
|
||||
info "Launching bash session on #{MRSK.primary_host}"
|
||||
exec MRSK.app.bash(host: MRSK.primary_host)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -77,26 +77,36 @@ class Mrsk::Cli::App < Mrsk::Cli::Base
|
||||
on(MRSK.hosts) { |host| puts_by_host host, capture_with_info(*MRSK.app.current_container_id) }
|
||||
end
|
||||
|
||||
desc "logs", "Show last 100 log lines from app on servers"
|
||||
desc "logs", "Show lines from app on servers"
|
||||
option :since, aliases: "-s", desc: "Show logs since timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42 minutes)"
|
||||
option :lines, type: :numeric, aliases: "-n", desc: "Number of log lines to pull from each server"
|
||||
option :grep, aliases: "-g", desc: "Show lines with grep match only (use this to fetch specific requests by id)"
|
||||
option :follow, aliases: "-f", desc: "Follow logs on primary server (or specific host set by --hosts)"
|
||||
def logs
|
||||
# FIXME: Catch when app containers aren't running
|
||||
|
||||
since = options[:since]
|
||||
lines = options[:lines]
|
||||
grep = options[:grep]
|
||||
grep = options[:grep]
|
||||
|
||||
on(MRSK.hosts) do |host|
|
||||
begin
|
||||
puts_by_host host, capture_with_info(*MRSK.app.logs(since: since, lines: lines, grep: grep))
|
||||
rescue SSHKit::Command::Failed
|
||||
puts_by_host host, "Nothing found"
|
||||
if options[:follow]
|
||||
run_locally do
|
||||
info "Following logs on #{MRSK.primary_host}..."
|
||||
info MRSK.app.follow_logs(host: MRSK.primary_host, grep: grep)
|
||||
exec MRSK.app.follow_logs(host: MRSK.primary_host, grep: grep)
|
||||
end
|
||||
else
|
||||
since = options[:since]
|
||||
lines = options[:lines]
|
||||
|
||||
on(MRSK.hosts) do |host|
|
||||
begin
|
||||
puts_by_host host, capture_with_info(*MRSK.app.logs(since: since, lines: lines, grep: grep))
|
||||
rescue SSHKit::Command::Failed
|
||||
puts_by_host host, "Nothing found"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
desc "remove", "Remove app containers and images from servers"
|
||||
option :only, default: "", desc: "Use 'containers' or 'images'"
|
||||
def remove
|
||||
|
||||
@@ -13,13 +13,11 @@ class Mrsk::Cli::Build < Mrsk::Cli::Base
|
||||
|
||||
run_locally do
|
||||
begin
|
||||
debug "Using builder: #{MRSK.builder.name}"
|
||||
info "Building image may take a while (run with --verbose for progress logging)" unless verbose
|
||||
execute *MRSK.builder.push
|
||||
MRSK.verbosity(:debug) { execute *MRSK.builder.push }
|
||||
rescue SSHKit::Command::Failed => e
|
||||
error "Missing compatible builder, so creating a new one first"
|
||||
execute *MRSK.builder.create
|
||||
execute *MRSK.builder.push
|
||||
MRSK.verbosity(:debug) { execute *MRSK.builder.push }
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -48,7 +46,7 @@ class Mrsk::Cli::Build < Mrsk::Cli::Base
|
||||
desc "details", "Show the name of the configured builder"
|
||||
def details
|
||||
run_locally do
|
||||
puts "Builder: #{MRSK.builder.name} (#{MRSK.builder.target.class.name})"
|
||||
puts "Builder: #{MRSK.builder.name}"
|
||||
puts capture(*MRSK.builder.info)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -6,6 +6,13 @@ class Mrsk::Cli::Traefik < Mrsk::Cli::Base
|
||||
on(MRSK.traefik_hosts) { execute *MRSK.traefik.run, raise_on_non_zero_exit: false }
|
||||
end
|
||||
|
||||
desc "reboot", "Reboot Traefik on servers (stop container, remove container, start new container)"
|
||||
def reboot
|
||||
invoke :stop
|
||||
invoke :remove_container
|
||||
invoke :boot
|
||||
end
|
||||
|
||||
desc "start", "Start existing Traefik on servers"
|
||||
def start
|
||||
on(MRSK.traefik_hosts) { execute *MRSK.traefik.start, raise_on_non_zero_exit: false }
|
||||
@@ -27,18 +34,44 @@ class Mrsk::Cli::Traefik < Mrsk::Cli::Base
|
||||
on(MRSK.traefik_hosts) { |host| puts_by_host host, capture_with_info(*MRSK.traefik.info), type: "Traefik" }
|
||||
end
|
||||
|
||||
desc "logs", "Show last 100 log lines from Traefik on servers"
|
||||
desc "logs", "Show log lines from Traefik on servers"
|
||||
option :since, aliases: "-s", desc: "Show logs since timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42 minutes)"
|
||||
option :lines, type: :numeric, aliases: "-n", desc: "Number of log lines to pull from each server"
|
||||
option :grep, aliases: "-g", desc: "Show lines with grep match only (use this to fetch specific requests by id)"
|
||||
option :follow, aliases: "-f", desc: "Follow logs on primary server (or specific host set by --hosts)"
|
||||
def logs
|
||||
on(MRSK.traefik_hosts) { |host| puts_by_host host, capture(*MRSK.traefik.logs), type: "Traefik" }
|
||||
grep = options[:grep]
|
||||
|
||||
if options[:follow]
|
||||
run_locally do
|
||||
info "Following logs on #{MRSK.primary_host}..."
|
||||
info MRSK.traefik.follow_logs(host: MRSK.primary_host, grep: grep)
|
||||
exec MRSK.traefik.follow_logs(host: MRSK.primary_host, grep: grep)
|
||||
end
|
||||
else
|
||||
since = options[:since]
|
||||
lines = options[:lines]
|
||||
|
||||
on(MRSK.traefik_hosts) do |host|
|
||||
puts_by_host host, capture(*MRSK.traefik.logs(since: since, lines: lines, grep: grep)), type: "Traefik"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
desc "remove", "Remove Traefik container and image from servers"
|
||||
def remove
|
||||
invoke :stop
|
||||
invoke :remove_container
|
||||
invoke :remove_image
|
||||
end
|
||||
|
||||
on(MRSK.traefik_hosts) do
|
||||
execute *MRSK.traefik.remove_container
|
||||
execute *MRSK.traefik.remove_image
|
||||
end
|
||||
desc "remove_container", "Remove Traefik container from servers"
|
||||
def remove_container
|
||||
on(MRSK.traefik_hosts) { execute *MRSK.traefik.remove_container }
|
||||
end
|
||||
|
||||
desc "remove_container", "Remove Traefik image from servers"
|
||||
def remove_image
|
||||
on(MRSK.traefik_hosts) { execute *MRSK.traefik.remove_image }
|
||||
end
|
||||
end
|
||||
|
||||
@@ -60,6 +60,14 @@ class Mrsk::Commands::App < Mrsk::Commands::Base
|
||||
*command
|
||||
end
|
||||
|
||||
def follow_logs(host:, grep: nil)
|
||||
run_over_ssh pipe(
|
||||
current_container_id,
|
||||
"xargs docker logs -t -n 10 -f 2>&1",
|
||||
("grep '#{grep}'" if grep)
|
||||
).join(" "), host: host
|
||||
end
|
||||
|
||||
def console(host:)
|
||||
exec_over_ssh "bin/rails", "c", host: host
|
||||
end
|
||||
@@ -82,7 +90,7 @@ class Mrsk::Commands::App < Mrsk::Commands::Base
|
||||
|
||||
private
|
||||
def exec_over_ssh(*command, host:)
|
||||
"ssh -t #{config.ssh_user}@#{host} '#{run_exec(*command, interactive: true).join(" ")}'"
|
||||
run_over_ssh run_exec(*command, interactive: true).join(" "), host: host
|
||||
end
|
||||
|
||||
def service_filter
|
||||
|
||||
@@ -23,5 +23,9 @@ module Mrsk::Commands
|
||||
def docker(*args)
|
||||
args.compact.unshift :docker
|
||||
end
|
||||
|
||||
def run_over_ssh(command, host:)
|
||||
"ssh -t #{config.ssh_user}@#{host} '#{command}'"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -2,10 +2,9 @@ require "mrsk/commands/base"
|
||||
|
||||
class Mrsk::Commands::Builder < Mrsk::Commands::Base
|
||||
delegate :create, :remove, :push, :pull, :info, to: :target
|
||||
delegate :native?, :multiarch?, :remote?, to: :name
|
||||
|
||||
def name
|
||||
target.class.to_s.demodulize.downcase.inquiry
|
||||
target.class.to_s.remove("Mrsk::Commands::Builder::").underscore
|
||||
end
|
||||
|
||||
def target
|
||||
@@ -14,6 +13,8 @@ class Mrsk::Commands::Builder < Mrsk::Commands::Base
|
||||
native
|
||||
when config.builder && config.builder["local"] && config.builder["remote"]
|
||||
multiarch_remote
|
||||
when config.builder && config.builder["remote"]
|
||||
native_remote
|
||||
else
|
||||
multiarch
|
||||
end
|
||||
@@ -23,6 +24,10 @@ class Mrsk::Commands::Builder < Mrsk::Commands::Base
|
||||
@native ||= Mrsk::Commands::Builder::Native.new(config)
|
||||
end
|
||||
|
||||
def native_remote
|
||||
@native ||= Mrsk::Commands::Builder::Native::Remote.new(config)
|
||||
end
|
||||
|
||||
def multiarch
|
||||
@multiarch ||= Mrsk::Commands::Builder::Multiarch.new(config)
|
||||
end
|
||||
@@ -33,5 +38,6 @@ class Mrsk::Commands::Builder < Mrsk::Commands::Base
|
||||
end
|
||||
|
||||
require "mrsk/commands/builder/native"
|
||||
require "mrsk/commands/builder/native/remote"
|
||||
require "mrsk/commands/builder/multiarch"
|
||||
require "mrsk/commands/builder/multiarch/remote"
|
||||
|
||||
@@ -17,10 +17,10 @@ class Mrsk::Commands::Builder::Base < Mrsk::Commands::Base
|
||||
|
||||
private
|
||||
def args
|
||||
config.builder["args"] || {}
|
||||
(config.builder && config.builder["args"]) || {}
|
||||
end
|
||||
|
||||
def secrets
|
||||
config.builder["secrets"] || []
|
||||
(config.builder && config.builder["secrets"]) || []
|
||||
end
|
||||
end
|
||||
|
||||
@@ -12,7 +12,8 @@ class Mrsk::Commands::Builder::Multiarch < Mrsk::Commands::Builder::Base
|
||||
def push
|
||||
docker :buildx, :build,
|
||||
"--push",
|
||||
"--platform linux/amd64,linux/arm64",
|
||||
"--platform", "linux/amd64,linux/arm64",
|
||||
"--builder", builder_name,
|
||||
"-t", config.absolute_image,
|
||||
*build_args,
|
||||
*build_secrets,
|
||||
@@ -27,6 +28,6 @@ class Mrsk::Commands::Builder::Multiarch < Mrsk::Commands::Builder::Base
|
||||
|
||||
private
|
||||
def builder_name
|
||||
"mrsk-#{config.service}"
|
||||
"mrsk-#{config.service}-multiarch"
|
||||
end
|
||||
end
|
||||
|
||||
@@ -15,8 +15,16 @@ class Mrsk::Commands::Builder::Multiarch::Remote < Mrsk::Commands::Builder::Mult
|
||||
end
|
||||
|
||||
private
|
||||
def builder_name
|
||||
super + "-remote"
|
||||
end
|
||||
|
||||
def builder_name_with_arch(arch)
|
||||
"#{builder_name}-#{arch}"
|
||||
end
|
||||
|
||||
def create_local_buildx
|
||||
docker :buildx, :create, "--use", "--name", builder_name, builder_name_with_arch(local["arch"]), "--platform", "linux/#{local["arch"]}"
|
||||
docker :buildx, :create, "--name", builder_name, builder_name_with_arch(local["arch"]), "--platform", "linux/#{local["arch"]}"
|
||||
end
|
||||
|
||||
def append_remote_buildx
|
||||
@@ -50,9 +58,4 @@ class Mrsk::Commands::Builder::Multiarch::Remote < Mrsk::Commands::Builder::Mult
|
||||
def remote
|
||||
config.builder["remote"]
|
||||
end
|
||||
|
||||
private
|
||||
def builder_name_with_arch(arch)
|
||||
"#{builder_name}-#{arch}"
|
||||
end
|
||||
end
|
||||
|
||||
71
lib/mrsk/commands/builder/native/remote.rb
Normal file
71
lib/mrsk/commands/builder/native/remote.rb
Normal file
@@ -0,0 +1,71 @@
|
||||
require "mrsk/commands/builder/native"
|
||||
|
||||
class Mrsk::Commands::Builder::Native::Remote < Mrsk::Commands::Builder::Native
|
||||
def create
|
||||
combine \
|
||||
create_context,
|
||||
create_buildx
|
||||
end
|
||||
|
||||
def remove
|
||||
combine \
|
||||
remove_context,
|
||||
remove_buildx
|
||||
end
|
||||
|
||||
def push
|
||||
docker :buildx, :build,
|
||||
"--push",
|
||||
"--platform", platform,
|
||||
"--builder", builder_name,
|
||||
"-t", config.absolute_image,
|
||||
*build_args,
|
||||
*build_secrets,
|
||||
"."
|
||||
end
|
||||
|
||||
def info
|
||||
combine \
|
||||
docker(:context, :ls),
|
||||
docker(:buildx, :ls)
|
||||
end
|
||||
|
||||
|
||||
private
|
||||
def arch
|
||||
config.builder["remote"]["arch"]
|
||||
end
|
||||
|
||||
def host
|
||||
config.builder["remote"]["host"]
|
||||
end
|
||||
|
||||
def builder_name
|
||||
"mrsk-#{config.service}-native-remote"
|
||||
end
|
||||
|
||||
def builder_name_with_arch
|
||||
"#{builder_name}-#{arch}"
|
||||
end
|
||||
|
||||
def platform
|
||||
"linux/#{arch}"
|
||||
end
|
||||
|
||||
def create_context
|
||||
docker :context, :create,
|
||||
builder_name_with_arch, "--description", "'#{builder_name} #{arch} native host'", "--docker", "'host=#{host}'"
|
||||
end
|
||||
|
||||
def remove_context
|
||||
docker :context, :rm, builder_name_with_arch
|
||||
end
|
||||
|
||||
def create_buildx
|
||||
docker :buildx, :create, "--name", builder_name, builder_name_with_arch, "--platform", platform
|
||||
end
|
||||
|
||||
def remove_buildx
|
||||
docker :buildx, :rm, builder_name
|
||||
end
|
||||
end
|
||||
@@ -8,7 +8,8 @@ class Mrsk::Commands::Traefik < Mrsk::Commands::Base
|
||||
"-p 80:80",
|
||||
"-v /var/run/docker.sock:/var/run/docker.sock",
|
||||
"traefik",
|
||||
"--providers.docker"
|
||||
"--providers.docker",
|
||||
"--log.level=DEBUG"
|
||||
end
|
||||
|
||||
def start
|
||||
@@ -23,8 +24,17 @@ class Mrsk::Commands::Traefik < Mrsk::Commands::Base
|
||||
docker :ps, "--filter", "name=traefik"
|
||||
end
|
||||
|
||||
def logs
|
||||
docker :logs, "traefik", "-n", "100", "-t"
|
||||
def logs(since: nil, lines: nil, grep: nil)
|
||||
pipe \
|
||||
docker(:logs, "traefik", (" --since #{since}" if since), (" -n #{lines}" if lines), "-t", "2>&1"),
|
||||
("grep '#{grep}'" if grep)
|
||||
end
|
||||
|
||||
def follow_logs(host:, grep: nil)
|
||||
run_over_ssh pipe(
|
||||
docker(:logs, "traefik", "-t", "-n", "10", "-f", "2>&1"),
|
||||
("grep '#{grep}'" if grep)
|
||||
).join(" "), host: host
|
||||
end
|
||||
|
||||
def remove_container
|
||||
|
||||
Reference in New Issue
Block a user