Commit 742bda69 authored by Mitchell Moore's avatar Mitchell Moore
Browse files

Implemented celery to communicate with rabbitmq. Capture username and blazerid...

Implemented celery to communicate with rabbitmq. Capture username and blazerid to create accoutn and send vis tasks.py (celery)

- Make unique test username for debugging
- Trim requirements.txt
- Begin celery implementation and call
- Finish minor celery implementation
- Fix variable mixup. Log Room ID to client console
- Adding a process to start account creation process by calling flask_producer script
- Sending a message to the client webapp to start layover before starting celery task
- Adding another field to the form to capture username to create account with
- Capturing the value of username(Blazerid) field
- Remove unused base_consumer.py and test_producer.py scripts
parent 0e7cc949
......@@ -7,6 +7,7 @@ from __future__ import print_function
import uuid
from flask import Flask, redirect, url_for, request, render_template, flash, session
from flask_bootstrap import Bootstrap
import random
def create_app(config_name):
......@@ -19,7 +20,9 @@ def create_app(config_name):
@app.route('/', methods=['GET', 'POST']) # initial route to display the reg page
def index():
global return_url
username = "name_test"
if 'username' not in session:
session['username'] = "name_test" + str(random.randint(0, 10000))
if 'uid' not in session:
session['uid']=str(uuid.uuid4())
......@@ -28,7 +31,7 @@ def create_app(config_name):
return_url = request.args.get("redir") or "/pun/sys/dashboard"
if name != "":
return render_template('auth/SignUp.html', user=username)
return render_template('auth/SignUp.html', user=session['username'], room_id=session['uid'])
# misc page error catching
@app.errorhandler(403)
......
......@@ -20,10 +20,10 @@ function request_account() {
socket.emit('request account', {
fullname: document.getElementById("fullname").value,
reason: document.getElementById("reason").value,
username: document.getElementById("username").innerText
username: document.getElementById("bid").value
})
}
function refresh() {
document.location.reload(true);
}
\ No newline at end of file
}
......@@ -7,6 +7,7 @@
<script src="static/scripts/function.js"></script>
<script>
console.info('Room ID: {{ room_id }}');
var socket = io.connect('http://' + document.domain + ':' + location.port);
socket.on( 'connect', function() {
......@@ -85,10 +86,14 @@
</a>
</div>
<h2>Hello, <span id="username">{{ user }}</span>!</h2>
<!-- <h2>Hello, <span id="username">{{ user }}</span>!</h2> -->
<h2>Hi, </h2>
<div id="test">
<form action="." method="post" onsubmit="">
<div class="signUpContainer">
<label><b><label for="username">Blazer Id: </label><br></b></label>
<input class="form-control" id="bid" name="bid" placeholder="Enter BlazerId" required="" type="text">
<label><b><label for="fullname">Full Name: </label><br></b></label>
<input class="form-control" id="fullname" name="fullname" placeholder="Enter Full Name" required="" type="text">
<label><b><label for="reason">Reason for Requesting Account: </label><br></b></label>
......
#!/usr/bin/env python
import pika # python client
import sys
credentials = pika.PlainCredentials('reggie', 'reggie')
parameters = pika.ConnectionParameters('ood',
5672,
'/',
credentials)
connection = pika.BlockingConnection(parameters)
channel = connection.channel()
channel.exchange_declare(exchange='direct_logs', exchange_type='direct') # create exchange to pass messages
result = channel.queue_declare(queue='', exclusive=True)
queue_name = result.method.queue # creates a random name for the newly generated queue
nodes = sys.argv[1:]
if not nodes:
sys.stderr.write("Usage: %s [ood] [ohpc] [manager]\n" % sys.argv[0])
sys.exit(1)
for node in nodes:
channel.queue_bind(
exchange='direct_logs', queue=queue_name, routing_key=node) # combine exchange, queue, and define routing name
print(' [*] Waiting for logs. To exit press CTRL+C')
def callback(ch, method, properties, body):
print(" [x] %r:%r" % (method.routing_key, body))
print('[%r] User creation task is done.' % method.routing_key)
channel.basic_consume(
queue=queue_name, on_message_callback=callback, auto_ack=True) # ingest messages, and assume delivered via auto_ack
channel.start_consuming() # initiate message ingestion
alembic==1.4.0
appdirs==1.4.3
argh==0.26.2
blinker==1.4
certifi==2019.11.28
chardet==3.0.4
amqp==2.5.2
billiard==3.6.2.0
celery==4.4.0
Click==7.0
distlib==0.3.0
dnspython==1.16.0
dominate==2.4.0
enum34==1.1.6
eventlet==0.25.1
filelock==3.0.12
Flask==1.1.1
Flask-Bootstrap==3.3.7.1
Flask-Dance==3.0.0
Flask-DebugToolbar==0.11.0
Flask-Login==0.5.0
Flask-Migrate==2.5.2
Flask-OAuth==0.12
Flask-OAuthlib==0.9.5
Flask-SocketIO==4.2.1
Flask-SQLAlchemy==2.4.1
Flask-Testing==0.7.1
Flask-WTF==0.14.3
gevent==1.4.0
greenlet==0.4.15
gunicorn==20.0.4
httplib2==0.17.0
idna==2.9
importlib-metadata==1.5.0
itsdangerous==1.1.0
Jinja2==2.11.1
lazy==1.4
Mako==1.1.1
kombu==4.6.7
MarkupSafe==1.1.1
monotonic==1.5
oauth2==1.9.0.post1
oauthlib==3.1.0
pathtools==0.1.2
pbr==5.4.4
pika==1.1.0
python-dateutil==2.8.1
python-editor==1.0.4
python-engineio==3.11.2
python-socketio==4.4.0
pytz==2019.3
PyYAML==5.3
requests==2.23.0
requests-oauthlib==1.3.0
six==1.14.0
SQLAlchemy==1.3.13
SQLAlchemy-Utils==0.36.1
stevedore==1.32.0
supervisor==4.1.0
urllib3==1.25.8
URLObject==2.4.3
virtualenv==20.0.4
virtualenv-clone==0.5.3
virtualenvwrapper==4.8.4
vine==1.3.0
visitor==0.1.3
watchdog==0.10.2
Werkzeug==1.0.0
wincertstore==0.2
WTForms==2.2.1
zipp==3.0.0
......@@ -3,8 +3,9 @@
import os
import time
import signal
import tasks
import pika
from flask import session
from flask_socketio import SocketIO, join_room
......@@ -15,15 +16,14 @@ app = create_app(config_name)
app.config['SECRET_KEY'] = 'vnkdjnfjknfl1232#'
socketio = SocketIO(app)
global username_global
# global username_global
# global room_global
def create_account(username, fullname, reason):
# Todo: Ravi's and Louis's code goes here
print (time.strftime("%m-%d-%Y_%H:%M:%S") + '\tUser ' + username + ' added to queue')
global username_global
username_global = username
socketio.emit("creating account")
socketio.emit("creating account", room=room_global)
signal.signal(signal.SIGALRM, account_agent)
signal.alarm(5)
......@@ -31,17 +31,18 @@ def create_account(username, fullname, reason):
def account_agent(*args):
# Todo: Code to create a consumer based on the username goes here
# Todo: Goal is to have it listening for confirmation.
global username_global
username = username_global
username = session['username']
room = str(session['uid'])
print(time.strftime("%m-%d-%Y_%H:%M:%S") + '\tAccount successfully created for ' + username)
socketio.emit("account ready")
socketio.emit("account ready", room=room)
return True
@socketio.on('user connected')
def user_connected(json, methods=['GET', 'POST']):
username = json["user"]
username = json["user"]
room = str(session['uid'])
join_room(room)
......@@ -53,15 +54,18 @@ def user_connected(json, methods=['GET', 'POST']):
@socketio.on('request account')
def request_account(json, methods=['GET', 'POST']):
print (time.strftime("%m-%d-%Y_%H:%M:%S") + '\tQueue request received: ' + str(json))
print(json)
room = str(session['uid'])
socketio.emit("creating account", room=room)
try:
create_account(json['username'], json['fullname'], json['reason'])
# create_account(json['username'], json['fullname'], json['reason'])
tasks.celery_create_account(json['username'], json['fullname'], json['reason'], room, socketio)
except Exception as e:
print(time.strftime("%m-%d-%Y_%H:%M:%S") + "\tError in account creation: ", e)
socketio.emit("Account creation failed")
socketio.emit("Account creation failed", room)
if __name__ == '__main__':
# app.run()
socketio.run(app)
socketio.run(app, host='0.0.0.0')
from celery import Celery
import time
from flask_socketio import SocketIO
import subprocess
from gevent import monkey
monkey.patch_all(subprocess=True)
broker_url = 'amqp://reggie:reggie@ohpc:5672/'
#broker_url = 'amqp://'
celery = Celery('flask_user_reg', broker=broker_url)
# socketio = SocketIO(message_queue='amqp:///socketio')
@celery.task
def celery_create_account(username, fullname, reason, room, socketio):
# Todo: Ravi's and Louis's code goes here
print(time.strftime("%m-%d-%Y_%H:%M:%S") + '\tUser ' + username + ' added to queue')
socketio.emit("creating account", room=room)
print(username)
subprocess.call(["/opt/rabbitmq_agents/flask_producer.py", "ohpc_account_create", username])
time.sleep(5)
print(time.strftime("%m-%d-%Y_%H:%M:%S") + '\tAccount successfully created for ' + username)
socketio.emit('account ready', room=room)
import pika
import sys
# Begin RabbitMQ process.
credentials = pika.PlainCredentials('reggie', 'reggie')
parameters = pika.ConnectionParameters('ood',
5672,
'/',
credentials)
connection = pika.BlockingConnection(parameters)
channel = connection.channel()
channel.exchange_declare(exchange='direct_logs', exchange_type='direct')
node = sys.argv[1] if len(sys.argv) > 1 else 'info'
message = ' '.join(sys.argv[2:]) or 'Hello World!'
channel.basic_publish(
exchange='direct_logs', routing_key=node, body=message)
print(" [x] Sent %r:%r" % (node, message))
connection.close()
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment