1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-04-28 19:25:03 +00:00
seahub/scripts/migrate_ldapusers.py

55 lines
1.6 KiB
Python
Raw Normal View History

2023-07-05 08:54:21 +00:00
# -*- coding: utf-8 -*-
import os
import configparser
import pymysql
2023-07-07 01:39:38 +00:00
pymysql.install_as_MySQLdb()
2023-07-05 08:54:21 +00:00
2023-07-07 01:39:38 +00:00
install_path = os.path.dirname(os.path.abspath(__file__))
2023-07-05 08:54:21 +00:00
top_dir = os.path.dirname(install_path)
ccnet_conf = os.path.join(top_dir, 'conf', 'ccnet.conf')
2023-07-07 01:39:38 +00:00
sql = "INSERT IGNORE INTO EmailUser (email, passwd, is_staff, is_active, ctime) SELECT email, '!', is_staff, is_active, REPLACE(UNIX_TIMESTAMP(CURRENT_TIMESTAMP(6)),'.','') FROM LDAPUsers"
2023-07-05 08:54:21 +00:00
def migrate_ldapusers():
print('Start migrate LDAPUsers')
config = configparser.ConfigParser()
try:
config.read(ccnet_conf)
db_user = config.get('Database', 'USER')
db_host = config.get('Database', 'HOST')
2023-07-07 01:39:38 +00:00
db_port = config.getint('Database', 'PORT')
2023-07-05 08:54:21 +00:00
db_password = config.get('Database', 'PASSWD')
db_name = config.get('Database', 'DB')
except Exception as e:
print("Failed to read ccnet config file %s: %s" % (ccnet_conf, e))
return
try:
conn = pymysql.connect(user=db_user, host=db_host, port=db_port, password=db_password, database=db_name)
2023-07-07 01:39:38 +00:00
conn.autocommit(True)
2023-07-05 08:54:21 +00:00
cursor = conn.cursor()
except Exception as e:
print('Failed to connect to mysql database: %s' % e)
return
try:
cursor.execute(sql)
2023-07-07 01:39:38 +00:00
print('Migrated %s records' % cursor.rowcount)
2023-07-05 08:54:21 +00:00
print('Finish migrate LDAPUsers.')
except Exception as e:
print('Failed to exec sql: %s' % e)
return
finally:
if cursor:
cursor.close()
if conn:
conn.close()
if __name__ == '__main__':
migrate_ldapusers()