migration à mysql

This commit is contained in:
guimoz 2017-01-31 14:17:50 +01:00
parent e7383cfc0e
commit 9e40c297b4
2 changed files with 40 additions and 78 deletions

View file

@ -1,14 +1,16 @@
import sqlite3 import MySQLdb
from users.models import User from users.models import User
from machines.models import Machine,Interface from machines.models import Machine,Interface
SQLITE_FILENAME = '/var/www/re2o/players.db'
def connect_sql():
return MySQLdb.connect(host="roulette.rez", # your host, usually localhost
user="hydra-user", # your username
passwd="q5kBLchS6ooAZDSxMeJG2gdf4gmJcfS5", # your password
db="roulette") # name of the data base
def connect_sqlite():
return sqlite3.connect(SQLITE_FILENAME)
# Connexion à la base SQLite locale # Connexion à la base SQLite locale
con = connect_sqlite() con = connect_sql()
cur = con.cursor() cur = con.cursor()
# cur.execute('''create table players (id,prenom,nom, etat)''') # cur.execute('''create table players (id,prenom,nom, etat)''')
# cur.execute('''create table machines (id,uid_user,ip)''') # cur.execute('''create table machines (id,uid_user,ip)''')
@ -24,9 +26,10 @@ for user in User.objects.filter(school=1):
if user.has_access() and (user.is_adherent() or user.end_adhesion()): if user.has_access() and (user.is_adherent() or user.end_adhesion()):
if user.uid_number not in players: if user.uid_number not in players:
cur.execute("""insert into players values (?,?,?,?)""",(user.uid_number, user.name, user.surname, 0)) cur.execute("""insert into players values (?,?,?,?)""",(user.uid_number, user.name, user.surname, 0))
print(user.surname+' '+user.name)
for m in Machine.objects.filter(user= user): for m in Machine.objects.filter(user= user):
for i in Interface.objects.filter(machine = m): for i in Interface.objects.filter(machine = m):
if i.ipv4.ipv4 not in machines: if i.ipv4.ipv4 not in machines:
cur.execute("""insert into machines values (?,?,?) """,(i.id, user.uid_number, i.ipv4.ipv4)) cur.execute("""insert into machines values (?,?,?) """,(i.id, user.uid_number, i.ipv4.ipv4))
con.commit()
con.close() con.close()

View file

