( deployment: Omit< z.infer<typeof apiCreateDeploymentCompose>, "deploymentId" | "createdAt" | "status" | "logPath" >, )
| 274 | }; |
| 275 | |
| 276 | export const createDeploymentCompose = async ( |
| 277 | deployment: Omit< |
| 278 | z.infer<typeof apiCreateDeploymentCompose>, |
| 279 | "deploymentId" | "createdAt" | "status" | "logPath" |
| 280 | >, |
| 281 | ) => { |
| 282 | const compose = await findComposeById(deployment.composeId); |
| 283 | await removeLastTenDeployments( |
| 284 | deployment.composeId, |
| 285 | "compose", |
| 286 | compose.serverId, |
| 287 | ); |
| 288 | try { |
| 289 | const { LOGS_PATH } = paths(!!compose.serverId); |
| 290 | const formattedDateTime = format(new Date(), "yyyy-MM-dd:HH:mm:ss"); |
| 291 | const fileName = `${compose.appName}-${formattedDateTime}.log`; |
| 292 | const logFilePath = path.join(LOGS_PATH, compose.appName, fileName); |
| 293 | |
| 294 | if (compose.serverId) { |
| 295 | const server = await findServerById(compose.serverId); |
| 296 | |
| 297 | const command = ` |
| 298 | mkdir -p ${LOGS_PATH}/${compose.appName}; |
| 299 | echo "Initializing deployment\n" >> ${logFilePath}; |
| 300 | `; |
| 301 | |
| 302 | await execAsyncRemote(server.serverId, command); |
| 303 | } else { |
| 304 | await fsPromises.mkdir(path.join(LOGS_PATH, compose.appName), { |
| 305 | recursive: true, |
| 306 | }); |
| 307 | await fsPromises.writeFile(logFilePath, "Initializing deployment\n"); |
| 308 | } |
| 309 | |
| 310 | const deploymentCreate = await db |
| 311 | .insert(deployments) |
| 312 | .values({ |
| 313 | composeId: deployment.composeId, |
| 314 | title: deployment.title || "Deployment", |
| 315 | description: deployment.description || "", |
| 316 | status: "running", |
| 317 | logPath: logFilePath, |
| 318 | startedAt: new Date().toISOString(), |
| 319 | }) |
| 320 | .returning(); |
| 321 | if (deploymentCreate.length === 0 || !deploymentCreate[0]) { |
| 322 | throw new TRPCError({ |
| 323 | code: "BAD_REQUEST", |
| 324 | message: "Error creating the deployment", |
| 325 | }); |
| 326 | } |
| 327 | return deploymentCreate[0]; |
| 328 | } catch (error) { |
| 329 | await db |
| 330 | .insert(deployments) |
| 331 | .values({ |
| 332 | composeId: deployment.composeId, |
| 333 | title: deployment.title || "Deployment", |
no test coverage detected