forked from bqckup/bqckup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
291 lines (232 loc) · 9.33 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
from modules.auth import auth
from modules.backup import backup
from classes.server import Server
from classes.s3 import s3
from helpers import today24Format, timeSince, bytes_to
from constant import BQ_PATH, STORAGE_CONFIG_PATH, SITE_CONFIG_PATH, VERSION, CONFIG_PATH
import sys, os, ruamel.yaml as rYaml
from datetime import timedelta
from flask.json import jsonify
from flask import Flask, render_template, request, redirect, url_for
from werkzeug.utils import secure_filename
sys.path.insert(0, os.path.abspath(
os.path.join(os.path.dirname(__file__), "..")))
# SECRET_KEY=os.urandom(24)
SECRET_KEY="secret_key"
app = Flask(__name__)
app.permanent_session_lifetime = timedelta(minutes=30)
app.secret_key = SECRET_KEY
# app.cache_type = "SimpleCache"
# app.cache_default_timeout = 300
app.register_blueprint(auth, url_prefix="/auth/")
app.register_blueprint(backup, url_prefix="/backup/")
# cache = Cache(app)
# assign global variable
# ref : https://stackoverflow.com/a/43336023
@app.context_processor
def globalVariable():
currentUrl = request.url
currentUrlSplit = request.url.split("/")
currentTime = today24Format()
currentVersion = VERSION
return dict(
currentUrl=currentUrl,
currentUrlSplit=currentUrlSplit,
currentTime=currentTime,
currentVersion=currentVersion,
)
@app.errorhandler(500)
def page_internal_error(e):
return render_template("500.html"), 500
@app.errorhandler(401)
def page_unauthorized(e):
return redirect(url_for('auth.login'))
@app.errorhandler(404)
def page_not_found(e):
return render_template("404.html"), 404
@app.before_request
def before_request():
from classes.auth import Auth
allowed_path = ['login', 'static', 'setup']
if not Auth.is_authorized() and (request.path in allowed_path and '/' != request.path):
return jsonify(message="Access blocked"), 401
@app.get('/setup')
def setup():
if os.path.exists(STORAGE_CONFIG_PATH):
return redirect(url_for('index'))
return render_template('wizard.html')
"""
Create key
Validate the config is it success connected to s3
"""
@app.post('/setup/save')
def save_setup():
# Write security key
try:
from classes.config import Config
from classes.file import File
cfg = Config()
cfg.config_parser['web']['port'] = cfg.read('web', 'port')
cfg.config_parser['auth']['password'] = request.form.get('key')
cfg.config_parser['bqckup']['config_backup'] = cfg.read('bqckup', 'config_backup')
with open(CONFIG_PATH, 'w') as configfile:
cfg.config_parser.write(configfile)
# Storage config
if request.form.get('skip'):
File().create_file(STORAGE_CONFIG_PATH, '')
return jsonify(message=f"Success"), 200
if len(request.files.getlist('config_storage')) > 0:
for cs in request.files.getlist('config_storage'):
if not cs.filename.endswith('.yml'):
return jsonify(message="Config storage must be .yml file"), 400
cs.save(STORAGE_CONFIG_PATH)
if len(request.files.getlist('config_storage')) <= 0:
with open(STORAGE_CONFIG_PATH, 'w+') as f:
config_content = {
"storages": {
request.form.get('name'): {
"bucket": request.form.get('bucket'),
"access_key_id": request.form.get('client_id'),
"secret_access_key": request.form.get('client_secret'),
"region": request.form.get('region'),
"endpoint": request.form.get('endpoint_url'),
"primary": "yes"
}
}
}
yaml = rYaml.YAML()
yaml.indent(sequence=4, offset=2)
yaml.dump(config_content, f)
# Test connection
try:
_s3 = s3(storage_name=request.form.get('name'))
_s3.list()
except Exception as e:
print(e)
return jsonify(message="Failed to connect to your s3 account"), 500
# Backup config
if len(request.files.getlist('config_bqckup')) > 0:
for cb in request.files.getlist('config_bqckup'):
if not cb.filename.endswith('.yml'):
return jsonify(message="Backup config must be .yml file"), 400
cb.save(os.path.join(SITE_CONFIG_PATH, secure_filename(cb.filename)))
return jsonify(message=f"Success"), 200
except Exception as e:
return jsonify(message=f"Failed to save config, {str(e)}"), 500
@app.get("/do_update")
def do_update():
pass
@app.get("/")
@app.get("/index")
# @cache.cached(timeout=60)
def index():
from classes.auth import Auth
from classes.bqckup import Bqckup
from classes.storage import Storage
if not Auth.is_authorized():
return redirect(url_for('auth.login'))
_server_storage = Server().get_storage_information()
server_storage = {
"used": bytes_to('g', _server_storage.used),
"free": bytes_to('g', _server_storage.free),
"total": bytes_to('g', _server_storage.total),
}
try:
cloud_storage = Storage().parsed_storage
except Exception as e:
print(f"Failed to connect to cloud storage, {str(e)}")
cloud_storage = False
cloud_storage_used = 0
if cloud_storage:
cloud_storage_used = s3(Storage().get_primary_storage()).get_total_used()
return render_template(
"index.html",
server_storage=server_storage,
cloud_storage_used=cloud_storage_used,
bqckups=Bqckup().list(),
cloud_storage=cloud_storage,
)
# jinja
@app.template_filter("tSince")
def tSince(s):
if not s:
return "-"
return timeSince(s)
@app.template_filter("humanReadableSize")
def humanReadableSize(s):
if not isinstance(s, int):
return "-"
from hurry.filesize import size, alternative
return size(s, system=alternative)
@app.template_filter('get_base_name')
def get_base_name(path):
return os.path.basename(path)
@app.template_filter('time_since')
def time_since(unix):
from helpers import time_since
return time_since(unix)
def initialization():
from models import database
from models.log import Log
from models.notification_log import NotificationLog
db_path = os.path.join(BQ_PATH, 'database', 'bqckup.db')
if not os.path.exists(db_path):
for folder in ['config', 'database', 'sites']:
if not os.path.exists(os.path.join(BQ_PATH, folder)):
os.makedirs(os.path.join(BQ_PATH, folder))
os.system(f"touch {db_path}")
os.system(f"chmod 755 {db_path}")
database.connect()
if not database.table_exists('log'):
database.create_tables([Log])
if not database.table_exists('notification_logs'):
database.create_tables([NotificationLog])
database.close()
dummy_storge_config = STORAGE_CONFIG_PATH.replace('.yml', '.yml.example')
dummy_site_config = os.path.join(SITE_CONFIG_PATH, 'domain.yml.example')
if not os.path.exists(dummy_site_config):
with open(dummy_site_config, 'w+') as stream:
_yaml = rYaml.YAML()
_yaml.indent(sequence=4, offset=2)
_yaml.dump({
"bqckup": {
"name": "domain",
"path": ['/var/www/html'],
"database": {
"type": "mysql",
"host": "localhost",
"port": 3306,
"user": "root",
"password": "root",
"name": "database"
},
"options": {
"storage": "dummy",
"interval": "daily",
"retention": "7",
"save_locally": "no",
"save_locally_path": os.path.join(BQ_PATH, 'tmp'),
"notification_email": "[email protected]",
"provider": "s3"
}
}
}, stream)
if not os.path.exists(dummy_storge_config):
with open(dummy_storge_config, 'w+') as stream:
_yaml = rYaml.YAML()
_yaml.indent(sequence=4, offset=2)
_yaml.dump({
"storages": {
"dummy": {
"bucket": "dummy",
"access_key_id": "dummy",
"secret_access_key": "dummy",
"region": "dummy",
"endpoint": "dummy",
"primary": "no"
}
}
}, stream)
if __name__ == "__main__":
initialization()
app.run(host="0.0.0.0", debug=True, port=9393)