Skip to content

Commit

Permalink
use cmdrunner singleton
Browse files Browse the repository at this point in the history
Signed-off-by: Peter Hunt <[email protected]>
  • Loading branch information
haircommander committed Jan 11, 2022
1 parent 2237f26 commit d0d8fb3
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 18 deletions.
4 changes: 2 additions & 2 deletions internal/config/node/systemd.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
package node

import (
"os/exec"
"sync"

"github.com/cri-o/cri-o/utils/cmdrunner"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -37,7 +37,7 @@ func SystemdHasAllowedCPUs() bool {
// systemdSupportsProperty checks whether systemd supports a property
// It returns an error if it does not.
func systemdSupportsProperty(property string) (bool, error) {
output, err := exec.Command("systemctl", "show", "-p", property, "systemd").Output()
output, err := cmdrunner.Command("systemctl", "show", "-p", property, "systemd").Output()
if err != nil {
return false, errors.Wrapf(err, "check systemd %s", property)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/config/nsmgr/nsmgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import (
"bytes"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"syscall"

nspkg "github.com/containernetworking/plugins/pkg/ns"
"github.com/containers/storage/pkg/idtools"
"github.com/cri-o/cri-o/utils"
"github.com/cri-o/cri-o/utils/cmdrunner"
"github.com/google/uuid"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -120,7 +120,7 @@ func (mgr *NamespaceManager) NewPodNamespaces(cfg *PodNamespacesConfig) ([]Names
}

logrus.Debugf("Calling pinns with %v", pinnsArgs)
output, err := exec.Command(mgr.pinnsPath, pinnsArgs...).CombinedOutput()
output, err := cmdrunner.Command(mgr.pinnsPath, pinnsArgs...).CombinedOutput()
if err != nil {
logrus.Warnf("Pinns %v failed: %s (%v)", pinnsArgs, string(output), err)
// cleanup the mounts
Expand Down
6 changes: 3 additions & 3 deletions internal/dbusmgr/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import (
"bufio"
"bytes"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"

systemdDbus "github.com/coreos/go-systemd/v22/dbus"
"github.com/cri-o/cri-o/utils/cmdrunner"
dbus "github.com/godbus/dbus/v5"
"github.com/opencontainers/runc/libcontainer/userns"
"github.com/pkg/errors"
Expand Down Expand Up @@ -55,7 +55,7 @@ func DetectUID() (int, error) {
if !userns.RunningInUserNS() {
return os.Getuid(), nil
}
b, err := exec.Command("busctl", "--user", "--no-pager", "status").CombinedOutput()
b, err := cmdrunner.Command("busctl", "--user", "--no-pager", "status").CombinedOutput()
if err != nil {
return -1, errors.Wrapf(err, "could not execute `busctl --user --no-pager status`: %q", string(b))
}
Expand Down Expand Up @@ -91,7 +91,7 @@ func DetectUserDbusSessionBusAddress() (string, error) {
return busAddress, nil
}
}
b, err := exec.Command("systemctl", "--user", "--no-pager", "show-environment").CombinedOutput()
b, err := cmdrunner.Command("systemctl", "--user", "--no-pager", "show-environment").CombinedOutput()
if err != nil {
return "", errors.Wrapf(err, "could not execute `systemctl --user --no-pager show-environment`, output=%q", string(b))
}
Expand Down
11 changes: 6 additions & 5 deletions internal/oci/runtime_oci.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/cri-o/cri-o/pkg/config"
"github.com/cri-o/cri-o/server/metrics"
"github.com/cri-o/cri-o/utils"
"github.com/cri-o/cri-o/utils/cmdrunner"
"github.com/fsnotify/fsnotify"
json "github.com/json-iterator/go"
rspec "github.com/opencontainers/runtime-spec/specs-go"
Expand Down Expand Up @@ -137,7 +138,7 @@ func (r *runtimeOCI) CreateContainer(ctx context.Context, c *Container, cgroupPa
"args": args,
}).Debugf("running conmon: %s", r.config.Conmon)

cmd := exec.Command(r.config.Conmon, args...) // nolint: gosec
cmd := cmdrunner.Command(r.config.Conmon, args...) // nolint: gosec
cmd.Dir = c.bundlePath
cmd.SysProcAttr = sysProcAttrPlatform()
cmd.Stdin = os.Stdin
Expand Down Expand Up @@ -353,7 +354,7 @@ func (r *runtimeOCI) ExecContainer(ctx context.Context, c *Container, cmd []stri

args := []string{rootFlag, r.root, "exec"}
args = append(args, "--process", processFile, c.ID())
execCmd := exec.Command(r.path, args...) // nolint: gosec
execCmd := cmdrunner.Command(r.path, args...) // nolint: gosec
if v, found := os.LookupEnv("XDG_RUNTIME_DIR"); found {
execCmd.Env = append(execCmd.Env, fmt.Sprintf("XDG_RUNTIME_DIR=%s", v))
}
Expand Down Expand Up @@ -487,7 +488,7 @@ func (r *runtimeOCI) ExecSyncContainer(ctx context.Context, c *Container, comman
"--exec-process-spec", processFile,
"--runtime-arg", fmt.Sprintf("%s=%s", rootFlag, r.root))

cmd := exec.Command(r.config.Conmon, args...) // nolint: gosec
cmd := cmdrunner.Command(r.config.Conmon, args...) // nolint: gosec

var stdoutBuf, stderrBuf bytes.Buffer
cmd.Stdout = &stdoutBuf
Expand Down Expand Up @@ -598,7 +599,7 @@ func (r *runtimeOCI) UpdateContainer(ctx context.Context, c *Container, res *rsp
return nil
}

cmd := exec.Command(r.path, rootFlag, r.root, "update", "--resources", "-", c.ID()) // nolint: gosec
cmd := cmdrunner.Command(r.path, rootFlag, r.root, "update", "--resources", "-", c.ID()) // nolint: gosec
var stdout bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &stdout
Expand Down Expand Up @@ -811,7 +812,7 @@ func (r *runtimeOCI) UpdateContainerStatus(ctx context.Context, c *Container) er
}

stateCmd := func() (*ContainerState, bool, error) {
cmd := exec.Command(r.path, rootFlag, r.root, "state", c.ID()) // nolint: gosec
cmd := cmdrunner.Command(r.path, rootFlag, r.root, "state", c.ID()) // nolint: gosec
if v, found := os.LookupEnv("XDG_RUNTIME_DIR"); found {
cmd.Env = append(cmd.Env, fmt.Sprintf("XDG_RUNTIME_DIR=%s", v))
}
Expand Down
3 changes: 2 additions & 1 deletion internal/runtimehandlerhooks/high_performance_hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/cri-o/cri-o/internal/log"
"github.com/cri-o/cri-o/internal/oci"
crioannotations "github.com/cri-o/cri-o/pkg/annotations"
"github.com/cri-o/cri-o/utils/cmdrunner"
"github.com/opencontainers/runc/libcontainer/cgroups"
"github.com/opencontainers/runc/libcontainer/cgroups/systemd"
"github.com/pkg/errors"
Expand Down Expand Up @@ -263,7 +264,7 @@ func setIRQLoadBalancing(c *oci.Container, enable bool, irqSmpAffinityFile, irqB
return nil
}
// run irqbalance in daemon mode, so this won't cause delay
cmd := exec.Command(irqBalancedName, "--oneshot")
cmd := cmdrunner.Command(irqBalancedName, "--oneshot")
additionalEnv := irqBalanceBannedCpus + "=" + newIRQBalanceSetting
cmd.Env = append(os.Environ(), additionalEnv)
return cmd.Run()
Expand Down
6 changes: 3 additions & 3 deletions internal/runtimehandlerhooks/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"strings"
"unicode"

"github.com/cri-o/cri-o/utils/cmdrunner"
"github.com/sirupsen/logrus"
"k8s.io/kubernetes/pkg/kubelet/cm/cpuset"
)
Expand Down Expand Up @@ -138,11 +138,11 @@ func UpdateIRQSmpAffinityMask(cpus, current string, set bool) (cpuMask, bannedCP
}

func restartIrqBalanceService() error {
return exec.Command("systemctl", "restart", "irqbalance").Run()
return cmdrunner.Command("systemctl", "restart", "irqbalance").Run()
}

func isServiceEnabled(serviceName string) bool {
cmd := exec.Command("systemctl", "is-enabled", serviceName)
cmd := cmdrunner.Command("systemctl", "is-enabled", serviceName)
status, err := cmd.CombinedOutput()
if err != nil {
logrus.Infof("Service %s is-enabled check returned with: %v", serviceName, err)
Expand Down
4 changes: 2 additions & 2 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"runtime"
"strconv"
Expand All @@ -18,6 +17,7 @@ import (

"github.com/containers/podman/v3/pkg/lookup"
"github.com/cri-o/cri-o/internal/dbusmgr"
"github.com/cri-o/cri-o/utils/cmdrunner"
securejoin "github.com/cyphar/filepath-securejoin"
"github.com/opencontainers/runc/libcontainer/user"
"github.com/pkg/errors"
Expand All @@ -32,7 +32,7 @@ import (
// ExecCmd executes a command with args and returns its output as a string along
// with an error, if any
func ExecCmd(name string, args ...string) (string, error) {
cmd := exec.Command(name, args...)
cmd := cmdrunner.Command(name, args...)
var stdout bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &stdout
Expand Down

0 comments on commit d0d8fb3

Please sign in to comment.