Skip to content

Commit

Permalink
Merge pull request #7603 from mook-as/factory-reset-shutdown-extensions
Browse files Browse the repository at this point in the history
Factory reset: shutdown extensions
  • Loading branch information
jandubois authored Oct 17, 2024
2 parents b94404c + 3a3ccc3 commit b172203
Show file tree
Hide file tree
Showing 14 changed files with 400 additions and 171 deletions.
3 changes: 3 additions & 0 deletions .github/actions/spelling/expect.txt
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,7 @@ Kgm
kiali
kib
killall
Kinfo
kiwano
KNOWNFOLDERID
kontainer
Expand Down Expand Up @@ -596,6 +597,7 @@ PFlags
PGID
pgrep
pidfile
pidfd
pids
PII
pikachu
Expand Down Expand Up @@ -627,6 +629,7 @@ PQgrl
prakhar
prebuilds
Privs
PROCARGS
progresskey
projectroletemplatebinding
Prometheis
Expand Down
2 changes: 1 addition & 1 deletion src/go/rdctl/cmd/factoryReset.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ Use the --remove-kubernetes-cache=BOOLEAN flag to also remove the cached Kuberne
if err != nil {
return fmt.Errorf("failed to get paths: %w", err)
}
return factoryreset.DeleteData(paths, removeKubernetesCache)
return factoryreset.DeleteData(cmd.Context(), paths, removeKubernetesCache)
},
}

Expand Down
53 changes: 53 additions & 0 deletions src/go/rdctl/pkg/directories/directories.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
Copyright © 2024 SUSE LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package directories

import (
"os"
"path/filepath"
"runtime"
)

// GetApplicationDirectory returns the installation directory of the application.
func GetApplicationDirectory() (string, error) {
exePathWithSymlinks, err := os.Executable()
if err != nil {
return "", err
}

exePath, err := filepath.EvalSymlinks(exePathWithSymlinks)
if err != nil {
return "", err
}

platform := runtime.GOOS
if runtime.GOOS == "windows" {
// On Windows, we use "win32" instead of "windows".
platform = "win32"
}

// Given the path to the exe, find its directory, and drop the
// "resources\win32\bin" suffix (possibly with another "resources" in front).
// On mac, we need to drop "Contents/Resources/resources/darwin/bin".
resultDir := filepath.Dir(exePath)
for _, part := range []string{"bin", platform, "resources", "Resources", "Contents"} {
for filepath.Base(resultDir) == part {
resultDir = filepath.Dir(resultDir)
}
}
return resultDir, nil
}
31 changes: 31 additions & 0 deletions src/go/rdctl/pkg/directories/directories_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
Copyright © 2024 SUSE LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package directories_test

import (
"testing"

"github.com/rancher-sandbox/rancher-desktop/src/go/rdctl/pkg/directories"
"github.com/stretchr/testify/assert"
)

func TestGetApplicationDirectory(t *testing.T) {
_, err := directories.GetApplicationDirectory()
assert.NoError(t, err)
// `go test` makes a temporary directory, so we can't sensibly test the
// return value.
}
38 changes: 0 additions & 38 deletions src/go/rdctl/pkg/directories/directories_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package directories
import (
"errors"
"fmt"
"path/filepath"
"unsafe"

"golang.org/x/sys/windows"
Expand All @@ -44,43 +43,6 @@ func InvokeWin32WithBuffer(cb func(size int) error) error {
}
}

// GetApplicationDirectory returns the installation directory of the application.
func GetApplicationDirectory() (string, error) {
var exePath string
err := InvokeWin32WithBuffer(func(bufSize int) error {
buf := make([]uint16, bufSize)
n, err := windows.GetModuleFileName(windows.Handle(0), &buf[0], uint32(bufSize))
if err != nil {
return err
}
if n == uint32(bufSize) {
// If the buffer is too small, GetModuleFileName returns the buffer size,
// and the result includes the null character. If the buffer is large
// enough, GetModuleFileName returns the string length, _excluding_ the
// null character.
if buf[bufSize-1] == 0 {
// The buffer contains a null character
return windows.ERROR_INSUFFICIENT_BUFFER
}
}
exePath = windows.UTF16ToString(buf[:n])
return nil
})
if err != nil {
return "", err
}

// Given the path to the exe, find its directory, and drop the
// "resources\win32\bin" suffix (possibly with another "resources" in front).
resultDir := filepath.Dir(exePath)
for _, part := range []string{"bin", "win32", "resources"} {
for filepath.Base(resultDir) == part {
resultDir = filepath.Dir(resultDir)
}
}
return resultDir, nil
}

