Class: Nvoi::Cli::Logs::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/nvoi/cli/logs/command.rb

Overview

Command streams logs from app pods

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Command

Returns a new instance of Command.



8
9
10
11
# File 'lib/nvoi/cli/logs/command.rb', line 8

def initialize(options)
  @options = options
  @log = Nvoi.logger
end

Instance Method Details

#run(app_name) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/nvoi/cli/logs/command.rb', line 13

def run(app_name)
  config_path = resolve_config_path
  @config = Utils::ConfigLoader.load(config_path)

  # Apply branch override if specified
  apply_branch_override if @options[:branch]

  # Initialize cloud provider
  @provider = External::Cloud.for(@config)

  # Find main server
  server = @provider.find_server(@config.server_name)
  raise Errors::ServiceError, "server not found: #{@config.server_name}" unless server

  # Build deployment name from app name
  deployment_name = @config.namer.app_deployment_name(app_name)

  # Build kubectl logs command
  # --prefix shows pod name, --all-containers handles multi-container pods
  follow_flag = @options[:follow] ? "-f" : ""
  tail_flag = "--tail=#{@options[:tail]}"

  kubectl_cmd = "kubectl logs -l app=#{deployment_name} --prefix --all-containers #{follow_flag} #{tail_flag}".strip.squeeze(" ")

  @log.info "Streaming logs for %s", deployment_name

  ssh = External::Ssh.new(server.public_ipv4, @config.ssh_key_path)
  ssh.execute(kubectl_cmd, stream: true)
end