-
Notifications
You must be signed in to change notification settings - Fork 0
/
d81s_maintenance.py
executable file
·121 lines (98 loc) · 4.71 KB
/
d81s_maintenance.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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# ------------------------------------------------------------------------------
# Deck 8 One Step - Maintenance utility
# ------------------------------------------------------------------------------
import configparser
import mysql.connector
from mysql.connector import errorcode
import time
# ------------------------------------------------------------------------------
# Connect with the MySql Database
# ------------------------------------------------------------------------------
def connect_db(host, database, user, password):
try:
cnx = mysql.connector.connect(host=host, database=database,
user=user, password=password)
except mysql.connector.Error as err:
if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
print(time.ctime(time.time()) + ":",
"Fatal: Wrong DB user/password specified in the INI-file")
elif err.errno == errorcode.ER_BAD_DB_ERROR:
print(time.ctime(time.time()) + ":",
"Fatal: Database does not exists")
else:
print(err)
return cnx
# ------------------------------------------------------------------------------
# Read configuration parameters from the INI-file
# Gets the 'ini-filename' as the parameter, returns the 'config' object
# ------------------------------------------------------------------------------
def read_ini(inifile):
config = configparser.ConfigParser()
if inifile not in config.read(inifile):
print(time.ctime(time.time()) + ":", "Fatal: Unable to find INI-file")
exit(1)
# Read the [MAINTENANCE] section
if 'MAINTENANCE' in config.sections():
if not config['MAINTENANCE']['sessions_storage_depth']:
print(time.ctime(time.time()) + ":",
"Fatal: Missing [MAINTENANCE][essions_storage_depth] variable of the INI-file")
exit(1)
else:
print(time.ctime(time.time()) + ":",
"Fatal: [MAINTENANCE] section is NOT present in the INI-file")
exit(1)
# Fetch variables of the [DATABASE] section or die
if 'DATABASE' in config.sections():
if not config['DATABASE']['host']:
print(time.ctime(time.time()) + ":",
"Fatal: Missing [DATABASE][host] variable of the INI-file")
exit(1)
if not config['DATABASE']['database']:
print(time.ctime(time.time()) + ":",
"Fatal: Missing [DATABASE][database] variable of the INI-file")
exit(1)
if not config['DATABASE']['user']:
print(time.ctime(time.time()) + ":",
"Fatal: Missing [DATABASE][user] variable of the INI-file")
exit(1)
if not config['DATABASE']['password']:
print(time.ctime(time.time()) + ":",
"Fatal: Missing [DATABASE][password] variable of the INI-file")
exit(1)
else:
print(time.ctime(time.time()) + ":",
"Fatal: [DATABASE] section is NOT present in the INI-file")
exit(1)
return config
# ------------------------------------------------------------------------------
# Connect to the database and delete old records based on the
# "sessions_storage_depth" value from the INI-file
# ------------------------------------------------------------------------------
def delete_old_sessions(sessions_storage_depth):
cnx = connect_db(config['DATABASE']['host'], config['DATABASE']['database'],
config['DATABASE']['user'], config['DATABASE']['password'])
cursor = cnx.cursor()
delete_sessions = ("DELETE FROM `d81s`.`sessions` "
"WHERE `session_id` < "
"(SELECT min(s1.session_id) FROM "
"(SELECT session_id FROM d81s.sessions "
"ORDER BY session_id DESC LIMIT " + sessions_storage_depth + " ) "
"as s1)")
cursor.execute(delete_sessions, "")
cnx.commit()
cursor.close()
cnx.close()
# ------------------------------------------------------------------------------
# Deck 8 One Step - Maintenance utility - main()
# ------------------------------------------------------------------------------
print(time.ctime(time.time()) + ":", "Starting Maintenance")
# Read INI-file
print(time.ctime(time.time()) + ":", "Read INI-file")
config = read_ini('d81s.ini')
print(time.ctime(time.time()) + ":", "Deleting old sessions: Start")
delete_old_sessions(config['MAINTENANCE']['sessions_storage_depth'])
print(time.ctime(time.time()) + ":", "Deleting old sessions: End")
print(time.ctime(time.time()) + ":", "Rest Avatar, you need it")
exit(0)