Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
0833884
docs: add CLI CRUD and verbosity design document
ArangoGutierrez Feb 5, 2026
72aacd7
docs: add CLI CRUD implementation plan
ArangoGutierrez Feb 5, 2026
7fad32a
feat(logger): add verbosity levels with Debug and Trace methods
ArangoGutierrez Feb 5, 2026
6d90bb2
feat(cli): add global verbosity flags (-q, --verbose, -d)
ArangoGutierrez Feb 5, 2026
9cd1b6b
refactor(cli): rename list --quiet to --ids-only
ArangoGutierrez Feb 5, 2026
3b6a39d
test(output): add unit tests for output formatting package
ArangoGutierrez Feb 5, 2026
4670134
feat(output): add output formatting package with table/JSON/YAML support
ArangoGutierrez Feb 5, 2026
54f1c58
feat(cli): add output formatting to status command
ArangoGutierrez Feb 5, 2026
dda3b16
feat(cli): add describe, get, ssh, scp, update commands with tests
ArangoGutierrez Feb 5, 2026
57d6d80
test(list): update tests for --ids-only flag rename
ArangoGutierrez Feb 5, 2026
ec65767
fix(cli): fix nil map panic and SSH command quoting
ArangoGutierrez Feb 5, 2026
fb19ac0
refactor(cli): extract shared utilities and fix review issues
ArangoGutierrez Feb 6, 2026
8440213
fix(ci): resolve lint issues, update go.mod, and remove plan docs
ArangoGutierrez Feb 6, 2026
80e2802
fix(security): correct misleading host key comments (CWE-322)
ArangoGutierrez Feb 6, 2026
8a168c8
refactor(cli): use common.GetHostURL in update runProvision (S4)
ArangoGutierrez Feb 6, 2026
2867418
fix(cli): consolidate update command cache file writes (C2)
ArangoGutierrez Feb 6, 2026
ec2770c
feat(cli): add -q as alias for --ids-only for backwards compatibility…
ArangoGutierrez Feb 6, 2026
1c0c325
refactor(cli): extract SSH retry constants in ConnectSSH (S2)
ArangoGutierrez Feb 6, 2026
6b12fbb
docs(cli): document SSH remote command quoting behavior (S3)
ArangoGutierrez Feb 6, 2026
4cc2ec7
fix(logger): warnings always print regardless of verbosity (M1)
ArangoGutierrez Feb 6, 2026
7ee9120
refactor(cli): consolidate GetHostURL tests into common package (M3)
ArangoGutierrez Feb 6, 2026
ce7aef7
fix(logger): make Verbosity thread-safe with atomic.Int32 (M6)
ArangoGutierrez Feb 6, 2026
607a329
fix(logger): add nolint directive for gosec G115 in SetVerbosity
ArangoGutierrez Feb 6, 2026
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
120 changes: 120 additions & 0 deletions cmd/cli/common/host.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*
* Copyright (c) 2024, NVIDIA CORPORATION. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package common

import (
"fmt"
"os"
"time"

"golang.org/x/crypto/ssh"

"github.com/NVIDIA/holodeck/api/holodeck/v1alpha1"
"github.com/NVIDIA/holodeck/internal/logger"
"github.com/NVIDIA/holodeck/pkg/provider/aws"
)

