Skip to content

Commit

Permalink
Deployment script auto-updates only once
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardogsilva committed Nov 27, 2024
1 parent a61fd9c commit 418e9c2
Showing 1 changed file with 32 additions and 8 deletions.
40 changes: 32 additions & 8 deletions deployments/deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

logger = logging.getLogger(__name__)

_DO_NOT_UPDATE_FLAG_NAME = "--no-auto-update"


@dataclasses.dataclass
class DeploymentConfiguration:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 418e9c2

Please sign in to comment.