-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Implement clean verb for the agent (#702)
``` ubuntu-pro-agent.exe clean ``` This command removes directories `~/.ubuntupro` and `$LocalAppData\Ubuntu Pro`. Eventually the MSIX uninstaller should call this command so as to clean up after itself. It's also useful for local testing. --- UDENG-2231
- Loading branch information
Showing
5 changed files
with
293 additions
and
103 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package agent | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"time" | ||
|
||
"github.com/canonical/ubuntu-pro-for-wsl/common" | ||
"github.com/canonical/ubuntu-pro-for-wsl/common/i18n" | ||
log "github.com/sirupsen/logrus" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func (a *App) installClean() { | ||
cmd := &cobra.Command{ | ||
Use: "clean", | ||
Short: i18n.G("Removes all the agent's data and exits"), | ||
Args: cobra.NoArgs, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
defer log.Debug("clean command finished") | ||
|
||
// Stop the agent so that it doesn't interfere with file removal. | ||
if err := stopAgent(); err != nil { | ||
log.Warningf("could not stop agent: %v", err) | ||
} | ||
|
||
// Clean up the agent's data. | ||
return errors.Join( | ||
cleanLocation("LocalAppData", common.LocalAppDataDir), | ||
cleanLocation("UserProfile", common.UserProfileDir), | ||
) | ||
}, | ||
} | ||
a.rootCmd.AddCommand(cmd) | ||
} | ||
|
||
// stopAgent stops all other ubuntu-pro-agent instances (but not itself!). | ||
func stopAgent() error { | ||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) | ||
defer cancel() | ||
|
||
filterPID := fmt.Sprintf("PID ne %d", os.Getpid()) | ||
|
||
//nolint:gosec // The return value of cmdName() is not user input. | ||
out, err := exec.CommandContext(ctx, "taskkill.exe", | ||
"/F", // Force-stop the process | ||
"/IM", cmdName(), // Match the process name | ||
"/FI", filterPID, // Filter out the current process. | ||
).CombinedOutput() | ||
if err != nil { | ||
return fmt.Errorf("could not stop process %s: %v. %s", cmdName(), err, out) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func cleanLocation(rootEnv, relpath string) error { | ||
root := os.Getenv(rootEnv) | ||
if root == "" { | ||
return fmt.Errorf("could not clean up location: environment variable %q is not set", rootEnv) | ||
} | ||
|
||
path := filepath.Join(root, relpath) | ||
if err := os.RemoveAll(path); err != nil { | ||
return fmt.Errorf("could not clean up location %s: %v", path, err) | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.