Commit 1371d75c authored by Mitchell Moore's avatar Mitchell Moore
Browse files

Add watchdog to check for file changes in flatdb. Add username to the file...

Add watchdog to check for file changes in flatdb. Add username to the file being written in flatdb. Update requirements

- Include watchdog integration to monitor dir changes
- Update requirements and comment prints
- File write and logic fix. Responds to file change
- Properly initiate while loop, include debug info
- Finalize change loop
- Fix variable confusion in index route
- Fix variables
- Update documentation
- Fix comment
- Add username to file name
parent 267fde3a
...@@ -4,67 +4,143 @@ ...@@ -4,67 +4,143 @@
from __future__ import print_function from __future__ import print_function
import os import os
import os.path
import sys import sys
import time
import datetime
import subprocess import subprocess
import time
# third-party imports # third-party imports
from flask import Flask, redirect, url_for, request, render_template, flash from flask import Flask, redirect, url_for, request, render_template, flash
from flask_wtf import FlaskForm
from flask_bootstrap import Bootstrap from flask_bootstrap import Bootstrap
from wtforms import StringField, SubmitField, validators
from watchdog.observers import Observer
from import FileSystemEventHandler
from watchdog.utils import dirsnapshot
def create_app(config_name):
app = Flask(__name__) global snap_before
Bootstrap(app) global snap_after
global snap_diff
global observing
global time_stamp
observing = False
class MyHandler(FileSystemEventHandler): # Watchdog handler class to take action when observation requested
def on_modified(self, event):
global snap_before
global snap_after
global snap_diff
global observing
global time_stamp
# print(event.src_path + " modified.")
snap_after = dirsnapshot.DirectorySnapshot("/home/reggie/flat_db", True) # take post flat_db creation snapshot of the directory
snap_diff = dirsnapshot.DirectorySnapshotDiff(snap_before, snap_after) # object to compare the initial snapshot with the final snapshot
if ("/home/reggie/flat_db/" + time_stamp + ".done") in snap_diff.files_moved[0]: # check for timestamped string with .done extention in flat_db
observing = False
# print("YES!")
except Exception as e:
# print("Created: ", snap_diff.files_created)
# print("Deleted: ", snap_diff.files_deleted)
# print("Modified: ", snap_diff.files_modified)
# print("Moved: ", snap_diff.files_moved)
def on_created(self, event):
print(event.src_path + " created.")
def create_app(config_name):
app = Flask(__name__) # initialization of the flask app
Bootstrap(app) # allowing app to use bootstrap
global return_url global return_url
return_url = '' return_url = ''
@app.route('/', methods=['GET', 'POST']) class MainForm(FlaskForm): # class for the form itself
def index(): fullname = StringField('Full Name: ', [validators.DataRequired(), ])
submit = SubmitField('Submit')
user = request.remote_user @app.route('/', methods=['GET', 'POST']) # initial route to display the reg page
def index():
global return_url
username = request.remote_user
if "redir" in request.args and return_url == "": if "redir" in request.args and return_url == "": # check for redir arg in url
return_url = request.args.get("redir") or "/pun/sys/dashboard" return_url = request.args.get("redir") or "/pun/sys/dashboard"
if name != "": if name != "":
return redirect(url_for('success', username=str(user), fullname=name)) fullname = False
form = MainForm() # initialize form object
if form.is_submitted():
fullname = = '' # reset form data upon capture
return redirect(url_for('success', username=str(username), fullname=fullname))
else: return render_template('auth/SignUp.html', form=form, user=username)
return render_template("auth/SignUp.html", user=user)
@app.route('/success/<username>/<fullname>') @app.route('/success/<username>/<fullname>')
def success(username, fullname): def success(username, fullname):
global return_url global return_url
global snap_before
global observing
global time_stamp
print(username, fullname, return_url, file=sys.stdout) print(username, fullname, return_url, file=sys.stdout)
# Deliver arguments to script. # Deliver arguments to script. (for local vagrant implementation)
tempString = 'ssh ohpc "sudo /opt/ohpc_user_create/user_create ' + username + " " + fullname + '"' tempString = 'ssh ohpc "sudo /opt/ohpc_user_create/user_create ' + username + " " + fullname + '"'
print(tempString, file=sys.stdout) print(tempString, file=sys.stdout)
try: try:
# function to write out a flatdb with the name of the file being a timestamp and the content # function to write out a flatdb with the name of the file being a timestamp and the content
# of the file beieng blazerID the user submitted from the flask form (fullname) # of the file beieng blazerID the user submitted from the flask form (fullname)
time_stamp = time.strftime("%m-%d-%Y_%H:%M:%S") time_stamp = time.strftime("%m-%d-%Y_%H:%M:%S")
directory = "/var/www/ood/register/flask_user_reg/app/flat_db" directory = "/home/reggie/flat_db/"
complete_file_name = os.path.join(directory, time_stamp + ".txt") complete_file_name = os.path.join(directory, time_stamp + "_" + request.remote_user + ".txt")
if not os.path.exists(directory): if not os.path.exists(directory):
os.makedirs(directory) os.makedirs(directory)
file = open(complete_file_name, "w") event_handler = MyHandler() # initialize handler
file.write(fullname) observer = Observer() # initialize obsever to relay to handler
observer.schedule(event_handler, path='/home/reggie/flat_db', recursive=True)
observing = True
file = open(complete_file_name, "w") # create time stamped file to be queued
# take an initial state snapshot of the db after file queued
snap_before = dirsnapshot.DirectorySnapshot("/home/reggie/flat_db", True)
while observing:
# TODO: Update page ui element dynamically
file.close() file.close()
return redirect(return_url, 302) return render_template("errors/registration_failed.html") # Todo: replace template with redirect
# return redirect(return_url, 302)
except Exception as e:
flash("Registration Failed. Please try again.") # show error message upon failure
return redirect(url_for('index'))
except: # misc page error catching
flash("Registration Failed")
return redirect(return_url)
@app.errorhandler(403) @app.errorhandler(403)
def forbidden(error): def forbidden(error):
Click==7.0 Click==7.0
dominate==2.3.5 dominate==2.3.5
Flask==1.0.2 Flask==1.0.2
...@@ -6,18 +7,21 @@ Flask-Login==0.4.1 ...@@ -6,18 +7,21 @@ Flask-Login==0.4.1
Flask-Testing==0.7.1 Flask-Testing==0.7.1
Flask-WTF==0.14.2 Flask-WTF==0.14.2
itsdangerous==1.1.0 itsdangerous==1.1.0
Jinja2>=2.10.1 Jinja2==2.10.1
Mako==1.0.7 Mako==1.0.7
MarkupSafe==1.1.1 MarkupSafe==1.1.1
pbr==5.1.3 pbr==5.1.3
python-dateutil==1.5 python-dateutil==1.5
python-editor==1.0.4 python-editor==1.0.4
pytz==2013.7 pytz==2013.7
six==1.12.0 six==1.12.0
stevedore==1.30.1 stevedore==1.30.1
virtualenv==16.4.3 virtualenv==16.4.3
virtualenv-clone==0.5.1 virtualenv-clone==0.5.1
virtualenvwrapper==4.8.4 virtualenvwrapper==4.8.4
visitor==0.1.3 visitor==0.1.3
Werkzeug==0.14.1 Werkzeug==0.14.1
WTForms==2.2.1 WTForms==2.2.1
Markdown is supported
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