Class: Riak::Node::Console

Inherits:
Object show all
Includes:
Util::Translation
Defined in:
lib/riak/node/console.rb

Overview

Eases working with the Erlang console when attached to the Riak node.

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods included from Util::Translation

#i18n_scope, #t

Constructor Details

- (Console) initialize(pipedir, nodename)

Creates a Riak::Node::Console from the IO pipes connected to the node.

Parameters:

  • pipedir (String, Pathname)

    path to the pipes opened by run_erl

  • nodename (String)

    the name of the node the Console will be attached to

Raises:

  • (ArgumentError)


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/riak/node/console.rb', line 30

def initialize(pipedir, nodename)
  @nodename = nodename
  @mutex = Mutex.new
  @winch = Signal.trap("WINCH", &method(:handle_winch))
  @prompt = /\(#{Regexp.escape(nodename)}\)\d+>\s*/
  pipedir = Pathname(pipedir)
  pipedir.children.each do |path|
    if path.pipe?
      if path.fnmatch("*.r") # Read pipe
        # debug "Found read pipe: #{path}"
        @rfile ||= path
      elsif path.fnmatch("*.w") # Write pipe
        # debug "Found write pipe: #{path}"
        @wfile ||= path
      end
    else
      debug "Non-pipe found! #{path}"
    end
  end
  raise ArgumentError, t('no_pipes', :path => pipedir.to_s) if [@rfile, @wfile].any? {|p| p.nil? }
  # We have to open the read pipe AFTER we have sent some data
  # to the write pipe, otherwise JRuby hangs.
  begin
    debug "Opening write pipe."
    @w = open_write_pipe
    @w.sync = true
    debug "Opening read pipe."
    @r = open_read_pipe
    command 'ok.'
    debug "Initialized console: #{@r.inspect} #{@w.inspect}"
  rescue => e
    debug e.message
    close
    raise t('no_pipes', :path => pipedir.to_s) + "[ #{e.message} ]"
  end
end

Instance Attribute Details

- (String) nodename

The name of the connected node

Returns:

  • (String)

    the name of the connected node



17
18
19
# File 'lib/riak/node/console.rb', line 17

def nodename
  @nodename
end

Class Method Details

+ (Console) open(node)

Opens a Riak::Node::Console by connecting to the node.

Returns:



21
22
23
# File 'lib/riak/node/console.rb', line 21

def self.open(node)
  new node.pipe, node.name
end

Instance Method Details

- (Object) close

Closes the console by detaching from the pipes.



103
104
105
106
107
108
# File 'lib/riak/node/console.rb', line 103

def close
  @r.close if @r && !@r.closed?
  @w.close if @w && !@w.closed?
  Signal.trap("WINCH", @winch)
  freeze
end

- (Object) command(cmd)

Sends an Erlang command to the console

Parameters:

  • cmd (String)

    an Erlang expression to send to the node



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/riak/node/console.rb', line 69

def command(cmd)
  @mutex.synchronize do
    begin
      debug "Sending command #{cmd.inspect}"
      @w.print "#{cmd}\n"
      wait_for_erlang_prompt
    rescue SystemCallError
      close
    end
  end
end

- (Boolean) open?

Detects whether the console connection is still open, that is, if the node hasn't disconnected from the other side of the pipe.

Returns:

  • (Boolean)


84
85
86
# File 'lib/riak/node/console.rb', line 84

def open?
  (@r && !@r.closed?) && (@w && !@w.closed?)
end

- (Object) wait_for(pattern)

Scans the output of the console for the given pattern.

Parameters:

  • pattern (String, Regexp)

    the pattern to scan for



97
98
99
100
# File 'lib/riak/node/console.rb', line 97

def wait_for(pattern)
  debug "Scanning for #{pattern.inspect}"
  @r.expect(pattern)
end

- (Object) wait_for_erlang_prompt

Scans the output of the console until an Erlang shell prompt is found. Called by #command to ensure that the submitted command succeeds.



91
92
93
# File 'lib/riak/node/console.rb', line 91

def wait_for_erlang_prompt
  wait_for @prompt
end