feat: update pre-installed extensions on re-create

This commit is contained in:
Vlad Stan 2023-01-19 10:15:56 +02:00
parent 69b8dfa21f
commit aa006b66d1
3 changed files with 25 additions and 10 deletions

View File

@ -7,9 +7,7 @@ import shutil
import signal
import sys
import traceback
import zipfile
from http import HTTPStatus
from pathlib import Path
from typing import Callable
from fastapi import FastAPI, Request
@ -146,12 +144,12 @@ async def check_installed_extensions(app: FastAPI):
def check_installed_extension(ext: InstallableExtension) -> bool:
extensions_data_dir = os.path.join(settings.lnbits_data_folder, "extensions")
extensions_dir = os.path.join("lnbits", "extensions")
zip_files = glob.glob(f"{extensions_data_dir}/*.zip")
zip_files = glob.glob(
os.path.join(settings.lnbits_data_folder, "extensions", "*.zip")
)
if Path(os.path.join(extensions_dir, ext.id)).is_dir():
return True # todo: pre-installed that require upgrade
if ext.has_installed_version:
return True
if ext.zip_path in zip_files:
ext.extract_archive()
else:

View File

@ -5,7 +5,6 @@ import time
import uuid
from http import HTTPStatus
from io import BytesIO
from pathlib import Path
from typing import Dict, List, Optional, Tuple, Union
from urllib.parse import ParseResult, parse_qs, urlencode, urlparse, urlunparse

View File

@ -6,6 +6,7 @@ import sys
import urllib.request
import zipfile
from http import HTTPStatus
from pathlib import Path
from typing import Any, List, NamedTuple, Optional
import httpx
@ -108,7 +109,6 @@ class ExtensionRelease(BaseModel):
archive: str
source_repo: str
hash: Optional[str]
published_at: Optional[str]
html_url: Optional[str]
description: Optional[str]
details_html: Optional[str] = None
@ -122,7 +122,6 @@ class ExtensionRelease(BaseModel):
archive=r["zipball_url"],
source_repo=source_repo,
# description=r["body"], # bad for JSON
published_at=r["published_at"],
html_url=r["html_url"],
)
@ -184,6 +183,14 @@ class InstallableExtension(BaseModel):
def module_installed(self) -> bool:
return self.module_name in sys.modules
@property
def has_installed_version(self) -> bool:
if not Path(self.ext_dir).is_dir():
return False
with open(os.path.join(self.ext_dir, "config.json"), "r") as json_file:
config_json = json.load(json_file)
return config_json.get("is_installed") == True
def download_archive(self):
ext_zip_file = self.zip_path
if os.path.isfile(ext_zip_file):
@ -218,6 +225,17 @@ class InstallableExtension(BaseModel):
os.path.join(self.ext_upgrade_dir, self.id),
)
# Pre-packed extensions can be upgraded
# Mark the extension as installed so we know it is not the pre-packed version
with open(
os.path.join(self.ext_upgrade_dir, self.id, "config.json"), "r+"
) as json_file:
config_json = json.load(json_file)
config_json["is_installed"] = True
json_file.seek(0)
json.dump(config_json, json_file)
json_file.truncate()
shutil.rmtree(self.ext_dir, True)
shutil.copytree(
os.path.join(self.ext_upgrade_dir, self.id),