From a01856626280a6ded1d449b3c975d08ebe930b23 Mon Sep 17 00:00:00 2001 From: Son Roy Almerol Date: Mon, 11 Nov 2024 08:27:28 -0500 Subject: [PATCH] remove windows agent shortcut + fix some workflows --- build/package/debian/DEBIAN/postinst | 6 +- cmd/windows_agent/main.go | 89 ---------------------------- cmd/windows_agent/service.go | 42 ++++--------- cmd/windows_agent/systray.go | 6 -- 4 files changed, 16 insertions(+), 127 deletions(-) diff --git a/build/package/debian/DEBIAN/postinst b/build/package/debian/DEBIAN/postinst index b15bcee..a83a2b8 100755 --- a/build/package/debian/DEBIAN/postinst +++ b/build/package/debian/DEBIAN/postinst @@ -1,14 +1,14 @@ #!/bin/bash set -e -chmod +x /usr/bin/pbs-plus-overlay +chmod +x /usr/bin/pbs-plus systemctl daemon-reload # Enable the service to start on boot -systemctl enable pbs-plus-overlay.service +systemctl enable pbs-plus.service # Start the service -systemctl start pbs-plus-overlay.service +systemctl start pbs-plus.service exit 0 diff --git a/cmd/windows_agent/main.go b/cmd/windows_agent/main.go index 6cff03f..dd57c82 100644 --- a/cmd/windows_agent/main.go +++ b/cmd/windows_agent/main.go @@ -7,11 +7,7 @@ import ( "context" "fmt" "os" - "os/user" - "path/filepath" - "github.com/go-ole/go-ole" - "github.com/go-ole/go-ole/oleutil" "github.com/kardianos/service" "github.com/sonroyaalmerol/pbs-plus/internal/syslog" ) @@ -59,12 +55,6 @@ func main() { logger.Errorf("Failed to install service: %s", err) } else { logger.Info("Service installed") - err = createStartupShortcut() - if err != nil { - logger.Errorf("Failed to create startup shortcut: %s", err) - } else { - logger.Info("Startup shortcut created") - } } return case "uninstall": @@ -73,12 +63,6 @@ func main() { logger.Errorf("Failed to uninstall service: %s", err) } else { logger.Info("Service uninstalled") - err = removeStartupShortcut() - if err != nil { - logger.Errorf("Failed to remove startup shortcut: %s", err) - } else { - logger.Info("Startup shortcut removed") - } } return case "start": @@ -116,76 +100,3 @@ func main() { } } } - -// createStartupShortcut creates a shortcut in the Windows Startup folder for the application. -func createStartupShortcut() error { - usr, err := user.Current() - if err != nil { - return fmt.Errorf("failed to get current user: %w", err) - } - - startupDir := filepath.Join(usr.HomeDir, "AppData", "Roaming", "Microsoft", "Windows", "Start Menu", "Programs", "Startup") - shortcutPath := filepath.Join(startupDir, "PBSPlusAgent.lnk") - - // Get the executable path - executable, err := os.Executable() - if err != nil { - return fmt.Errorf("failed to get executable path: %w", err) - } - - // Initialize COM library - ole.CoInitialize(0) - defer ole.CoUninitialize() - - // Create the WScript.Shell COM object - shell, err := oleutil.CreateObject("WScript.Shell") - if err != nil { - return fmt.Errorf("failed to create WScript.Shell object: %w", err) - } - defer shell.Release() - - // Cast the object to IDispatch - shellDispatch, err := shell.QueryInterface(ole.IID_IDispatch) - if err != nil { - return fmt.Errorf("failed to query IDispatch interface: %w", err) - } - defer shellDispatch.Release() - - // Create the shortcut using the COM object - link, err := oleutil.CallMethod(shellDispatch, "CreateShortcut", shortcutPath) - if err != nil { - return fmt.Errorf("failed to create shortcut: %w", err) - } - defer link.Clear() - - // Set shortcut properties - oleutil.PutProperty(link.ToIDispatch(), "TargetPath", executable) - oleutil.PutProperty(link.ToIDispatch(), "WorkingDirectory", filepath.Dir(executable)) - oleutil.PutProperty(link.ToIDispatch(), "WindowStyle", 1) // Normal window - oleutil.PutProperty(link.ToIDispatch(), "Description", "PBS Plus Agent") - - // Save the shortcut - _, err = oleutil.CallMethod(link.ToIDispatch(), "Save") - if err != nil { - return fmt.Errorf("failed to save shortcut: %w", err) - } - - return nil -} - -// removeStartupShortcut removes the shortcut from the Windows Startup folder. -func removeStartupShortcut() error { - usr, err := user.Current() - if err != nil { - return fmt.Errorf("failed to get current user: %w", err) - } - - startupDir := filepath.Join(usr.HomeDir, "AppData", "Roaming", "Microsoft", "Windows", "Start Menu", "Programs", "Startup") - shortcutPath := filepath.Join(startupDir, "PBSPlusAgent.lnk") - - err = os.Remove(shortcutPath) - if err != nil && !os.IsNotExist(err) { - return fmt.Errorf("failed to remove startup shortcut: %w", err) - } - return nil -} diff --git a/cmd/windows_agent/service.go b/cmd/windows_agent/service.go index e63b870..fecb8bd 100644 --- a/cmd/windows_agent/service.go +++ b/cmd/windows_agent/service.go @@ -36,12 +36,10 @@ type agentService struct { pingCancel context.CancelFunc wg sync.WaitGroup exit chan struct{} - restart chan struct{} } func (p *agentService) Start(s service.Service) error { p.exit = make(chan struct{}) - p.restart = make(chan struct{}) p.ctx, p.cancel = context.WithCancel(context.Background()) p.pingCtx, p.pingCancel = context.WithCancel(context.Background()) @@ -78,21 +76,24 @@ func (p *agentService) runLoop() { close(p.exit) }() + logger, err := syslog.InitializeLogger(p.svc) + if err != nil { + utils.SetEnvironment("PBS_AGENT_STATUS", fmt.Sprintf("Failed to initialize logger -> %s", err.Error())) + return + } + for { - select { - case <-p.exit: - return - default: - p.run() - } + p.run() + wgDone := utils.WaitChan(&p.wg) select { case <-p.exit: + snapshots.CloseAllSnapshots() return - case <-p.restart: - p.cancel() - p.wg.Wait() - p.ctx, p.cancel = context.WithCancel(context.Background()) + case <-wgDone: + utils.SetEnvironment("PBS_AGENT_STATUS", "Unexpected shutdown - restarting SSH endpoints") + logger.Error("SSH endpoints stopped unexpectedly. Restarting...") + p.wg = sync.WaitGroup{} } } } @@ -147,18 +148,6 @@ waitUrl: go sftp.Serve(p.ctx, &p.wg, sftpConfig, "0.0.0.0", port, driveLetter) } - wgDone := utils.WaitChan(&p.wg) - - select { - case <-p.ctx.Done(): - snapshots.CloseAllSnapshots() - return - case <-wgDone: - utils.SetEnvironment("PBS_AGENT_STATUS", "Unexpected shutdown - restarting SFTP servers") - logger.Error("SFTP servers stopped unexpectedly. Restarting...") - // Reset the WaitGroup to prepare for new SFTP servers - p.wg = sync.WaitGroup{} - } } } @@ -170,8 +159,3 @@ func (p *agentService) Stop(s service.Service) error { p.wg.Wait() return nil } - -func (p *agentService) Restart() { - p.restart <- struct{}{} -} - diff --git a/cmd/windows_agent/systray.go b/cmd/windows_agent/systray.go index 8806180..7b58e3d 100644 --- a/cmd/windows_agent/systray.go +++ b/cmd/windows_agent/systray.go @@ -149,12 +149,6 @@ func (p *agentTray) onReady(url string) func() { continue } } - - err = p.svc.Restart() - if err != nil { - logger.Errorf("Failed to restart service: %s", err) - continue - } } } }(p.ctx, changeUrl)