(ctx context.Context, logger *zap.Logger, opts models.SetupOptions)
| 1330 | } |
| 1331 | |
| 1332 | func (a *AgentClient) startInDocker(ctx context.Context, logger *zap.Logger, opts models.SetupOptions) error { |
| 1333 | keployAlias, err := kdocker.GetKeployDockerAlias(ctx, logger, &config.Config{ |
| 1334 | InstallationID: a.conf.InstallationID, |
| 1335 | }, opts) |
| 1336 | if err != nil { |
| 1337 | utils.LogError(logger, err, "failed to prepare docker command and environment") |
| 1338 | return err |
| 1339 | } |
| 1340 | |
| 1341 | cmd := kdocker.PrepareDockerCommand(ctx, keployAlias) |
| 1342 | |
| 1343 | cmd.Cancel = func() error { |
| 1344 | logger.Debug("Context cancelled. Explicitly stopping the 'keploy-v3' Docker container.") |
| 1345 | |
| 1346 | containerName := opts.KeployContainer |
| 1347 | |
| 1348 | // Try stopping the container without sudo first (works if user is in docker group) |
| 1349 | stopCmd := exec.Command("docker", "stop", containerName) |
| 1350 | if output, err := stopCmd.CombinedOutput(); err != nil { |
| 1351 | // If that fails on Linux, try with sudo -n (non-interactive, won't prompt for password) |
| 1352 | if runtime.GOOS == "linux" { |
| 1353 | logger.Debug("docker stop without sudo failed, trying with sudo -n", zap.Error(err)) |
| 1354 | stopCmd = exec.Command("sudo", "-n", "docker", "stop", containerName) |
| 1355 | if output, err := stopCmd.CombinedOutput(); err != nil { |
| 1356 | logger.Debug("Could not stop the docker container. It may have already stopped.", |
| 1357 | zap.String("container", containerName), |
| 1358 | zap.Error(err), |
| 1359 | zap.String("output", string(output))) |
| 1360 | } else { |
| 1361 | logger.Debug("Successfully sent stop command to the container.", zap.String("container", containerName)) |
| 1362 | } |
| 1363 | } else { |
| 1364 | logger.Debug("Could not stop the docker container. It may have already stopped.", |
| 1365 | zap.String("container", containerName), |
| 1366 | zap.Error(err), |
| 1367 | zap.String("output", string(output))) |
| 1368 | } |
| 1369 | } else { |
| 1370 | logger.Debug("Successfully sent stop command to the container.", zap.String("container", containerName)) |
| 1371 | } |
| 1372 | |
| 1373 | if cmd.Process != nil { |
| 1374 | return utils.SendSignal(logger, cmd.Process.Pid, syscall.SIGKILL) |
| 1375 | } |
| 1376 | return nil |
| 1377 | } |
| 1378 | |
| 1379 | logger.Debug("running the following command to start agent in docker", zap.String("command", cmd.String())) |
| 1380 | |
| 1381 | // Check if we need PTY for interactive input (e.g., sudo password on Linux) |
| 1382 | if agentUtils.NeedsPTY() { |
| 1383 | return a.startInDockerWithPTY(ctx, logger, cmd) |
| 1384 | } |
| 1385 | |
| 1386 | cmd.Stdout = os.Stdout |
| 1387 | cmd.Stderr = os.Stderr |
| 1388 | |
| 1389 | if err := cmd.Run(); err != nil { |
no test coverage detected