-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy_and_init.py
More file actions
91 lines (75 loc) · 2.83 KB
/
deploy_and_init.py
File metadata and controls
91 lines (75 loc) · 2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
"""
Script to deploy to Render and initialize the production database.
This script will:
1. Deploy the application to Render
2. Initialize the database with required data
"""
import os
import sys
import subprocess
import time
import requests
import json
import logging
# Set up logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
def run_command(command):
"""Run a shell command and return the output."""
logger.info(f"Running command: {command}")
try:
result = subprocess.run(command, shell=True, check=True, capture_output=True, text=True)
logger.info(f"Command output: {result.stdout}")
return result.stdout
except subprocess.CalledProcessError as e:
logger.error(f"Command failed with error: {e.stderr}")
return None
def deploy_to_render():
"""Deploy the application to Render."""
logger.info("Deploying to Render...")
# Check if render-cli is installed
if not run_command("render --version"):
logger.error("render-cli is not installed. Please install it first.")
return False
# Deploy to Render
deploy_result = run_command("render deploy")
if not deploy_result:
logger.error("Deployment to Render failed.")
return False
logger.info("Deployment to Render completed successfully.")
return True
def wait_for_deployment(minutes=5):
"""Wait for the deployment to complete."""
logger.info(f"Waiting for deployment to complete (up to {minutes} minutes)...")
for i in range(minutes * 60):
if i % 30 == 0: # Log every 30 seconds
logger.info(f"Still waiting... ({i//60} minutes, {i%60} seconds)")
time.sleep(1)
logger.info("Wait time completed.")
def initialize_database():
"""Initialize the production database with required data."""
logger.info("Initializing production database...")
# Run the initialization script
init_result = run_command("python init_production_db.py")
if not init_result:
logger.error("Database initialization failed.")
return False
logger.info("Database initialization completed successfully.")
return True
def main():
"""Main function to deploy and initialize the database."""
logger.info("Starting deployment and initialization process...")
# Deploy to Render
if not deploy_to_render():
logger.error("Deployment failed. Exiting.")
return 1
# Wait for deployment to complete
wait_for_deployment(minutes=5)
# Initialize the database
if not initialize_database():
logger.error("Database initialization failed. Exiting.")
return 1
logger.info("Deployment and initialization completed successfully.")
return 0
if __name__ == "__main__":
sys.exit(main())