(runtime *model.Runtime, oldImageID string, oldEnv string, rebuild bool)
| 328 | } |
| 329 | |
| 330 | func buildRuntime(runtime *model.Runtime, oldImageID string, oldEnv string, rebuild bool) { |
| 331 | runtimePath := runtime.GetPath() |
| 332 | composePath := runtime.GetComposePath() |
| 333 | logPath := path.Join(runtimePath, "build.log") |
| 334 | |
| 335 | logFile, err := os.OpenFile(logPath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, constant.FilePerm) |
| 336 | if err != nil { |
| 337 | global.LOG.Errorf("failed to open log file: %v", err) |
| 338 | return |
| 339 | } |
| 340 | defer func() { |
| 341 | _ = logFile.Close() |
| 342 | }() |
| 343 | |
| 344 | newPHPVersion := getRuntimeEnv(runtime.Env, "PHP_VERSION") |
| 345 | oldPHPVersion := getRuntimeEnv(oldEnv, "PHP_VERSION") |
| 346 | if newPHPVersion != oldPHPVersion { |
| 347 | _ = os.Rename(path.Join(runtimePath, "extensions", getExtensionDir(oldPHPVersion)), path.Join(runtimePath, "extensions", getExtensionDir(newPHPVersion))) |
| 348 | } |
| 349 | |
| 350 | ctx, cancel := context.WithTimeout(context.Background(), 1*time.Hour) |
| 351 | defer cancel() |
| 352 | |
| 353 | dockerCommand := global.CONF.DockerConfig.Command |
| 354 | var cmd *exec.Cmd |
| 355 | if dockerCommand == "docker-compose" { |
| 356 | cmd = exec.CommandContext(ctx, "docker-compose", "-f", composePath, "build") |
| 357 | } else { |
| 358 | cmd = exec.CommandContext(ctx, "docker", "compose", "-f", composePath, "build") |
| 359 | } |
| 360 | cmd.Stdout = logFile |
| 361 | var stderrBuf bytes.Buffer |
| 362 | multiWriterStderr := io.MultiWriter(&stderrBuf, logFile) |
| 363 | cmd.Stderr = multiWriterStderr |
| 364 | |
| 365 | err = cmd.Run() |
| 366 | if err != nil { |
| 367 | runtime.Status = constant.StatusError |
| 368 | runtime.Message = buserr.New("ErrImageBuildErr").Error() + ":" + stderrBuf.String() |
| 369 | if errors.Is(ctx.Err(), context.DeadlineExceeded) { |
| 370 | runtime.Message = buserr.New("ErrImageBuildErr").Error() + ":" + buserr.New("ErrCmdTimeout").Error() |
| 371 | } else { |
| 372 | runtime.Message = buserr.New("ErrImageBuildErr").Error() + ":" + stderrBuf.String() |
| 373 | } |
| 374 | _ = runtimeRepo.Save(runtime) |
| 375 | return |
| 376 | } |
| 377 | if err = runComposeCmdWithLog(constant.RuntimeDown, runtime.GetComposePath(), runtime.GetLogPath()); err != nil { |
| 378 | return |
| 379 | } |
| 380 | client, err := docker.NewClient() |
| 381 | if err != nil { |
| 382 | _, _ = logFile.WriteString(fmt.Sprintf("failed to connect to docker client: %v", err)) |
| 383 | return |
| 384 | } |
| 385 | runtime.Message = "" |
| 386 | if rebuild && runtime.ID > 0 { |
| 387 | extensionsStr := getRuntimeEnv(runtime.Env, "PHP_EXTENSIONS") |
no test coverage detected