Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Updater] restore binary updater #2682

Merged
merged 1 commit into from
Jul 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions octobot/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ async def update_or_repair_tentacles_if_necessary(community_auth, selected_profi

to_install_urls, to_remove_tentacles, force_refresh_tentacles_setup_config = \
community_tentacles_packages.get_to_install_and_remove_tentacles(
community_auth, selected_profile_tentacles_setup_config
community_auth, selected_profile_tentacles_setup_config, constants.LONG_VERSION
)
if to_remove_tentacles:
await community_tentacles_packages.uninstall_tentacles(to_remove_tentacles)
Expand Down Expand Up @@ -167,7 +167,8 @@ async def install_or_update_tentacles(


async def install_all_tentacles(
tentacles_url=None, additional_tentacles_package_urls: typing.Optional[list] = None, only_additional: bool = False
tentacles_url=None, additional_tentacles_package_urls: typing.Optional[list] = None, only_additional: bool = False,
bot_version: str = constants.LONG_VERSION
):
if tentacles_url is None:
tentacles_url = configuration_manager.get_default_tentacles_url()
Expand All @@ -185,7 +186,7 @@ async def install_all_tentacles(
if url is None:
continue
hide_url = url in additional_tentacles_package_urls
url = community_tentacles_packages.adapt_url_to_bot_version(url)
url = community_tentacles_packages.adapt_url_to_bot_version(url, bot_version)
await tentacles_manager_api.install_all_tentacles(
url,
aiohttp_session=aiohttp_session,
Expand Down
14 changes: 7 additions & 7 deletions octobot/community/tentacles_packages.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ async def has_tentacles_to_install_and_uninstall_tentacles_if_necessary(communit
community_auth.config.get_tentacles_config_path()
)
to_install, to_remove_tentacles, force_refresh_tentacles_setup_config = get_to_install_and_remove_tentacles(
community_auth, tentacles_setup_config
community_auth, tentacles_setup_config, constants.LONG_VERSION
)
if to_remove_tentacles:
logging.get_logger(__name__).debug(
Expand All @@ -36,29 +36,29 @@ async def has_tentacles_to_install_and_uninstall_tentacles_if_necessary(communit
return bool(to_install)


def adapt_url_to_bot_version(package_url: str) -> str:
def adapt_url_to_bot_version(package_url: str, bot_version: str) -> str:
if constants.VERSION_PLACEHOLDER in package_url:
package_url = package_url.replace(constants.VERSION_PLACEHOLDER, constants.LONG_VERSION)
package_url = package_url.replace(constants.VERSION_PLACEHOLDER, bot_version)
return package_url


def get_to_install_and_remove_tentacles(
community_auth, selected_profile_tentacles_setup_config
community_auth, selected_profile_tentacles_setup_config, bot_version: str
):
installed_community_package_urls = [
adapt_url_to_bot_version(package_url)
adapt_url_to_bot_version(package_url, bot_version)
for package_url in tentacles_manager_api.get_all_installed_package_urls(
selected_profile_tentacles_setup_config
)
if is_community_tentacle_url(package_url)
]
additional_tentacles_package_urls = [
adapt_url_to_bot_version(package_url)
adapt_url_to_bot_version(package_url, bot_version)
for package_url in community_auth.get_saved_package_urls()
] if community_auth.is_logged_in() else []
to_keep_tentacles = set(
additional_tentacles_package_urls + [
adapt_url_to_bot_version(package_url)
adapt_url_to_bot_version(package_url, bot_version)
for package_url in get_env_variable_tentacles_urls()
] + get_env_variable_tentacles_urls()
)
Expand Down
22 changes: 10 additions & 12 deletions octobot/updater/binary_updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,6 @@ async def get_latest_version(self):
return self._parse_latest_version(await self._get_latest_release_data())

async def update_impl(self) -> bool:
# TODO fix binary updater to use release endpoint (assents can't be downloaded from gh directly)
self.logger.error(f"Please manually update your OctoBot executable from this page: "
f"{self._get_latest_release_url(False)}")
return False
new_binary_file = await self._download_binary()
if new_binary_file is not None:
self._give_execution_rights(new_binary_file)
Expand All @@ -73,10 +69,11 @@ def _get_latest_release_url(self, use_api_url):

async def _get_latest_release_data(self):
try:
async with aiohttp.ClientSession().get(self._get_latest_release_url(True)) as resp:
text = await resp.text()
if resp.status == 200:
return json.loads(text)
async with aiohttp.ClientSession() as session:
async with session.get(self._get_latest_release_url(True)) as resp:
text = await resp.text()
if resp.status == 200:
return json.loads(text)
return None
except Exception as e:
self.logger.debug(f"Error when fetching latest binary data : {e}")
Expand Down Expand Up @@ -137,10 +134,11 @@ async def _download_binary(self):
return None
self.logger.info(f"Start downloading OctoBot update at {new_binary_file_url}")
async with aiofiles.open(new_binary_file, 'wb+') as downloaded_file:
await aiohttp_util.download_stream_file(output_file=downloaded_file,
file_url=new_binary_file_url,
aiohttp_session=aiohttp.ClientSession(),
is_aiofiles_output_file=True)
async with aiohttp.ClientSession() as session:
await aiohttp_util.download_stream_file(output_file=downloaded_file,
file_url=new_binary_file_url,
aiohttp_session=session,
is_aiofiles_output_file=True)
self.logger.info(f"OctoBot update downloaded successfully")
return new_binary_file

Expand Down
9 changes: 5 additions & 4 deletions octobot/updater/python_updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,11 @@ def _get_latest_pypi_release_url(self):

async def _get_latest_pypi_version_data(self):
try:
async with aiohttp.ClientSession().get(self._get_latest_pypi_release_url()) as resp:
text = await resp.text()
if resp.status == 200:
return json.loads(text)
async with aiohttp.ClientSession() as session:
async with session.get(self._get_latest_pypi_release_url()) as resp:
text = await resp.text()
if resp.status == 200:
return json.loads(text)
return None
except Exception as e:
self.logger.debug(f"Error when fetching latest pypi package data : {e}")
Expand Down
6 changes: 4 additions & 2 deletions octobot/updater/updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,13 @@ async def update_impl(self) -> bool:
raise NotImplementedError("update_impl is not implemented")

async def update_tentacles(self):
bot_version = await self.get_latest_version()
authenticator = authentication.Authenticator.instance()
additional_tentacles_package_urls = authenticator.get_saved_package_urls()
await commands.install_all_tentacles(
tentacles_url=configuration_manager.get_default_tentacles_url(version=await self.get_latest_version()),
additional_tentacles_package_urls=additional_tentacles_package_urls
tentacles_url=configuration_manager.get_default_tentacles_url(version=bot_version),
additional_tentacles_package_urls=additional_tentacles_package_urls,
bot_version=bot_version
)

async def post_update(self):
Expand Down
Loading