mirror of
https://github.com/NVIDIA/nvidia-container-toolkit
synced 2025-06-26 18:18:24 +00:00
@@ -18,6 +18,8 @@ package container
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
|
||||
"github.com/NVIDIA/nvidia-container-toolkit/pkg/config/engine"
|
||||
"github.com/NVIDIA/nvidia-container-toolkit/tools/container/operator"
|
||||
@@ -25,6 +27,12 @@ import (
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
const (
|
||||
restartModeNone = "none"
|
||||
restartModeSignal = "signal"
|
||||
restartModeSystemd = "systemd"
|
||||
)
|
||||
|
||||
// Options defines the shared options for the CLIs to configure containers runtimes.
|
||||
type Options struct {
|
||||
Config string
|
||||
@@ -121,3 +129,41 @@ func (o Options) RevertConfig(cfg engine.Interface) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Restart restarts the specified service
|
||||
func (o Options) Restart(service string, withSignal func(string) error) error {
|
||||
switch o.RestartMode {
|
||||
case restartModeNone:
|
||||
logrus.Warnf("Skipping restart of %v due to --restart-mode=%v", service, o.RestartMode)
|
||||
return nil
|
||||
case restartModeSignal:
|
||||
return withSignal(o.Socket)
|
||||
case restartModeSystemd:
|
||||
return o.SystemdRestart(service)
|
||||
}
|
||||
|
||||
return fmt.Errorf("invalid restart mode specified: %v", o.RestartMode)
|
||||
}
|
||||
|
||||
// SystemdRestart restarts the specified service using systemd
|
||||
func (o Options) SystemdRestart(service string) error {
|
||||
var args []string
|
||||
var msg string
|
||||
if o.HostRootMount != "" {
|
||||
msg = " on host"
|
||||
args = append(args, "chroot", o.HostRootMount)
|
||||
}
|
||||
args = append(args, "systemctl", "restart", service)
|
||||
|
||||
logrus.Infof("Restarting %v%v using systemd: %v", service, msg, args)
|
||||
|
||||
cmd := exec.Command(args[0], args[1:]...)
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
err := cmd.Run()
|
||||
if err != nil {
|
||||
return fmt.Errorf("error restarting %v using systemd: %v", service, err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user