fix: execute valid bash cmd when running init as root

This commit is contained in:
Connor Edwards
2025-02-07 21:14:27 +00:00
parent 653f47f507
commit b2f7c6fb33
2 changed files with 41 additions and 10 deletions
+22 -10
View File
@@ -17,24 +17,36 @@ type RemoteMachine struct {
KeyPath string
}
func installCmd(user string) string {
sudoPrefix := "sudo"
// Add the SSH user (non-root) to the uncloud group to allow access to the Uncloud daemon unix socket.
env := "UNCLOUD_GROUP_ADD_USER=" + user
curlBashCmd := fmt.Sprintf(
"curl -fsSL %s | %s %s bash", sshexec.Quote(installScriptURL), sudoPrefix, sshexec.Quote(env),
)
if user == "root" {
curlBashCmd = fmt.Sprintf(
"curl -fsSL %s | bash", sshexec.Quote(installScriptURL),
)
}
return curlBashCmd
}
// provisionMachine provisions the remote machine by downloading the Uncloud install script from GitHub and running it.
func provisionMachine(ctx context.Context, exec sshexec.Executor) error {
user, err := exec.Run(ctx, "whoami")
if err != nil {
return fmt.Errorf("run whoami: %w", err)
}
sudoPrefix, env := "", ""
if user != "root" {
sudoPrefix = "sudo"
// Add the SSH user (non-root) to the uncloud group to allow access to the Uncloud daemon unix socket.
env = "UNCLOUD_GROUP_ADD_USER=" + user
}
installCmd := installCmd(user)
fmt.Println("Downloading Uncloud install script:", installScriptURL)
curlBashCmd := fmt.Sprintf(
"curl -fsSL %s | %s %s bash", sshexec.Quote(installScriptURL), sudoPrefix, sshexec.Quote(env),
)
cmd := sshexec.QuoteCommand("bash", "-c", "set -o pipefail; "+curlBashCmd)
cmd := sshexec.QuoteCommand("bash", "-c", "set -o pipefail; "+installCmd)
if err = exec.Stream(ctx, cmd, os.Stdout, os.Stderr); err != nil {
return fmt.Errorf("download and run install script: %w", err)
}
+19
View File
@@ -0,0 +1,19 @@
package cli
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestInstallCmd(t *testing.T) {
t.Run("root", func(t *testing.T) {
cmd := installCmd("root")
assert.NotContains(t, cmd, "sudo")
})
t.Run("nonroot", func(t *testing.T) {
cmd := installCmd("nonroot")
assert.Contains(t, cmd, "sudo")
})
}