mirror of
https://github.com/fleetdm/fleet
synced 2026-05-08 09:40:49 +00:00
For #25924 This PR attempts to fix the issue where the Fleet desktop icon sometimes fails to appear on MacOS hosts until the hosts are rebooted. Anecdotal evidence points to this being an issue when system setup is happening, leading to the theory that Orbit is attempting to launch the app as `_mbsetupuser` rather than the real logged-in user. The fix here is to use a different command to get the name of the logged-in user (ignoring `_mbsetupuser` if it appears), and to launch the desktop app as that user using `sudo`. I have tested this on MacOS and Ubuntu hosts, and verified that the desktop app launches as expected on both. We don't have a solid reproduction scenario for the issue, but we do have [some ways to look for relevant errors](https://github.com/fleetdm/fleet/issues/19172#issuecomment-2627812786), so we can try this out and see if those errors cease.
61 lines
1.6 KiB
Go
61 lines
1.6 KiB
Go
package execuser
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"os/exec"
|
|
)
|
|
|
|
// run uses macOS open command to start application as the current login user.
|
|
// Note that the child process spawns a new process in user space and thus it is not
|
|
// effective to add a context to this function to cancel the child process.
|
|
func run(path string, opts eopts) (lastLogs string, err error) {
|
|
info, err := os.Stat(path)
|
|
if err != nil {
|
|
return "", fmt.Errorf("stat path %q: %w", path, err)
|
|
}
|
|
|
|
if !info.IsDir() {
|
|
return "", fmt.Errorf("path is not an .app directory: %s", path)
|
|
}
|
|
var arg []string
|
|
if opts.stderrPath != "" {
|
|
arg = append(arg, "--stderr", opts.stderrPath)
|
|
}
|
|
|
|
// set environment variables
|
|
for _, nv := range opts.env {
|
|
arg = append(arg, "--env", fmt.Sprintf("%s=%s", nv[0], nv[1]))
|
|
}
|
|
|
|
// set the path to be executed
|
|
arg = append(arg, path)
|
|
|
|
// set the program arguments
|
|
if len(opts.args) > 0 {
|
|
arg = append(arg, "--args")
|
|
for _, nv := range opts.args {
|
|
arg = append(arg, nv[0], nv[1])
|
|
}
|
|
}
|
|
|
|
arg = append([]string{"-u", opts.user, "/usr/bin/open"}, arg...)
|
|
cmd := exec.Command("sudo", arg...)
|
|
tw := &TransientWriter{}
|
|
cmd.Stderr = io.MultiWriter(tw, os.Stderr)
|
|
cmd.Stdout = io.MultiWriter(tw, os.Stdout)
|
|
if err := cmd.Run(); err != nil {
|
|
return tw.String(), fmt.Errorf("open path %q: %w", path, err)
|
|
}
|
|
return tw.String(), nil
|
|
}
|
|
|
|
func runWithOutput(path string, opts eopts) (output []byte, exitCode int, err error) {
|
|
return nil, 0, errors.New("not implemented")
|
|
}
|
|
|
|
func runWithStdin(path string, opts eopts) (io.WriteCloser, error) {
|
|
return nil, errors.New("not implemented")
|
|
}
|