Skip to content

Commit

Permalink
Merge pull request #6192 from dimagi/gh/formplayer/check-maintenance-…
Browse files Browse the repository at this point in the history
…window

Reuse maintenance window check for formplayer deploys
  • Loading branch information
gherceg authored Jan 25, 2024
2 parents 489cc99 + 04e1332 commit 70ffb4f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 21 deletions.
19 changes: 2 additions & 17 deletions src/commcare_cloud/commands/deploy/commcare.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import shlex
from datetime import datetime, timedelta

import pytz

from commcare_cloud.alias import commcare_cloud
from commcare_cloud.cli_utils import ask
from commcare_cloud.colors import color_error, color_notice, color_summary
Expand All @@ -19,7 +17,7 @@
record_deploy_start,
announce_deploy_success,
create_release_tag,
within_maintenance_window,
confirm_environment_time,
DeployContext,
record_deploy_failed,
)
Expand Down Expand Up @@ -115,7 +113,7 @@ def confirm_deploy(environment, deploy_revs, rev_diffs, args):

if not (
_confirm_translated(environment, quiet=args.quiet)
and _confirm_environment_time(environment, quiet=args.quiet)
and confirm_environment_time(environment, quiet=args.quiet)
):
return False

Expand Down Expand Up @@ -178,19 +176,6 @@ def _confirm_translated(environment, quiet=False):
)


def _confirm_environment_time(environment, quiet=False):
if within_maintenance_window(environment):
return True
window = environment.fab_settings_config.acceptable_maintenance_window
d = datetime.now(pytz.timezone(window['timezone']))
message = (
"Whoa there bud! You're deploying '%s' outside the configured maintenance window. "
"The current local time is %s.\n"
"ARE YOU DOING SOMETHING EXCEPTIONAL THAT WARRANTS THIS?"
) % (environment.name, d.strftime("%-I:%M%p on %h. %d %Z"))
return ask(message, quiet=quiet)


def _print_same_code_warning(code_branch):
if code_branch == 'master':
branch_specific_msg = "Perhaps you meant to merge a PR or specify a --set code_branch=<branch> ?"
Expand Down
14 changes: 12 additions & 2 deletions src/commcare_cloud/commands/deploy/formplayer.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,14 @@
from commcare_cloud.commands.ansible.helpers import AnsibleContext
from commcare_cloud.commands.deploy.deploy_diff import DeployDiff
from commcare_cloud.commands.deploy.sentry import update_sentry_post_deploy
from commcare_cloud.commands.deploy.utils import record_deploy_start, record_deploy_failed, \
announce_deploy_success, create_release_tag, DeployContext
from commcare_cloud.commands.deploy.utils import (
announce_deploy_success,
confirm_environment_time,
create_release_tag,
record_deploy_start,
record_deploy_failed,
DeployContext
)
from commcare_cloud.user_utils import get_default_username
from commcare_cloud.commands.utils import timeago
from commcare_cloud.events import publish_deploy_event
Expand Down Expand Up @@ -47,6 +53,10 @@ def _format_time_ago(time):


def deploy_formplayer(environment, args):
if not confirm_environment_time(environment, quiet=args.quiet):
print(color_notice("Aborted by user"))
return 1

print(color_notice("\nPreparing to deploy Formplayer to: "), end="")
print(f"{environment.name}\n")

Expand Down
19 changes: 17 additions & 2 deletions src/commcare_cloud/commands/deploy/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from memoized import memoized

from commcare_cloud.alias import commcare_cloud
from commcare_cloud.cli_utils import ask
from commcare_cloud.colors import color_summary, color_error
from commcare_cloud.commands.deploy.slack import notify_slack_deploy_start, notify_slack_deploy_end
from commcare_cloud.user_utils import get_default_username
Expand Down Expand Up @@ -55,8 +56,8 @@ def record_deploy_start(environment, context):
def send_deploy_start_email(environment, context):
is_nonstandard_deploy_time = not within_maintenance_window(environment)
is_non_default_branch = (
context.revision != environment.fab_settings_config.default_branch and
context.revision is not None
context.revision != environment.fab_settings_config.default_branch
and context.revision is not None
)
env_name = environment.meta_config.deploy_env
subject = f"{context.user} has initiated a {context.service_name} deploy to {env_name}"
Expand Down Expand Up @@ -126,6 +127,20 @@ def send_email(environment, subject, message='', to_admins=True, recipients=None
show_command=False
)


def confirm_environment_time(environment, quiet=False):
if within_maintenance_window(environment):
return True
window = environment.fab_settings_config.acceptable_maintenance_window
d = datetime.now(pytz.timezone(window['timezone']))
message = (
"Whoa there bud! You're deploying '%s' outside the configured maintenance window. "
"The current local time is %s.\n"
"ARE YOU DOING SOMETHING EXCEPTIONAL THAT WARRANTS THIS?"
) % (environment.name, d.strftime("%-I:%M%p on %h. %d %Z"))
return ask(message, quiet=quiet)


def within_maintenance_window(environment):
window = environment.fab_settings_config.acceptable_maintenance_window
if window:
Expand Down

0 comments on commit 70ffb4f

Please sign in to comment.