Allow custom version to be passed in via CLI

This commit is contained in:
David Heinemeier Hansson
2023-01-20 17:46:09 +01:00
parent 3d66e9ed33
commit 3bf56c2fdb
7 changed files with 39 additions and 29 deletions

View File

@@ -9,6 +9,8 @@ module Mrsk::Cli
class_option :verbose, type: :boolean, aliases: "-v", desc: "Detailed logging"
class_option :version, desc: "Run commands against a specific app version"
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)"
@@ -27,6 +29,7 @@ module Mrsk::Cli
commander.config_file = Pathname.new(File.expand_path(options[:config_file]))
commander.destination = options[:destination]
commander.verbose = options[:verbose]
commander.version = options[:version]
commander.specific_hosts = options[:hosts]&.split(",")
commander.specific_roles = options[:roles]&.split(",")

View File

@@ -8,14 +8,17 @@ require "mrsk/commands/traefik"
require "mrsk/commands/registry"
class Mrsk::Commander
attr_accessor :config_file, :destination, :verbose
attr_accessor :config_file, :destination, :verbose, :version
def initialize(config_file: nil, destination: nil, verbose: false)
@config_file, @destination, @verbose = config_file, destination, verbose
end
def config
@config ||= Mrsk::Configuration.create_from(config_file, destination: destination).tap { |config| setup_with(config) }
@config ||= \
Mrsk::Configuration
.create_from(config_file, destination: destination, version: cascading_version)
.tap { |config| configure_sshkit_with(config) }
end
attr_accessor :specific_hosts
@@ -71,8 +74,12 @@ class Mrsk::Commander
end
private
def cascading_version
version.presence || ENV["VERSION"] || `git rev-parse HEAD`.strip
end
# Lazy setup of SSHKit
def setup_with(config)
def configure_sshkit_with(config)
SSHKit::Backend::Netssh.configure { |ssh| ssh.ssh_options = config.ssh_options }
SSHKit.config.command_map[:docker] = "docker" # No need to use /usr/bin/env, just clogs up the logs
SSHKit.config.output_verbosity = :debug if verbose

View File

@@ -10,13 +10,13 @@ class Mrsk::Configuration
delegate :argumentize, to: Mrsk::Utils
class << self
def create_from(base_config_file, destination: nil)
def create_from(base_config_file, destination: nil, version: "missing")
new(load_config_file(base_config_file).tap do |config|
if destination
config.merge! \
load_config_file destination_config_file(base_config_file, destination)
end
end)
end, version: version)
end
private
@@ -34,8 +34,9 @@ class Mrsk::Configuration
end
end
def initialize(config, validate: true)
def initialize(config, version: "missing", validate: true)
@config = ActiveSupport::InheritableOptions.new(config)
@version = version
ensure_required_keys_present if validate
end
@@ -62,7 +63,7 @@ class Mrsk::Configuration
def version
@version ||= ENV["VERSION"] || `git rev-parse HEAD`.strip
@version
end
def repository