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

Release automation with the new redeploy logic #3120

Merged
merged 1 commit into from
Oct 22, 2024
Merged
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
89 changes: 16 additions & 73 deletions scripts/redeploy-release-prep-tenant.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,36 +73,16 @@ def update_tenant(token: str, tenant_id: str, new_version: str) -> None:
},
},
},
"desired_state": "available",
}

response = requests.patch(url, json=data, headers=headers)
response = requests.patch(
url, json=data, headers=headers, params={"force": True}
)
if response.status_code != 200:
raise requests.HTTPError("There was a problem updating the tenant.")


def deactivate_tenant(token: str, tenant_id: str) -> None:
"""Deactivate a specific tenant.

Args:
token: The access token for authentication.
tenant_id: The ID of the tenant to deactivate.

Raises:
requests.HTTPError: If the API request fails.
"""
url = f"https://staging.cloudapi.zenml.io/tenants/{tenant_id}/deactivate"
headers = {
"Authorization": f"Bearer {token}",
"accept": "application/json",
}
response = requests.patch(url, headers=headers)

if response.status_code != 200:
raise requests.HTTPError(
"There was a problem deactivating the tenant."
)


def get_tenant_status(token: str, tenant_id: str) -> str:
"""Get the current status of a specific tenant.

Expand All @@ -129,27 +109,6 @@ def get_tenant_status(token: str, tenant_id: str) -> str:
return response.json()["status"]


def redeploy_tenant(token: str, tenant_id: str) -> None:
"""Redeploy a specific tenant.

Args:
token: The access token for authentication.
tenant_id: The ID of the tenant to redeploy.

Raises:
requests.HTTPError: If the API request fails.
"""
url = f"https://staging.cloudapi.zenml.io/tenants/{tenant_id}/deploy"
headers = {
"Authorization": f"Bearer {token}",
"accept": "application/json",
}
response = requests.patch(url, headers=headers)

if response.status_code != 200:
raise requests.HTTPError("There was a problem redeploying the tenant.")


def main() -> None:
"""Main function to orchestrate the tenant management process.

Expand All @@ -165,6 +124,10 @@ def main() -> None:
EnvironmentError: If required environment variables are missing.
requests.HTTPError: If any API requests fail.
"""
# Constants
timeout = 600
sleep_period = 20

# Get environment variables
client_id = os.environ.get("CLOUD_STAGING_CLIENT_ID")
client_secret = os.environ.get("CLOUD_STAGING_CLIENT_SECRET")
Expand All @@ -178,45 +141,25 @@ def main() -> None:
token = get_token(client_id, client_secret)
print("Fetched the token.")

# Deactivate the tenant
status = get_tenant_status(token, tenant_id)
if status == "available":
deactivate_tenant(token, tenant_id)
print("Tenant deactivation initiated.")

# Wait until it's deactivated
time.sleep(10)

status = get_tenant_status(token, tenant_id)
while status == "pending":
print(f"Waiting... Current tenant status: {status}.")
time.sleep(20)
status = get_tenant_status(token, tenant_id)

if status != "deactivated":
raise RuntimeError("Tenant deactivation failed.")
print("Tenant deactivated.")

# Update the tenant
update_tenant(token, tenant_id, new_version)
print("Tenant updated.")

# Redeploy the tenant
redeploy_tenant(token, tenant_id)
print("Tenant redeployment initiated.")

# Wait until it's deployed
time.sleep(10)

# Check the status
status = get_tenant_status(token, tenant_id)
while status == "pending":
print(f"Waiting... Current tenant status: {status}.")
time.sleep(20)
time.sleep(sleep_period)
status = get_tenant_status(token, tenant_id)

timeout -= sleep_period
if timeout <= 0:
raise RuntimeError(
"Timed out! The tenant could be stuck in a `pending` state."
)

if status != "available":
raise RuntimeError("Tenant redeployment failed.")
print("Tenant redeployed.")


if __name__ == "__main__":
Expand Down
Loading