// GetHostURL resolves the SSH-reachable host URL for an environment.
// If nodeName is set, it looks for that specific node.
// If preferControlPlane is true and no nodeName is set, it prefers a control-plane node.
// Falls back to the first available node, then single-node properties.
func GetHostURL(env *v1alpha1.Environment, nodeName string, preferControlPlane bool) (string, error) {
// For multinode clusters, find the appropriate node
if env.Spec.Cluster != nil && env.Status.Cluster != nil && len(env.Status.Cluster.Nodes) > 0 {
if nodeName != "" {
for _, node := range env.Status.Cluster.Nodes {
if node.Name == nodeName {
return node.PublicIP, nil
}
}
return "", fmt.Errorf("node %q not found in cluster", nodeName)
}

if preferControlPlane {
for _, node := range env.Status.Cluster.Nodes {
if node.Role == "control-plane" {
return node.PublicIP, nil
}
}
}

// Fallback to first node
return env.Status.Cluster.Nodes[0].PublicIP, nil
}

// Single node - get from properties
switch env.Spec.Provider {
case v1alpha1.ProviderAWS:
for _, p := range env.Status.Properties {
if p.Name == aws.PublicDnsName {
return p.Value, nil
}
}
case v1alpha1.ProviderSSH:
return env.Spec.HostUrl, nil
Copy link

Copilot AI Feb 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The field path should be env.Spec.Instance.HostUrl not env.Spec.HostUrl. The HostUrl field is defined on the Instance struct, not directly on EnvironmentSpec. This will cause a compilation error or runtime panic.

Suggested change
return env.Spec.HostUrl, nil
if env.Spec.Instance != nil {
return env.Spec.Instance.HostUrl, nil
}

Copilot uses AI. Check for mistakes.
}

return "", fmt.Errorf("unable to determine host URL")
}

const (
// sshMaxRetries is the number of SSH connection attempts before giving up.
sshMaxRetries = 3
// sshRetryDelay is the delay between SSH connection retry attempts.
sshRetryDelay = 2 * time.Second
)

