Skip to content

Commit

Permalink
fix ns and windows agent
Browse files Browse the repository at this point in the history
  • Loading branch information
sonroyaalmerol committed Nov 3, 2024
1 parent b2a5208 commit f38f124
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 84 deletions.
2 changes: 1 addition & 1 deletion build/package/windows/wix.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"product": "pbs-d2d-backup",
"product": "pbs-agent",
"company": "snry",
"license": "LICENSE",
"upgrade-code": "784ea465-1680-4997-9f00-ee8d228ab646",
Expand Down
104 changes: 25 additions & 79 deletions cmd/pbs_windows_agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,44 +17,21 @@ import (
"golang.org/x/sys/windows"

"github.com/getlantern/systray"
"github.com/kardianos/service"
"github.com/sonroyaalmerol/pbs-d2d-backup/internal/agent/sftp"
"github.com/sonroyaalmerol/pbs-d2d-backup/internal/agent/snapshots"
"github.com/sonroyaalmerol/pbs-d2d-backup/internal/utils"
)

//go:embed icon/logo.png
//go:embed icon/logo.ico
var icon []byte

var logger service.Logger

type AgentProgram struct {
Exec func()
}

func (p *AgentProgram) Start(s service.Service) error {
go p.Exec()
return nil
}
func (p *AgentProgram) Stop(s service.Service) error {
return nil
}

var svc service.Service

func main() {
if !isAdmin() {
fmt.Println("This program needs to be run as administrator.")
runAsAdmin()
os.Exit(0)
}

svcConfig := &service.Config{
Name: "PBSAgent",
DisplayName: "Proxmox Backup Agent",
Description: "Orchestrating backups with Proxmox Backup Server",
}

serverUrl, ok := os.LookupEnv("PBS_AGENT_SERVER")
if !ok {
for {
Expand All @@ -70,66 +47,44 @@ func main() {
utils.SetEnvironment("PBS_AGENT_SERVER", serverUrl)
}

var err error
svc, err = service.New(
&AgentProgram{Exec: run(serverUrl)},
svcConfig,
)
_, err := url.ParseRequestURI(serverUrl)
if err != nil {
showMessageBox("Error", err.Error())
showMessageBox("Error", fmt.Sprintf("Invalid server URL: %s", err))
os.Exit(1)
}

logger, err = svc.Logger(nil)
if err != nil {
showMessageBox("Error", err.Error())
os.Exit(1)
}
// Reserve port 33450-33476
drives := utils.GetLocalDrives()
ctx := context.Background()

svc.Run()
}
var wg sync.WaitGroup
for _, driveLetter := range drives {
rune := []rune(driveLetter)[0]

func run(serverUrl string) func() {
return func() {
_, err := url.ParseRequestURI(serverUrl)
sftpConfig, _ := sftp.InitializeSFTPConfig(serverUrl, driveLetter)

err = sftpConfig.PopulateKeys()
if err != nil {
showMessageBox("Error", fmt.Sprintf("Invalid server URL: %s", err))
showMessageBox("Error", fmt.Sprintf("Unable to populate SFTP keys: %s", err))
os.Exit(1)
}

// Reserve port 33450-33476
drives := utils.GetLocalDrives()
ctx := context.Background()

var wg sync.WaitGroup
for _, driveLetter := range drives {
rune := []rune(driveLetter)[0]

sftpConfig, _ := sftp.InitializeSFTPConfig(serverUrl, driveLetter)

err = sftpConfig.PopulateKeys()
if err != nil {
showMessageBox("Error", fmt.Sprintf("Unable to populate SFTP keys: %s", err))
os.Exit(1)
}

port, err := utils.DriveLetterPort(rune)
if err != nil {
showMessageBox("Error", fmt.Sprintf("Unable to map letter to port: %s", err))
os.Exit(1)
}

wg.Add(1)
go sftp.Serve(ctx, &wg, sftpConfig, "0.0.0.0", port, fmt.Sprintf("%s:\\", driveLetter))
port, err := utils.DriveLetterPort(rune)
if err != nil {
showMessageBox("Error", fmt.Sprintf("Unable to map letter to port: %s", err))
os.Exit(1)
}

defer snapshots.CloseAllSnapshots()
wg.Add(1)
go sftp.Serve(ctx, &wg, sftpConfig, "0.0.0.0", port, fmt.Sprintf("%s:\\", driveLetter))
}

systray.Run(onReady(serverUrl), onExit)
defer systray.Quit()
defer snapshots.CloseAllSnapshots()

wg.Wait()
}
systray.Run(onReady(serverUrl), onExit)
defer systray.Quit()

wg.Wait()
}

func showMessageBox(title, message string) {
Expand Down Expand Up @@ -168,19 +123,10 @@ func onReady(serverUrl string) func() {

serverIP := systray.AddMenuItem(fmt.Sprintf("Server: %s", url), "Proxmox Backup Server address")
serverIP.Disable()

mQuit := systray.AddMenuItem("Quit", "Quit service")

go func() {
<-mQuit.ClickedCh
systray.Quit()
}()
}
}

func onExit() {
snapshots.CloseAllSnapshots()
_ = svc.Stop()
}

func isAdmin() bool {
Expand Down
Binary file added cmd/pbs_windows_agent/icon/logo.ico
Binary file not shown.
8 changes: 4 additions & 4 deletions internal/proxy/controllers/jobs/jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func ExtJsJobHandler(storeInstance *store.Store) func(http.ResponseWriter, *http
Target: r.FormValue("target"),
Schedule: r.FormValue("schedule"),
Comment: r.FormValue("comment"),
Namespace: r.FormValue("namespace"),
Namespace: r.FormValue("ns"),
NotificationMode: r.FormValue("notification-mode"),
}

Expand Down Expand Up @@ -146,8 +146,8 @@ func ExtJsJobSingleHandler(storeInstance *store.Store) func(http.ResponseWriter,
if r.FormValue("comment") != "" {
job.Comment = r.FormValue("comment")
}
if r.FormValue("namespace") != "" {
job.Namespace = r.FormValue("namespace")
if r.FormValue("ns") != "" {
job.Namespace = r.FormValue("ns")
}
if r.FormValue("notification-mode") != "" {
job.NotificationMode = r.FormValue("notification-mode")
Expand All @@ -164,7 +164,7 @@ func ExtJsJobSingleHandler(storeInstance *store.Store) func(http.ResponseWriter,
job.Schedule = ""
case "comment":
job.Comment = ""
case "namespace":
case "ns":
job.Namespace = ""
case "notification-mode":
job.NotificationMode = ""
Expand Down

0 comments on commit f38f124

Please sign in to comment.