InstallScriptInLXC installs a script inside an existing LXC container via pct exec. It SSHes to the node, then runs pct exec -- bash -c "curl ... | bash".
(user, nodeIP string, vmid int, script Script)
| 916 | // InstallScriptInLXC installs a script inside an existing LXC container via pct exec. |
| 917 | // It SSHes to the node, then runs pct exec <vmid> -- bash -c "curl ... | bash". |
| 918 | func InstallScriptInLXC(user, nodeIP string, vmid int, script Script) (int, error) { |
| 919 | if err := validateScriptPath(script.ScriptPath); err != nil { |
| 920 | return -1, err |
| 921 | } |
| 922 | |
| 923 | getScriptsLogger().Debug("Installing script %s in LXC %d on %s", script.ScriptPath, vmid, nodeIP) |
| 924 | |
| 925 | scriptURL := rawScriptURL(script) |
| 926 | innerCmd := buildInstallScriptCommand(scriptURL) |
| 927 | pctCmd := fmt.Sprintf("pct exec %d -- %s", vmid, innerCmd) |
| 928 | if !strings.EqualFold(user, "root") { |
| 929 | pctCmd = "sudo " + pctCmd |
| 930 | } |
| 931 | pctCmd = wrapRemoteCommandWithBash(pctCmd) |
| 932 | |
| 933 | // #nosec G204 -- command arguments are constructed from validated paths and vmid. |
| 934 | sshCmd := exec.Command("ssh", "-t", fmt.Sprintf("%s@%s", user, nodeIP), pctCmd) |
| 935 | sshCmd.Stdin = os.Stdin |
| 936 | sshCmd.Stdout = os.Stdout |
| 937 | sshCmd.Stderr = os.Stderr |
| 938 | sshCmd.Env = append(os.Environ(), "TERM=xterm-256color") |
| 939 | |
| 940 | err := sshCmd.Run() |
| 941 | exitCode := 0 |
| 942 | if err != nil { |
| 943 | if exitErr, ok := err.(*exec.ExitError); ok { |
| 944 | exitCode = exitErr.ExitCode() |
| 945 | } else { |
| 946 | exitCode = -1 |
| 947 | } |
| 948 | } |
| 949 | |
| 950 | if err != nil { |
| 951 | return exitCode, fmt.Errorf("script installation failed: %w", err) |
| 952 | } |
| 953 | |
| 954 | return exitCode, nil |
| 955 | } |
| 956 | |
| 957 | // ValidateConnection checks if SSH connection to the node is possible. |
| 958 | func ValidateConnection(user, nodeIP string) error { |
nothing calls this directly
no test coverage detected