mirror of
https://github.com/ceph/ceph-csi.git
synced 2024-11-22 14:20:19 +00:00
util: use context.Context for logging in ExecCommand
All calls to util.ExecCommand() now pass the context.Context. In some cases this is not possible or needed, and util.ExecCommand() will not log the command. This should make debugging easier when command executions fail. Signed-off-by: Niels de Vos <ndevos@redhat.com>
This commit is contained in:
parent
bb4f1c7c9d
commit
ddac66d76b
@ -31,12 +31,12 @@ import (
|
|||||||
type volumeID string
|
type volumeID string
|
||||||
|
|
||||||
func execCommandErr(ctx context.Context, program string, args ...string) error {
|
func execCommandErr(ctx context.Context, program string, args ...string) error {
|
||||||
_, _, err := util.ExecCommand(program, args...)
|
_, _, err := util.ExecCommand(ctx, program, args...)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func execCommandJSON(ctx context.Context, v interface{}, program string, args ...string) error {
|
func execCommandJSON(ctx context.Context, v interface{}, program string, args ...string) error {
|
||||||
stdout, _, err := util.ExecCommand(program, args...)
|
stdout, _, err := util.ExecCommand(ctx, program, args...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -52,6 +52,7 @@ func getVolumeRootPathCephDeprecated(volID volumeID) string {
|
|||||||
|
|
||||||
func getVolumeRootPathCeph(ctx context.Context, volOptions *volumeOptions, cr *util.Credentials, volID volumeID) (string, error) {
|
func getVolumeRootPathCeph(ctx context.Context, volOptions *volumeOptions, cr *util.Credentials, volID volumeID) (string, error) {
|
||||||
stdout, stderr, err := util.ExecCommand(
|
stdout, stderr, err := util.ExecCommand(
|
||||||
|
ctx,
|
||||||
"ceph",
|
"ceph",
|
||||||
"fs",
|
"fs",
|
||||||
"subvolume",
|
"subvolume",
|
||||||
|
@ -167,7 +167,7 @@ func mountFuse(ctx context.Context, mountPoint string, cr *util.Credentials, vol
|
|||||||
args = append(args, "--client_mds_namespace="+volOptions.FsName)
|
args = append(args, "--client_mds_namespace="+volOptions.FsName)
|
||||||
}
|
}
|
||||||
|
|
||||||
_, stderr, err := util.ExecCommand("ceph-fuse", args[:]...)
|
_, stderr, err := util.ExecCommand(ctx, "ceph-fuse", args[:]...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -77,14 +77,14 @@ type nbdDeviceInfo struct {
|
|||||||
|
|
||||||
// rbdGetDeviceList queries rbd about mapped devices and returns a list of rbdDeviceInfo
|
// rbdGetDeviceList queries rbd about mapped devices and returns a list of rbdDeviceInfo
|
||||||
// It will selectively list devices mapped using krbd or nbd as specified by accessType.
|
// It will selectively list devices mapped using krbd or nbd as specified by accessType.
|
||||||
func rbdGetDeviceList(accessType string) ([]rbdDeviceInfo, error) {
|
func rbdGetDeviceList(ctx context.Context, accessType string) ([]rbdDeviceInfo, error) {
|
||||||
// rbd device list --format json --device-type [krbd|nbd]
|
// rbd device list --format json --device-type [krbd|nbd]
|
||||||
var (
|
var (
|
||||||
rbdDeviceList []rbdDeviceInfo
|
rbdDeviceList []rbdDeviceInfo
|
||||||
nbdDeviceList []nbdDeviceInfo
|
nbdDeviceList []nbdDeviceInfo
|
||||||
)
|
)
|
||||||
|
|
||||||
stdout, _, err := util.ExecCommand(rbd, "device", "list", "--format="+"json", "--device-type", accessType)
|
stdout, _, err := util.ExecCommand(ctx, rbd, "device", "list", "--format="+"json", "--device-type", accessType)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error getting device list from rbd for devices of type (%s): (%v)", accessType, err)
|
return nil, fmt.Errorf("error getting device list from rbd for devices of type (%s): (%v)", accessType, err)
|
||||||
}
|
}
|
||||||
@ -122,7 +122,7 @@ func findDeviceMappingImage(ctx context.Context, pool, image string, useNbdDrive
|
|||||||
accessType = accessTypeNbd
|
accessType = accessTypeNbd
|
||||||
}
|
}
|
||||||
|
|
||||||
rbdDeviceList, err := rbdGetDeviceList(accessType)
|
rbdDeviceList, err := rbdGetDeviceList(ctx, accessType)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
klog.Warningf(util.Log(ctx, "failed to determine if image (%s/%s) is mapped to a device (%v)"), pool, image, err)
|
klog.Warningf(util.Log(ctx, "failed to determine if image (%s/%s) is mapped to a device (%v)"), pool, image, err)
|
||||||
return "", false
|
return "", false
|
||||||
@ -159,13 +159,13 @@ func checkRbdNbdTools() bool {
|
|||||||
_, err := os.Stat(fmt.Sprintf("/sys/module/%s", moduleNbd))
|
_, err := os.Stat(fmt.Sprintf("/sys/module/%s", moduleNbd))
|
||||||
if os.IsNotExist(err) {
|
if os.IsNotExist(err) {
|
||||||
// try to load the module
|
// try to load the module
|
||||||
_, _, err = util.ExecCommand("modprobe", moduleNbd)
|
_, _, err = util.ExecCommand(context.TODO(), "modprobe", moduleNbd)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
util.ExtendedLogMsg("rbd-nbd: nbd modprobe failed with error %v", err)
|
util.ExtendedLogMsg("rbd-nbd: nbd modprobe failed with error %v", err)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if _, _, err := util.ExecCommand(rbdTonbd, "--version"); err != nil {
|
if _, _, err := util.ExecCommand(context.TODO(), rbdTonbd, "--version"); err != nil {
|
||||||
util.ExtendedLogMsg("rbd-nbd: running rbd-nbd --version failed with error %v", err)
|
util.ExtendedLogMsg("rbd-nbd: running rbd-nbd --version failed with error %v", err)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
@ -229,7 +229,7 @@ func createPath(ctx context.Context, volOpt *rbdVolume, cr *util.Credentials) (s
|
|||||||
mapOptions = append(mapOptions, "--read-only")
|
mapOptions = append(mapOptions, "--read-only")
|
||||||
}
|
}
|
||||||
// Execute map
|
// Execute map
|
||||||
stdout, stderr, err := util.ExecCommand(rbd, mapOptions...)
|
stdout, stderr, err := util.ExecCommand(ctx, rbd, mapOptions...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
klog.Warningf(util.Log(ctx, "rbd: map error %v, rbd output: %s"), err, string(stderr))
|
klog.Warningf(util.Log(ctx, "rbd: map error %v, rbd output: %s"), err, string(stderr))
|
||||||
// unmap rbd image if connection timeout
|
// unmap rbd image if connection timeout
|
||||||
@ -306,7 +306,7 @@ func detachRBDImageOrDeviceSpec(ctx context.Context, imageOrDeviceSpec string, i
|
|||||||
}
|
}
|
||||||
options := []string{"unmap", "--device-type", accessType, imageOrDeviceSpec}
|
options := []string{"unmap", "--device-type", accessType, imageOrDeviceSpec}
|
||||||
|
|
||||||
_, stderr, err := util.ExecCommand(rbd, options...)
|
_, stderr, err := util.ExecCommand(ctx, rbd, options...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// Messages for krbd and nbd differ, hence checking either of them for missing mapping
|
// Messages for krbd and nbd differ, hence checking either of them for missing mapping
|
||||||
// This is not applicable when a device path is passed in
|
// This is not applicable when a device path is passed in
|
||||||
|
@ -303,7 +303,7 @@ func addRbdManagerTask(ctx context.Context, pOpts *rbdVolume, arg []string) (boo
|
|||||||
args = append(args, arg...)
|
args = append(args, arg...)
|
||||||
util.DebugLog(ctx, "executing %v for image (%s) using mon %s, pool %s", args, pOpts.RbdImageName, pOpts.Monitors, pOpts.Pool)
|
util.DebugLog(ctx, "executing %v for image (%s) using mon %s, pool %s", args, pOpts.RbdImageName, pOpts.Monitors, pOpts.Pool)
|
||||||
supported := true
|
supported := true
|
||||||
_, stderr, err := util.ExecCommand("ceph", args...)
|
_, stderr, err := util.ExecCommand(ctx, "ceph", args...)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
switch {
|
switch {
|
||||||
@ -872,7 +872,9 @@ func (rv *rbdVolume) updateVolWithImageInfo(cr *util.Credentials) error {
|
|||||||
// rbd --format=json info [image-spec | snap-spec]
|
// rbd --format=json info [image-spec | snap-spec]
|
||||||
var imgInfo imageInfo
|
var imgInfo imageInfo
|
||||||
|
|
||||||
stdout, stderr, err := util.ExecCommand("rbd",
|
stdout, stderr, err := util.ExecCommand(
|
||||||
|
context.TODO(),
|
||||||
|
"rbd",
|
||||||
"-m", rv.Monitors,
|
"-m", rv.Monitors,
|
||||||
"--id", cr.ID,
|
"--id", cr.ID,
|
||||||
"--keyfile="+cr.KeyFile,
|
"--keyfile="+cr.KeyFile,
|
||||||
@ -1046,7 +1048,7 @@ func resizeRBDImage(rbdVol *rbdVolume, cr *util.Credentials) error {
|
|||||||
volSzMiB := fmt.Sprintf("%dM", util.RoundOffVolSize(rbdVol.VolSize))
|
volSzMiB := fmt.Sprintf("%dM", util.RoundOffVolSize(rbdVol.VolSize))
|
||||||
|
|
||||||
args := []string{"resize", rbdVol.String(), "--size", volSzMiB, "--id", cr.ID, "-m", mon, "--keyfile=" + cr.KeyFile}
|
args := []string{"resize", rbdVol.String(), "--size", volSzMiB, "--id", cr.ID, "-m", mon, "--keyfile=" + cr.KeyFile}
|
||||||
_, stderr, err := util.ExecCommand("rbd", args...)
|
_, stderr, err := util.ExecCommand(context.TODO(), "rbd", args...)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to resize rbd image (%w), command output: %s", err, string(stderr))
|
return fmt.Errorf("failed to resize rbd image (%w), command output: %s", err, string(stderr))
|
||||||
@ -1114,7 +1116,9 @@ type snapshotInfo struct {
|
|||||||
func (rv *rbdVolume) listSnapshots(ctx context.Context, cr *util.Credentials) ([]snapshotInfo, error) {
|
func (rv *rbdVolume) listSnapshots(ctx context.Context, cr *util.Credentials) ([]snapshotInfo, error) {
|
||||||
// rbd snap ls <image> --pool=<pool-name> --all --format=json
|
// rbd snap ls <image> --pool=<pool-name> --all --format=json
|
||||||
var snapInfo []snapshotInfo
|
var snapInfo []snapshotInfo
|
||||||
stdout, stderr, err := util.ExecCommand("rbd",
|
stdout, stderr, err := util.ExecCommand(
|
||||||
|
ctx,
|
||||||
|
"rbd",
|
||||||
"-m", rv.Monitors,
|
"-m", rv.Monitors,
|
||||||
"--id", cr.ID,
|
"--id", cr.ID,
|
||||||
"--keyfile="+cr.KeyFile,
|
"--keyfile="+cr.KeyFile,
|
||||||
|
@ -30,8 +30,10 @@ import (
|
|||||||
// InvalidPoolID used to denote an invalid pool.
|
// InvalidPoolID used to denote an invalid pool.
|
||||||
const InvalidPoolID int64 = -1
|
const InvalidPoolID int64 = -1
|
||||||
|
|
||||||
// ExecCommand executes passed in program with args and returns separate stdout and stderr streams.
|
// ExecCommand executes passed in program with args and returns separate stdout
|
||||||
func ExecCommand(program string, args ...string) (stdout, stderr []byte, err error) {
|
// and stderr streams. In case ctx is not set to context.TODO(), the command
|
||||||
|
// will be logged after it was executed.
|
||||||
|
func ExecCommand(ctx context.Context, program string, args ...string) (stdout, stderr []byte, err error) {
|
||||||
var (
|
var (
|
||||||
cmd = exec.Command(program, args...) // #nosec:G204, commands executing not vulnerable.
|
cmd = exec.Command(program, args...) // #nosec:G204, commands executing not vulnerable.
|
||||||
sanitizedArgs = StripSecretInArgs(args)
|
sanitizedArgs = StripSecretInArgs(args)
|
||||||
@ -43,8 +45,15 @@ func ExecCommand(program string, args ...string) (stdout, stderr []byte, err err
|
|||||||
cmd.Stderr = &stderrBuf
|
cmd.Stderr = &stderrBuf
|
||||||
|
|
||||||
if err := cmd.Run(); err != nil {
|
if err := cmd.Run(); err != nil {
|
||||||
return stdoutBuf.Bytes(), stderrBuf.Bytes(), fmt.Errorf("an error (%v)"+
|
err = fmt.Errorf("an error (%w) occurred while running %s args: %v", err, program, sanitizedArgs)
|
||||||
" occurred while running %s args: %v", err, program, sanitizedArgs)
|
if ctx != context.TODO() {
|
||||||
|
UsefulLog(ctx, "%s", err)
|
||||||
|
}
|
||||||
|
return stdoutBuf.Bytes(), stderrBuf.Bytes(), err
|
||||||
|
}
|
||||||
|
|
||||||
|
if ctx != context.TODO() {
|
||||||
|
UsefulLog(ctx, "command succeeded: %s %v", program, sanitizedArgs)
|
||||||
}
|
}
|
||||||
|
|
||||||
return stdoutBuf.Bytes(), nil, nil
|
return stdoutBuf.Bytes(), nil, nil
|
||||||
|
Loading…
Reference in New Issue
Block a user