@ -21,11 +21,6 @@ ASSHOLES = []
SQLITE_FILENAME = '/var/roulette/players.db' SQLITE_FILENAME = '/var/roulette/players.db'
SQLITE_SCHEMA = 'schema.sql' SQLITE_SCHEMA = 'schema.sql'
MYSQL_HOST = 'mysql.rez'
MYSQL_USER = 'rezo_admin_ro'
MYSQL_PASSWORD = 'rezopaspipo'
MYSQL_DB = 'rezo_admin'
BAN_DURATION = 30. * 60. BAN_DURATION = 30. * 60.
IMMUNITY_FILE = '/var/roulette/immunity' IMMUNITY_FILE = '/var/roulette/immunity'
@ -46,51 +41,15 @@ locale.setlocale(locale.LC_ALL, 'fr_FR.utf8')
def connect_sqlite(): def connect_sqlite():
return sqlite3.connect(SQLITE_FILENAME) return sqlite3.connect(SQLITE_FILENAME)
def connect_sql():
return MySQLdb.connect(host="localhost", # your host, usually localhost
user="root", # your username
passwd="F5XUaZmkWaWp3GDnT73WuEGZqAN5hMF3", # your password
db="roulette") # name of the data base
def init_db(): def init_db():
# Initialisation de la base SQLite # Initialisation de la base SQLite
with closing(connect_sqlite()) as con_sqlite: pass
with app.open_resource('schema.sql') as f:
con_sqlite.cursor().executescript(f.read().decode("utf-8"))
con_sqlite.commit()
# Connexion à la base SQLite locale
con_sqlite = connect_sqlite()
cur_sqlite = con_sqlite.cursor()
# Connexion à la base MySQL sur babel
con_mysql = mdb.connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DB, \
charset='utf8', use_unicode=True)
cur_mysql = con_mysql.cursor(mdb.cursors.DictCursor)
# Remplissage de la table players à partir de la table utilisateurs
cur_mysql.execute("""select id,prenom,nom from utilisateurs
where etat='STATE_ACTIVE' and ecole_id=1 and id<>1
and typeUtilisateur='membre'""")
rows = cur_mysql.fetchall()
print('players :')
for row in rows:
if row['prenom'] + ' ' + row['nom'] not in IMMUNITY:
print(row)
cur_sqlite.execute("""insert into players values (?,?,?,?)""", \
((row["id"]), row["prenom"], row["nom"], 0))
# Remplissage de la table ip à partir de la table equipements
cur_mysql.execute("""select equipements.id,utilisateurs.id,equipements.ip
from utilisateurs
inner join equipements on utilisateurs.id=equipements.utilisateur_id
where utilisateurs.ecole_id=1 and utilisateurs.id<>1
and utilisateurs.etat='STATE_ACTIVE' and equipements.etat='STATE_ACTIVE'
and utilisateurs.typeUtilisateur='membre'""")
rows = cur_mysql.fetchall()
print('machines :')
for row in rows:
print(row)
cur_sqlite.execute("""insert into machines values (?,?,?)""", \
(row["id"], row["utilisateurs.id"], row["ip"]))
con_sqlite.commit()
cur_sqlite.close()
cur_mysql.close()
def duration_format(seconds): def duration_format(seconds):
hours = seconds // 3600 hours = seconds // 3600
@ -110,8 +69,8 @@ def get_ip():
return request.remote_addr return request.remote_addr
def get_player(player_id): def get_player(player_id):
con = connect_sqlite() con = connect_sql()
cur = con.cursor() cur = db.cursor()
cur.execute("""select id,firstname,name,ban_end from players cur.execute("""select id,firstname,name,ban_end from players
where id=(?)""", [player_id]) where id=(?)""", [player_id])
@ -122,8 +81,8 @@ def get_player(player_id):
return {'id': row[0], 'firstname': row[1], 'name': row[2], 'ban_end': row[3]} return {'id': row[0], 'firstname': row[1], 'name': row[2], 'ban_end': row[3]}
def get_player_from_ip(ip): def get_player_from_ip(ip):
con = connect_sqlite() con = connect_sql()
cur = con.cursor() cur = db.cursor()
cur.execute("""select players.id,players.firstname,players.name, cur.execute("""select players.id,players.firstname,players.name,
machines.id,machines.ip,players.ban_end machines.id,machines.ip,players.ban_end
@ -143,8 +102,8 @@ def get_player_from_ip(ip):
return user return user
def get_player_from_full_name(firstname, name): def get_player_from_full_name(firstname, name):
con = connect_sqlite() con = connect_sql()
cur = con.cursor() cur = db.cursor()
cur.execute("""select players.id,players.firstname,players.name, cur.execute("""select players.id,players.firstname,players.name,
machines.id,machines.ip,players.ban_end machines.id,machines.ip,players.ban_end
@ -163,8 +122,8 @@ def get_player_from_full_name(firstname, name):
return user return user
def is_banned(user_id): def is_banned(user_id):
con = connect_sqlite() con = connect_sql()
cur = con.cursor() cur = db.cursor()
cur.execute("""select ban_end from players where id=(?)""", [user_id]) cur.execute("""select ban_end from players where id=(?)""", [user_id])
@ -190,8 +149,8 @@ def playable_required(f):
return decorated_function return decorated_function
def get_players_not_banned(): def get_players_not_banned():
con = connect_sqlite() con = connect_sql()
cur = con.cursor() cur = db.cursor()
cur.execute("""select id,firstname,name from players cur.execute("""select id,firstname,name from players
where (?) > ban_end """, [time()]) where (?) > ban_end """, [time()])
@ -238,16 +197,16 @@ def ban(player_id, target_id, success):
banned_player = success and target or player banned_player = success and target or player
con = connect_sqlite() con = connect_sql()
cur = con.cursor() cur = db.cursor()
cur.execute("""select id,ban_end from players cur.execute("""select id,ban_end from players
where id=(?)""", [banned_player['id']]) where id=(?)""", [banned_player['id']])
con.commit() con.commit()
con.close() con.close()
con = connect_sqlite() con = connect_sql()
cur = con.cursor() cur = db.cursor()
ban_end = cur.fetchone()[0] ban_end = cur.fetchone()[0]
ban_end = time() + BAN_DURATION ban_end = time() + BAN_DURATION
@ -258,8 +217,8 @@ def ban(player_id, target_id, success):
con.commit() con.commit()
con.close() con.close()
con = connect_sqlite() con = connect_sql()
cur = con.cursor() cur = db.cursor()
cur.execute("""insert into bans (player_id,target_id,success,time) cur.execute("""insert into bans (player_id,target_id,success,time)
values (?,?,?,?)""", [player['id'], target['id'], \ values (?,?,?,?)""", [player['id'], target['id'], \
success and 1 or 0, time()]) success and 1 or 0, time()])
@ -268,8 +227,8 @@ def ban(player_id, target_id, success):
def unban(player_id): def unban(player_id):
con = connect_sqlite() con = connect_sql()
cur = con.cursor() cur = db.cursor()
cur.execute("""update players set ban_end=(?) cur.execute("""update players set ban_end=(?)
where id=(?)""", [time() - BAN_DURATION, player_id]) where id=(?)""", [time() - BAN_DURATION, player_id])
@ -278,8 +237,8 @@ def unban(player_id):
con.close() con.close()
def get_bans(player_id): def get_bans(player_id):
con = connect_sqlite() con = connect_sql()
cur = con.cursor() cur = db.cursor()
# Bannissements concernant le joueur : # Bannissements concernant le joueur :
cur.execute("""select player_id,target_id,success,time from bans cur.execute("""select player_id,target_id,success,time from bans
@ -313,8 +272,8 @@ def banned():
def statistiques(): def statistiques():
#Calcul des statistiques #Calcul des statistiques
con = connect_sqlite() con = connect_sql()
cur = con.cursor() cur = db.cursor()
cur.execute("""select firstname,name,ban_end from players""") cur.execute("""select firstname,name,ban_end from players""")
@ -348,8 +307,8 @@ def banned_ip():
if get_ip() not in ['10.7.0.254']: if get_ip() not in ['10.7.0.254']:
abort(403) abort(403)
con = connect_sqlite() con = connect_sql()
cur = con.cursor() cur = db.cursor()
cur.execute("""select machines.ip from players cur.execute("""select machines.ip from players
inner join machines on players.id=machines.player_id inner join machines on players.id=machines.player_id