From a4daa3599a22b9a2a3e5b6316b13a85a0ff1eb8a Mon Sep 17 00:00:00 2001 From: Igor Shishkin Date: Sat, 25 May 2024 10:23:08 +0300 Subject: [PATCH] Add ability to use complex ceph binary path (#22) Add ability to pass complex `--ceph-binary` parameter like `ssh $hostname ceph` to run cephctl on any platform as a client accessing to ceph via SSH Signed-off-by: Igor Shishkin --- ceph/ceph.go | 32 ++++++++++---------------------- ceph/cmd.go | 16 ++++++++++++++++ ceph/cmd_darwin.go | 6 ++++++ ceph/cmd_linux.go | 6 ++++++ ceph/cmd_test.go | 17 +++++++++++++++++ ceph/cmd_windows.go | 6 ++++++ 6 files changed, 61 insertions(+), 22 deletions(-) create mode 100644 ceph/cmd.go create mode 100644 ceph/cmd_darwin.go create mode 100644 ceph/cmd_linux.go create mode 100644 ceph/cmd_test.go create mode 100644 ceph/cmd_windows.go diff --git a/ceph/ceph.go b/ceph/ceph.go index a7e64b5..23c7f34 100644 --- a/ceph/ceph.go +++ b/ceph/ceph.go @@ -6,7 +6,6 @@ import ( "encoding/json" "os" "os/exec" - "strings" "github.com/pkg/errors" log "github.com/sirupsen/logrus" @@ -33,11 +32,9 @@ func New(binaryPath string) Ceph { } func (c *ceph) ApplyCephConfigOption(ctx context.Context, section, key, value string) error { - args := []string{"config", "set", section, key, value} + bin, args := mkCommand(c.binaryPath, []string{"config", "set", section, key, value}) - log.Tracef("preparing to run %s %s", c.binaryPath, strings.Join(args, " ")) - - cmd := exec.CommandContext(ctx, c.binaryPath, args...) + cmd := exec.CommandContext(ctx, bin, args...) cmd.Stderr = os.Stderr if err := cmd.Run(); err != nil { return errors.Wrap(err, "error applying configuration") @@ -47,11 +44,9 @@ func (c *ceph) ApplyCephConfigOption(ctx context.Context, section, key, value st func (c *ceph) ClusterReport(ctx context.Context) (models.ClusterReport, error) { buf := &bytes.Buffer{} - args := []string{"report", "--format=json"} - - log.Tracef("preparing to run %s %s", c.binaryPath, strings.Join(args, " ")) + bin, args := mkCommand(c.binaryPath, []string{"report", "--format=json"}) - cmd := exec.CommandContext(ctx, c.binaryPath, args...) + cmd := exec.CommandContext(ctx, bin, args...) cmd.Stdout = buf cmd.Stderr = os.Stderr if err := cmd.Run(); err != nil { @@ -70,11 +65,9 @@ func (c *ceph) ClusterReport(ctx context.Context) (models.ClusterReport, error) func (c ceph) ClusterStatus(ctx context.Context) (models.ClusterStatus, error) { buf := &bytes.Buffer{} - args := []string{"status", "--format=json"} - - log.Tracef("preparing to run %s %s", c.binaryPath, strings.Join(args, " ")) + bin, args := mkCommand(c.binaryPath, []string{"status", "--format=json"}) - cmd := exec.CommandContext(ctx, c.binaryPath, args...) + cmd := exec.CommandContext(ctx, bin, args...) cmd.Stdout = buf cmd.Stderr = os.Stderr if err := cmd.Run(); err != nil { @@ -94,12 +87,9 @@ func (c ceph) ClusterStatus(ctx context.Context) (models.ClusterStatus, error) { func (c *ceph) DumpConfig(ctx context.Context) (models.CephConfig, error) { cfg := []cephModels.ConfigOption{} buf := &bytes.Buffer{} + bin, args := mkCommand(c.binaryPath, []string{"config", "dump", "--format=json"}) - args := []string{"config", "dump", "--format=json"} - - log.Tracef("preparing to run %s %s", c.binaryPath, strings.Join(args, " ")) - - cmd := exec.CommandContext(ctx, c.binaryPath, args...) + cmd := exec.CommandContext(ctx, bin, args...) cmd.Stdout = buf cmd.Stderr = os.Stderr if err := cmd.Run(); err != nil { @@ -124,11 +114,9 @@ func (c *ceph) DumpConfig(ctx context.Context) (models.CephConfig, error) { } func (c *ceph) RemoveCephConfigOption(ctx context.Context, section, key string) error { - args := []string{"config", "rm", section, key} - - log.Tracef("preparing to run %s %s", c.binaryPath, strings.Join(args, " ")) + bin, args := mkCommand(c.binaryPath, []string{"config", "rm", section, key}) - cmd := exec.CommandContext(ctx, c.binaryPath, args...) + cmd := exec.CommandContext(ctx, bin, args...) cmd.Stderr = os.Stderr if err := cmd.Run(); err != nil { return errors.Wrap(err, "error applying configuration") diff --git a/ceph/cmd.go b/ceph/cmd.go new file mode 100644 index 0000000..f696a9a --- /dev/null +++ b/ceph/cmd.go @@ -0,0 +1,16 @@ +package ceph + +import ( + "strings" + + log "github.com/sirupsen/logrus" +) + +func mkCommand(cephBinary string, args []string) (string, []string) { + outCmd := shellCommand + outArgs := []string{"-c", strings.Join(append([]string{cephBinary}, args...), " ")} + + log.Tracef("preparing command: `%s` `%#v`", outCmd, outArgs) + + return shellCommand, outArgs +} diff --git a/ceph/cmd_darwin.go b/ceph/cmd_darwin.go new file mode 100644 index 0000000..e8bbfb2 --- /dev/null +++ b/ceph/cmd_darwin.go @@ -0,0 +1,6 @@ +package ceph + +const ( + shellCommand = "sh" + shellArg = "-c" +) diff --git a/ceph/cmd_linux.go b/ceph/cmd_linux.go new file mode 100644 index 0000000..e8bbfb2 --- /dev/null +++ b/ceph/cmd_linux.go @@ -0,0 +1,6 @@ +package ceph + +const ( + shellCommand = "sh" + shellArg = "-c" +) diff --git a/ceph/cmd_test.go b/ceph/cmd_test.go new file mode 100644 index 0000000..a4284f1 --- /dev/null +++ b/ceph/cmd_test.go @@ -0,0 +1,17 @@ +package ceph + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func TestMkCommand(t *testing.T) { + r := require.New(t) + + bin, args := mkCommand("testcmd", []string{"arg1", "arg2"}) + r.Equal(shellCommand, bin) + r.Equal([]string{ + shellArg, "testcmd arg1 arg2", + }, args) +} diff --git a/ceph/cmd_windows.go b/ceph/cmd_windows.go new file mode 100644 index 0000000..bbce8f6 --- /dev/null +++ b/ceph/cmd_windows.go @@ -0,0 +1,6 @@ +package ceph + +const ( + shellCommand = "cmd.exe" + shellArg = "/C" +)