dragxd commited on
Commit
1274ac5
·
verified ·
1 Parent(s): fda9c8b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -14
app.py CHANGED
@@ -8,6 +8,9 @@ import requests
8
  from urllib.parse import urlencode
9
  import aiofiles
10
  import asyncio
 
 
 
11
 
12
  app = FastAPI()
13
  deployments = {}
@@ -48,22 +51,53 @@ class RepoSelectRequest(BaseModel):
48
  repo_full_name: str
49
 
50
  async def simulate_deployment(deploy_id, user_id, repo_full_name):
51
- logs = [
52
- f"Starting deployment for {repo_full_name}...",
53
- "Cloning repository...",
54
- "Installing dependencies...",
55
- "Setting environment variables...",
56
- "Running build...",
57
- "Deploying application...",
58
- "Finalizing...",
59
- "Deployment successful!"
60
- ]
61
  deploy_logs[deploy_id] = []
62
  deploy_status[deploy_id] = "deploying"
63
- for log in logs:
64
- deploy_logs[deploy_id].append(log)
65
- await asyncio.sleep(2) # Simulate time between log lines
66
- deploy_status[deploy_id] = "success"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67
 
68
  @app.post("/deploy")
69
  async def deploy(req: DeployRequest, background_tasks: BackgroundTasks):
 
8
  from urllib.parse import urlencode
9
  import aiofiles
10
  import asyncio
11
+ import subprocess
12
+ import tempfile
13
+ import shutil
14
 
15
  app = FastAPI()
16
  deployments = {}
 
51
  repo_full_name: str
52
 
53
  async def simulate_deployment(deploy_id, user_id, repo_full_name):
 
 
 
 
 
 
 
 
 
 
54
  deploy_logs[deploy_id] = []
55
  deploy_status[deploy_id] = "deploying"
56
+ temp_dir = tempfile.mkdtemp()
57
+ try:
58
+ # 1. Clone the repo
59
+ cmd = ["git", "clone", f"https://github.com/{repo_full_name}.git", temp_dir]
60
+ proc = await asyncio.create_subprocess_exec(*cmd, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.STDOUT)
61
+ while True:
62
+ line = await proc.stdout.readline()
63
+ if not line:
64
+ break
65
+ deploy_logs[deploy_id].append(line.decode().rstrip())
66
+ await proc.wait()
67
+ # 2. Install dependencies (if requirements.txt exists)
68
+ req_path = f"{temp_dir}/requirements.txt"
69
+ if os.path.exists(req_path):
70
+ cmd = ["pip", "install", "-r", req_path]
71
+ proc = await asyncio.create_subprocess_exec(*cmd, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.STDOUT)
72
+ while True:
73
+ line = await proc.stdout.readline()
74
+ if not line:
75
+ break
76
+ deploy_logs[deploy_id].append(line.decode().rstrip())
77
+ await proc.wait()
78
+ # 3. Set environment variables from .env (if uploaded)
79
+ env_content = env_files.get((user_id, repo_full_name))
80
+ if env_content:
81
+ env_path = f"{temp_dir}/.env"
82
+ with open(env_path, "w") as f:
83
+ f.write(env_content)
84
+ deploy_logs[deploy_id].append(".env file written.")
85
+ # 4. (Optional) Run build or start script if present
86
+ # For demo, just list files
87
+ cmd = ["ls", "-l", temp_dir]
88
+ proc = await asyncio.create_subprocess_exec(*cmd, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.STDOUT)
89
+ while True:
90
+ line = await proc.stdout.readline()
91
+ if not line:
92
+ break
93
+ deploy_logs[deploy_id].append(line.decode().rstrip())
94
+ await proc.wait()
95
+ deploy_status[deploy_id] = "success"
96
+ except Exception as e:
97
+ deploy_logs[deploy_id].append(f"Error: {e}")
98
+ deploy_status[deploy_id] = "failed"
99
+ finally:
100
+ shutil.rmtree(temp_dir, ignore_errors=True)
101
 
102
  @app.post("/deploy")
103
  async def deploy(req: DeployRequest, background_tasks: BackgroundTasks):