Skip to content

Commit

Permalink
feat: Merge pull request #26 from jpradoar/feat/issues/25 issue/#25
Browse files Browse the repository at this point in the history
feat: update apps and dockerfiles issue/#25
  • Loading branch information
jpradoar authored Nov 17, 2024
2 parents 981a1b1 + 6871cf3 commit 7a60798
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 176 deletions.
1 change: 1 addition & 0 deletions 03-producer/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,4 @@ def main():
# uso el trheading para exponer cada funcion en un hilo separado.
Thread(target = main).start()
Thread(target = monitoring).start()

149 changes: 0 additions & 149 deletions 03-producer/api_v2.py

This file was deleted.

2 changes: 1 addition & 1 deletion 03-producer/templates/custom.css
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@
cursor: pointer;
font-size: 12px; /* Reducido el tamaño de la fuente */
transition: background-color 0.3s ease;
}
}
2 changes: 1 addition & 1 deletion 03-producer/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,4 @@ <h3>Payment</h3>


</body>
</html>
</html>
24 changes: 12 additions & 12 deletions 04-consumer/sub.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import json
import sys
import subprocess
import time
import time
import logging # Para login a consola (stdout)
from prometheus_client import start_http_server # usado para levantar el http_server
from prometheus_client import Info, Counter # usado para exponer metricas de info y contadores
Expand Down Expand Up @@ -40,28 +40,28 @@ def sendmsg(message):
# Esta funcion es la que se encarga de enviar el mensaje a la cola para que el DBWriter agregue el cliente a la DB de clientes.
# recibe los parametros filtrados del JSON original que envio la api customer-portal y genera un nuevo JSON que lo envia a la cola de clientes
# donde será utilizado para escribir dicha data en la DB
def WriteDB(client,archtype,hardware,product,trace_id):
def WriteDB(client,archtype,hardware,product,trace_id):
message = "- *[Consumer] Send messaje to dbwriter to create new user in DBCLIENTS: [" + client + "] "
sendmsg(message)
channel.queue_declare(queue='clients', durable=True)
data = '{"client": "'+client+'","archtype": "'+archtype+'","hardware": "'+hardware+'","product": "'+product+'"}'
data = '{"client": "'+client+'","archtype": "'+archtype+'","hardware": "'+hardware+'","product": "'+product+'","trace_id": "'+trace_id+'"}'
channel.basic_publish(exchange='', routing_key='clients', body=data)
sendmsg("- *[Consumer] publish " + data)
sendmsg("- *[Consumer] publish " + data)

# Esta funcion recibe el environment donde se va a deployar, el namespace y el nombre del cliente
# Con todo eso genera un namespace y deploya el cliente.
# En este caso para ahorrar APIs uso un subprocess para ejecutar un comando de linux (helm).
# Idealmente deberia ser otra api o proceso que ejecute esto pero a fines de la demo funciona bien como ejemplo.
def executeDeployment(environment,client,product):
# Idealmente deberia ser otra api o proceso que ejecute esto pero a fines de la demo funciona bien como ejemplo.
def executeDeployment(environment,client,product,trace_id):
#
# Esta parte se ve fea, pero es solo para la demo
# Esta parte se ve fea, pero es solo para la demo
# y para no publicar codigo propietario use directamente el subprocess.
# A fines practicos la funcionalidad es la misma.
#
subprocess.run("helm repo add bitnami https://charts.bitnami.com/bitnami", shell=True)
sendmsg("- *[Consumer] [run] Deployment for "+client+" with product "+product+" ")
#subprocess.run("helm upgrade -i -n mqtt-poc --create-namespace "+client+" bitnami/"+product+" ", shell=True)
subprocess.run("helm upgrade -i -n mqtt-poc --create-namespace "+client+" bitnami/"+product+" --set podAnnotations.trace_id="+trace_id+",podAnnotations.client="+client+",podLabels.trace_id="+trace_id+",podLabels.trace_id="+trace_id+" ", shell=True)
subprocess.run("helm upgrade -i -n "+client+" --create-namespace "+client+" bitnami/"+product+" --set podAnnotations.trace_id="+trace_id+",podAnnotations.client="+client+",podLabels.trace_id="+trace_id+",podLabels.trace_id="+trace_id+" ", shell=True)
time.sleep(3) # Los sleep solo los uso para que en la demo se vea con un poco de delay
sendmsg("- *[Consumer] deployment client: [" + client + "] DONE" )

