Global option for designating primary host only
This commit is contained in:
@@ -39,49 +39,33 @@ class Mrsk::Cli::App < Mrsk::Cli::Base
|
||||
on(MRSK.hosts) { |host| puts_by_host host, capture_with_info(*MRSK.app.info) }
|
||||
end
|
||||
|
||||
desc "exec [CMD]", "Execute a custom task on servers passed in as CMD='bin/rake some:task'"
|
||||
option :once, type: :boolean, default: false, desc: "Only perform command on primary host"
|
||||
desc "exec [CMD]", "Execute a custom command on servers"
|
||||
option :run, type: :boolean, default: false, desc: "Start a new container to run the command rather than reusing existing"
|
||||
def exec(cmd)
|
||||
runner = options[:run] ? :run_exec : :exec
|
||||
|
||||
if options[:once]
|
||||
on(MRSK.config.primary_host) { puts capture_with_info(*MRSK.app.send(runner, cmd)) }
|
||||
else
|
||||
on(MRSK.hosts) { |host| puts_by_host host, capture_with_info(*MRSK.app.send(runner, cmd)) }
|
||||
end
|
||||
on(MRSK.hosts) { |host| puts_by_host host, capture_with_info(*MRSK.app.send(runner, cmd)) }
|
||||
end
|
||||
|
||||
desc "console", "Start Rails Console on primary host"
|
||||
option :host, desc: "Start console on a different host"
|
||||
desc "console", "Start Rails Console on primary host (or specific host set by --hosts)"
|
||||
def console
|
||||
host = options[:host] || MRSK.config.primary_host
|
||||
|
||||
run_locally do
|
||||
puts "Launching Rails console on #{host}..."
|
||||
exec MRSK.app.console(host: host)
|
||||
puts "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"
|
||||
option :host, desc: "Start bash on a different host"
|
||||
def bash
|
||||
host = options[:host] || MRSK.config.primary_host
|
||||
|
||||
run_locally do
|
||||
puts "Launching bash session on #{host}..."
|
||||
exec MRSK.app.bash(host: host)
|
||||
puts "Launching bash session on #{MRSK.primary_host}..."
|
||||
exec MRSK.app.bash(host: MRSK.primary_host)
|
||||
end
|
||||
end
|
||||
|
||||
desc "runner [EXPRESSION]", "Execute Rails runner with given expression"
|
||||
option :once, type: :boolean, default: false, desc: "Only perform runner on primary host"
|
||||
def runner(expression)
|
||||
if options[:once]
|
||||
on(MRSK.config.primary_host) { puts capture_with_info(*MRSK.app.exec("bin/rails", "runner", "'#{expression}'")) }
|
||||
else
|
||||
on(MRSK.hosts) { |host| puts_by_host host, capture_with_info(*MRSK.app.exec("bin/rails", "runner", "'#{expression}'")) }
|
||||
end
|
||||
on(MRSK.hosts) { |host| puts_by_host host, capture_with_info(*MRSK.app.exec("bin/rails", "runner", "'#{expression}'")) }
|
||||
end
|
||||
|
||||
desc "containers", "List all the app containers currently on servers"
|
||||
|
||||
@@ -9,6 +9,7 @@ module Mrsk::Cli
|
||||
|
||||
class_option :verbose, type: :boolean, aliases: "-v", desc: "Detailed logging"
|
||||
|
||||
class_option :primary, type: :boolean, aliases: "-p", desc: "Run commands only on primary host instead of all"
|
||||
class_option :hosts, aliases: "-h", desc: "Run commands on these hosts instead of all (separate by comma)"
|
||||
class_option :roles, aliases: "-r", desc: "Run commands on these roles instead of all (separate by comma)"
|
||||
|
||||
@@ -25,9 +26,11 @@ module Mrsk::Cli
|
||||
MRSK.tap do |commander|
|
||||
commander.config_file = Pathname.new(File.expand_path(options[:config_file]))
|
||||
commander.destination = options[:destination]
|
||||
commander.hosts = options[:hosts]&.split(",")
|
||||
commander.roles = options[:roles]&.split(",")
|
||||
commander.verbose = options[:verbose]
|
||||
|
||||
commander.specific_hosts = options[:hosts]&.split(",")
|
||||
commander.specific_roles = options[:roles]&.split(",")
|
||||
commander.specific_primary! if options[:primary]
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
require "active_support/core_ext/enumerable"
|
||||
|
||||
require "mrsk/configuration"
|
||||
require "mrsk/commands/app"
|
||||
require "mrsk/commands/builder"
|
||||
@@ -16,20 +18,26 @@ class Mrsk::Commander
|
||||
@config ||= Mrsk::Configuration.create_from(config_file, destination: destination).tap { |config| setup_with(config) }
|
||||
end
|
||||
|
||||
def hosts=(hosts)
|
||||
@hosts = hosts if hosts.present?
|
||||
attr_accessor :specific_hosts
|
||||
|
||||
def specific_primary!
|
||||
self.specific_hosts = [ config.primary_web_host ]
|
||||
end
|
||||
|
||||
def roles=(role_names)
|
||||
@hosts = config.roles.select { |r| role_names.include?(r.name) }.flat_map(&:hosts) if role_names.present?
|
||||
def specific_roles=(role_names)
|
||||
self.specific_hosts = config.roles.select { |r| role_names.include?(r.name) }.flat_map(&:hosts) if role_names.present?
|
||||
end
|
||||
|
||||
def primary_host
|
||||
specific_hosts&.sole || config.primary_web_host
|
||||
end
|
||||
|
||||
def hosts
|
||||
@hosts || config.all_hosts
|
||||
specific_hosts || config.all_hosts
|
||||
end
|
||||
|
||||
def traefik_hosts
|
||||
@hosts || config.traefik_hosts
|
||||
specific_hosts || config.traefik_hosts
|
||||
end
|
||||
|
||||
|
||||
|
||||
@@ -57,11 +57,11 @@ class Mrsk::Commands::App < Mrsk::Commands::Base
|
||||
*command
|
||||
end
|
||||
|
||||
def console(host: config.primary_host)
|
||||
def console(host:)
|
||||
exec_over_ssh "bin/rails", "c", host: host
|
||||
end
|
||||
|
||||
def bash(host: config.primary_host)
|
||||
def bash(host:)
|
||||
exec_over_ssh "bash", host: host
|
||||
end
|
||||
|
||||
|
||||
@@ -52,7 +52,7 @@ class Mrsk::Configuration
|
||||
roles.flat_map(&:hosts)
|
||||
end
|
||||
|
||||
def primary_host
|
||||
def primary_web_host
|
||||
role(:web).hosts.first
|
||||
end
|
||||
|
||||
@@ -101,8 +101,8 @@ class Mrsk::Configuration
|
||||
def to_h
|
||||
{
|
||||
roles: role_names,
|
||||
hosts: hosts,
|
||||
primary_host: primary_host,
|
||||
hosts: all_hosts,
|
||||
primary_host: primary_web_host,
|
||||
version: version,
|
||||
repository: repository,
|
||||
absolute_image: absolute_image,
|
||||
|
||||
Reference in New Issue
Block a user