Class: Rancher::Shell::Commands::Exec

Inherits:
Object
  • Object
show all
Includes:
LoggerHelper
Defined in:
lib/rancher/shell/commands/exec.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from LoggerHelper

#exit_with_error, #logger

Constructor Details

#initializeExec

Returns a new instance of Exec.



13
14
15
16
17
18
19
20
21
22
# File 'lib/rancher/shell/commands/exec.rb', line 13

def initialize
  @config = Config.get_all
  logger.debug "config = #{@config}"
  exit_with_error "Project not found: #{@config['project']}" unless @config['project']
  exit_with_error "Command not specified" unless @config['options']['command'] && @config['options']['command'] != ''
  exit_with_error "Container not specified" unless @config['options']['container'] && @config['options']['container'] != ''
  exit_with_error "API Host Required" unless @config['project']['api'] && @config['project']['api']['host']
  exit_with_error "API Key Required" unless @config['project']['api'] && @config['project']['api']['key']
  exit_with_error "API Secret Required" unless @config['project']['api'] && @config['project']['api']['secret']
end

Instance Attribute Details

#apiObject (readonly)

Returns the value of attribute api.



11
12
13
# File 'lib/rancher/shell/commands/exec.rb', line 11

def api
  @api
end

#websocketObject (readonly)

Returns the value of attribute websocket.



11
12
13
# File 'lib/rancher/shell/commands/exec.rb', line 11

def websocket
  @websocket
end

Instance Method Details

#listen!Object



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rancher/shell/commands/exec.rb', line 24

def listen!
  begin
    logger.info "listening"
    system("stty raw")
    while input = STDIN.getc
      @websocket.send Base64.encode64 input
    end
  ensure
    system("stty -raw echo")
  end
end

#retrieve_containers!Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/rancher/shell/commands/exec.rb', line 44

def retrieve_containers!
  @response = @api.get(
    "containers",
  )
  @containers = @response.json['data'].map do |container|
    {
      'id' => container['id'],
      'name' => container['name'],
      'state' => container['state'],
      'ports' => container['ports'],
    }
  end
  @container = @containers.find { |container| container['name'] === @config['options']['container'] }
  exit_with_error "could not find container: #{@config['options']['container']}" unless @container
end

#setup_api!Object



36
37
38
39
40
41
42
# File 'lib/rancher/shell/commands/exec.rb', line 36

def setup_api!
  @api = Rancher::Shell::Api.new(
    host: @config['project']['api']['host'],
    user: @config['project']['api']['key'],
    pass: @config['project']['api']['secret'],
  )
end

#setup_websocket!Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/rancher/shell/commands/exec.rb', line 60

def setup_websocket!
  logger.info "container = #{@container['id']}"
  # default_bash_command = "TERM=xterm-256color; export TERM; [ -x /bin/bash ] && ([ -x /usr/bin/script ] && /usr/bin/script -q -c \"/bin/bash\" /dev/null || exec /bin/bash) || exec /bin/sh"
  # @config['options']['command'] = default_bash_command if @config['options']['command'] === 'bash'
  logger.debug "running command: #{@config['options']['command']}"
  @response = @api.post(
    "containers/#{@container['id']}?action=execute",
    "command" => [
      "/bin/sh",
      "-c",
      @config['options']['command'],
    ],
    "attachStdin" => true,
    "attachStdout" => true,
    "tty" => true,
  )
  websocket_url = "#{@response.json['url']}?token=#{@response.json['token']}"
  logger.info "connecting to #{@response.json['url']} ..."
  @websocket = Rancher::Shell::WebsocketClient.new websocket_url, headers: { 'Authorization' => "Bearer #{@response.json['token']}"}
  @websocket.on :open do |event|
    logger.info "  connected!"
  end
  @websocket.on :chunk do |encoded_chunk|
    chunk = Base64.decode64 encoded_chunk
    emit :message, chunk
  end
  @websocket.on :message do |data|
    $stdout.print data
  end
  @websocket.on :error do |event|
    logger.error "socket error: #{event}"
    Kernel.exit true
  end
  @websocket.on :close do
    logger.error "closed connection"
    Kernel.exit true
  end
end