From 418e9c2091ed99270d958fa3334109393c5af757 Mon Sep 17 00:00:00 2001 From: Ricardo Garcia Silva Date: Wed, 27 Nov 2024 10:50:09 +0000 Subject: [PATCH] Deployment script auto-updates only once --- deployments/deploy.py | 40 ++++++++++++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/deployments/deploy.py b/deployments/deploy.py index 2af39c43..af48f4f1 100644 --- a/deployments/deploy.py +++ b/deployments/deploy.py @@ -25,6 +25,8 @@ logger = logging.getLogger(__name__) +_DO_NOT_UPDATE_FLAG_NAME = "--no-auto-update" + @dataclasses.dataclass class DeploymentConfiguration: @@ -322,7 +324,11 @@ class _RelaunchDeploymentScript: name: str = "Relaunch the updated deployment script" def handle(self) -> None: - os.execv(sys.executable, self.original_call_args[:]) + call_args = self.original_call_args[:] + # prevent infinite loops by ensuring we set the --no-auto-update flag + if args.index(_DO_NOT_UPDATE_FLAG_NAME) == -1: + call_args.append(_DO_NOT_UPDATE_FLAG_NAME) + os.execv(sys.executable, call_args) @dataclasses.dataclass @@ -466,19 +472,27 @@ def get_configuration(config_file: Path) -> DeploymentConfiguration: def perform_deployment( *, configuration: DeploymentConfiguration, + auto_update: bool, confirmed: bool = False, ): deployment_steps = [ _CloneRepo(config=configuration), _CopyRelevantRepoFiles(config=configuration), - _RelaunchDeploymentScript(config=configuration, original_call_args=sys.argv), - _StopCompose(config=configuration), - _GenerateComposeFile(config=configuration), - _PullImages(config=configuration), - _StartCompose(config=configuration), - _RunMigrations(config=configuration), - _CompileTranslations(config=configuration), ] + if auto_update: + deployment_steps.append( + _RelaunchDeploymentScript(config=configuration, original_call_args=sys.argv) + ) + deployment_steps.extend( + [ + _StopCompose(config=configuration), + _GenerateComposeFile(config=configuration), + _PullImages(config=configuration), + _StartCompose(config=configuration), + _RunMigrations(config=configuration), + _CompileTranslations(config=configuration), + ] + ) this_host = socket.gethostname() if len(configuration.discord_notification_urls) > 0: deployment_steps.append( @@ -507,6 +521,15 @@ def perform_deployment( help="Path to configuration file", type=Path, ) + parser.add_argument( + _DO_NOT_UPDATE_FLAG_NAME, + action="store_true", + help=( + "Whether to avoid auto-updating this deployment script with the current " + "version from the repo. The default is to update this script and then " + "relaunch, which ensures it runs the most up to date deployer." + ), + ) parser.add_argument( "--verbose", action="store_true", @@ -556,6 +579,7 @@ def perform_deployment( try: perform_deployment( configuration=deployment_config, + auto_update=not args.no_auto_update, confirmed=args.confirm, ) except RuntimeError as err: