Ask Your Question
1

How to connect to a serial port of an instance via websocket?

asked 2014-10-09 10:52:44 -0500

My question is similar to https://ask.openstack.org/en/question/50235/accessing-serial-port-of-running-instance/ (this).

The blueprint https://blueprints.launchpad.net/nova/+spec/serial-ports (serial-ports) is implemented. The nova-client can query the serial ports connection info of an instance with:

nova get-serial-console myInstance

It returns a string like:

ws://127.0.0.1:6083/?token=e2b42240-375d-41fe-a166-367e4bbdce35

How do I consume (connect to) such a serial port?

I tried to create a client like the following code[1] and got an error (undefined):

<!DOCTYPE html>
<meta charset="utf-8" />
<title>WebSocket Test</title>
<script language="javascript" type="text/javascript">

  // var wsUri = "ws://echo.websocket.org/";
  var wsUri = "ws://127.0.0.1:6083/?token=e2b42240-375d-41fe-a166-367e4bbdce35"
  var output;

  function init()
  {
    output = document.getElementById("output");
    testWebSocket();
  }

  function testWebSocket()
  {
    websocket = new WebSocket(wsUri);
    websocket.onopen = function(evt) { onOpen(evt) };
    websocket.onclose = function(evt) { onClose(evt) };
    websocket.onmessage = function(evt) { onMessage(evt) };
    websocket.onerror = function(evt) { onError(evt) };
  }

  function onOpen(evt)
  {
    writeToScreen("CONNECTED");
    doSend("WebSocket rocks");
  }

  function onClose(evt)
  {
    writeToScreen("DISCONNECTED");
  }

  function onMessage(evt)
  {
    writeToScreen('<span style="color: blue;">RESPONSE: ' + evt.data+'</span>');
    websocket.close();
  }

  function onError(evt)
  {
    writeToScreen('<span style="color: red;">ERROR:</span> ' + evt.data);
  }

  function doSend(message)
  {
    writeToScreen("SENT: " + message); 
    websocket.send(message);
  }

  function writeToScreen(message)
  {
    var pre = document.createElement("p");
    pre.style.wordWrap = "break-word";
    pre.innerHTML = message;
    output.appendChild(pre);
  }

  window.addEventListener("load", init, false);

</script>
<h2>WebSocket Test</h2>

<div id="output"></div>

Sources:

  • [1] http://www.websocket.org/echo.html
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2014-10-30 09:12:37 -0500

A parallel discussion to this topic was done via OpenStacks mailing list [1][2]. I paste the results here, as it solved my problem.

prerequisites

The serialproxy was not started. So nobody was listening to the port 6083. Validate with:

$ netstat -nat | grep :608 

tcp  0  0 0.0.0.0:6080          0.0.0.0:*            LISTEN     
tcp  0  0 0.0.0.0:6081          0.0.0.0:*            LISTEN     
tcp  0  0 192.168.122.41:60858  192.168.122.41:5672  ESTABLISHED
tcp  0  0 192.168.122.41:60859  192.168.122.41:5672  ESTABLISHED
tcp6 0  0 192.168.122.41:5672   192.168.122.41:60858 ESTABLISHED
tcp6 0  0 192.168.122.41:5672   192.168.122.41:60859 ESTABLISHED

After finding [4] all I had to do was to start this proxy manually with:

$ nova-serialproxy

INFO nova.console.websocketproxy [-] WebSocket server settings:
INFO nova.console.websocketproxy [-]  - Listen on 0.0.0.0:6083
INFO nova.console.websocketproxy [-]  - Flash security policy server
INFO nova.console.websocketproxy [-]  - No SSL/TLS support (no cert file)
INFO nova.console.websocketproxy [-]  - proxying from 0.0.0.0:6083 to None:None

After executing this command, the netstat command from above shows a listener for port 6083:

$ netstat -nat | grep :608 

tcp  0  0 0.0.0.0:6080          0.0.0.0:*            LISTEN     
tcp  0  0 0.0.0.0:6081          0.0.0.0:*            LISTEN     
tcp  0  0 0.0.0.0:6083          0.0.0.0:*            LISTEN  
tcp  0  0 192.168.122.41:60858  192.168.122.41:5672  ESTABLISHED
tcp  0  0 192.168.122.41:60859  192.168.122.41:5672  ESTABLISHED
tcp6 0  0 192.168.122.41:5672   192.168.122.41:60858 ESTABLISHED
tcp6 0  0 192.168.122.41:5672   192.168.122.41:60859 ESTABLISHED

By using Sahids websocketclient (see below) and the URI I got from the command nova get-serial-console instance1 the connection gets established and one will see the login screen (e.g. from cirros).

client

This great example of a websocket client which can consume the URI the Nova API provides is written by Sahid Orentino Ferdjaoui [3]:

# demo os serial-console
#
# deps:
#  easy_install ws4py
#
# nova.conf:
#  [serial_console]
#  enabled = true
# 
# nova boot --image cirros --flavor 1 i1
# nova get-serial-console i1
# python client.py ws://127.0.0.1:6083/?token=5ed671c...

import sys
from ws4py.client.threadedclient import WebSocketClient

class LazyClient(WebSocketClient):
    def run(self):
        try:
            while not self.terminated:
                try:
                    b = self.sock.recv(4096)
                    sys.stdout.write(b)
                    sys.stdout.flush()
                except: # socket error expected
                    pass
        finally:
            self.terminate()

if __name__ == '__main__':
    if len(sys.argv) != 2 or not sys.argv[1].startswith("ws"):
        print "Usage %s: Please use websocket url"
        print "Example: ws://127.0.0.1:6083/?token=xxx"
        exit(1)
    try:
        ws = LazyClient(sys.argv[1], protocols=['binary'])
        ws.connect()
        while True:
            # keyboard event...
            c = sys.stdin.read(1)
            if c:
                ws.send(c)
        ws.run_forever()

    except KeyboardInterrupt:
        ws.close()

consuming ...

(more)
edit flag offensive delete link more

Comments

Followup:

Markus Zoeller gravatar imageMarkus Zoeller ( 2015-01-05 10:43:42 -0500 )edit

Hi Markus, can you help to answer my question ? https://ask.openstack.org/en/question/94416/how-to-make-serial-proxy-working/ (https://ask.openstack.org/en/question...) Thanks,

ColbyW gravatar imageColbyW ( 2016-07-08 17:12:03 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Get to know Ask OpenStack

Resources for moderators

Question Tools

2 followers

Stats

Asked: 2014-10-09 10:52:44 -0500

Seen: 2,641 times

Last updated: Oct 30 '14