From 5e39db030efdd2fc2997ba2f70472fc284e340ba Mon Sep 17 00:00:00 2001 From: wh1te909 Date: Sat, 17 Oct 2020 15:33:32 -0700 Subject: [PATCH] cleanup old agent files on upgrade --- agent/agent_windows.go | 44 ++++++++++++++++++++++++++++++++++++++++ agent/service_windows.go | 1 + 2 files changed, 45 insertions(+) diff --git a/agent/agent_windows.go b/agent/agent_windows.go index 63aff1f..90914e2 100644 --- a/agent/agent_windows.go +++ b/agent/agent_windows.go @@ -818,3 +818,47 @@ func (a *WindowsAgent) installerMsg(msg, alert string) { a.Logger.Fatalln(msg) } } + +// CleanupPythonAgent cleans up files from the old python agent if this is an upgrade +func (a *WindowsAgent) CleanupPythonAgent() { + cderr := os.Chdir(a.ProgramDir) + if cderr != nil { + return + } + + fileMatches := []string{ + "*.dll", + "*.pyd", + "base_library*", + "*.manifest", + "onit.ico", + "VERSION", + } + + for _, match := range fileMatches { + files, err := filepath.Glob(match) + if err == nil { + for _, f := range files { + os.Remove(f) + } + } + } + + folderMatches := []string{ + "certifi", + "Include", + "lib2to3", + "psutil", + "tcl", + "tk", + } + + for _, match := range folderMatches { + folders, err := filepath.Glob(match) + if err == nil { + for _, f := range folders { + os.RemoveAll(f) + } + } + } +} diff --git a/agent/service_windows.go b/agent/service_windows.go index 5667ac7..32af931 100644 --- a/agent/service_windows.go +++ b/agent/service_windows.go @@ -32,6 +32,7 @@ type HelloPatch struct { // WinAgentSvc tacticalagent windows nssm service func (a *WindowsAgent) WinAgentSvc() { a.Logger.Infoln("Agent service started") + a.CleanupPythonAgent() var data map[string]interface{} var sleep int