-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
force new snapshots on job run trigger
- Loading branch information
1 parent
89ebd9d
commit 990d735
Showing
4 changed files
with
95 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
//go:build linux | ||
|
||
package store | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
"time" | ||
|
||
"github.com/sonroyaalmerol/pbs-plus/internal/utils" | ||
"golang.org/x/crypto/ssh" | ||
) | ||
|
||
func (storeInstance *Store) AgentSnapshot(agentTarget *Target) (bool, error) { | ||
privKeyDir := filepath.Join(DbBasePath, "agent_keys") | ||
privKeyFile := filepath.Join(privKeyDir, strings.ReplaceAll(fmt.Sprintf("%s.key", agentTarget.Name), " ", "-")) | ||
|
||
pemBytes, err := os.ReadFile(privKeyFile) | ||
if err != nil { | ||
return false, fmt.Errorf("AgentSnapshot: error reading private key file -> %w", err) | ||
} | ||
|
||
signer, err := ssh.ParsePrivateKey(pemBytes) | ||
if err != nil { | ||
return false, fmt.Errorf("AgentSnapshot: error parsing private key -> %w", err) | ||
} | ||
|
||
agentPath := strings.TrimPrefix(agentTarget.Path, "agent://") | ||
agentPathParts := strings.Split(agentPath, "/") | ||
agentHost := agentPathParts[0] | ||
agentDrive := agentPathParts[1] | ||
agentDriveRune := []rune(agentDrive)[0] | ||
agentPort, err := utils.DriveLetterPort(agentDriveRune) | ||
|
||
sshConfig := &ssh.ClientConfig{ | ||
User: "proxmox", | ||
Auth: []ssh.AuthMethod{ssh.PublicKeys(signer)}, | ||
HostKeyCallback: ssh.InsecureIgnoreHostKey(), | ||
Timeout: time.Second * 2, | ||
} | ||
|
||
client, err := ssh.Dial("tcp", fmt.Sprintf("%s:%s", agentHost, agentPort), sshConfig) | ||
if err != nil { | ||
return false, fmt.Errorf("AgentSnapshot: error dialing ssh client -> %w", err) | ||
} | ||
defer client.Close() | ||
|
||
session, err := client.NewSession() | ||
if err != nil { | ||
return false, fmt.Errorf("AgentSnapshot: error creating new ssh session -> %w", err) | ||
} | ||
defer session.Close() | ||
|
||
resp, err := session.SendRequest("snapshot", true, []byte(agentDrive)) | ||
if err != nil { | ||
return false, fmt.Errorf("AgentSnapshot: error sending ping request -> %w", err) | ||
} | ||
|
||
return resp, nil | ||
} |