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)
2024-11-11 12:05:26 +00:00
seafile_conf = os.path.join(top_dir, 'conf', 'seafile.conf')
2023-07-05 08:54:21 +00:00
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:
2024-11-11 12:05:26 +00:00
config.read(seafile_conf)
db_user = config.get('database', 'user')
db_host = config.get('database', 'host')
db_port = config.getint('database', 'port')
db_password = config.get('database', 'password')
db_name = os.environ.get('SEAFILE_MYSQL_DB_CCNET_DB_NAME', '') or 'ccnet_db'
2023-07-05 08:54:21 +00:00
except Exception as e:
2024-11-11 12:05:26 +00:00
print("Failed to read seafile config file %s: %s" % (seafile_conf, e))
2023-07-05 08:54:21 +00:00
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()