|
| 1 | +// Package autostart manage start at login unit files for darwin/linux |
| 2 | +package autostart |
| 3 | + |
| 4 | +import ( |
| 5 | + _ "embed" |
| 6 | + "errors" |
| 7 | + "fmt" |
| 8 | + "os" |
| 9 | + "os/exec" |
| 10 | + "path" |
| 11 | + "path/filepath" |
| 12 | + "strconv" |
| 13 | + "strings" |
| 14 | + |
| 15 | + "github.com/lima-vm/lima/pkg/textutil" |
| 16 | +) |
| 17 | + |
| 18 | +//go:embed lima-vm@INSTANCE.service |
| 19 | +var systemdTemplate string |
| 20 | + |
| 21 | +//go:embed io.lima-vm.autostart.INSTANCE.plist |
| 22 | +var launchdTemplate string |
| 23 | + |
| 24 | +// CreateStartAtLoginEntry respect host OS arch and create unit file |
| 25 | +func CreateStartAtLoginEntry(hostOS, instName, workDir string) error { |
| 26 | + unitPath := GetFilePath(hostOS, instName) |
| 27 | + if _, err := os.Stat(unitPath); err != nil && !errors.Is(err, os.ErrNotExist) { |
| 28 | + return err |
| 29 | + } |
| 30 | + tmpl, err := renderTemplate(hostOS, instName, workDir, os.Executable) |
| 31 | + if err != nil { |
| 32 | + return err |
| 33 | + } |
| 34 | + if err := os.MkdirAll(filepath.Dir(unitPath), os.ModePerm); err != nil { |
| 35 | + return err |
| 36 | + } |
| 37 | + if err := os.WriteFile(unitPath, tmpl, 0o644); err != nil { |
| 38 | + return err |
| 39 | + } |
| 40 | + return enableDisableService("enable", hostOS, GetFilePath(hostOS, instName)) |
| 41 | +} |
| 42 | + |
| 43 | +// DeleteStartAtLoginEntry respect host OS arch and delete unit file |
| 44 | +// return true, nil if unit file has been deleted |
| 45 | +func DeleteStartAtLoginEntry(hostOS, instName string) (bool, error) { |
| 46 | + unitPath := GetFilePath(hostOS, instName) |
| 47 | + if _, err := os.Stat(unitPath); err != nil { |
| 48 | + return false, err |
| 49 | + } |
| 50 | + if err := enableDisableService("disable", hostOS, GetFilePath(hostOS, instName)); err != nil { |
| 51 | + return false, err |
| 52 | + } |
| 53 | + if err := os.Remove(unitPath); err != nil { |
| 54 | + return false, err |
| 55 | + } |
| 56 | + return true, nil |
| 57 | +} |
| 58 | + |
| 59 | +// GetFilePath returns the path to autostart file with respect of host |
| 60 | +func GetFilePath(hostOS, instName string) string { |
| 61 | + var fileTmpl string |
| 62 | + if hostOS == "darwin" { // launchd plist |
| 63 | + fileTmpl = fmt.Sprintf("%s/Library/LaunchAgents/io.lima-vm.autostart.%s.plist", os.Getenv("HOME"), instName) |
| 64 | + } |
| 65 | + if hostOS == "linux" { // systemd service |
| 66 | + // Use instance name as argument to systemd service |
| 67 | + // Instance name available in unit file as %i |
| 68 | + xdgConfigHome := os.Getenv("XDG_CONFIG_HOME") |
| 69 | + if xdgConfigHome == "" { |
| 70 | + xdgConfigHome = filepath.Join(os.Getenv("HOME"), ".config") |
| 71 | + } |
| 72 | + fileTmpl = fmt.Sprintf("%s/systemd/user/lima-vm@%s.service", xdgConfigHome, instName) |
| 73 | + } |
| 74 | + return fileTmpl |
| 75 | +} |
| 76 | + |
| 77 | +func enableDisableService(action, hostOS, serviceWithPath string) error { |
| 78 | + // Get filename without extension |
| 79 | + filename := strings.TrimSuffix(path.Base(serviceWithPath), filepath.Ext(path.Base(serviceWithPath))) |
| 80 | + |
| 81 | + var args []string |
| 82 | + if hostOS == "darwin" { |
| 83 | + // man launchctl |
| 84 | + args = append(args, []string{ |
| 85 | + "launchctl", |
| 86 | + action, |
| 87 | + fmt.Sprintf("gui/%s/%s", strconv.Itoa(os.Getuid()), filename), |
| 88 | + }...) |
| 89 | + } else { |
| 90 | + args = append(args, []string{ |
| 91 | + "systemctl", |
| 92 | + "--user", |
| 93 | + action, |
| 94 | + filename, |
| 95 | + }...) |
| 96 | + } |
| 97 | + cmd := exec.Command(args[0], args[1:]...) |
| 98 | + cmd.Stdout = os.Stdout |
| 99 | + cmd.Stderr = os.Stderr |
| 100 | + return cmd.Run() |
| 101 | +} |
| 102 | + |
| 103 | +func renderTemplate(hostOS, instName, workDir string, getExecutable func() (string, error)) ([]byte, error) { |
| 104 | + selfExeAbs, err := getExecutable() |
| 105 | + if err != nil { |
| 106 | + return nil, err |
| 107 | + } |
| 108 | + tmpToExecute := systemdTemplate |
| 109 | + if hostOS == "darwin" { |
| 110 | + tmpToExecute = launchdTemplate |
| 111 | + } |
| 112 | + return textutil.ExecuteTemplate( |
| 113 | + tmpToExecute, |
| 114 | + map[string]string{ |
| 115 | + "Binary": selfExeAbs, |
| 116 | + "Instance": instName, |
| 117 | + "WorkDir": workDir, |
| 118 | + }) |
| 119 | +} |
0 commit comments