func GetLocalAppDataDirectory() (string, error) {
dir, err := getKnownFolder(windows.FOLDERID_LocalAppData)
if err != nil {
Expand Down
7 changes: 0 additions & 7 deletions src/go/rdctl/pkg/directories/directories_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,6 @@ import (
"golang.org/x/sys/windows"
)

func TestGetApplicationDirectory(t *testing.T) {
_, err := GetApplicationDirectory()
assert.NoError(t, err)
// `go test` makes a temporary directory, so we can't sensibly test the
// return value.
}

func TestGetKnownFolder(t *testing.T) {
t.Run("AppData", func(t *testing.T) {
expected := os.Getenv("APPDATA")
Expand Down
8 changes: 7 additions & 1 deletion src/go/rdctl/pkg/factoryreset/delete_data_darwin.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
package factoryreset

import (
"context"
"os"
"path/filepath"

"github.com/rancher-sandbox/rancher-desktop/src/go/rdctl/pkg/autostart"
"github.com/rancher-sandbox/rancher-desktop/src/go/rdctl/pkg/paths"
"github.com/rancher-sandbox/rancher-desktop/src/go/rdctl/pkg/process"
"github.com/sirupsen/logrus"
)

func DeleteData(appPaths paths.Paths, removeKubernetesCache bool) error {
func DeleteData(ctx context.Context, appPaths paths.Paths, removeKubernetesCache bool) error {
if err := autostart.EnsureAutostart(false); err != nil {
logrus.Errorf("Failed to remove autostart configuration: %s", err)
}

if err := process.TerminateProcessInDirectory(appPaths.ExtensionRoot, false); err != nil {
logrus.Errorf("Failed to stop extension processes, ignoring: %s", err)
}

pathList := []string{
appPaths.AltAppHome,
appPaths.Config,
Expand Down
8 changes: 7 additions & 1 deletion src/go/rdctl/pkg/factoryreset/delete_data_linux.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package factoryreset

import (
"context"
"os"
"path/filepath"

"github.com/rancher-sandbox/rancher-desktop/src/go/rdctl/pkg/autostart"
"github.com/rancher-sandbox/rancher-desktop/src/go/rdctl/pkg/paths"
"github.com/rancher-sandbox/rancher-desktop/src/go/rdctl/pkg/process"
"github.com/sirupsen/logrus"
)

func DeleteData(appPaths paths.Paths, removeKubernetesCache bool) error {
func DeleteData(ctx context.Context, appPaths paths.Paths, removeKubernetesCache bool) error {
if err := autostart.EnsureAutostart(false); err != nil {
logrus.Errorf("Failed to remove autostart configuration: %s", err)
}
Expand All @@ -19,6 +21,10 @@ func DeleteData(appPaths paths.Paths, removeKubernetesCache bool) error {
logrus.Errorf("Error getting home directory: %s", err)
}

if err := process.TerminateProcessInDirectory(appPaths.ExtensionRoot, false); err != nil {
logrus.Errorf("Failed to stop extension processes, ignoring: %s", err)
}

pathList := []string{
appPaths.AltAppHome,
appPaths.Config,
Expand Down
8 changes: 7 additions & 1 deletion src/go/rdctl/pkg/factoryreset/delete_data_windows.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package factoryreset

import (
"context"

"github.com/rancher-sandbox/rancher-desktop/src/go/rdctl/pkg/autostart"
"github.com/rancher-sandbox/rancher-desktop/src/go/rdctl/pkg/paths"
"github.com/rancher-sandbox/rancher-desktop/src/go/rdctl/pkg/process"
"github.com/rancher-sandbox/rancher-desktop/src/go/rdctl/pkg/wsl"
"github.com/sirupsen/logrus"
)

func DeleteData(paths paths.Paths, removeKubernetesCache bool) error {
func DeleteData(ctx context.Context, appPaths paths.Paths, removeKubernetesCache bool) error {
if err := autostart.EnsureAutostart(false); err != nil {
logrus.Errorf("Failed to remove autostart configuration: %s", err)
}
Expand All @@ -16,6 +19,9 @@ func DeleteData(paths paths.Paths, removeKubernetesCache bool) error {
logrus.Errorf("could not unregister WSL: %s", err)
return err
}
if err := process.TerminateProcessInDirectory(appPaths.ExtensionRoot, false); err != nil {
logrus.Errorf("Failed to stop extension processes, ignoring: %s", err)
}
if err := deleteWindowsData(!removeKubernetesCache, "rancher-desktop"); err != nil {
logrus.Errorf("could not delete data: %s", err)
return err
Expand Down
Loading

0 comments on commit b172203

Please sign in to comment.