From bcdeeff94ff83f64bdd181b651d54d3fd9d9c405 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Tue, 10 Jan 2023 20:45:15 +0100 Subject: [PATCH] Start remote Rails console on primary host --- lib/mrsk/commands/app.rb | 4 ++++ lib/mrsk/configuration.rb | 8 ++++++-- lib/tasks/mrsk/app.rake | 6 ++++++ 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/lib/mrsk/commands/app.rb b/lib/mrsk/commands/app.rb index f59cd7f6..da756d05 100644 --- a/lib/mrsk/commands/app.rb +++ b/lib/mrsk/commands/app.rb @@ -47,6 +47,10 @@ class Mrsk::Commands::App < Mrsk::Commands::Base *command end + def console + "ssh -t #{config.ssh_user}@#{config.primary_host} 'docker exec -it -e RAILS_MASTER_KEY=#{config.master_key} #{config.env_args.join(" ")} #{config.service_with_version} bin/rails c'" + end + def list_containers docker :container, :ls, "-a", *service_filter end diff --git a/lib/mrsk/configuration.rb b/lib/mrsk/configuration.rb index 21365be2..37135f32 100644 --- a/lib/mrsk/configuration.rb +++ b/lib/mrsk/configuration.rb @@ -3,7 +3,7 @@ require "active_support/core_ext/string/inquiry" require "erb" class Mrsk::Configuration - delegate :service, :image, :servers, :env, :registry, :ssh_user, to: :config, allow_nil: true + delegate :service, :image, :servers, :env, :registry, to: :config, allow_nil: true class << self def load_file(file) @@ -78,8 +78,12 @@ class Mrsk::Configuration self.class.argumentize "-e", config.env if config.env.present? end + def ssh_user + config.ssh_user || "root" + end + def ssh_options - { user: config.ssh_user || "root", auth_methods: [ "publickey" ] } + { user: ssh_user, auth_methods: [ "publickey" ] } end def master_key diff --git a/lib/tasks/mrsk/app.rake b/lib/tasks/mrsk/app.rake index ff4f0ce0..0f73cc0a 100644 --- a/lib/tasks/mrsk/app.rake +++ b/lib/tasks/mrsk/app.rake @@ -58,6 +58,12 @@ namespace :mrsk do on(MRSK_CONFIG.hosts) { |host| puts "App Host: #{host}\n" + capture(*app.exec(ENV["CMD"])) + "\n\n" } end + desc "Start Rails Console on primary host" + task :console do + puts "Launching Rails console on #{MRSK_CONFIG.primary_host}..." + exec app.console + end + namespace :exec do desc "Execute Rails command on servers, like CMD='runner \"puts %(Hello World)\"" task :rails do