Skip to content

Commit 8dd5d1d

Browse files
private repo ssh
1 parent 670708f commit 8dd5d1d

File tree

3 files changed

+95
-7
lines changed

3 files changed

+95
-7
lines changed

cmd/nodecmd/load_test_start.go

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"os"
88
"path/filepath"
9+
"strings"
910

1011
"github.com/ava-labs/avalanche-cli/pkg/ansible"
1112
"github.com/ava-labs/avalanche-cli/pkg/application"
@@ -29,13 +30,14 @@ import (
2930
)
3031

3132
var (
32-
loadTestRepoURL string
33-
loadTestBuildCmd string
34-
loadTestCmd string
35-
loadTestRepoCommit string
36-
repoDirName string
37-
loadTestHostRegion string
38-
loadTestBranch string
33+
loadTestRepoURL string
34+
loadTestBuildCmd string
35+
loadTestCmd string
36+
loadTestRepoCommit string
37+
repoDirName string
38+
loadTestHostRegion string
39+
loadTestBranch string
40+
loadTestDeployKeyPath string
3941
)
4042

4143
type clusterInfo struct {
@@ -80,6 +82,7 @@ The command will then run the load test binary based on the provided load test r
8082
cmd.Flags().StringVar(&loadTestCmd, "load-test-cmd", "", "command to run load test")
8183
cmd.Flags().StringVar(&loadTestHostRegion, "region", "", "create load test node in a given region")
8284
cmd.Flags().StringVar(&loadTestBranch, "load-test-branch", "", "load test branch or commit")
85+
cmd.Flags().StringVar(&loadTestDeployKeyPath, "load-test-deploy-key", "", "path to SSH deploy key for private repository access")
8386
return cmd
8487
}
8588

@@ -357,6 +360,20 @@ func startLoadTest(_ *cobra.Command, args []string) error {
357360
}
358361

359362
ux.Logger.GreenCheckmarkToUser("Load test environment is ready!")
363+
364+
// Setup deploy key for private repository access if provided
365+
if loadTestDeployKeyPath != "" {
366+
ux.Logger.PrintToUser("%s Setting up deploy key for private repository", logging.Green.Wrap(">"))
367+
if err := ssh.RunSSHSetupDeployKey(currentLoadTestHost[0], loadTestDeployKeyPath); err != nil {
368+
return fmt.Errorf("failed to setup deploy key for private repository: %w", err)
369+
}
370+
371+
// Update repository URL to use SSH config if it's a GitHub SSH URL
372+
if strings.HasPrefix(loadTestRepoURL, "git@github.com:") {
373+
loadTestRepoURL = strings.Replace(loadTestRepoURL, "git@github.com:", "git@github.com-avalanche:", 1)
374+
}
375+
}
376+
360377
ux.Logger.PrintToUser("%s Building load test code", logging.Green.Wrap(">"))
361378
if err := ssh.RunSSHBuildLoadTestCode(currentLoadTestHost[0], loadTestRepoURL, loadTestBuildCmd, loadTestRepoCommit, repoDirName, loadTestBranch, checkoutCommit); err != nil {
362379
return err

pkg/ssh/shell/setupDeployKey.sh

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/usr/bin/env bash
2+
# Setup SSH deploy key for private repository access
3+
4+
# Create .ssh directory if it doesn't exist
5+
mkdir -p ~/.ssh
6+
7+
# Set proper permissions
8+
chmod 700 ~/.ssh
9+
10+
# Upload the deploy key (this will be done by the Go code before calling this script)
11+
# The key should already be at ~/.ssh/avalanche-deploy-key
12+
13+
# Set proper permissions for the deploy key
14+
chmod 600 ~/.ssh/avalanche-deploy-key
15+
16+
# Configure SSH to use the deploy key
17+
cat >> ~/.ssh/config <<'EOF'
18+
Host github.com-avalanche
19+
HostName github.com
20+
User git
21+
IdentityFile ~/.ssh/avalanche-deploy-key
22+
IdentitiesOnly yes
23+
StrictHostKeyChecking accept-new
24+
EOF
25+
26+
# Test the SSH connection
27+
echo "Testing SSH connection to GitHub..."
28+
ssh -T github.com-avalanche || true
29+
30+
echo "Deploy key setup completed successfully!"

pkg/ssh/ssh.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -820,6 +820,47 @@ func RunSSHBuildLoadTestCode(host *models.Host, loadTestRepo, loadTestPath, load
820820
)
821821
}
822822

823+
// RunSSHSetupDeployKey sets up SSH deploy key for private repository access
824+
func RunSSHSetupDeployKey(host *models.Host, deployKeyPath string) error {
825+
// Expand tilde in the path
826+
expandedPath, err := expandTilde(deployKeyPath)
827+
if err != nil {
828+
return fmt.Errorf("failed to expand deploy key path: %w", err)
829+
}
830+
831+
// Check if the deploy key file exists
832+
if _, err := os.Stat(expandedPath); os.IsNotExist(err) {
833+
return fmt.Errorf("deploy key file not found: %s", expandedPath)
834+
}
835+
836+
// Upload the deploy key first
837+
remoteDeployKeyPath := "~/.ssh/avalanche-deploy-key"
838+
if err := host.Upload(expandedPath, remoteDeployKeyPath, constants.SSHFileOpsTimeout); err != nil {
839+
return fmt.Errorf("failed to upload deploy key: %w", err)
840+
}
841+
842+
// Run the setup script
843+
return RunOverSSH(
844+
"Setup Deploy Key",
845+
host,
846+
constants.SSHFileOpsTimeout,
847+
"shell/setupDeployKey.sh",
848+
scriptInputs{},
849+
)
850+
}
851+
852+
// expandTilde expands ~ to the user's home directory
853+
func expandTilde(path string) (string, error) {
854+
if strings.HasPrefix(path, "~/") {
855+
homeDir, err := os.UserHomeDir()
856+
if err != nil {
857+
return "", err
858+
}
859+
return filepath.Join(homeDir, path[2:]), nil
860+
}
861+
return path, nil
862+
}
863+
823864
func RunSSHBuildLoadTestDependencies(host *models.Host) error {
824865
return RunOverSSH(
825866
"Build Load Test",

0 commit comments

Comments
 (0)