-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #52 from metacoma/mindwm_io_objects
Mindwm io objects
- Loading branch information
Showing
5 changed files
with
137 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
import sys | ||
import textfsm | ||
import pprint | ||
import json | ||
from tabulate import tabulate | ||
import io | ||
from neo4j import GraphDatabase | ||
import grpc | ||
import freeplane_pb2 | ||
import freeplane_pb2_grpc | ||
import pika | ||
import pprint | ||
|
||
template = sys.argv[1] | ||
output_file = sys.argv[2] | ||
|
||
rabbitmq_url = "amqp://user:[email protected]:30466/%2f" | ||
neo4j_url = "bolt://192.168.49.2:31237" | ||
neo4j_username = 'neo4j' | ||
neo4j_password = 'password' | ||
|
||
exchange_name = "io-document" | ||
|
||
connection = pika.BlockingConnection(pika.URLParameters(rabbitmq_url)) | ||
rabbitmq_channel = connection.channel() | ||
|
||
result = rabbitmq_channel.queue_declare(queue="", exclusive=True) | ||
queue_name = result.method.queue | ||
|
||
rabbitmq_channel.queue_bind(exchange=exchange_name, queue=queue_name) | ||
|
||
grpc_channel = grpc.insecure_channel('localhost:50051') | ||
fp = freeplane_pb2_grpc.FreeplaneStub(grpc_channel) | ||
|
||
def do_textfsm(stdout): | ||
print(stdout) | ||
with open(template) as f: | ||
re_table = textfsm.TextFSM(io.StringIO(f.read())) | ||
header = re_table.header | ||
result = re_table.ParseText(stdout) | ||
# pprint.pprint(json.dumps(result)) | ||
print(json.dumps({"header": header, "result": result})) | ||
#print(tabulate(result, headers=header)) | ||
return header, result | ||
|
||
class IoContextDocument: | ||
__io_document = {} | ||
__host = "" | ||
__tmux_session_name = "" | ||
__tmux_pane_id = "" | ||
|
||
def getUniqSessionId(self): | ||
return 'mindwm-{hostname}-{tmux_session_name}-{tmux_pane_id}'.format( | ||
hostname = self.getHost(), | ||
tmux_session_name = self.getTmuxSessionName(), | ||
tmux_pane_id = self.getTmuxPaneId() | ||
) | ||
|
||
def getHost(self): | ||
return self.__io_document['host'] | ||
|
||
def getTmuxSessionName(self): | ||
return self.__io_document['metadata']['tmux']['session_name'] | ||
|
||
def getTmuxPaneId(self): | ||
return self.__io_document['metadata']['tmux']['pane_id'] | ||
|
||
def getPS1Start(self): | ||
# fix for the new pane | ||
ps1 = self.__io_document['message']['ps1_start'] | ||
if ps1 == '': | ||
ps1 = self.__io_document['message']['ps1_end'] | ||
|
||
return ps1 | ||
|
||
def getPS1End(self): | ||
return self.__io_document['message']['ps1_end'] | ||
|
||
|
||
def getInput(self): | ||
return self.__io_document['message']['input'] | ||
|
||
def getOutput(self): | ||
return self.__io_document['message']['output'] | ||
|
||
def __init__(self, document): | ||
self.__io_document = document | ||
pprint.pprint(document) | ||
print(f"IoContextDocument: {self.getHost()}->{self.getTmuxSessionName()}") | ||
|
||
def fpDrawTable(fp_node_id, header, table_body): | ||
for item in table_body: | ||
fp_node = fp.CreateChild(freeplane_pb2.CreateChildRequest(name=item[0], parent_node_id = fp_node_id)) | ||
name = item[0] | ||
for i in range(1, len(item)): | ||
fp.NodeAttributeAdd(freeplane_pb2.NodeAttributeAddRequest(node_id=fp_node.node_id, attribute_name=header[i], attribute_value=item[i])) | ||
print(f"{header[i]}: {item[i]}\n") | ||
|
||
|
||
|
||
def callback(ch, method, properties, body): | ||
data = json.loads(body.decode()) | ||
io_context_document = IoContextDocument(data) | ||
original_string = io_context_document.getOutput() | ||
|
||
lines = original_string.splitlines() | ||
non_empty_lines = [line for line in lines if line.strip()] | ||
result_string = "\n".join(non_empty_lines) | ||
|
||
header, result = do_textfsm(result_string) | ||
fpDrawTable(data['_fp_node_id'], header, result) | ||
|
||
rabbitmq_channel.basic_consume(queue=queue_name, on_message_callback=callback, auto_ack=True) | ||
print("Waiting for messages. To exit, press Ctrl+C") | ||
rabbitmq_channel.start_consuming() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
pika | ||
neo4j | ||
grpcio | ||
protobuf | ||
textfsm | ||
tabulate |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
Value Key POD_NAME ([^ ]+) | ||
Value Required NOT_READY ([0-9]) | ||
Value Required TOTAL ([0-9]) | ||
Value Required STATUS (\S+) | ||
Value Required RESTARTS ([0-9]+) | ||
Value Required AGE ([0-9]+[a-z]) | ||
|
||
Start | ||
^${POD_NAME}\s+${NOT_READY}/${TOTAL}\s+${STATUS}\s+${RESTARTS} \([0-9a-z]+ ago\)\s+${AGE} -> Continue.Record | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters