|
| 1 | +from flask import Flask, render_template, request, redirect, url_for, flash, session |
| 2 | +import paramiko |
| 3 | +from flask_session import Session |
| 4 | +import os |
| 5 | +import logging |
| 6 | +from yaml_handler import YAMLHandler # Import the refactored YAML handler |
| 7 | +from shAndCrontab import SHAndCRONTABHandler |
| 8 | + |
| 9 | + |
| 10 | +from browse import browse_blueprint # Blueprint for browsing directories |
| 11 | +from configure import configure_blueprint # Blueprint for configuring directory pairs |
| 12 | +from logs import log_blueprint # Import the blueprint for managing log files |
| 13 | +from view_tasks import view_tasks_blueprint |
| 14 | +from server_management import server_management_blueprint |
| 15 | + |
| 16 | +from netbios import netbios_bp |
| 17 | +from smb_shares import smb_shares_bp |
| 18 | + |
| 19 | +app = Flask(__name__) |
| 20 | + |
| 21 | +app.config['ENV'] = os.environ.get("FLASK_ENV", "development") |
| 22 | + |
| 23 | +logging.basicConfig(filename='app.log', level=logging.DEBUG) |
| 24 | + |
| 25 | +app.secret_key = os.environ.get("SECRET_KEY", "default_secret_key") |
| 26 | +app.config['SESSION_TYPE'] = 'filesystem' |
| 27 | +Session(app) |
| 28 | + |
| 29 | +app.register_blueprint(browse_blueprint, url_prefix="/browse") |
| 30 | +app.register_blueprint(configure_blueprint, url_prefix="/configure") |
| 31 | +app.register_blueprint(log_blueprint, url_prefix="/logs") # Optional prefix for the blueprint |
| 32 | +app.register_blueprint(view_tasks_blueprint, url_prefix="/view_tasks") |
| 33 | +app.register_blueprint(server_management_blueprint, url_prefix="/server_management") |
| 34 | +# Register the NetBIOS blueprint |
| 35 | +app.register_blueprint(netbios_bp, url_prefix='/netbios') |
| 36 | +app.register_blueprint(smb_shares_bp) |
| 37 | + |
| 38 | +@app.route('/') |
| 39 | +def index(): |
| 40 | + |
| 41 | + flash(f"app Environment = {app.config['ENV']}",'info') |
| 42 | + |
| 43 | + if 'logged_in' not in session: |
| 44 | + return redirect(url_for('login')) |
| 45 | + |
| 46 | + hostname = os.uname().nodename |
| 47 | + |
| 48 | + server = session.get('server', '') |
| 49 | + # Create a YAMLHandler instance with remote handling |
| 50 | + yaml_handler = YAMLHandler(use_remote=True) |
| 51 | + # Load YAML data |
| 52 | + yaml_data = yaml_handler.get_or_create_default_settings() # Load YAML data |
| 53 | + |
| 54 | + log_files_dir = yaml_data[server].get('log_files_dir', None) |
| 55 | + # Get the directory pairs for the logged-in server |
| 56 | + if log_files_dir is None: |
| 57 | + log_files_dir = '/home/'+session['username']+'/rsyncwebapp' |
| 58 | + yaml_data[server]['log_files_dir'] = log_files_dir |
| 59 | + yaml_handler.save_yaml(yaml_data) |
| 60 | + |
| 61 | + pairs = yaml_data[server].get('pairs', []) |
| 62 | + if not pairs: |
| 63 | + pairs = [] |
| 64 | + yaml_data[server]['pairs'] = pairs |
| 65 | + yaml_handler.save_yaml(yaml_data) |
| 66 | + |
| 67 | + # get server information |
| 68 | + server_data = yaml_data.get(server, []) |
| 69 | + |
| 70 | + # get server information |
| 71 | + server_data = yaml_data.get(server, []) |
| 72 | + return render_template('index.html', hostname=hostname, server=server, logfiles=log_files_dir, server_data=pairs, enumerate=enumerate) # Pass 'enumerate' |
| 73 | + |
| 74 | +@app.route('/remove_pair/<int:pair_index>', methods=['GET']) |
| 75 | +def remove_pair(pair_index): |
| 76 | + if 'logged_in' not in session: |
| 77 | + return redirect(url_for('login')) |
| 78 | + |
| 79 | + server = session.get('server', '') |
| 80 | + # Create a YAMLHandler instance with remote handling |
| 81 | + yaml_handler = YAMLHandler(use_remote=True) |
| 82 | + # Load YAML data |
| 83 | + yaml_data = yaml_handler.load_yaml() |
| 84 | + |
| 85 | + if server in yaml_data: |
| 86 | + directory_pairs = yaml_data[server].get('pairs', []) |
| 87 | + if 0 <= pair_index < len(directory_pairs): |
| 88 | + #remove from crontab and shell script if they exist |
| 89 | + shAndCrontab = SHAndCRONTABHandler(use_remote=True) |
| 90 | + shAndCrontab.remove_from_crontab(directory_pairs[pair_index]) |
| 91 | + shAndCrontab.remove_from_script(directory_pairs[pair_index]) |
| 92 | + |
| 93 | + del directory_pairs[pair_index] # Remove the pair |
| 94 | + yaml_handler.save_yaml(yaml_data) # Persist changes |
| 95 | + flash("Pair removed successfully", "info") |
| 96 | + else: |
| 97 | + flash("Invalid pair index", "warning") |
| 98 | + else: |
| 99 | + flash("Server not found", "warning") |
| 100 | + |
| 101 | + return redirect(url_for('index')) |
| 102 | + |
| 103 | +@app.route('/login', methods=['GET', 'POST']) |
| 104 | +def login(): |
| 105 | + hostname = os.uname().nodename |
| 106 | + if request.method == 'POST': |
| 107 | + server = request.form.get('server', 'hp-debian').strip() |
| 108 | + username = request.form.get('username', 'ghassan').strip() |
| 109 | + password = request.form.get('password', '').strip() |
| 110 | + private_key_path = request.form.get('private_key_path', '').strip() |
| 111 | + passphrase = request.form.get('passphrase', '').strip() |
| 112 | + |
| 113 | + if not server or not username: |
| 114 | + flash("Server and username are required.", "warning") |
| 115 | + return render_template('login.html', hostname=hostname) |
| 116 | + |
| 117 | + ssh = paramiko.SSHClient() |
| 118 | + ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) |
| 119 | + |
| 120 | + try: |
| 121 | + if private_key_path: |
| 122 | + if not passphrase: |
| 123 | + flash("Passphrase is required for encrypted private keys.", 'warning') |
| 124 | + return render_template('login.html') |
| 125 | + |
| 126 | + pkey = paramiko.RSAKey.from_private_key_file(private_key_path, password=passphrase) |
| 127 | + ssh.connect(server, username=username, pkey=pkey) |
| 128 | + else: |
| 129 | + ssh.connect(server, username=username, password=password) |
| 130 | + |
| 131 | + # Create the app directory to store the app files |
| 132 | + stdin, stdout, stderr = ssh.exec_command(f"mkdir -p /home/{username}/rsyncwebapp") |
| 133 | + error = stderr.read().decode().strip() |
| 134 | + if error: |
| 135 | + flash(f"Directory creation failed: {error}", "danger") |
| 136 | + return render_template('login.html', hostname=hostname) |
| 137 | + |
| 138 | + # Store session information |
| 139 | + session['logged_in'] = True |
| 140 | + session['server'] = server |
| 141 | + session['username'] = username |
| 142 | + session['password'] = password |
| 143 | + session['private_key_path'] = private_key_path |
| 144 | + session['passphrase'] = passphrase |
| 145 | + |
| 146 | + # Assuming YAMLHandler class and yaml operations are defined elsewhere |
| 147 | + yaml_handler = YAMLHandler(use_remote=True) |
| 148 | + yaml_handler.ensure_yaml_file() |
| 149 | + |
| 150 | + yaml_data = yaml_handler.load_yaml() # Load YAML data |
| 151 | + |
| 152 | + return redirect(url_for('index')) |
| 153 | + |
| 154 | + except paramiko.AuthenticationException: |
| 155 | + flash("Invalid username or password.", "danger") |
| 156 | + return render_template('login.html', hostname=hostname) |
| 157 | + |
| 158 | + except paramiko.SSHException as e: |
| 159 | + flash(f"SSH connection failed: {e}", "danger") |
| 160 | + return render_template('login.html', hostname=hostname) |
| 161 | + |
| 162 | + except Exception as e: |
| 163 | + flash(f"Login failed: {e}", "danger") |
| 164 | + return render_template('login.html', hostname=hostname) |
| 165 | + |
| 166 | + return render_template('login.html', hostname=hostname) |
| 167 | + |
| 168 | +@app.route('/logout') |
| 169 | +def logout(): |
| 170 | + session.clear() # Clear session data |
| 171 | + return redirect(url_for('login')) |
| 172 | + |
| 173 | +if __name__ == '__main__': |
| 174 | + logging.info("app config: %s\n ", app.config) |
| 175 | + |
| 176 | + app.run(debug=True, host='0.0.0.0', port=5003) |
0 commit comments