From 15316f4fc521834dbcda9f38ceda0ffd9f804d45 Mon Sep 17 00:00:00 2001 From: Nitin Aggarwal Date: Tue, 16 Aug 2022 12:47:03 -0700 Subject: [PATCH] Remove upgrade and upgrade check (#933) * done * more fixes * remove file * fix-tests --- opta/cli.py | 4 -- opta/commands/apply.py | 12 +---- opta/commands/output.py | 2 - opta/commands/upgrade.py | 54 ---------------------- opta/core/upgrade.py | 70 ---------------------------- tests/commands/test_apply.py | 7 --- tests/commands/test_upgrade.py | 29 ------------ tests/test_upgrade.py | 83 ---------------------------------- 8 files changed, 2 insertions(+), 259 deletions(-) delete mode 100644 opta/commands/upgrade.py delete mode 100644 opta/core/upgrade.py delete mode 100644 tests/commands/test_upgrade.py delete mode 100644 tests/test_upgrade.py diff --git a/opta/cli.py b/opta/cli.py index d0a2839bb..629974df9 100755 --- a/opta/cli.py +++ b/opta/cli.py @@ -23,10 +23,8 @@ from opta.commands.secret import secret from opta.commands.shell import shell from opta.commands.show import show -from opta.commands.upgrade import upgrade from opta.commands.validate import validate from opta.commands.version import version -from opta.core.upgrade import check_version_upgrade from opta.crash_reporter import CURRENT_CRASH_REPORTER from opta.exceptions import UserErrors from opta.one_time import one_time @@ -59,7 +57,6 @@ def cli() -> None: cli.add_command(version) cli.add_command(events) cli.add_command(force_unlock) -cli.add_command(upgrade) cli.add_command(generate_terraform) cli.add_command(help) cli.add_command(show) @@ -97,6 +94,5 @@ def cli() -> None: # NOTE: Statements after the cli() invocation in the try clause are not executed. # A quick glance at click documentation did not show why that is the case or any workarounds. # Therefore adding this version check in the finally clause for now. - check_version_upgrade() if os.environ.get("OPTA_DEBUG") is None: cleanup_files() diff --git a/opta/commands/apply.py b/opta/commands/apply.py index 0c9d4d47a..b24e12ed6 100644 --- a/opta/commands/apply.py +++ b/opta/commands/apply.py @@ -14,7 +14,6 @@ from opta.amplitude import amplitude_client from opta.cleanup_files import cleanup_files from opta.commands.local_flag import _clean_tf_folder, _handle_local_flag -from opta.commands.upgrade import _upgrade from opta.constants import DEV_VERSION, TF_PLAN_PATH, UPGRADE_WARNINGS, VERSION from opta.core.aws import AWS from opta.core.azure import Azure @@ -334,16 +333,9 @@ def _verify_semver( old_semver = semver.VersionInfo.parse(old_semver_string) current_semver = semver.VersionInfo.parse(current_semver_string) if old_semver > current_semver: - logger.warning( - f"You're trying to run an older version ({current_semver}) of opta (last run with version {old_semver})." + raise Exception( + f"You're trying to run an older version ({current_semver}) of opta (last run with version {old_semver}). Please upgrade before re-running" ) - if not auto_approve: - click.confirm( - "Do you wish to upgrade to the latest version of Opta?", abort=True, - ) - _upgrade() - logger.info("Please rerun the command if the upgrade was successful.") - exit(0) present_modules = [k.aliased_type or k.type for k in layer.modules] diff --git a/opta/commands/output.py b/opta/commands/output.py index f1c4d297e..16c6137a6 100644 --- a/opta/commands/output.py +++ b/opta/commands/output.py @@ -6,7 +6,6 @@ from opta.commands.apply import local_setup from opta.core.generator import gen_all from opta.core.terraform import get_terraform_outputs -from opta.core.upgrade import disable_version_upgrade from opta.layer import Layer from opta.utils import check_opta_file_exists, json from opta.utils.clickoptions import ( @@ -26,7 +25,6 @@ def output( config: str, env: Optional[str], local: Optional[bool], var: Dict[str, str], ) -> None: """Print TF outputs""" - disable_version_upgrade() config = check_opta_file_exists(config) if local: config = local_setup(config, var, None) diff --git a/opta/commands/upgrade.py b/opta/commands/upgrade.py deleted file mode 100644 index f6793475c..000000000 --- a/opta/commands/upgrade.py +++ /dev/null @@ -1,54 +0,0 @@ -import os - -import click -import requests - -from opta.constants import OPTA_INSTALL_URL -from opta.core.upgrade import check_version_upgrade -from opta.nice_subprocess import nice_run -from opta.utils import logger -from opta.utils.globals import OptaUpgrade - -TEMP_INSTALLATION_FILENAME = "opta_installation.sh" - - -def _make_installation_file() -> None: - logger.debug(f"Querying {OPTA_INSTALL_URL}") - resp = requests.get(OPTA_INSTALL_URL) - resp.raise_for_status() - with open(TEMP_INSTALLATION_FILENAME, "w") as file: - file.write(resp.text) - nice_run(["chmod", "777", TEMP_INSTALLATION_FILENAME]) - - -def _upgrade_successful() -> None: - OptaUpgrade.success() - - -def _cleanup_installation_file() -> None: - if os.path.isfile(TEMP_INSTALLATION_FILENAME): - os.remove(TEMP_INSTALLATION_FILENAME) - - -@click.command() -def upgrade() -> None: - """ - Upgrade Opta to the latest version available - """ - _upgrade() - - -def _upgrade() -> None: - try: - upgrade_present = check_version_upgrade(is_upgrade_call=True) - if upgrade_present: - _make_installation_file() - nice_run([f"./{TEMP_INSTALLATION_FILENAME}"], input=b"y") - _upgrade_successful() - except Exception: - logger.error( - "\nUnable to install latest version of Opta." - "\nPlease follow the instructions on https://docs.opta.dev/installation" - ) - finally: - _cleanup_installation_file() diff --git a/opta/core/upgrade.py b/opta/core/upgrade.py deleted file mode 100644 index 04d74c03a..000000000 --- a/opta/core/upgrade.py +++ /dev/null @@ -1,70 +0,0 @@ -import os -import random - -import requests -import semver - -from opta.constants import DEV_VERSION, VERSION -from opta.utils import logger -from opta.utils.globals import OptaUpgrade - -LATEST_VERSION_FILE_URL = "https://dev-runx-opta-binaries.s3.amazonaws.com/latest" -UPGRADE_CHECK_PROBABILITY: float = float( - os.environ.get("OPTA_UPGRADE_CHECK_PROBABILITY", "0.2") -) -# TODO: Change this to the actual upgrade URL. -UPGRADE_INSTRUCTIONS_URL = "https://docs.opta.dev/installation/" - - -def _should_check_for_version_upgrade() -> bool: - return (VERSION not in [DEV_VERSION, "", None]) and ( - random.random() < UPGRADE_CHECK_PROBABILITY # nosec - ) - - -def _get_latest_version() -> str: - logger.debug(f"Querying {LATEST_VERSION_FILE_URL}") - resp = requests.get(LATEST_VERSION_FILE_URL) - resp.raise_for_status() - return resp.text.strip().strip("v") - - -def disable_version_upgrade() -> None: - global UPGRADE_CHECK_PROBABILITY - UPGRADE_CHECK_PROBABILITY = 0 - - -def check_version_upgrade(is_upgrade_call: bool = False) -> bool: - """Logs a warning if newer version of opta is available. - - The version check is not always performed when this function is called. - It is performed non-deterministically with a probability of UPGRADE_CHECK_PROBABILITY - in order to not spam the user. - """ - if OptaUpgrade.successful: - OptaUpgrade.unset() - return True - if is_upgrade_call or _should_check_for_version_upgrade(): - logger.info("Checking for version upgrades...") - try: - latest_version = _get_latest_version() - except Exception as e: - logger.debug(e, exc_info=True) - logger.info("Unable to find latest version.") - return False - try: - if semver.VersionInfo.parse(VERSION.strip("v")).compare(latest_version) < 0: - logger.warning( - "New version available.\n" - f"You have {VERSION} installed. Latest version is {latest_version}." - ) - if not is_upgrade_call: - print( - f"Upgrade instructions are available at {UPGRADE_INSTRUCTIONS_URL} or simply use the `opta upgrade` command" - ) - return True - else: - logger.info("User on the latest version.") - except Exception as e: - logger.info(f"Semver check failed with error {e}") - return False diff --git a/tests/commands/test_apply.py b/tests/commands/test_apply.py index 02abb72d3..23e9a6868 100644 --- a/tests/commands/test_apply.py +++ b/tests/commands/test_apply.py @@ -213,13 +213,6 @@ def test_verify_semver_all_good(mocker: MockFixture, mocked_layer: Any) -> None: _verify_semver("0.1.0", "0.2.0", mocked_layer) -def test_verify_semver_older_version(mocker: MockFixture, mocked_layer: Any) -> None: - mock_upgrade = mocker.patch("opta.commands.apply._upgrade") - with pytest.raises(SystemExit): - _verify_semver("0.2.0", "0.1.0", mocked_layer, True) - mock_upgrade.assert_called_once() - - def test_verify_semver_upgrade_warning(mocker: MockFixture, mocked_layer: Any) -> None: mocked_logger = mocker.patch("opta.commands.apply.logger") mocked_click = mocker.patch("opta.commands.apply.click") diff --git a/tests/commands/test_upgrade.py b/tests/commands/test_upgrade.py deleted file mode 100644 index 5c6d6eb4e..000000000 --- a/tests/commands/test_upgrade.py +++ /dev/null @@ -1,29 +0,0 @@ -from click.testing import CliRunner -from pytest_mock import MockFixture - -from opta.cli import cli -from opta.commands.upgrade import TEMP_INSTALLATION_FILENAME - - -def test_upgrade(mocker: MockFixture) -> None: - mock_check_version_upgrade = mocker.patch( - "opta.commands.upgrade.check_version_upgrade" - ) - mock_check_version_upgrade.return_value = True - mock_make_installation_file = mocker.patch( - "opta.commands.upgrade._make_installation_file" - ) - mock_upgrade_successful = mocker.patch("opta.commands.upgrade._upgrade_successful") - mock_cleanup_installation_file = mocker.patch( - "opta.commands.upgrade._cleanup_installation_file" - ) - mock_nice_run = mocker.patch("opta.commands.upgrade.nice_run") - - runner = CliRunner() - result = runner.invoke(cli, ["upgrade"]) - assert result.exit_code == 0 - mock_check_version_upgrade.assert_called_once_with(is_upgrade_call=True) - mock_make_installation_file.assert_called_once() - mock_nice_run.assert_called_once_with([f"./{TEMP_INSTALLATION_FILENAME}"], input=b"y") - mock_upgrade_successful.assert_called_once() - mock_cleanup_installation_file.assert_called_once() diff --git a/tests/test_upgrade.py b/tests/test_upgrade.py deleted file mode 100644 index e18e4a5af..000000000 --- a/tests/test_upgrade.py +++ /dev/null @@ -1,83 +0,0 @@ -import pytest -import requests -import requests_mock -from pytest_mock import MockFixture - -from opta.core.upgrade import ( - LATEST_VERSION_FILE_URL, - _get_latest_version, - check_version_upgrade, -) - -TEST_LATEST_VERSION = "1.11.1" -TEST_OLD_VERSION = "1.9.6" - - -class TestGetLatestVersion: - def test_returns_version_number_from_file(self) -> None: - with requests_mock.Mocker() as m: - m.register_uri( - "GET", LATEST_VERSION_FILE_URL, text=f"{TEST_LATEST_VERSION}\n" - ) - assert _get_latest_version() == TEST_LATEST_VERSION - - def test_raises_exception_if_connection_error(self) -> None: - with requests_mock.Mocker() as m: - m.register_uri( - "GET", LATEST_VERSION_FILE_URL, exc=requests.exceptions.ConnectTimeout - ) - with pytest.raises(Exception): - _get_latest_version() - - def test_raises_exception_if_error_response(self) -> None: - with requests_mock.Mocker() as m: - m.register_uri( - "GET", LATEST_VERSION_FILE_URL, status_code=404, text="Not Found" - ) - with pytest.raises(Exception): - _get_latest_version() - - m.register_uri( - "GET", LATEST_VERSION_FILE_URL, status_code=500, text="Server error" - ) - with pytest.raises(Exception): - _get_latest_version() - - -class TestCheckVersionUpgrade: - def test_does_not_check_if_should_check_false(self, mocker: MockFixture) -> None: - mocked_should_check = mocker.patch( - "opta.core.upgrade._should_check_for_version_upgrade", return_value=False - ) - mocked_get_latest_version = mocker.patch("opta.core.upgrade._get_latest_version") - check_version_upgrade() - mocked_should_check.assert_called_once() - mocked_get_latest_version.assert_not_called() - - def test_logs_update_instructions_if_newer_version_available( - self, mocker: MockFixture - ) -> None: - mocker.patch( - "opta.core.upgrade._should_check_for_version_upgrade", return_value=True - ) - mocker.patch( - "opta.core.upgrade._get_latest_version", return_value=TEST_LATEST_VERSION - ) - mocker.patch("opta.core.upgrade.VERSION", TEST_OLD_VERSION) - mocked_logger_warning = mocker.patch("opta.core.upgrade.logger.warning") - check_version_upgrade() - mocked_logger_warning.assert_called_once() - warning_message: str = mocked_logger_warning.call_args.args[0] - assert warning_message.find(TEST_OLD_VERSION) > -1 - assert warning_message.find(TEST_LATEST_VERSION) > -1 - - def test_handles_get_latest_version_exceptions(self, mocker: MockFixture) -> None: - mocker.patch( - "opta.core.upgrade._should_check_for_version_upgrade", return_value=True - ) - mocked_get_latest_version = mocker.patch( - "opta.core.upgrade._get_latest_version", - side_effect=requests.exceptions.ConnectTimeout, - ) - check_version_upgrade() - mocked_get_latest_version.assert_called_once()