Module: Kontena::Cli::Helpers::ExecHelper

Included in:
Containers::ExecCommand, Services::ExecCommand
Defined in:
lib/kontena/cli/helpers/exec_helper.rb

Instance Method Summary collapse

Instance Method Details

#connect(url, token) ⇒ WebSocket::Client::Simple

Parameters:

Returns:

  • (WebSocket::Client::Simple)


75
76
77
78
79
80
81
82
83
84
85
# File 'lib/kontena/cli/helpers/exec_helper.rb', line 75

def connect(url, token)
  options = {
    headers: {
      'Authorization' => "Bearer #{token.access_token}"
    }
  }
  if ENV['SSL_IGNORE_ERRORS'].to_s == 'true'
    options[:verify_mode] = ::OpenSSL::SSL::VERIFY_NONE
  end
  Kontena::Websocket::Client.new(url, options)
end

#handle_message(msg) ⇒ Object

Parameters:

  • msg (Hash)


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/kontena/cli/helpers/exec_helper.rb', line 27

def handle_message(msg)
  if msg.has_key?('exit')
    if msg['message']
      exit_with_error(msg['message'])
    else
      exit msg['exit'].to_i
    end
  elsif msg.has_key?('stream')
    if msg['stream'] == 'stdout'
      $stdout << msg['chunk']
    else
      $stderr << msg['chunk']
    end
  end
end

#parse_message(msg) ⇒ Object

Parameters:

  • msg (Websocket::Frame::Incoming)


44
45
46
47
48
# File 'lib/kontena/cli/helpers/exec_helper.rb', line 44

def parse_message(msg)
  JSON.parse(msg.data)
rescue JSON::ParserError
  nil
end

#stream_stdin_to_ws(ws, tty: nil) ⇒ Thread

Parameters:

  • ws (WebSocket::Client::Simple)

Returns:

  • (Thread)


8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/kontena/cli/helpers/exec_helper.rb', line 8

def stream_stdin_to_ws(ws, tty: nil)
  require 'io/console'
  Thread.new {
    if tty
      STDIN.raw {
        while char = STDIN.readpartial(1024)
          ws.text(JSON.dump({ stdin: char }))
        end
      }
    else
      while char = STDIN.gets
        ws.text(JSON.dump({ stdin: char }))
      end
      ws.text(JSON.dump({ stdin: nil }))
    end
  }
end

#ws_url(container_id, interactive: false, shell: false, tty: false) ⇒ String

Parameters:

  • container_id (String) —

    The container id

  • interactive (Boolean) (defaults to: false) —

    Interactive TTY on/off

  • shell (Boolean) (defaults to: false) —

    Shell on/of

  • tty (Boolean) (defaults to: false) —

    TTY on/of

Returns:



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/kontena/cli/helpers/exec_helper.rb', line 55

def ws_url(container_id, interactive: false, shell: false, tty: false)
  require 'uri' unless Object.const_defined?(:URI)
  extend Kontena::Cli::Common unless self.respond_to?(:require_current_master)

  url = URI.parse(require_current_master.url)
  url.scheme = url.scheme.sub('http', 'ws')
  url.path = "/v1/containers/#{container_id}/exec"
  if shell || interactive || tty
    query = {}
    query.merge!(interactive: true) if interactive
    query.merge!(shell: true) if shell
    query.merge!(tty: true) if tty
    url.query = URI.encode_www_form(query)
  end
  url.to_s
end