Rolify app cli/command
This commit is contained in:
@@ -7,24 +7,26 @@ class Mrsk::Cli::App < Mrsk::Cli::Base
|
||||
|
||||
cli = self
|
||||
|
||||
MRSK.config.roles.each do |role|
|
||||
on(role.hosts) do |host|
|
||||
execute *MRSK.auditor.record("Booted app version #{version}"), verbosity: :debug
|
||||
on(MRSK.hosts) do |host|
|
||||
roles = MRSK.roles_on(host)
|
||||
|
||||
roles.each do |role|
|
||||
execute *MRSK.auditor(role: role.name).record("Booted app version #{version}"), verbosity: :debug
|
||||
|
||||
begin
|
||||
old_version = capture_with_info(*MRSK.app.current_running_version).strip
|
||||
execute *MRSK.app.run(role: role.name)
|
||||
old_version = capture_with_info(*MRSK.app(role: role.name).current_running_version).strip
|
||||
execute *MRSK.app(role: role.name).run
|
||||
sleep MRSK.config.readiness_delay
|
||||
execute *MRSK.app.stop(version: old_version), raise_on_non_zero_exit: false if old_version.present?
|
||||
execute *MRSK.app(role: role.name).stop(version: old_version), raise_on_non_zero_exit: false if old_version.present?
|
||||
|
||||
rescue SSHKit::Command::Failed => e
|
||||
if e.message =~ /already in use/
|
||||
error "Rebooting container with same version #{version} already deployed on #{host} (may cause gap in zero-downtime promise!)"
|
||||
execute *MRSK.auditor.record("Rebooted app version #{version}"), verbosity: :debug
|
||||
execute *MRSK.auditor(role: role.name).record("Rebooted app version #{version}"), verbosity: :debug
|
||||
|
||||
execute *MRSK.app.stop(version: version)
|
||||
execute *MRSK.app.remove_container(version: version)
|
||||
execute *MRSK.app.run(role: role.name)
|
||||
execute *MRSK.app(role: role.name).stop(version: version)
|
||||
execute *MRSK.app(role: role.name).remove_container(version: version)
|
||||
execute *MRSK.app(role: role.name).run
|
||||
else
|
||||
raise
|
||||
end
|
||||
@@ -36,24 +38,38 @@ class Mrsk::Cli::App < Mrsk::Cli::Base
|
||||
|
||||
desc "start", "Start existing app container on servers"
|
||||
def start
|
||||
on(MRSK.hosts) do
|
||||
execute *MRSK.auditor.record("Started app version #{MRSK.version}"), verbosity: :debug
|
||||
execute *MRSK.app.start, raise_on_non_zero_exit: false
|
||||
on(MRSK.hosts) do |host|
|
||||
roles = MRSK.roles_on(host)
|
||||
|
||||
roles.each do |role|
|
||||
execute *MRSK.auditor(role: role.name).record("Started app version #{MRSK.version}"), verbosity: :debug
|
||||
execute *MRSK.app(role: role.name).start, raise_on_non_zero_exit: false
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
desc "stop", "Stop app container on servers"
|
||||
def stop
|
||||
on(MRSK.hosts) do
|
||||
execute *MRSK.auditor.record("Stopped app"), verbosity: :debug
|
||||
execute *MRSK.app.stop, raise_on_non_zero_exit: false
|
||||
on(MRSK.hosts) do |host|
|
||||
roles = MRSK.roles_on(host)
|
||||
|
||||
roles.each do |role|
|
||||
execute *MRSK.auditor(role: role.name).record("Stopped app"), verbosity: :debug
|
||||
execute *MRSK.app(role: role.name).stop, raise_on_non_zero_exit: false
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# FIXME: Drop in favor of just containers?
|
||||
desc "details", "Show details about app containers"
|
||||
def details
|
||||
on(MRSK.hosts) { |host| puts_by_host host, capture_with_info(*MRSK.app.info) }
|
||||
on(MRSK.hosts) do |host|
|
||||
roles = MRSK.roles_on(host)
|
||||
|
||||
roles.each do |role|
|
||||
puts_by_host host, capture_with_info(*MRSK.app(role: role.name).info)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
desc "exec [CMD]", "Execute a custom command on servers (use --help to show options)"
|
||||
@@ -65,7 +81,7 @@ class Mrsk::Cli::App < Mrsk::Cli::Base
|
||||
say "Get current version of running container...", :magenta unless options[:version]
|
||||
using_version(options[:version] || current_running_version) do |version|
|
||||
say "Launching interactive command with version #{version} via SSH from existing container on #{MRSK.primary_host}...", :magenta
|
||||
run_locally { exec MRSK.app.execute_in_existing_container_over_ssh(cmd, host: MRSK.primary_host) }
|
||||
run_locally { exec MRSK.app(role: role.name).execute_in_existing_container_over_ssh(cmd, host: MRSK.primary_host) }
|
||||
end
|
||||
|
||||
when options[:interactive]
|
||||
@@ -81,8 +97,12 @@ class Mrsk::Cli::App < Mrsk::Cli::Base
|
||||
say "Launching command with version #{version} from existing container...", :magenta
|
||||
|
||||
on(MRSK.hosts) do |host|
|
||||
execute *MRSK.auditor.record("Executed cmd '#{cmd}' on app version #{version}"), verbosity: :debug
|
||||
puts_by_host host, capture_with_info(*MRSK.app.execute_in_existing_container(cmd))
|
||||
roles = MRSK.roles_on(host)
|
||||
|
||||
roles.each do |role|
|
||||
execute *MRSK.auditor(role: role.name).record("Executed cmd '#{cmd}' on app version #{version}"), verbosity: :debug
|
||||
puts_by_host host, capture_with_info(*MRSK.app(role: role.name).execute_in_existing_container(cmd))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -147,17 +167,25 @@ class Mrsk::Cli::App < Mrsk::Cli::Base
|
||||
|
||||
desc "remove_container [VERSION]", "Remove app container with given version from servers", hide: true
|
||||
def remove_container(version)
|
||||
on(MRSK.hosts) do
|
||||
execute *MRSK.auditor.record("Removed app container with version #{version}"), verbosity: :debug
|
||||
execute *MRSK.app.remove_container(version: version)
|
||||
on(MRSK.hosts) do |host|
|
||||
roles = MRSK.roles_on(host)
|
||||
|
||||
roles.each do |role|
|
||||
execute *MRSK.auditor(role: role.name).record("Removed app container with version #{version}"), verbosity: :debug
|
||||
execute *MRSK.app(role: role.name).remove_container(version: version)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
desc "remove_containers", "Remove all app containers from servers", hide: true
|
||||
def remove_containers
|
||||
on(MRSK.hosts) do
|
||||
execute *MRSK.auditor.record("Removed all app containers"), verbosity: :debug
|
||||
execute *MRSK.app.remove_containers
|
||||
on(MRSK.hosts) do |host|
|
||||
roles = MRSK.roles_on(host)
|
||||
|
||||
roles.each do |role|
|
||||
execute *MRSK.auditor(role: role.name).record("Removed all app containers"), verbosity: :debug
|
||||
execute *MRSK.app(role: role.name).remove_containers
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -45,7 +45,7 @@ class Mrsk::Commander
|
||||
end
|
||||
|
||||
def roles_on(host)
|
||||
roles.select { |role| role.hosts.include?(host) }
|
||||
roles.select { |role| role.hosts.include?(host.to_s) }
|
||||
end
|
||||
|
||||
def traefik_hosts
|
||||
@@ -61,8 +61,8 @@ class Mrsk::Commander
|
||||
end
|
||||
|
||||
|
||||
def app
|
||||
@app ||= Mrsk::Commands::App.new(config)
|
||||
def app(role: nil)
|
||||
Mrsk::Commands::App.new(config, role: role)
|
||||
end
|
||||
|
||||
def accessory(name)
|
||||
|
||||
@@ -1,12 +1,19 @@
|
||||
class Mrsk::Commands::App < Mrsk::Commands::Base
|
||||
def run(role: :web)
|
||||
role = config.role(role)
|
||||
attr_reader :role
|
||||
|
||||
def initialize(config, role: nil)
|
||||
super(config)
|
||||
@role = role
|
||||
end
|
||||
|
||||
def run
|
||||
role = config.role(self.role)
|
||||
|
||||
docker :run,
|
||||
"--detach",
|
||||
"--restart unless-stopped",
|
||||
"--log-opt", "max-size=#{MAX_LOG_SIZE}",
|
||||
"--name", service_with_version_and_destination,
|
||||
"--name", service_with_version_and_destination_and_role,
|
||||
*role.env_args,
|
||||
*config.volume_args,
|
||||
*role.label_args,
|
||||
@@ -16,7 +23,7 @@ class Mrsk::Commands::App < Mrsk::Commands::Base
|
||||
end
|
||||
|
||||
def start
|
||||
docker :start, service_with_version_and_destination
|
||||
docker :start, service_with_version_and_destination_and_role
|
||||
end
|
||||
|
||||
def stop(version: nil)
|
||||
@@ -26,7 +33,7 @@ class Mrsk::Commands::App < Mrsk::Commands::Base
|
||||
end
|
||||
|
||||
def info
|
||||
docker :ps, *service_filter_with_destination
|
||||
docker :ps, *service_filter_with_destination_and_role
|
||||
end
|
||||
|
||||
|
||||
@@ -51,7 +58,7 @@ class Mrsk::Commands::App < Mrsk::Commands::Base
|
||||
def execute_in_existing_container(*command, interactive: false)
|
||||
docker :exec,
|
||||
("-it" if interactive),
|
||||
service_with_version_and_destination,
|
||||
service_with_version_and_destination_and_role,
|
||||
*command
|
||||
end
|
||||
|
||||
@@ -75,13 +82,13 @@ class Mrsk::Commands::App < Mrsk::Commands::Base
|
||||
|
||||
|
||||
def current_container_id
|
||||
docker :ps, "--quiet", *service_filter_with_destination
|
||||
docker :ps, "--quiet", *service_filter_with_destination_and_role
|
||||
end
|
||||
|
||||
def current_running_version
|
||||
# FIXME: Find more graceful way to extract the version from "app-version" than using sed and tail!
|
||||
pipe \
|
||||
docker(:ps, *service_filter_with_destination, "--format", '"{{.Names}}"'),
|
||||
docker(:ps, *service_filter_with_destination_and_role, "--format", '"{{.Names}}"'),
|
||||
%(sed 's/-/\\n/g'),
|
||||
"tail -n 1"
|
||||
end
|
||||
@@ -100,7 +107,7 @@ class Mrsk::Commands::App < Mrsk::Commands::Base
|
||||
|
||||
|
||||
def list_containers
|
||||
docker :container, :ls, "--all", *service_filter_with_destination
|
||||
docker :container, :ls, "--all", *service_filter_with_destination_and_role # TODO: role hier needed oder sogar falsch?
|
||||
end
|
||||
|
||||
def list_container_names
|
||||
@@ -109,12 +116,12 @@ class Mrsk::Commands::App < Mrsk::Commands::Base
|
||||
|
||||
def remove_container(version:)
|
||||
pipe \
|
||||
container_id_for(container_name: service_with_version_and_destination(version)),
|
||||
container_id_for(container_name: service_with_version_and_destination_and_role(version)),
|
||||
xargs(docker(:container, :rm))
|
||||
end
|
||||
|
||||
def remove_containers
|
||||
docker :container, :prune, "--force", *service_filter_with_destination
|
||||
docker :container, :prune, "--force", *service_filter_with_destination_and_role
|
||||
end
|
||||
|
||||
def list_images
|
||||
@@ -127,23 +134,22 @@ class Mrsk::Commands::App < Mrsk::Commands::Base
|
||||
|
||||
|
||||
private
|
||||
def service_with_version_and_destination(version = nil)
|
||||
[ config.service, config.destination, version || config.version ].compact.join("-")
|
||||
def service_with_version_and_destination_and_role(version = nil)
|
||||
[ config.service, role, config.destination, version || config.version ].compact.join("-") # TODO: is role sometimes nil here? bis jetzt wars nie nil
|
||||
end
|
||||
|
||||
def container_id_for_version(version)
|
||||
container_id_for(container_name: service_with_version_and_destination(version))
|
||||
container_id_for(container_name: service_with_version_and_destination_and_role(version))
|
||||
end
|
||||
|
||||
def service_filter
|
||||
[ "--filter", "label=service=#{config.service}" ]
|
||||
end
|
||||
|
||||
def service_filter_with_destination
|
||||
if config.destination
|
||||
service_filter << "label=destination=#{config.destination}"
|
||||
else
|
||||
service_filter
|
||||
def service_filter_with_destination_and_role
|
||||
service_filter.tap do |filter|
|
||||
filter << "label=destination=#{config.destination}" if config.destination
|
||||
filter << "label=role=#{role}" if role
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user