lnbits-legend/lnbits/extensions/admin/migrations.py

331 lines
9.3 KiB
Python
Raw Normal View History

2022-03-07 05:03:32 +00:00
from os import getenv
2022-03-12 14:18:09 +00:00
from sqlalchemy.exc import OperationalError # type: ignore
2022-03-18 16:59:06 +00:00
from lnbits.config import conf
2022-03-07 05:03:32 +00:00
from lnbits.helpers import urlsafe_short_hash
2022-05-16 10:49:21 +01:00
async def get_admin_user():
if(conf.admin_users[0]):
return conf.admin_users[0]
from lnbits.core.crud import create_account, get_user
print("Seems like there's no admin users yet. Let's create an account for you!")
account = await create_account()
user = account.id
assert user, "Newly created user couldn't be retrieved"
print(f"Your newly created account/user id is: {user}. This will be the Super Admin user.")
return user
2022-03-07 05:03:32 +00:00
async def m001_create_admin_table(db):
2022-03-18 16:59:06 +00:00
# users/server
2022-05-16 10:49:21 +01:00
user = await get_admin_user()
2022-03-18 16:59:06 +00:00
admin_users = ",".join(conf.admin_users)
allowed_users = ",".join(conf.allowed_users)
admin_ext = ",".join(conf.admin_ext)
disabled_ext = ",".join(conf.disabled_ext)
funding_source = conf.funding_source
#operational
data_folder = conf.data_folder
database_url = conf.database_url
force_https = conf.force_https
service_fee = conf.service_fee
hide_api = conf.hide_api
denomination = conf.denomination
# Theme'ing
site_title = conf.site_title
site_tagline = conf.site_tagline
site_description = conf.site_description
default_wallet_name = conf.default_wallet_name
theme = ",".join(conf.theme)
ad_space = ",".join(conf.ad_space)
# if getenv("LNBITS_ADMIN_EXTENSIONS"):
# admin_ext = getenv("LNBITS_ADMIN_EXTENSIONS")
# if getenv("LNBITS_DATABASE_URL"):
# database_url = getenv("LNBITS_DATABASE_URL")
# if getenv("LNBITS_HIDE_API"):
# hide_api = getenv("LNBITS_HIDE_API")
# if getenv("LNBITS_THEME_OPTIONS"):
# theme = getenv("LNBITS_THEME_OPTIONS")
# if getenv("LNBITS_AD_SPACE"):
# ad_space = getenv("LNBITS_AD_SPACE")
# if getenv("LNBITS_SITE_TITLE"):
# site_title = getenv("LNBITS_SITE_TITLE")
# if getenv("LNBITS_SITE_TAGLINE"):
# site_tagline = getenv("LNBITS_SITE_TAGLINE")
# if getenv("LNBITS_SITE_DESCRIPTION"):
# site_description = getenv("LNBITS_SITE_DESCRIPTION")
# if getenv("LNBITS_ALLOWED_USERS"):
# allowed_users = getenv("LNBITS_ALLOWED_USERS")
# if getenv("LNBITS_ADMIN_USERS"):
# admin_users = "".join(getenv("LNBITS_ADMIN_USERS").split())
# user = admin_users.split(',')[0]
# if getenv("LNBITS_DEFAULT_WALLET_NAME"):
# default_wallet_name = getenv("LNBITS_DEFAULT_WALLET_NAME")
# if getenv("LNBITS_DATA_FOLDER"):
# data_folder = getenv("LNBITS_DATA_FOLDER")
# if getenv("LNBITS_DISABLED_EXTENSIONS"):
# disabled_ext = getenv("LNBITS_DISABLED_EXTENSIONS")
# if getenv("LNBITS_FORCE_HTTPS"):
# force_https = getenv("LNBITS_FORCE_HTTPS")
# if getenv("LNBITS_SERVICE_FEE"):
# service_fee = getenv("LNBITS_SERVICE_FEE")
# if getenv("LNBITS_DENOMINATION"):
# denomination = getenv("LNBITS_DENOMINATION", "sats")
# if getenv("LNBITS_BACKEND_WALLET_CLASS"):
# funding_source = getenv("LNBITS_BACKEND_WALLET_CLASS")
2022-03-07 05:03:32 +00:00
await db.execute(
"""
2022-05-16 12:29:58 +01:00
CREATE TABLE IF NOT EXISTS admin.admin (
2022-03-18 16:59:06 +00:00
"user" TEXT PRIMARY KEY,
2022-03-12 14:18:09 +00:00
admin_users TEXT,
2022-03-07 05:03:32 +00:00
allowed_users TEXT,
2022-03-18 16:59:06 +00:00
admin_ext TEXT,
2022-03-07 05:03:32 +00:00
disabled_ext TEXT,
2022-03-18 16:59:06 +00:00
funding_source TEXT,
data_folder TEXT,
database_url TEXT,
2022-03-07 05:03:32 +00:00
force_https BOOLEAN,
2022-03-12 14:18:09 +00:00
service_fee REAL,
2022-03-18 16:59:06 +00:00
hide_api BOOLEAN,
denomination TEXT,
site_title TEXT,
site_tagline TEXT,
site_description TEXT,
default_wallet_name TEXT,
theme TEXT,
ad_space TEXT
2022-03-07 05:03:32 +00:00
);
"""
)
await db.execute(
"""
2022-05-16 12:29:58 +01:00
INSERT INTO admin.admin (
2022-03-18 16:59:06 +00:00
"user",
admin_users,
allowed_users,
admin_ext,
disabled_ext,
funding_source,
data_folder,
database_url,
force_https,
service_fee,
hide_api,
denomination,
2022-03-07 05:03:32 +00:00
site_title,
site_tagline,
site_description,
default_wallet_name,
2022-03-18 16:59:06 +00:00
theme,
ad_space)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
""",
(
user,
admin_users,
allowed_users,
admin_ext,
2022-03-07 05:03:32 +00:00
disabled_ext,
2022-03-18 16:59:06 +00:00
funding_source,
data_folder,
database_url,
2022-03-07 05:03:32 +00:00
force_https,
service_fee,
2022-03-18 16:59:06 +00:00
hide_api,
denomination,
site_title,
site_tagline,
site_description,
default_wallet_name,
theme,
ad_space,
2022-03-07 05:03:32 +00:00
),
)
async def m001_create_funding_table(db):
funding_wallet = getenv("LNBITS_BACKEND_WALLET_CLASS")
# Make the funding table, if it does not already exist
await db.execute(
"""
2022-05-16 12:29:58 +01:00
CREATE TABLE IF NOT EXISTS admin.funding (
2022-03-07 05:03:32 +00:00
id TEXT PRIMARY KEY,
backend_wallet TEXT,
endpoint TEXT,
port INT,
read_key TEXT,
invoice_key TEXT,
admin_key TEXT,
cert TEXT,
balance INT,
selected INT
);
"""
)
await db.execute(
"""
2022-05-16 12:29:58 +01:00
INSERT INTO admin.funding (id, backend_wallet, endpoint, selected)
2022-03-07 05:03:32 +00:00
VALUES (?, ?, ?, ?)
""",
(
urlsafe_short_hash(),
"CLightningWallet",
getenv("CLIGHTNING_RPC"),
1 if funding_wallet == "CLightningWallet" else 0,
),
)
await db.execute(
"""
2022-05-16 12:29:58 +01:00
INSERT INTO admin.funding (id, backend_wallet, endpoint, admin_key, selected)
2022-03-07 05:03:32 +00:00
VALUES (?, ?, ?, ?, ?)
""",
(
urlsafe_short_hash(),
"SparkWallet",
getenv("SPARK_URL"),
getenv("SPARK_TOKEN"),
1 if funding_wallet == "SparkWallet" else 0,
),
)
await db.execute(
"""
2022-05-16 12:29:58 +01:00
INSERT INTO admin.funding (id, backend_wallet, endpoint, admin_key, selected)
2022-03-07 05:03:32 +00:00
VALUES (?, ?, ?, ?, ?)
""",
(
urlsafe_short_hash(),
"LnbitsWallet",
getenv("LNBITS_ENDPOINT"),
getenv("LNBITS_KEY"),
1 if funding_wallet == "LnbitsWallet" else 0,
),
)
await db.execute(
"""
2022-05-16 12:29:58 +01:00
INSERT INTO admin.funding (id, backend_wallet, endpoint, port, admin_key, cert, selected)
2022-03-07 05:03:32 +00:00
VALUES (?, ?, ?, ?, ?, ?, ?)
""",
(
urlsafe_short_hash(),
"LndWallet",
getenv("LND_GRPC_ENDPOINT"),
getenv("LND_GRPC_PORT"),
getenv("LND_GRPC_MACAROON"),
getenv("LND_GRPC_CERT"),
1 if funding_wallet == "LndWallet" else 0,
),
)
await db.execute(
"""
2022-05-16 12:29:58 +01:00
INSERT INTO admin.funding (id, backend_wallet, endpoint, admin_key, cert, selected)
2022-03-07 05:03:32 +00:00
VALUES (?, ?, ?, ?, ?, ?)
""",
(
urlsafe_short_hash(),
"LndRestWallet",
getenv("LND_REST_ENDPOINT"),
getenv("LND_REST_MACAROON"),
getenv("LND_REST_CERT"),
1 if funding_wallet == "LndWallet" else 0,
),
)
await db.execute(
"""
2022-05-16 12:29:58 +01:00
INSERT INTO admin.funding (id, backend_wallet, endpoint, admin_key, cert, selected)
2022-03-07 05:03:32 +00:00
VALUES (?, ?, ?, ?, ?, ?)
""",
(
urlsafe_short_hash(),
"LNPayWallet",
getenv("LNPAY_API_ENDPOINT"),
getenv("LNPAY_WALLET_KEY"),
getenv("LNPAY_API_KEY"), # this is going in as the cert
1 if funding_wallet == "LNPayWallet" else 0,
),
)
await db.execute(
"""
2022-05-16 12:29:58 +01:00
INSERT INTO admin.funding (id, backend_wallet, endpoint, admin_key, selected)
2022-03-07 05:03:32 +00:00
VALUES (?, ?, ?, ?, ?)
""",
(
urlsafe_short_hash(),
"LntxbotWallet",
getenv("LNTXBOT_API_ENDPOINT"),
getenv("LNTXBOT_KEY"),
1 if funding_wallet == "LntxbotWallet" else 0,
),
)
await db.execute(
"""
2022-05-16 12:29:58 +01:00
INSERT INTO admin.funding (id, backend_wallet, endpoint, admin_key, selected)
2022-03-07 05:03:32 +00:00
VALUES (?, ?, ?, ?, ?)
""",
(
urlsafe_short_hash(),
"OpenNodeWallet",
getenv("OPENNODE_API_ENDPOINT"),
getenv("OPENNODE_KEY"),
1 if funding_wallet == "OpenNodeWallet" else 0,
),
)
await db.execute(
"""
2022-05-16 12:29:58 +01:00
INSERT INTO admin.funding (id, backend_wallet, endpoint, admin_key, selected)
2022-03-07 05:03:32 +00:00
VALUES (?, ?, ?, ?, ?)
""",
(
urlsafe_short_hash(),
"SparkWallet",
getenv("SPARK_URL"),
getenv("SPARK_TOKEN"),
1 if funding_wallet == "SparkWallet" else 0,
),
)
## PLACEHOLDER FOR ECLAIR WALLET
# await db.execute(
# """
2022-05-16 12:29:58 +01:00
# INSERT INTO admin.funding (id, backend_wallet, endpoint, admin_key, selected)
2022-03-07 05:03:32 +00:00
# VALUES (?, ?, ?, ?, ?)
# """,
# (
# urlsafe_short_hash(),
# "EclairWallet",
# getenv("ECLAIR_URL"),
# getenv("ECLAIR_PASS"),
# 1 if funding_wallet == "EclairWallet" else 0,
# ),
# )