Repository: mrsked/mrsk
File: lib/mrsk/cli/app.rb (Ruby)

Code before:
        end
      end
    end
  end

  desc "stop", "Stop app container on servers"
  def stop
    with_lock do
      MRSK.hosts.each do |host|
        roles = MRSK.roles_on(host)

        roles.each do |role|
          on(host) { execute *MRSK.auditor(role: role).record("Stopped app"), verbosity: :debug }
          all_versions = list_versions(host: host, role: role)
          all_versions.each do |version|
            on(host) { execute *MRSK.app(role: role).stop(version: version), raise_on_non_zero_exit: false }
          end
        end
      end
    end
  end

  desc "stop_stale_containers", "Stop all app stale containers"
  def stop_stale_containers
    with_lock do
      MRSK.hosts.each do |host|
        roles = MRSK.roles_on(host)

        roles.each do |role|
          stale_versions = list_versions(host: host, role: role).drop(1)
          stale_versions.each do |version|
            say "Stopping stale container with version #{version}", :red
            on(host) { execute *MRSK.app(role: role).stop(version: version), raise_on_non_zero_exit: false }
          end
        end
      end
    end
  end

  # FIXME: Drop in favor of just containers?
  desc "details", "Show details about app containers"
  def details
    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).info)
      end
    end
  end


Review comment (suggestion):
Possible to share implementation with `stop`? Both are stopping containers, just with different versions.

Code after:
        roles.each do |role|
          execute *MRSK.auditor.record("Started app version #{MRSK.config.version}"), verbosity: :debug
          execute *MRSK.app(role: role).start, raise_on_non_zero_exit: false
        end
      end
    end
  end

  desc "stop", "Stop app container on servers"
  def stop
    with_lock do
      on(MRSK.hosts) do |host|
        roles = MRSK.roles_on(host)

        roles.each do |role|
          execute *MRSK.auditor(role: role).record("Stopped app"), verbosity: :debug
          execute *MRSK.app(role: role).stop, raise_on_non_zero_exit: false
        end
      end
    end
  end

  # FIXME: Drop in favor of just containers?
  desc "details", "Show details about app containers"
  def details
    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).info)
      end
    end
  end

  desc "exec [CMD]", "Execute a custom command on servers (use --help to show options)"
  option :interactive, aliases: "-i", type: :boolean, default: false, desc: "Execute command over ssh for an interactive shell (use for console/bash)"
  option :reuse, type: :boolean, default: false, desc: "Reuse currently running container instead of starting a new one"
  def exec(cmd)
    case
    when options[:interactive] && options[:reuse]
      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(role: "web").execute_in_existing_container_over_ssh(cmd, host: MRSK.primary_host) }
      end

    when options[:interactive]
      say "Get most recent version available as an image...", :magenta unless options[:version]
      using_version(version_or_latest) do |version|
        say "Launching interactive command with version #{version} via SSH from new container on #{MRSK.primary_host}...", :magenta
        run_locally { exec MRSK.app.execute_in_new_container_over_ssh(cmd, host: MRSK.primary_host) }