Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Valkey support #1521

Open
wants to merge 2 commits into
base: 5.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions go-tests/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ module github.com/platformsh/legacy-cli/tests
go 1.22.9

require (
github.com/platformsh/cli v0.0.0-20241229194532-b86546247906
github.com/platformsh/cli v0.0.0-20250115153051-60dcb793eb89
github.com/stretchr/testify v1.9.0
golang.org/x/crypto v0.31.0
)

require (
Expand All @@ -13,7 +14,6 @@ require (
github.com/kr/pretty v0.3.1 // indirect
github.com/oklog/ulid/v2 v2.1.0 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
golang.org/x/crypto v0.31.0 // indirect
golang.org/x/sys v0.28.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
4 changes: 2 additions & 2 deletions go-tests/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ github.com/oklog/ulid/v2 v2.1.0 h1:+9lhoxAP56we25tyYETBBY1YLA2SaoLvUFgrP2miPJU=
github.com/oklog/ulid/v2 v2.1.0/go.mod h1:rcEKHmBBKfef9DhnvX7y1HZBYxjXb0cP5ExxNsTT1QQ=
github.com/pborman/getopt v0.0.0-20170112200414-7148bc3a4c30/go.mod h1:85jBQOZwpVEaDAr341tbn15RS4fCAsIst0qp7i8ex1o=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/platformsh/cli v0.0.0-20241229194532-b86546247906 h1:BvQf2yvT7USm80fAJ0C5FXfTfjjK2onrnoVMPz5CT9k=
github.com/platformsh/cli v0.0.0-20241229194532-b86546247906/go.mod h1:jMxyJGLMlkjDq7l9cVWFhsX/8xxFqEOH95rWbDFyO08=
github.com/platformsh/cli v0.0.0-20250115153051-60dcb793eb89 h1:+EN931RRA5tsviOEqw6HW62u6UusZJq7LJKY3EIYrE0=
github.com/platformsh/cli v0.0.0-20250115153051-60dcb793eb89/go.mod h1:b1v98rkg8bScSoo5gK8Fc1qRta1FUU8wElRf+bGvV5Y=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
Expand Down
8 changes: 3 additions & 5 deletions go-tests/ssh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tests

import (
"net/http/httptest"
"os"
"os/exec"
"strconv"
"testing"
Expand All @@ -21,10 +22,6 @@ func TestSSH(t *testing.T) {
if err != nil {
t.Fatal(err)
}
sshServer.RemoteEnv = []string{
// TODO use this
"PLATFORM_RELATIONSHIPS=e30K",
}
t.Cleanup(func() {
if err := sshServer.Stop(); err != nil {
t.Error(err)
Expand Down Expand Up @@ -71,7 +68,8 @@ func TestSSH(t *testing.T) {
}

f.Run("cc")
assert.Equal(t, sshServer.RemoteDir+"\n", f.Run("ssh", "-p", projectID, "-e", ".", "pwd"))
wd, _ := os.Getwd()
assert.Equal(t, wd+"\n", f.Run("ssh", "-p", projectID, "-e", ".", "pwd"))

_, stdErr, err := f.RunCombinedOutput("ssh", "-p", projectID, "-e", "main", "--instance", "2", "pwd")
assert.Error(t, err)
Expand Down
115 changes: 115 additions & 0 deletions go-tests/valkey_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package tests

import (
"encoding/base64"
"encoding/json"
"fmt"
"net/http/httptest"
"net/url"
"strconv"
"strings"
"testing"

"golang.org/x/crypto/ssh"

"github.com/platformsh/cli/pkg/mockapi"
"github.com/platformsh/cli/pkg/mockssh"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestValkey(t *testing.T) {
authServer := mockapi.NewAuthServer(t)
defer authServer.Close()

myUserID := "my-user-id"

sshServer, err := mockssh.NewServer(t, authServer.URL+"/ssh/authority")
if err != nil {
t.Fatal(err)
}

relationships := map[string]any{
"cache": []map[string]any{{
"username": nil,
"host": "cache.internal",
"path": nil,
"query": url.Values{},
"password": nil,
"port": 6379,
"service": "cache",
"scheme": "valkey",
"type": "valkey:8.0",
"public": false,
}},
}
relationshipsJSON, err := json.Marshal(relationships)
require.NoError(t, err)

execHandler := mockssh.ExecHandler(t.TempDir(), []string{
"PLATFORM_RELATIONSHIPS=" + base64.StdEncoding.EncodeToString(relationshipsJSON),
})

sshServer.CommandHandler = func(conn ssh.ConnMetadata, command string, io mockssh.CommandIO) int {
if strings.HasPrefix(command, "valkey-cli") {
_, _ = fmt.Fprint(io.StdOut, "Received command: "+command)
return 0
}

return execHandler(conn, command, io)
}
t.Cleanup(func() {
if err := sshServer.Stop(); err != nil {
t.Error(err)
}
})

projectID := mockapi.ProjectID()

apiHandler := mockapi.NewHandler(t)
apiHandler.SetMyUser(&mockapi.User{ID: myUserID})
apiHandler.SetProjects([]*mockapi.Project{
{
ID: projectID,
Links: mockapi.MakeHALLinks(
"self=/projects/"+projectID,
"environments=/projects/"+projectID+"/environments",
),
DefaultBranch: "main",
},
})
mainEnv := makeEnv(projectID, "main", "production", "active", nil)
mainEnv.SetCurrentDeployment(&mockapi.Deployment{
WebApps: map[string]mockapi.App{
"app": {Name: "app", Type: "golang:1.23", Size: "M", Disk: 2048, Mounts: map[string]mockapi.Mount{}},
},
Services: map[string]mockapi.App{},
Workers: map[string]mockapi.Worker{},
Routes: mockRoutes(),
Links: mockapi.MakeHALLinks("self=/projects/" + projectID + "/environments/main/deployment/current"),
})
mainEnv.Links["pf:ssh:app:0"] = mockapi.HALLink{HREF: "ssh://app--0@ssh.cli-tests.example.com"}
apiHandler.SetEnvironments([]*mockapi.Environment{
mainEnv,
})

apiServer := httptest.NewServer(apiHandler)
defer apiServer.Close()

f := newCommandFactory(t, apiServer.URL, authServer.URL)
f.extraEnv = []string{
EnvPrefix + "SSH_OPTIONS=HostName 127.0.0.1\nPort " + strconv.Itoa(sshServer.Port()),
EnvPrefix + "SSH_HOST_KEYS=" + sshServer.HostKeyConfig(),
}

f.Run("cc")

assert.Equal(t, "Received command: valkey-cli -h cache.internal -p 6379 ping",
f.Run("valkey", "-p", projectID, "-e", ".", "ping"))

assert.Equal(t, "Received command: valkey-cli -h cache.internal -p 6379 --scan",
f.Run("valkey", "-p", projectID, "-e", ".", "--", "--scan"))

assert.Equal(t, "Received command: valkey-cli -h cache.internal -p 6379 --scan --pattern '*-11*'",
f.Run("valkey", "-p", projectID, "-e", ".", "--", "--scan --pattern '*-11*'"))
}
75 changes: 4 additions & 71 deletions src/Command/Service/RedisCliCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,79 +4,12 @@

namespace Platformsh\Cli\Command\Service;

use Platformsh\Cli\Selector\SelectorConfig;
use Platformsh\Cli\Service\Io;
use Platformsh\Cli\Selector\Selector;
use Platformsh\Cli\Command\CommandBase;
use Platformsh\Cli\Model\Host\RemoteHost;
use Platformsh\Cli\Service\Relationships;
use Platformsh\Cli\Service\Ssh;
use Platformsh\Cli\Util\OsUtil;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

#[AsCommand(name: 'service:redis-cli', description: 'Access the Redis CLI', aliases: ['redis'])]
class RedisCliCommand extends CommandBase
class RedisCliCommand extends ValkeyCliCommandBase
{
public function __construct(private readonly Io $io, private readonly Relationships $relationships, private readonly Selector $selector)
{
parent::__construct();
}
protected function configure(): void
{
$this->addArgument('args', InputArgument::OPTIONAL | InputArgument::IS_ARRAY, 'Arguments to add to the Redis command');
Relationships::configureInput($this->getDefinition());
Ssh::configureInput($this->getDefinition());
$this->selector->addProjectOption($this->getDefinition());
$this->selector->addEnvironmentOption($this->getDefinition());
$this->selector->addAppOption($this->getDefinition());
$this->addCompleter($this->selector);
$this->addExample('Open the redis-cli shell');
$this->addExample('Ping the Redis server', 'ping');
$this->addExample('Show Redis status information', 'info');
$this->addExample('Scan keys', "-- --scan");
$this->addExample('Scan keys matching a pattern', '-- "--scan --pattern \'*-11*\'"');
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
if ($this->runningViaMulti && !$input->getArgument('args')) {
throw new \RuntimeException('The redis-cli command cannot run as a shell via multi');
}

$selection = $this->selector->getSelection($input, new SelectorConfig(
allowLocalHost: $this->relationships->hasLocalEnvVar(),
chooseEnvFilter: SelectorConfig::filterEnvsMaybeActive(),
));
$host = $this->selector->getHostFromSelection($input, $selection);

$service = $this->relationships->chooseService($host, $input, $output, ['redis']);
if (!$service) {
return 1;
}

$redisCommand = sprintf(
'redis-cli -h %s -p %d',
OsUtil::escapePosixShellArg($service['host']),
$service['port'],
);
if ($args = $input->getArgument('args')) {
if (count($args) === 1) {
$redisCommand .= ' ' . $args[0];
} else {
$redisCommand .= ' ' . implode(' ', array_map(OsUtil::escapePosixShellArg(...), $args));
}
} elseif ($this->io->isTerminal(STDIN) && $host instanceof RemoteHost) {
// Force TTY output when the input is a terminal.
$host->setExtraSshOptions(['RequestTTY yes']);
}

$this->stdErr->writeln(
sprintf('Connecting to Redis service via relationship <info>%s</info> on <info>%s</info>', $service['_relationship_name'], $host->getLabel()),
);

return $host->runCommandDirect($redisCommand);
}
protected string $dbName = 'redis';
protected string $dbTitle = 'Redis';
protected string $dbCommand = 'redis-cli';
}
10 changes: 10 additions & 0 deletions src/Command/Service/ValkeyCliCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Platformsh\Cli\Command\Service;

use Symfony\Component\Console\Attribute\AsCommand;

#[AsCommand(name: 'service:valkey-cli', description: 'Access the Valkey CLI', aliases: ['valkey'])]
class ValkeyCliCommand extends ValkeyCliCommandBase {}
85 changes: 85 additions & 0 deletions src/Command/Service/ValkeyCliCommandBase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

declare(strict_types=1);

namespace Platformsh\Cli\Command\Service;

use Platformsh\Cli\Selector\SelectorConfig;
use Platformsh\Cli\Service\Io;
use Platformsh\Cli\Selector\Selector;
use Platformsh\Cli\Command\CommandBase;
use Platformsh\Cli\Model\Host\RemoteHost;
use Platformsh\Cli\Service\Relationships;
use Platformsh\Cli\Service\Ssh;
use Platformsh\Cli\Util\OsUtil;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

abstract class ValkeyCliCommandBase extends CommandBase
{
protected string $dbName = 'valkey';
protected string $dbTitle = 'valkey';
protected string $dbCommand = 'valkey-cli';

public function __construct(private readonly Io $io, private readonly Relationships $relationships, private readonly Selector $selector)
{
parent::__construct();
}
protected function configure(): void
{
$this->addArgument('args', InputArgument::OPTIONAL | InputArgument::IS_ARRAY, sprintf('Arguments to add to the %s command', $this->dbCommand));
Relationships::configureInput($this->getDefinition());
Ssh::configureInput($this->getDefinition());
$this->selector->addProjectOption($this->getDefinition());
$this->selector->addEnvironmentOption($this->getDefinition());
$this->selector->addAppOption($this->getDefinition());
$this->addCompleter($this->selector);
$this->addExample(sprintf('Open the %s shell', $this->dbCommand));
$this->addExample(sprintf('Ping the %s server', $this->dbTitle), 'ping');
$this->addExample(sprintf('Show %s status information', $this->dbTitle), 'info');
$this->addExample('Scan keys', "-- --scan");
$this->addExample('Scan keys matching a pattern', '-- "--scan --pattern \'*-11*\'"');
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
if ($this->runningViaMulti && !$input->getArgument('args')) {
throw new \RuntimeException(sprintf('The %s command cannot run as a shell via multi', $this->dbCommand));
}

$selection = $this->selector->getSelection($input, new SelectorConfig(
allowLocalHost: $this->relationships->hasLocalEnvVar(),
chooseEnvFilter: SelectorConfig::filterEnvsMaybeActive(),
));
$host = $this->selector->getHostFromSelection($input, $selection);

$service = $this->relationships->chooseService($host, $input, $output, [$this->dbName]);
if (!$service) {
return 1;
}

$command = sprintf(
'%s -h %s -p %d',
$this->dbCommand,
OsUtil::escapePosixShellArg($service['host']),
$service['port'],
);
if ($args = $input->getArgument('args')) {
if (count($args) === 1) {
$command .= ' ' . $args[0];
} else {
$command .= ' ' . implode(' ', array_map(OsUtil::escapePosixShellArg(...), $args));
}
} elseif ($this->io->isTerminal(STDIN) && $host instanceof RemoteHost) {
// Force TTY output when the input is a terminal.
$host->setExtraSshOptions(['RequestTTY yes']);
}

$this->stdErr->writeln(
sprintf('Connecting to %s service via relationship <info>%s</info> on <info>%s</info>', $this->dbTitle, $service['_relationship_name'], $host->getLabel()),
);

return $host->runCommandDirect($command);
}
}
Loading