Class: Train::Transports::KubernetesHack::Connection

Inherits:
BaseConnection
  • Object
show all
Defined in:
lib/kitchen/verifier/train_kubernetes_hack.rb

Instance Method Summary collapse

Instance Method Details

#file(path) ⇒ Object



52
53
54
# File 'lib/kitchen/verifier/train_kubernetes_hack.rb', line 52

def file(path)
  @files[path] ||= file_via_connection(path)
end

#file_via_connection(path) ⇒ Object



61
62
63
# File 'lib/kitchen/verifier/train_kubernetes_hack.rb', line 61

def file_via_connection(path)
  defined?(Train::File::Remote::Linux) ? Train::File::Remote::Linux.new(self, path) : LinuxFile.new(self, path)
end

#osObject

The API for a connection changed a lot in 0.30, this is a compat shim.



48
49
50
# File 'lib/kitchen/verifier/train_kubernetes_hack.rb', line 48

def os
  @os ||= OSCommon.new(self, family: 'unix')
end

#run_command(cmd) ⇒ Object



56
57
58
# File 'lib/kitchen/verifier/train_kubernetes_hack.rb', line 56

def run_command(cmd)
  run_command_via_connection(cmd)
end

#run_command_via_connection(cmd) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/kitchen/verifier/train_kubernetes_hack.rb', line 65

def run_command_via_connection(cmd)
  kubectl_cmd = [options[:kubectl_path], 'exec']
  kubectl_cmd.concat(['--context', options[:context]]) if options[:context]
  kubectl_cmd.concat(['--container', options[:container]]) if options[:container]
  kubectl_cmd.concat([options[:pod], '--', '/bin/sh', '-c', cmd])

  so = Mixlib::ShellOut.new(kubectl_cmd, logger: logger)
  so.run_command
  if so.error?
    # Trim the "command terminated with exit code N" line from the end
    # of the stderr content.
    so.stderr.gsub!(/command terminated with exit code #{so.exitstatus}\n\Z/, '')
  end
  CommandResult.new(so.stdout, so.stderr, so.exitstatus)
end

#uriObject



81
82
83
84
85
86
87
# File 'lib/kitchen/verifier/train_kubernetes_hack.rb', line 81

def uri
  if options[:container]
    "kubernetes://#{options[:pod]}/#{options[:container]}"
  else
    "kubernetes://#{options[:pod]}"
  end
end