uncia/uncia/migrate.py

89 lines
1.6 KiB
Python
Executable file

#!/usr/bin/env python3
import time, sys, ujson as json
from os.path import isfile, abspath
from datetime import datetime
from urllib.parse import urlparse
from .config import stor_path
from .database import get, put
if 'pleroma' in sys.argv:
import yaml
try:
with open('relay.yaml') as f:
config = yaml.load(f, Loader=yaml.SafeLoader)
except Exception as e:
print(f'Failed to open "relay.yaml": {e}')
sys.exit()
dbfile = config['db']
domainbans = config['ap']['blocked_instances']
whitelist = config['ap']['whitelist']
settings = {
'address': config['listen'],
'port': config['port'],
'host': config['ap']['host'],
'whitelist': config['ap']['whitelist_enabled']
}
try:
jsondb = json.load(open(dbfile))
except Exception as e:
print(f'Failed to open "{dbfile}": {e}')
sys.exit()
inboxes = jsondb['relay-list']
key = {
'privkey': jsondb['actorKeys']['privateKey'],
'pubkey': jsondb['actorKeys']['publicKey']
}
else:
print('heck')
sys.exit()
# migrate inboxes
for row in inboxes:
if type(row) == str:
domain = urlparse(row).netloc
row = {
'actor': f'https://{domain}/actor',
'inbox': f'https://{domain}/inbox',
'domain': domain
}
if not get.inbox(row['domain']):
urls = {
'actor': row['actor'],
'inbox': row['inbox'],
'domain': row['domain']
}
timestamp = row.get('timestamp')
put.inbox('add', urls, timestamp=timestamp)
# migrate actor key
put.rsa_key('default', key)
# migrate config
put.config(settings)
# migrate domain bans
for domain in domainbans:
put.ban('add', domain)
# migrate whitelist
for domain in whitelist:
put.whitelist('add', domain)