| 26 | } |
| 27 | |
| 28 | func (*containerRestarter) RestartContainer() error { |
| 29 | pidFilePath := restart.ProcessIDFilePath |
| 30 | |
| 31 | // check if restart script is there |
| 32 | _, err := os.Stat(restart.LegacyScriptPath) |
| 33 | if err == nil { |
| 34 | pidFilePath = restart.LegacyProcessIDFilePath |
| 35 | } else { |
| 36 | // check if restart script is there |
| 37 | _, err = os.Stat(restart.ScriptPath) |
| 38 | if err != nil { |
| 39 | if os.IsNotExist(err) { |
| 40 | return fmt.Errorf("the restart container utility script is not present in the container. Please make sure '%s' is in your container and wrapping the entrypoint", restart.ScriptPath) |
| 41 | } |
| 42 | |
| 43 | return errors.Wrap(err, "cannot access restart helper script") |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | // read current active process id |
| 48 | pgidBytes, err := os.ReadFile(pidFilePath) |
| 49 | if err != nil { |
| 50 | if os.IsNotExist(err) { |
| 51 | return nil |
| 52 | } |
| 53 | |
| 54 | return errors.Wrap(err, "cannot access restart process id file") |
| 55 | } |
| 56 | |
| 57 | // convert to int |
| 58 | pgid, err := strconv.Atoi(strings.TrimSpace(string(pgidBytes))) |
| 59 | if err != nil { |
| 60 | return err |
| 61 | } |
| 62 | |
| 63 | // delete the pid file |
| 64 | err = os.Remove(pidFilePath) |
| 65 | if err != nil { |
| 66 | // someone else was faster than we were |
| 67 | if os.IsNotExist(err) { |
| 68 | return nil |
| 69 | } |
| 70 | |
| 71 | return errors.Wrap(err, "cannot delete restart process id file") |
| 72 | } |
| 73 | |
| 74 | // kill the process group |
| 75 | procPath := "/proc/" + strconv.Itoa(pgid) |
| 76 | |
| 77 | for _, sig := range []syscall.Signal{syscall.SIGINT, syscall.SIGTERM, syscall.SIGKILL} { |
| 78 | stderrlog.Infof("Sending %s signal...", sig.String()) |
| 79 | err = syscall.Kill(-pgid, sig) |
| 80 | if err != nil { |
| 81 | return nil |
| 82 | } |
| 83 | err = wait.PollUntilContextTimeout(context.TODO(), time.Second, 5*time.Second, true, func(_ context.Context) (done bool, err error) { |
| 84 | _, err = os.Stat(procPath) |
| 85 | return os.IsNotExist(err), nil |