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 @@
from __future__ import print_function
import os
import os.path
import sys
import time
import datetime
import subprocess
import time
# third-party imports
from flask import Flask, redirect, url_for, request, render_template, flash
from flask_wtf import FlaskForm
from flask_bootstrap import Bootstrap
from wtforms import StringField, SubmitField, validators
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
from watchdog.utils import dirsnapshot
def create_app(config_name):
app = Flask(__name__)
Bootstrap(app)
global snap_before
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
try:
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(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
return_url = ''
@app.route('/', methods=['GET', 'POST'])
def index():
class MainForm(FlaskForm): # class for the form itself
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"
if name != "":
return redirect(url_for('success', username=str(user), fullname=name))
fullname = False
form = MainForm() # initialize form object
if form.is_submitted():
fullname = form.fullname.data
form.fullname.data = '' # reset form data upon capture
return redirect(url_for('success', username=str(username), fullname=fullname))
else:
return render_template("auth/SignUp.html", user=user)
return render_template('auth/SignUp.html', form=form, user=username)
@app.route('/success/<username>/<fullname>')
def success(username, fullname):
global return_url
global snap_before
global observing
global time_stamp
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 + '"'
print(tempString, file=sys.stdout)
try:
# 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)
time_stamp = time.strftime("%m-%d-%Y_%H:%M:%S")
directory = "/var/www/ood/register/flask_user_reg/app/flat_db"
complete_file_name = os.path.join(directory, time_stamp + ".txt")
directory = "/home/reggie/flat_db/"
complete_file_name = os.path.join(directory, time_stamp + "_" + request.remote_user + ".txt")
if not os.path.exists(directory):
os.makedirs(directory)
file = open(complete_file_name, "w")
file.write(fullname)
event_handler = MyHandler() # initialize handler
observer = Observer() # initialize obsever to relay to handler
observer.schedule(event_handler, path='/home/reggie/flat_db', recursive=True)
observer.start()
observing = True
file = open(complete_file_name, "w") # create time stamped file to be queued
file.write("Hey")
# 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
time.sleep(5)
observer.stop()
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:
print(e)
flash("Registration Failed. Please try again.") # show error message upon failure
return redirect(url_for('index'))
except:
flash("Registration Failed")
return redirect(return_url)
# misc page error catching
@app.errorhandler(403)
def forbidden(error):
......
argh==0.26.2
Click==7.0
dominate==2.3.5
Flask==1.0.2
......@@ -6,18 +7,21 @@ Flask-Login==0.4.1
Flask-Testing==0.7.1
Flask-WTF==0.14.2
itsdangerous==1.1.0
Jinja2>=2.10.1
Jinja2==2.10.1
Mako==1.0.7
MarkupSafe==1.1.1
pathtools==0.1.2
pbr==5.1.3
python-dateutil==1.5
python-editor==1.0.4
pytz==2013.7
PyYAML==5.1.2
six==1.12.0
stevedore==1.30.1
virtualenv==16.4.3
virtualenv-clone==0.5.1
virtualenvwrapper==4.8.4
visitor==0.1.3
watchdog==0.9.0
Werkzeug==0.14.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