Use absolute path, when calling the crictl command
To work on CentOS, which has a weird path in sudopull/6220/head
parent
118451f4b2
commit
4d0446afa5
|
@ -267,7 +267,7 @@ func (r *Containerd) StopContainers(ids []string) error {
|
|||
|
||||
// ContainerLogCmd returns the command to retrieve the log for a container based on ID
|
||||
func (r *Containerd) ContainerLogCmd(id string, len int, follow bool) string {
|
||||
return criContainerLogCmd(id, len, follow)
|
||||
return criContainerLogCmd(r.Runner, id, len, follow)
|
||||
}
|
||||
|
||||
// SystemLogCmd returns the command to retrieve system logs
|
||||
|
|
|
@ -331,16 +331,26 @@ plugin_dirs = [
|
|||
`
|
||||
)
|
||||
|
||||
// getCommandPath returns the absolute path of a command
|
||||
func getCommandPath(cr CommandRunner, cmd string) string {
|
||||
rr, err := cr.RunCmd(exec.Command("which", cmd))
|
||||
if err != nil {
|
||||
return cmd
|
||||
}
|
||||
return strings.Split(rr.Stdout.String(), "\n")[0]
|
||||
}
|
||||
|
||||
// listCRIContainers returns a list of containers using crictl
|
||||
func listCRIContainers(cr CommandRunner, filter string) ([]string, error) {
|
||||
var err error
|
||||
var rr *command.RunResult
|
||||
state := "Running"
|
||||
crictl := getCommandPath(cr, "crictl")
|
||||
if filter != "" {
|
||||
c := exec.Command("sudo", "crictl", "ps", "-a", fmt.Sprintf("--name=%s", filter), fmt.Sprintf("--state=%s", state), "--quiet")
|
||||
c := exec.Command("sudo", crictl, "ps", "-a", fmt.Sprintf("--name=%s", filter), fmt.Sprintf("--state=%s", state), "--quiet")
|
||||
rr, err = cr.RunCmd(c)
|
||||
} else {
|
||||
rr, err = cr.RunCmd(exec.Command("sudo", "crictl", "ps", "-a", fmt.Sprintf("--state=%s", state), "--quiet"))
|
||||
rr, err = cr.RunCmd(exec.Command("sudo", crictl, "ps", "-a", fmt.Sprintf("--state=%s", state), "--quiet"))
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@ -361,7 +371,8 @@ func killCRIContainers(cr CommandRunner, ids []string) error {
|
|||
}
|
||||
glog.Infof("Killing containers: %s", ids)
|
||||
|
||||
args := append([]string{"crictl", "rm"}, ids...)
|
||||
crictl := getCommandPath(cr, "crictl")
|
||||
args := append([]string{crictl, "rm"}, ids...)
|
||||
c := exec.Command("sudo", args...)
|
||||
if _, err := cr.RunCmd(c); err != nil {
|
||||
return errors.Wrap(err, "kill cri containers.")
|
||||
|
@ -375,7 +386,9 @@ func stopCRIContainers(cr CommandRunner, ids []string) error {
|
|||
return nil
|
||||
}
|
||||
glog.Infof("Stopping containers: %s", ids)
|
||||
args := append([]string{"crictl", "rm"}, ids...)
|
||||
|
||||
crictl := getCommandPath(cr, "crictl")
|
||||
args := append([]string{crictl, "rm"}, ids...)
|
||||
c := exec.Command("sudo", args...)
|
||||
if _, err := cr.RunCmd(c); err != nil {
|
||||
return errors.Wrap(err, "stop cri containers")
|
||||
|
@ -428,9 +441,12 @@ func generateCRIOConfig(cr CommandRunner, imageRepository string) error {
|
|||
}
|
||||
|
||||
// criContainerLogCmd returns the command to retrieve the log for a container based on ID
|
||||
func criContainerLogCmd(id string, len int, follow bool) string {
|
||||
func criContainerLogCmd(cr CommandRunner, id string, len int, follow bool) string {
|
||||
crictl := getCommandPath(cr, "crictl")
|
||||
var cmd strings.Builder
|
||||
cmd.WriteString("sudo crictl logs ")
|
||||
cmd.WriteString("sudo ")
|
||||
cmd.WriteString(crictl)
|
||||
cmd.WriteString(" logs ")
|
||||
if len > 0 {
|
||||
cmd.WriteString(fmt.Sprintf("--tail %d ", len))
|
||||
}
|
||||
|
|
|
@ -170,7 +170,7 @@ func (r *CRIO) StopContainers(ids []string) error {
|
|||
|
||||
// ContainerLogCmd returns the command to retrieve the log for a container based on ID
|
||||
func (r *CRIO) ContainerLogCmd(id string, len int, follow bool) string {
|
||||
return criContainerLogCmd(id, len, follow)
|
||||
return criContainerLogCmd(r.Runner, id, len, follow)
|
||||
}
|
||||
|
||||
// SystemLogCmd returns the command to retrieve system logs
|
||||
|
|
|
@ -105,7 +105,7 @@ func New(c Config) (Manager, error) {
|
|||
// ContainerStatusCommand works across container runtimes with good formatting
|
||||
func ContainerStatusCommand() string {
|
||||
// Fallback to 'docker ps' if it fails (none driver)
|
||||
return "sudo crictl ps -a || sudo docker ps -a"
|
||||
return "sudo `which crictl || echo crictl` ps -a || sudo docker ps -a"
|
||||
}
|
||||
|
||||
// disableOthers disables all other runtimes except for me.
|
||||
|
|
|
@ -143,9 +143,11 @@ func (f *FakeRunner) RunCmd(cmd *exec.Cmd) (*command.RunResult, error) {
|
|||
switch bin {
|
||||
case "systemctl":
|
||||
return buffer(f.systemctl(args, root))
|
||||
case "which":
|
||||
return buffer(f.which(args, root))
|
||||
case "docker":
|
||||
return buffer(f.docker(args, root))
|
||||
case "crictl":
|
||||
case "crictl", "/usr/bin/crictl":
|
||||
return buffer(f.crictl(args, root))
|
||||
case "crio":
|
||||
return buffer(f.crio(args, root))
|
||||
|
@ -321,6 +323,13 @@ func (f *FakeRunner) systemctl(args []string, root bool) (string, error) { // no
|
|||
return out, nil
|
||||
}
|
||||
|
||||
// which is a fake implementation of which
|
||||
func (f *FakeRunner) which(args []string, root bool) (string, error) { // nolint result 0 (string) is always ""
|
||||
command := args[0]
|
||||
path := fmt.Sprintf("/usr/bin/%s", command)
|
||||
return path, nil
|
||||
}
|
||||
|
||||
func TestVersion(t *testing.T) {
|
||||
var tests = []struct {
|
||||
runtime string
|
||||
|
|
Loading…
Reference in New Issue