Expand All @@ -87,12 +87,12 @@ def parseMsg(data):
sendmsg("- *[Consumer] Message: " + str(data) + "\n")
sendmsg("- *[Consumer] Message trace_id: " + str(trace_id) + "\n")
time.sleep (1)
WriteDB(client,archtype,hardware,product)
WriteDB(client,archtype,hardware,product,trace_id)
time.sleep (1)
executeDeployment(environment, client, product, trace_id)
time.sleep (1)
executeDeployment(environment, client, product)
time.sleep (1)
finish_message(client)

def callback(ch, method, properties, body):
parseMsg(body.decode("utf-8"))

Expand Down
22 changes: 11 additions & 11 deletions 05-dbwriter/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@
mqttvhost = str(os.environ.get('mqttvhost'))
mqttuser = str(os.environ.get('mqttuser'))
mqttpass = str(os.environ.get('mqttpass'))
mqttport = os.environ.get('mqttport')
mqttport = os.environ.get('mqttport') #
queue = str(os.environ.get('queue'))
credentials = pika.PlainCredentials(mqttuser, mqttpass)
parameters = pika.ConnectionParameters(host=mqtthost,port=mqttport,virtual_host=mqttvhost,credentials=credentials,connection_attempts=5, retry_delay=5)

# DATABASE
mysql_host = os.environ.get('MARIADB_HOST') # "mariadb"
mysql_user = os.environ.get('MARIADB_USER') # "admin"
mysql_pass = os.environ.get('MARIADB_PASSWORD') # "admin"
mysql_db = os.environ.get('MARIADB_DATABASE') # "clients"
mysql_host = os.environ.get('MARIADB_HOST') #"mariadb"
mysql_user = os.environ.get('MARIADB_USER') #"admin"
mysql_pass = os.environ.get('MARIADB_PASSWORD') #"admin"
mysql_db = os.environ.get('MARIADB_DATABASE') #"clients"

# TIEMPO
now = datetime.datetime.now()
Expand All @@ -49,10 +49,10 @@ def WriteDB(sql,val):
db = connector.cursor()
db.execute(sql,val)
connector.commit()
sendmsg("- *[DBWriter] One record was inserted, RecordID:"+ str(db.lastrowid))
sendmsg(" *[DBWriter] One record was inserted, RecordID:"+ str(db.lastrowid))
db.close()
connector.close()
sendmsg("- *[DBWriter] Waiting messages in Queue: [ "+ queue +" ] ")
sendmsg(" *[DBWriter] Waiting messages in Queue: [ "+ queue +" ] ")

# Función principal encargada de conectarse al MQTT Server, esperar mensajes y actuar en base al mensaje recibido.
def main():
Expand All @@ -64,7 +64,7 @@ def main():
def callback(ch, method, properties, body):
parseMsg(body.decode("utf-8"))

sendmsg("- *[DBWriter] Started and connected to Queue: [ "+ queue +" ] ")
sendmsg(" *[DBWriter] Started and connected to Queue: [ "+ queue +" ] ")
channel.basic_consume(queue=queue, on_message_callback=callback, auto_ack=True)
start_http_server(metrics_port)
metrics_info()
Expand All @@ -82,10 +82,10 @@ def parseMsg(data):
product = data['product']
xdate = dateformat
license = 'OpenSource'
trace_id = data['MessageAttributes']['trace_id']
trace_id = data['trace_id']
val = (client,archtype,hardware,product,xdate,license)
sql = "INSERT INTO clients (client,archtype,hardware,product,xdate,license) VALUES (%s,%s,%s,%s,%s,%s)"
sendmsg("- *[DBWriter] Message trace_id: " + str(trace_id) + "\n")
sendmsg(" *[DBWriter] Message trace_id: " + str(trace_id) + "\n")
WriteDB(sql,val)

# En general cuando uso docker-compose o kubernetes suele pasar que este servicio levanta antes que Rabbit.
Expand All @@ -97,7 +97,7 @@ def validateMQTTConnection():
channel = connection.channel()
break
except pika.exceptions.AMQPConnectionError:
sendmsg("- *[DBWriter] No se puede conectar a RabbitMQ, esperando 5 segundos para reconectar...")
sendmsg(" *[DBWriter] No se puede conectar a RabbitMQ, esperando 5 segundos para reconectar...")
time.sleep(5)

if __name__ == '__main__':
Expand Down
4 changes: 2 additions & 2 deletions 12-k8s-event-exporter/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
FROM alpine:3.19.0
FROM alpine:3.20.3
RUN apk update \
&& apk add curl \
&& curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl" \
&& chmod +x kubectl \
&& mv kubectl /usr/local/bin/
ENTRYPOINT ["/bin/sh","-c","kubectl get events -Aw"]
ENTRYPOINT ["/bin/sh","-c","kubectl get events -Aw"]

0 comments on commit 7a60798

Please sign in to comment.