Skip to content

Commit

Permalink
remove windows agent shortcut + fix some workflows
Browse files Browse the repository at this point in the history
  • Loading branch information
Son Roy Almerol committed Nov 11, 2024
1 parent ee610d0 commit a018566
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 127 deletions.
6 changes: 3 additions & 3 deletions build/package/debian/DEBIAN/postinst
Original file line number Diff line number Diff line change
@@ -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
89 changes: 0 additions & 89 deletions cmd/windows_agent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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":
Expand All @@ -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":
Expand Down Expand Up @@ -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
}
42 changes: 13 additions & 29 deletions cmd/windows_agent/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand Down Expand Up @@ -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{}
}
}
}
Expand Down Expand Up @@ -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{}
}
}
}

Expand All @@ -170,8 +159,3 @@ func (p *agentService) Stop(s service.Service) error {
p.wg.Wait()
return nil
}

func (p *agentService) Restart() {
p.restart <- struct{}{}
}

6 changes: 0 additions & 6 deletions cmd/windows_agent/systray.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit a018566

Please sign in to comment.