Skip to content

Commit

Permalink
fix: ext upgrade (lnbits#2776)
Browse files Browse the repository at this point in the history
  • Loading branch information
motorina0 authored Nov 13, 2024
1 parent bbae4a0 commit 8c5c455
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 9 deletions.
11 changes: 8 additions & 3 deletions lnbits/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,6 @@ async def check_installed_extensions(app: FastAPI):
persist state. Zips that are missing will be re-downloaded.
"""

shutil.rmtree(Path(settings.lnbits_extensions_path, "upgrades"), True)
installed_extensions = await build_all_installed_extensions_list(False)

for ext in installed_extensions:
Expand Down Expand Up @@ -336,8 +335,14 @@ def register_custom_extensions_path():
+ f" '{settings.lnbits_extensions_path}/extensions'"
)

sys.path.append(str(Path(settings.lnbits_extensions_path, "extensions")))
sys.path.append(str(Path(settings.lnbits_extensions_path, "upgrades")))
extensions_dir = Path(settings.lnbits_extensions_path, "extensions")
Path(extensions_dir).mkdir(parents=True, exist_ok=True)
sys.path.append(str(extensions_dir))

upgrades_dir = Path(settings.lnbits_extensions_path, "upgrades")
shutil.rmtree(upgrades_dir, True)
Path(upgrades_dir).mkdir(parents=True, exist_ok=True)
sys.path.append(str(upgrades_dir))


def register_new_ext_routes(app: FastAPI) -> Callable:
Expand Down
6 changes: 5 additions & 1 deletion lnbits/core/models/extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,11 @@ def from_installable_ext(cls, ext_info: InstallableExtension) -> Extension:
name=ext_info.name,
short_description=ext_info.short_description,
tile=ext_info.icon,
upgrade_hash=settings.extension_upgrade_hash(ext_info.id),
upgrade_hash=(
ext_info.hash
if settings.extension_has_been_activated(ext_info.id)
else ""
),
)


Expand Down
14 changes: 10 additions & 4 deletions lnbits/core/services/extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,23 @@
get_installed_extension,
update_installed_extension_state,
)
from lnbits.core.crud.extensions import get_installed_extensions
from lnbits.core.crud.extensions import (
get_installed_extensions,
update_installed_extension,
)
from lnbits.core.helpers import migrate_extension_database
from lnbits.settings import settings

from ..models.extensions import Extension, InstallableExtension
from ..models.extensions import Extension, ExtensionMeta, InstallableExtension


async def install_extension(ext_info: InstallableExtension) -> Extension:
ext_id = ext_info.id
extension = Extension.from_installable_ext(ext_info)
installed_ext = await get_installed_extension(ext_id)
if installed_ext:
ext_info.meta = installed_ext.meta
if installed_ext and installed_ext.meta:
ext_info.meta = ext_info.meta or ExtensionMeta()
ext_info.meta.payments = installed_ext.meta.payments

await ext_info.download_archive()

Expand All @@ -37,6 +41,8 @@ async def install_extension(ext_info: InstallableExtension) -> Extension:
# if it does exist, it will be activated later in the code
if not installed_ext:
await create_installed_extension(ext_info)
else:
await update_installed_extension(ext_info)

if extension.is_upgrade_extension:
# call stop while the old routes are still active
Expand Down
10 changes: 9 additions & 1 deletion lnbits/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,11 @@ class InstalledExtensionsSettings(LNbitsSettings):
lnbits_extensions_redirects: list[RedirectPath] = Field(default=[])

# list of all extension ids
lnbits_all_extensions_ids: set[Any] = Field(default=[])
lnbits_all_extensions_ids: set[str] = Field(default=[])

# list of all extension ids that have been activated at least once
# only add to this set, do not remove
lnbits_activated_paths_extensions_ids: set[str] = Field(default=[])

def find_extension_redirect(
self, path: str, req_headers: list[tuple[bytes, bytes]]
Expand Down Expand Up @@ -160,11 +164,15 @@ def activate_extension_paths(
self._activate_extension_redirects(ext_id, ext_redirects)

self.lnbits_all_extensions_ids.add(ext_id)
self.lnbits_activated_paths_extensions_ids.add(ext_id)

def deactivate_extension_paths(self, ext_id: str):
self.lnbits_deactivated_extensions.add(ext_id)
self._remove_extension_redirects(ext_id)

def extension_has_been_activated(self, ext_id: str) -> bool:
return ext_id in settings.lnbits_activated_paths_extensions_ids

def extension_upgrade_hash(self, ext_id: str) -> str:
return settings.lnbits_upgraded_extensions.get(ext_id, "")

Expand Down

0 comments on commit 8c5c455

Please sign in to comment.