0
0
mirror of https://github.com/bpg/terraform-provider-proxmox.git synced 2025-07-11 08:15:02 +00:00

Merge remote-tracking branch 'origin/main' into breackdown-vm-code

Signed-off-by: Pavel Boldyrev <627562+bpg@users.noreply.github.com>
This commit is contained in:
Pavel Boldyrev 2024-02-05 22:23:22 -05:00
parent 84df455c11
commit 4a0ceb9735
No known key found for this signature in database
GPG Key ID: 02A24794ADAC7455
3 changed files with 11 additions and 9 deletions

View File

@ -12,6 +12,7 @@ import (
"crypto/sha256" "crypto/sha256"
"crypto/tls" "crypto/tls"
"fmt" "fmt"
"github.com/bpg/terraform-provider-proxmox/proxmoxtf/resource/ssh"
"io" "io"
"net/http" "net/http"
"net/url" "net/url"
@ -594,7 +595,7 @@ func fileCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag
_, err := capi.SSH().ExecuteNodeCommands(ctx, nodeName, []string{ _, err := capi.SSH().ExecuteNodeCommands(ctx, nodeName, []string{
// the `mv` command should be scoped to the specific directories in sudoers! // the `mv` command should be scoped to the specific directories in sudoers!
fmt.Sprintf(`%s; try_sudo "mv %s/%s %s/%s" && rm %s/%s && rmdir -p %s`, fmt.Sprintf(`%s; try_sudo "mv %s/%s %s/%s" && rm %s/%s && rmdir -p %s`,
trySudo, ssh.TrySudo,
srcDir, *fileName, srcDir, *fileName,
dstDir, *fileName, dstDir, *fileName,
srcDir, *fileName, srcDir, *fileName,
@ -603,7 +604,7 @@ func fileCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag
}) })
if err != nil { if err != nil {
if matches, e := regexp.MatchString(`cannot move .* Permission denied`, err.Error()); e == nil && matches { if matches, e := regexp.MatchString(`cannot move .* Permission denied`, err.Error()); e == nil && matches {
return diag.FromErr(newErrSSHUserNoPermission(capi.SSH().Username())) return diag.FromErr(ssh.NewErrSSHUserNoPermission(capi.SSH().Username()))
} }
diags = append(diags, diag.Errorf("error moving file: %s", err.Error())...) diags = append(diags, diag.Errorf("error moving file: %s", err.Error())...)

View File

@ -1,14 +1,14 @@
package resource package ssh
import ( import (
"fmt" "fmt"
) )
const ( const (
trySudo = `try_sudo(){ if [ $(sudo -n pvesm apiinfo 2>&1 | grep "APIVER" | wc -l) -gt 0 ]; then sudo $1; else $1; fi }` TrySudo = `try_sudo(){ if [ $(sudo -n pvesm apiinfo 2>&1 | grep "APIVER" | wc -l) -gt 0 ]; then sudo $1; else $1; fi }`
) )
func newErrSSHUserNoPermission(username string) error { func NewErrSSHUserNoPermission(username string) error {
return fmt.Errorf("the SSH user '%s' does not have required permissions. "+ return fmt.Errorf("the SSH user '%s' does not have required permissions. "+
"Make sure 'sudo' is installed and the user is configured in sudoers file. "+ "Make sure 'sudo' is installed and the user is configured in sudoers file. "+
"Refer to the documentation for more details", username) "Refer to the documentation for more details", username)

View File

@ -3,6 +3,8 @@ package vm
import ( import (
"context" "context"
"fmt" "fmt"
"github.com/bpg/terraform-provider-proxmox/proxmoxtf/resource/ssh"
"regexp"
"strconv" "strconv"
"strings" "strings"
@ -370,7 +372,7 @@ func vmImportCustomDisks(ctx context.Context, d *schema.ResourceData, m interfac
commands = append( commands = append(
commands, commands,
`set -e`, `set -e`,
`try_sudo(){ if [ $(sudo -n echo tfpve 2>&1 | grep "tfpve" | wc -l) -gt 0 ]; then sudo $1; else $1; fi }`, ssh.TrySudo,
fmt.Sprintf(`file_id="%s"`, *d.FileID), fmt.Sprintf(`file_id="%s"`, *d.FileID),
fmt.Sprintf(`file_format="%s"`, *d.Format), fmt.Sprintf(`file_format="%s"`, *d.Format),
fmt.Sprintf(`datastore_id_target="%s"`, *d.ID), fmt.Sprintf(`datastore_id_target="%s"`, *d.ID),
@ -403,9 +405,8 @@ func vmImportCustomDisks(ctx context.Context, d *schema.ResourceData, m interfac
out, err := api.SSH().ExecuteNodeCommands(ctx, nodeName, commands) out, err := api.SSH().ExecuteNodeCommands(ctx, nodeName, commands)
if err != nil { if err != nil {
if strings.Contains(err.Error(), "pvesm: not found") { if matches, e := regexp.Match(`pvesm: .* not found`, out); e == nil && matches {
return fmt.Errorf("The configured SSH user '%s' does not have the required permissions to import disks. "+ return ssh.NewErrSSHUserNoPermission(api.SSH().Username())
"Make sure `sudo` is installed and the user is a member of sudoers.", api.SSH().Username())
} }
return err return err