diff --git a/lib/kamal/cli/accessory.rb b/lib/kamal/cli/accessory.rb index d03bf7cfa..fc64f2314 100644 --- a/lib/kamal/cli/accessory.rb +++ b/lib/kamal/cli/accessory.rb @@ -128,12 +128,13 @@ def restart(name) desc "details [NAME]", "Show details about accessory on host (use NAME=all to show all accessories)" def details(name) + quiet = options[:quiet] if name == "all" KAMAL.accessory_names.each { |accessory_name| details(accessory_name) } else type = "Accessory #{name}" with_accessory(name) do |accessory, hosts| - on(hosts) { puts_by_host host, capture_with_info(*accessory.info), type: type } + on(hosts) { puts_by_host host, capture_with_info(*accessory.info), type: type, quiet: quiet } end end end @@ -145,6 +146,8 @@ def exec(name, *cmd) pre_connect_if_required cmd = Kamal::Utils.join_commands(cmd) + quiet = options[:quiet] + with_accessory(name) do |accessory, hosts| case when options[:interactive] && options[:reuse] @@ -160,7 +163,7 @@ def exec(name, *cmd) say "Launching command from existing container...", :magenta on(hosts) do |host| execute *KAMAL.auditor.record("Executed cmd '#{cmd}' on #{name} accessory"), verbosity: :debug - puts_by_host host, capture_with_info(*accessory.execute_in_existing_container(cmd)) + puts_by_host host, capture_with_info(*accessory.execute_in_existing_container(cmd)), quiet: quiet end else @@ -168,7 +171,7 @@ def exec(name, *cmd) on(hosts) do |host| execute *KAMAL.registry.login execute *KAMAL.auditor.record("Executed cmd '#{cmd}' on #{name} accessory"), verbosity: :debug - puts_by_host host, capture_with_info(*accessory.execute_in_new_container(cmd)) + puts_by_host host, capture_with_info(*accessory.execute_in_new_container(cmd)), quiet: quiet end end end diff --git a/lib/kamal/cli/app.rb b/lib/kamal/cli/app.rb index 22f888174..8064857f4 100644 --- a/lib/kamal/cli/app.rb +++ b/lib/kamal/cli/app.rb @@ -92,11 +92,12 @@ def stop # FIXME: Drop in favor of just containers? desc "details", "Show details about app containers" def details + quiet = options[:quiet] on(KAMAL.app_hosts) do |host| roles = KAMAL.roles_on(host) roles.each do |role| - puts_by_host host, capture_with_info(*KAMAL.app(role: role, host: host).info) + puts_by_host host, capture_with_info(*KAMAL.app(role: role, host: host).info), quiet: quiet end end end @@ -120,6 +121,7 @@ def exec(*cmd) cmd = Kamal::Utils.join_commands(cmd) env = options[:env] detach = options[:detach] + quiet = options[:quiet] case when options[:interactive] && options[:reuse] say "Get current version of running container...", :magenta unless options[:version] @@ -148,7 +150,7 @@ def exec(*cmd) roles.each do |role| execute *KAMAL.auditor.record("Executed cmd '#{cmd}' on app version #{version}", role: role), verbosity: :debug - puts_by_host host, capture_with_info(*KAMAL.app(role: role, host: host).execute_in_existing_container(cmd, env: env)) + puts_by_host host, capture_with_info(*KAMAL.app(role: role, host: host).execute_in_existing_container(cmd, env: env)), quiet: quiet end end end @@ -164,7 +166,7 @@ def exec(*cmd) roles.each do |role| execute *KAMAL.auditor.record("Executed cmd '#{cmd}' on app version #{version}"), verbosity: :debug - puts_by_host host, capture_with_info(*KAMAL.app(role: role, host: host).execute_in_new_container(cmd, env: env, detach: detach)) + puts_by_host host, capture_with_info(*KAMAL.app(role: role, host: host).execute_in_new_container(cmd, env: env, detach: detach)), quiet: quiet end end end @@ -173,12 +175,14 @@ def exec(*cmd) desc "containers", "Show app containers on servers" def containers - on(KAMAL.app_hosts) { |host| puts_by_host host, capture_with_info(*KAMAL.app.list_containers) } + quiet = options[:quiet] + on(KAMAL.app_hosts) { |host| puts_by_host host, capture_with_info(*KAMAL.app.list_containers), quiet: quiet } end desc "stale_containers", "Detect app stale containers" option :stop, aliases: "-s", type: :boolean, default: false, desc: "Stop the stale containers found" def stale_containers + quiet = options[:quiet] stop = options[:stop] with_lock_if_stopping do @@ -192,10 +196,10 @@ def stale_containers versions.each do |version| if stop - puts_by_host host, "Stopping stale container for role #{role} with version #{version}" + puts_by_host host, "Stopping stale container for role #{role} with version #{version}", quiet: quiet execute *app.stop(version: version), raise_on_non_zero_exit: false else - puts_by_host host, "Detected stale container for role #{role} with version #{version} (use `kamal app stale_containers --stop` to stop)" + puts_by_host host, "Detected stale container for role #{role} with version #{version} (use `kamal app stale_containers --stop` to stop)", quiet: quiet end end end @@ -205,7 +209,8 @@ def stale_containers desc "images", "Show app images on servers" def images - on(KAMAL.app_hosts) { |host| puts_by_host host, capture_with_info(*KAMAL.app.list_images) } + quiet = options[:quiet] + on(KAMAL.app_hosts) { |host| puts_by_host host, capture_with_info(*KAMAL.app.list_images), quiet: quiet } end desc "logs", "Show log lines from app on servers (use --help to show options)" @@ -224,6 +229,7 @@ def logs since = options[:since] container_id = options[:container_id] timestamps = !options[:skip_timestamps] + quiet = options[:quiet] if options[:follow] lines = options[:lines].presence || ((since || grep) ? nil : 10) # Default to 10 lines if since or grep isn't set @@ -246,9 +252,9 @@ def logs roles.each do |role| begin - puts_by_host host, capture_with_info(*KAMAL.app(role: role, host: host).logs(container_id: container_id, timestamps: timestamps, since: since, lines: lines, grep: grep, grep_options: grep_options)) + puts_by_host host, capture_with_info(*KAMAL.app(role: role, host: host).logs(container_id: container_id, timestamps: timestamps, since: since, lines: lines, grep: grep, grep_options: grep_options)), quiet: quiet rescue SSHKit::Command::Failed - puts_by_host host, "Nothing found" + puts_by_host host, "Nothing found", quiet: quiet end end end @@ -352,9 +358,10 @@ def remove_app_directories desc "version", "Show app version currently running on servers" def version + quiet = options[:quiet] on(KAMAL.app_hosts) do |host| role = KAMAL.roles_on(host).first - puts_by_host host, capture_with_info(*KAMAL.app(role: role, host: host).current_running_version).strip + puts_by_host host, capture_with_info(*KAMAL.app(role: role, host: host).current_running_version).strip, quiet: quiet end end diff --git a/lib/kamal/cli/main.rb b/lib/kamal/cli/main.rb index 93e0c884c..5d2ade680 100644 --- a/lib/kamal/cli/main.rb +++ b/lib/kamal/cli/main.rb @@ -112,8 +112,9 @@ def details desc "audit", "Show audit log from servers" def audit + quiet = options[:quiet] on(KAMAL.hosts) do |host| - puts_by_host host, capture_with_info(*KAMAL.auditor.reveal) + puts_by_host host, capture_with_info(*KAMAL.auditor.reveal), quiet: quiet end end diff --git a/lib/kamal/cli/proxy.rb b/lib/kamal/cli/proxy.rb index 0e00af6c8..119482a14 100644 --- a/lib/kamal/cli/proxy.rb +++ b/lib/kamal/cli/proxy.rb @@ -197,7 +197,8 @@ def restart desc "details", "Show details about proxy container from servers" def details - on(KAMAL.proxy_hosts) { |host| puts_by_host host, capture_with_info(*KAMAL.proxy.info), type: "Proxy" } + quiet = options[:quiet] + on(KAMAL.proxy_hosts) { |host| puts_by_host host, capture_with_info(*KAMAL.proxy.info), type: "Proxy", quiet: quiet } end desc "logs", "Show log lines from proxy on servers" diff --git a/lib/kamal/cli/server.rb b/lib/kamal/cli/server.rb index e3da8c182..7e4155472 100644 --- a/lib/kamal/cli/server.rb +++ b/lib/kamal/cli/server.rb @@ -6,6 +6,7 @@ def exec(*cmd) cmd = Kamal::Utils.join_commands(cmd) hosts = KAMAL.hosts + quiet = options[:quiet] case when options[:interactive] @@ -19,7 +20,7 @@ def exec(*cmd) on(hosts) do |host| execute *KAMAL.auditor.record("Executed cmd '#{cmd}' on #{host}"), verbosity: :debug - puts_by_host host, capture_with_info(cmd) + puts_by_host host, capture_with_info(cmd), quiet: quiet end end end diff --git a/lib/kamal/sshkit_with_ext.rb b/lib/kamal/sshkit_with_ext.rb index 317456384..428a3c668 100644 --- a/lib/kamal/sshkit_with_ext.rb +++ b/lib/kamal/sshkit_with_ext.rb @@ -18,8 +18,11 @@ def capture_with_pretty_json(*args, **kwargs) JSON.pretty_generate(JSON.parse(capture(*args, **kwargs))) end - def puts_by_host(host, output, type: "App") - puts "#{type} Host: #{host}\n#{output}\n\n" + def puts_by_host(host, output, type: "App", quiet: false) + unless quiet + puts "#{type} Host: #{host}" + end + puts "#{output}\n\n" end # Our execution pattern is for the CLI execute args lists returned diff --git a/test/integration/docker/deployer/app_with_roles/config/deploy.yml b/test/integration/docker/deployer/app_with_roles/config/deploy.yml index 6c5ef9f4f..32e8f9c95 100644 --- a/test/integration/docker/deployer/app_with_roles/config/deploy.yml +++ b/test/integration/docker/deployer/app_with_roles/config/deploy.yml @@ -57,5 +57,7 @@ accessories: - web aliases: whome: version - worker_hostname: app exec -r workers -q --reuse hostname - uname: server exec -q -p uname + worker_hostname: app exec -r workers --reuse hostname + worker_hostname_quiet: app exec -r workers -q --reuse hostname + uname: server exec -p uname + uname_quiet: server exec -q -p uname diff --git a/test/integration/main_test.rb b/test/integration/main_test.rb index df5c7af27..1825bf200 100644 --- a/test/integration/main_test.rb +++ b/test/integration/main_test.rb @@ -87,8 +87,14 @@ class MainTest < IntegrationTest output = kamal :worker_hostname, capture: true assert_match /App Host: vm3\nvm3-[0-9a-f]{12}$/, output + output = kamal :worker_hostname_quiet, capture: true + assert_match /vm3-[0-9a-f]{12}$/, output + output = kamal :uname, "-o", capture: true assert_match "App Host: vm1\nGNU/Linux", output + + output = kamal :uname_quiet, "-o", capture: true + assert_match "GNU/Linux", output end test "setup and remove" do