// ConnectSSH establishes an SSH connection with retries.
//
// Host key verification is disabled because server host keys are generated
// at instance boot time and there is no trusted channel to distribute them
// to the client beforehand. The env file's privateKey/publicKey fields are
// SSH *authentication* keys (client-to-server), not server host keys.
func ConnectSSH(log *logger.FunLogger, keyPath, userName, hostUrl string) (*ssh.Client, error) {
key, err := os.ReadFile(keyPath) //nolint:gosec // keyPath is from trusted env config
if err != nil {
return nil, fmt.Errorf("failed to read key file %s: %v", keyPath, err)
}

signer, err := ssh.ParsePrivateKey(key)
if err != nil {
return nil, fmt.Errorf("failed to parse private key: %v", err)
}

config := &ssh.ClientConfig{
User: userName,
Auth: []ssh.AuthMethod{
ssh.PublicKeys(signer),
},
// Server host keys are generated at instance boot with no trusted
// distribution channel; disable verification (CWE-322 accepted risk).
HostKeyCallback: ssh.InsecureIgnoreHostKey(), //nolint:gosec
Timeout: 30 * time.Second,
}

var client *ssh.Client
for i := 0; i < sshMaxRetries; i++ {
client, err = ssh.Dial("tcp", hostUrl+":22", config)
if err == nil {
return client, nil
}
log.Warning("Connection attempt %d failed: %v", i+1, err)
time.Sleep(sshRetryDelay)
}

return nil, fmt.Errorf("failed to connect after %d attempts: %v", sshMaxRetries, err)
}
176 changes: 176 additions & 0 deletions cmd/cli/common/host_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
/*
* Copyright (c) 2024, NVIDIA CORPORATION. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package common

import (
"testing"

"github.com/NVIDIA/holodeck/api/holodeck/v1alpha1"
"github.com/NVIDIA/holodeck/pkg/provider/aws"
)

func TestGetHostURL_AWS_SingleNode(t *testing.T) {
env := &v1alpha1.Environment{
Spec: v1alpha1.EnvironmentSpec{
Provider: v1alpha1.ProviderAWS,
},
Status: v1alpha1.EnvironmentStatus{
Properties: []v1alpha1.Properties{
{Name: aws.PublicDnsName, Value: "ec2-1-2-3-4.compute.amazonaws.com"},
},
},
}

url, err := GetHostURL(env, "", false)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if url != "ec2-1-2-3-4.compute.amazonaws.com" {
t.Errorf("expected DNS name, got %s", url)
}
}

func TestGetHostURL_SSH(t *testing.T) {
env := &v1alpha1.Environment{
Spec: v1alpha1.EnvironmentSpec{
Provider: v1alpha1.ProviderSSH,
Instance: v1alpha1.Instance{
HostUrl: "192.168.1.100",
},
},
}

url, err := GetHostURL(env, "", false)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if url != "192.168.1.100" {
t.Errorf("expected 192.168.1.100, got %s", url)
}
}

func TestGetHostURL_NoProperties(t *testing.T) {
env := &v1alpha1.Environment{
Spec: v1alpha1.EnvironmentSpec{
Provider: v1alpha1.ProviderAWS,
},
Status: v1alpha1.EnvironmentStatus{
Properties: []v1alpha1.Properties{},
},
}

_, err := GetHostURL(env, "", false)
if err == nil {
t.Error("expected error for missing properties")
}
}

func TestGetHostURL_Cluster_PreferControlPlane(t *testing.T) {
env := &v1alpha1.Environment{
Spec: v1alpha1.EnvironmentSpec{
Provider: v1alpha1.ProviderAWS,
Cluster: &v1alpha1.ClusterSpec{},
},
Status: v1alpha1.EnvironmentStatus{
Cluster: &v1alpha1.ClusterStatus{
Nodes: []v1alpha1.NodeStatus{
{Name: "worker-0", Role: "worker", PublicIP: "10.0.0.1"},
{Name: "cp-0", Role: "control-plane", PublicIP: "10.0.0.2"},
},
},
},
}

url, err := GetHostURL(env, "", true)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if url != "10.0.0.2" {
t.Errorf("expected control-plane IP 10.0.0.2, got %s", url)
}
}

func TestGetHostURL_Cluster_FallbackToFirstNode(t *testing.T) {
env := &v1alpha1.Environment{
Spec: v1alpha1.EnvironmentSpec{
Provider: v1alpha1.ProviderAWS,
Cluster: &v1alpha1.ClusterSpec{},
},
Status: v1alpha1.EnvironmentStatus{
Cluster: &v1alpha1.ClusterStatus{
Nodes: []v1alpha1.NodeStatus{
{Name: "worker-0", Role: "worker", PublicIP: "10.0.0.1"},
{Name: "worker-1", Role: "worker", PublicIP: "10.0.0.2"},
},
},
},
}

url, err := GetHostURL(env, "", true)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if url != "10.0.0.1" {
t.Errorf("expected first node IP 10.0.0.1, got %s", url)
}
}

func TestGetHostURL_Cluster_SpecificNode(t *testing.T) {
env := &v1alpha1.Environment{
Spec: v1alpha1.EnvironmentSpec{
Provider: v1alpha1.ProviderAWS,
Cluster: &v1alpha1.ClusterSpec{},
},
Status: v1alpha1.EnvironmentStatus{
Cluster: &v1alpha1.ClusterStatus{
Nodes: []v1alpha1.NodeStatus{
{Name: "cp-0", Role: "control-plane", PublicIP: "10.0.0.1"},
{Name: "worker-0", Role: "worker", PublicIP: "10.0.0.2"},
},
},
},
}

url, err := GetHostURL(env, "worker-0", false)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if url != "10.0.0.2" {
t.Errorf("expected worker IP 10.0.0.2, got %s", url)
}
}

func TestGetHostURL_Cluster_NodeNotFound(t *testing.T) {
env := &v1alpha1.Environment{
Spec: v1alpha1.EnvironmentSpec{
Provider: v1alpha1.ProviderAWS,
Cluster: &v1alpha1.ClusterSpec{},
},
Status: v1alpha1.EnvironmentStatus{
Cluster: &v1alpha1.ClusterStatus{
Nodes: []v1alpha1.NodeStatus{
{Name: "cp-0", Role: "control-plane", PublicIP: "10.0.0.1"},
},
},
},
}

_, err := GetHostURL(env, "nonexistent", false)
if err == nil {
t.Error("expected error for nonexistent node")
}
}
Loading
Loading