execute.post.ts 2.29 KB
Newer Older
王曜嵚 Wang Yaoqin's avatar
王曜嵚 Wang Yaoqin committed
1 2 3
import LogUtil from "~/server/utils/log"

export default defineEventHandler(async (event) => {
4
  let username = event.context.username
王曜嵚 Wang Yaoqin's avatar
王曜嵚 Wang Yaoqin committed
5 6
  try {
    let username = event.context.username
7
    LogUtil.clear(username)
王曜嵚 Wang Yaoqin's avatar
王曜嵚 Wang Yaoqin committed
8
    let docker = createDockerFactory(username)
9 10 11 12 13
    let container = await docker.checkContainer( username + '.node')
    let rowUser = await getPgTableData( 'user', username)
    let rowV2 = await getPgTableData( 'v2', username)
    let tenants = rowV2?.tenants
    let port = rowUser?.["port"]
王曜嵚 Wang Yaoqin's avatar
王曜嵚 Wang Yaoqin committed
14 15

    LogUtil.printInfo(username, `程序初始化中......`)
16
    await changeProjectStatus( 'v2', username, 'status', 'running')
王曜嵚 Wang Yaoqin's avatar
王曜嵚 Wang Yaoqin committed
17 18 19 20 21 22 23 24 25 26 27 28 29
    if (!container) {
      throw new Error('没有创建容器,请先初始化容器')
    }

    // 有可能本身就关闭了
    try {
      await docker.execContainerCommand({ container, cmd: 'nginx -s stop'})
    } catch (err) {}

    await docker.preStartSystem({ container, username, platform: 'v2'})
    await copyAndCreateNginxConfInDocker(username)
    await docker.execContainerCommand({ container, cmd: 'nginx'})

30 31
    await docker.execContainerCommand({ container, cmd: 'pm2 start --name gateway --no-autorestart java -- -Xms256m -Xmx256m -XX:+UseG1GC --add-opens java.base/sun.util.locale.provider=ALL-UNNAMED -DbootUserName=root -jar logwire-gateway-starter.jar', dir: '/var/logwire-backend/build-output/gateway', quiet: true })
    await docker.execContainerCommand({ container, cmd: `pm2 start --name backend --no-autorestart java -- -Xms256m -Xmx256m -XX:+UseG1GC -DVALIDATE_XML_ENABLED=false --add-opens java.base/sun.util.locale.provider=ALL-UNNAMED --add-opens java.desktop/sun.awt=ALL-UNNAMED -DbootUserName=root -jar logwire-backend-starter.jar`, dir: '/var/logwire-backend/build-output/backend', quiet: true })
王曜嵚 Wang Yaoqin's avatar
王曜嵚 Wang Yaoqin committed
32 33

    LogUtil.printInfo(username, `程序运行中......`)
34 35
    LogUtil.printInfo(username, `请代理后端请求到 <strong>${HOST}:${port}</strong> 上`)
    LogUtil.printInfo(username, `请设置开发环境域名和端口号 <strong>${tenants?.host}</strong> 上`)
王曜嵚 Wang Yaoqin's avatar
王曜嵚 Wang Yaoqin committed
36

37
    await changeProjectStatus( 'v2', username, 'status', 'launched')
王曜嵚 Wang Yaoqin's avatar
王曜嵚 Wang Yaoqin committed
38
  } catch (err) {
39
    await changeProjectStatus( 'v2', username, 'status', 'compiled')
王曜嵚 Wang Yaoqin's avatar
王曜嵚 Wang Yaoqin committed
40 41 42 43 44
    LogUtil.printError(username, (err instanceof Error ? err.message : JSON.stringify(err)))
    setResponseStatus(event, 500)
    return err
  }
})