Skip to content

Commit

Permalink
Merge pull request #52 from metacoma/mindwm_io_objects
Browse files Browse the repository at this point in the history
Mindwm io objects
  • Loading branch information
metacoma authored Sep 24, 2023
2 parents 27fdce2 + d2da96a commit 0e7cf3a
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 2 deletions.
116 changes: 116 additions & 0 deletions files/textfsm/parser_output.py
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()

6 changes: 6 additions & 0 deletions files/textfsm/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pika
neo4j
grpcio
protobuf
textfsm
tabulate
10 changes: 10 additions & 0 deletions files/textfsm/template.txt
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

3 changes: 3 additions & 0 deletions inventory/classes/rabbitmq/common.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ parameters:
io-context:
type: fanout
durable: true
io-document:
type: fanout
durable: true
rabbitmq_version: 3.12.4
rabbitmq_user: user
rabbitmq_password: password
4 changes: 2 additions & 2 deletions templates/rabbitmq/create_exchange.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
RABBITMQ_ENDPOINT=`minikube ip`:{{ p.rabbitmq_manage_node_port }}

{% if "rabbitmq_exchanges" in p %}
{% for exhcnage_name, exchange in p.rabbitmq_exchanges.items() %}
kubectl -n {{ p.rabbitmq_namespace }} exec {{ p.rabbitmq_release_name }}-0 -ti -- curl -i -u {{ p.rabbitmq_user }}:{{ p.rabbitmq_password }} -H "Content-type: application/json" -XPUT -d'{{ exchange | to_json }}' localhost:15672/api/exchanges/%2f/io-context
{% for exchange_name, exchange in p.rabbitmq_exchanges.items() %}
kubectl -n {{ p.rabbitmq_namespace }} exec {{ p.rabbitmq_release_name }}-0 -ti -- curl -i -u {{ p.rabbitmq_user }}:{{ p.rabbitmq_password }} -H "Content-type: application/json" -XPUT -d'{{ exchange | to_json }}' localhost:15672/api/exchanges/%2f/{{ exchange_name }}
{% endfor %} {# for exhcnage_name, exchange in rabbitmq_exchanges.items() #}
{% endif %}

0 comments on commit 0e7cf3a

Please sign in to comment.