2019-11-30 14:07:16 +01:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
import getpass
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import psycopg2
|
|
|
|
from psycopg2 import sql
|
|
|
|
from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT
|
|
|
|
|
2022-08-01 11:52:30 +02:00
|
|
|
def get_config():
|
|
|
|
|
|
|
|
# Load configuration from config file
|
|
|
|
config_filepath = "config/db_config.txt"
|
|
|
|
mastodon_db = get_parameter("mastodon_db", config_filepath)
|
|
|
|
mastodon_db_user = get_parameter("mastodon_db_user", config_filepath)
|
|
|
|
mailing_db = get_parameter("mailing_db", config_filepath)
|
|
|
|
mailing_db_user = get_parameter("mailing_db_user", config_filepath)
|
|
|
|
mailing_db_table = get_parameter("mailing_db_table", config_filepath)
|
|
|
|
|
|
|
|
return (mastodon_db, mastodon_db_user, mailing_db, mailing_db_user, mailing_db_table)
|
|
|
|
|
2019-11-30 14:07:16 +01:00
|
|
|
def get_parameter( parameter, file_path ):
|
|
|
|
# Check if secrets file exists
|
|
|
|
if not os.path.isfile(file_path):
|
|
|
|
print("File %s not found, asking."%file_path)
|
|
|
|
write_parameter( parameter, file_path )
|
|
|
|
#sys.exit(0)
|
|
|
|
|
|
|
|
# Find parameter in file
|
|
|
|
with open( file_path ) as f:
|
|
|
|
for line in f:
|
|
|
|
if line.startswith( parameter ):
|
|
|
|
return line.replace(parameter + ":", "").strip()
|
|
|
|
|
|
|
|
# Cannot find parameter, exit
|
|
|
|
print(file_path + " Missing parameter %s "%parameter)
|
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
def write_parameter( parameter, file_path ):
|
|
|
|
|
2022-08-01 11:52:30 +02:00
|
|
|
if not os.path.exists('config'):
|
|
|
|
os.makedirs('config')
|
2019-11-30 14:07:16 +01:00
|
|
|
|
2022-08-01 11:52:30 +02:00
|
|
|
print("Setting up mailing DB parameters...")
|
|
|
|
print("\n")
|
|
|
|
mastodon_db = input("Mastodon db name: ")
|
|
|
|
mastodon_db_user = input("Mastodon db user: ")
|
|
|
|
mailing_db = input("Mailing db name: ")
|
|
|
|
mailing_db_user = input("Mailing db user: ")
|
|
|
|
mailing_db_table = input("Mailing db table: ")
|
|
|
|
with open(file_path, "w") as text_file:
|
|
|
|
print("mastodon_db: {}".format(mastodon_db), file=text_file)
|
|
|
|
print("mastodon_db_user: {}".format(mastodon_db_user), file=text_file)
|
|
|
|
print("mailing_db: {}".format(mailing_db), file=text_file)
|
|
|
|
print("mailing_db_user: {}".format(mailing_db_user), file=text_file)
|
|
|
|
print("mailing_db_table: {}".format(mailing_db_table), file=text_file)
|
2019-11-30 14:07:16 +01:00
|
|
|
|
2022-08-01 11:52:30 +02:00
|
|
|
def create_database():
|
2019-11-30 14:07:16 +01:00
|
|
|
|
2022-08-01 11:52:30 +02:00
|
|
|
try:
|
2019-11-30 14:07:16 +01:00
|
|
|
|
2022-08-01 11:52:30 +02:00
|
|
|
conn = psycopg2.connect(dbname='postgres',
|
|
|
|
user=mailing_db_user, host='',
|
|
|
|
password='')
|
2019-11-30 14:07:16 +01:00
|
|
|
|
2022-08-01 11:52:30 +02:00
|
|
|
conn.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT)
|
2019-11-30 14:07:16 +01:00
|
|
|
|
2022-08-01 11:52:30 +02:00
|
|
|
cur = conn.cursor()
|
2019-11-30 14:07:16 +01:00
|
|
|
|
2022-08-01 11:52:30 +02:00
|
|
|
print("Creating database " + mailing_db + ". Please wait...")
|
|
|
|
cur.execute(sql.SQL("CREATE DATABASE {}").format(
|
|
|
|
sql.Identifier(mailing_db))
|
|
|
|
)
|
|
|
|
print("Database " + mailing_db + " created!")
|
2019-11-30 14:07:16 +01:00
|
|
|
|
2022-08-01 11:52:30 +02:00
|
|
|
except (Exception, psycopg2.DatabaseError) as error:
|
2019-11-30 14:07:16 +01:00
|
|
|
|
2022-08-01 11:52:30 +02:00
|
|
|
print(error)
|
2019-11-30 14:07:16 +01:00
|
|
|
|
2022-08-01 11:52:30 +02:00
|
|
|
finally:
|
2019-11-30 14:07:16 +01:00
|
|
|
|
2022-08-01 11:52:30 +02:00
|
|
|
if conn is not None:
|
2020-05-22 09:11:31 +02:00
|
|
|
|
2022-08-01 11:52:30 +02:00
|
|
|
conn.close()
|
2019-11-30 14:07:16 +01:00
|
|
|
|
2022-08-01 11:52:30 +02:00
|
|
|
try:
|
2019-11-30 14:07:16 +01:00
|
|
|
|
2022-08-01 11:52:30 +02:00
|
|
|
conn = None
|
2019-11-30 14:07:16 +01:00
|
|
|
|
2022-08-01 11:52:30 +02:00
|
|
|
conn = psycopg2.connect(database = mailing_db, user = mailing_db_user, password = "", host = "/var/run/postgresql", port = "5432")
|
2019-11-30 14:07:16 +01:00
|
|
|
|
2022-08-01 11:52:30 +02:00
|
|
|
except (Exception, psycopg2.DatabaseError) as error:
|
2019-11-30 14:07:16 +01:00
|
|
|
|
2022-08-01 11:52:30 +02:00
|
|
|
print(error)
|
|
|
|
# Load configuration from config file
|
|
|
|
os.remove("config.txt")
|
|
|
|
print("Exiting. Run setup again with right parameters")
|
|
|
|
sys.exit(0)
|
2019-11-30 14:07:16 +01:00
|
|
|
|
2022-08-01 11:52:30 +02:00
|
|
|
if conn is not None:
|
|
|
|
|
|
|
|
print("\n")
|
|
|
|
print("Mailing db parameters saved to config.txt!")
|
|
|
|
print("\n")
|
|
|
|
|
|
|
|
def create_table(db, db_user, table, sql):
|
|
|
|
|
|
|
|
try:
|
2019-11-30 14:07:16 +01:00
|
|
|
|
2022-08-01 11:52:30 +02:00
|
|
|
conn = None
|
2019-11-30 14:07:16 +01:00
|
|
|
|
2022-08-01 11:52:30 +02:00
|
|
|
conn = psycopg2.connect(database = db, user = db_user, password = "", host = "/var/run/postgresql", port = "5432")
|
2019-11-30 14:07:16 +01:00
|
|
|
|
2022-08-01 11:52:30 +02:00
|
|
|
cur = conn.cursor()
|
2019-11-30 14:07:16 +01:00
|
|
|
|
2022-08-01 11:52:30 +02:00
|
|
|
print("Creating table.. "+table)
|
|
|
|
# Create the table in PostgreSQL database
|
|
|
|
cur.execute(sql)
|
2019-11-30 14:07:16 +01:00
|
|
|
|
2022-08-01 11:52:30 +02:00
|
|
|
conn.commit()
|
|
|
|
print("Table "+table+" created!")
|
|
|
|
print("\n")
|
2019-11-30 14:07:16 +01:00
|
|
|
|
2022-08-01 11:52:30 +02:00
|
|
|
except (Exception, psycopg2.DatabaseError) as error:
|
2019-11-30 14:07:16 +01:00
|
|
|
|
2022-08-01 11:52:30 +02:00
|
|
|
print(error)
|
2019-11-30 14:07:16 +01:00
|
|
|
|
2022-08-01 11:52:30 +02:00
|
|
|
finally:
|
2019-11-30 14:07:16 +01:00
|
|
|
|
2022-08-01 11:52:30 +02:00
|
|
|
if conn is not None:
|
2019-11-30 14:07:16 +01:00
|
|
|
|
2022-08-01 11:52:30 +02:00
|
|
|
conn.close()
|
2019-11-30 14:07:16 +01:00
|
|
|
|
2022-08-01 11:52:30 +02:00
|
|
|
# main
|
2019-11-30 14:07:16 +01:00
|
|
|
|
2022-08-01 11:52:30 +02:00
|
|
|
if __name__ == '__main__':
|
2019-11-30 14:07:16 +01:00
|
|
|
|
2022-08-01 11:52:30 +02:00
|
|
|
mastodon_db, mastodon_db_user, mailing_db, mailing_db_user, mailing_db_table = get_config()
|
2019-11-30 14:07:16 +01:00
|
|
|
|
2022-08-01 11:52:30 +02:00
|
|
|
create_database()
|
2019-11-30 14:07:16 +01:00
|
|
|
|
2022-08-01 11:52:30 +02:00
|
|
|
########################################
|
|
|
|
# create mailing DB table
|
2019-11-30 14:07:16 +01:00
|
|
|
|
2022-08-01 11:52:30 +02:00
|
|
|
db = mailing_db
|
|
|
|
db_user = mailing_db_user
|
|
|
|
table = mailing_db_table
|
|
|
|
sql = "create table " + table + "(datetime timestamptz, account_id int primary key, username varchar(30), email varchar(50), emailed_at timestamptz,"
|
|
|
|
sql += "emailed boolean default False, deleted boolean default False, elapsed_days varchar(30), to_be_deleted boolean default False,"
|
|
|
|
sql += "recipient_error boolean default False, feedback boolean default False, current_sign_in_at timestamptz)"
|
|
|
|
create_table(db, db_user, table, sql)
|
2019-11-30 14:07:16 +01:00
|
|
|
|
2022-08-01 11:52:30 +02:00
|
|
|
#####################################
|
2019-11-30 14:07:16 +01:00
|
|
|
|
2022-08-01 11:52:30 +02:00
|
|
|
print("Done!\nNow you can run setup.py!\n")
|