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