| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | package autostart |
| 6 | |
| 7 | import ( |
| 8 | "context" |
| 9 | "errors" |
| 10 | "fmt" |
| 11 | "maps" |
| 12 | "os" |
| 13 | "path/filepath" |
| 14 | "runtime" |
| 15 | |
| 16 | "github.com/lima-vm/lima/v2/pkg/limatype" |
| 17 | "github.com/lima-vm/lima/v2/pkg/limatype/dirnames" |
| 18 | "github.com/lima-vm/lima/v2/pkg/textutil" |
| 19 | ) |
| 20 | |
| 21 | type notSupportedManager struct{} |
| 22 | |
| 23 | var _ autoStartManager = (*notSupportedManager)(nil) |
| 24 | |
| 25 | var ErrNotSupported = fmt.Errorf("autostart is not supported on %s", runtime.GOOS) |
| 26 | |
| 27 | func (*notSupportedManager) IsRegistered(_ context.Context, _ *limatype.Instance) (bool, error) { |
| 28 | return false, ErrNotSupported |
| 29 | } |
| 30 | |
| 31 | func (*notSupportedManager) RegisterToStartAtLogin(_ context.Context, _ *limatype.Instance) error { |
| 32 | return ErrNotSupported |
| 33 | } |
| 34 | |
| 35 | func (*notSupportedManager) UnregisterFromStartAtLogin(_ context.Context, _ *limatype.Instance) error { |
| 36 | return ErrNotSupported |
| 37 | } |
| 38 | |
| 39 | func (*notSupportedManager) AutoStartedIdentifier() string { |
| 40 | return "" |
| 41 | } |
| 42 | |
| 43 | func (*notSupportedManager) RequestStart(_ context.Context, _ *limatype.Instance) error { |
| 44 | return ErrNotSupported |
| 45 | } |
| 46 | |
| 47 | func (*notSupportedManager) RequestStop(_ context.Context, _ *limatype.Instance) (bool, error) { |
| 48 | return false, ErrNotSupported |
| 49 | } |
| 50 | |
| 51 | |
| 52 | type TemplateFileBasedManager struct { |
| 53 | enabler func(ctx context.Context, enable bool, instName string) error |
| 54 | filePath func(instName string) string |
| 55 | template string |
| 56 | extraTemplateVars map[string]string |
| 57 | autoStartedIdentifier func() string |
| 58 | requestStart func(ctx context.Context, inst *limatype.Instance) error |
| 59 | requestStop func(ctx context.Context, inst *limatype.Instance) (bool, error) |
| 60 | } |
| 61 | |
| 62 | var _ autoStartManager = (*TemplateFileBasedManager)(nil) |
| 63 | |
| 64 | func (t *TemplateFileBasedManager) IsRegistered(_ context.Context, inst *limatype.Instance) (bool, error) { |
| 65 | if t.filePath == nil { |
| 66 | return false, errors.New("no filePath function available") |
| 67 | } |
| 68 | autostartFilePath := t.filePath(inst.Name) |
| 69 | if _, err := os.Stat(autostartFilePath); err != nil { |
| 70 | if os.IsNotExist(err) { |
| 71 | return false, nil |
| 72 | } |
| 73 | return false, err |
| 74 | } |
| 75 | return true, nil |
| 76 | } |
| 77 | |
| 78 | func (t *TemplateFileBasedManager) RegisterToStartAtLogin(ctx context.Context, inst *limatype.Instance) error { |
| 79 | if _, err := t.IsRegistered(ctx, inst); err != nil { |
| 80 | return fmt.Errorf("failed to check if the autostart entry for instance %#q is registered: %w", inst.Name, err) |
| 81 | } |
| 82 | content, err := t.renderTemplate(inst.Name, inst.Dir, os.Executable) |
| 83 | if err != nil { |
| 84 | return fmt.Errorf("failed to render the autostart entry for instance %#q: %w", inst.Name, err) |
| 85 | } |
| 86 | entryFilePath := t.filePath(inst.Name) |
| 87 | if err := os.MkdirAll(filepath.Dir(entryFilePath), os.ModePerm); err != nil { |
| 88 | return fmt.Errorf("failed to create the directory for the autostart entry for instance %#q: %w", inst.Name, err) |
| 89 | } |
| 90 | if err := os.WriteFile(entryFilePath, content, 0o644); err != nil { |
| 91 | return fmt.Errorf("failed to write the autostart entry for instance %#q: %w", inst.Name, err) |
| 92 | } |
| 93 | if t.enabler != nil { |
| 94 | return t.enabler(ctx, true, inst.Name) |
| 95 | } |
| 96 | return nil |
| 97 | } |
| 98 | |
| 99 | func (t *TemplateFileBasedManager) UnregisterFromStartAtLogin(ctx context.Context, inst *limatype.Instance) error { |
| 100 | if registered, err := t.IsRegistered(ctx, inst); err != nil { |
| 101 | return fmt.Errorf("failed to check if the autostart entry for instance %#q is registered: %w", inst.Name, err) |
| 102 | } else if !registered { |
| 103 | return nil |
| 104 | } |
| 105 | if t.enabler != nil { |
| 106 | if err := t.enabler(ctx, false, inst.Name); err != nil { |
| 107 | return fmt.Errorf("failed to disable the autostart entry for instance %#q: %w", inst.Name, err) |
| 108 | } |
| 109 | } |
| 110 | if err := os.Remove(t.filePath(inst.Name)); err != nil { |
| 111 | return fmt.Errorf("failed to remove the autostart entry for instance %#q: %w", inst.Name, err) |
| 112 | } |
| 113 | return nil |
| 114 | } |
| 115 | |
| 116 | func (t *TemplateFileBasedManager) renderTemplate(instName, workDir string, getExecutable func() (string, error)) ([]byte, error) { |
| 117 | if t.template == "" { |
| 118 | return nil, errors.New("no template available") |
| 119 | } |
| 120 | selfExeAbs, err := getExecutable() |
| 121 | if err != nil { |
| 122 | return nil, err |
| 123 | } |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | limaHome, err := dirnames.LimaDir() |
| 129 | if err != nil { |
| 130 | return nil, err |
| 131 | } |
| 132 | data := map[string]string{ |
| 133 | "Binary": selfExeAbs, |
| 134 | "Instance": instName, |
| 135 | "LimaHome": limaHome, |
| 136 | "WorkDir": workDir, |
| 137 | } |
| 138 | maps.Copy(data, t.extraTemplateVars) |
| 139 | return textutil.ExecuteTemplate(t.template, data) |
| 140 | } |
| 141 | |
| 142 | func (t *TemplateFileBasedManager) AutoStartedIdentifier() string { |
| 143 | if t.autoStartedIdentifier != nil { |
| 144 | return t.autoStartedIdentifier() |
| 145 | } |
| 146 | return "" |
| 147 | } |
| 148 | |
| 149 | func (t *TemplateFileBasedManager) RequestStart(ctx context.Context, inst *limatype.Instance) error { |
| 150 | if t.requestStart == nil { |
| 151 | return errors.New("no RequestStart function available") |
| 152 | } |
| 153 | return t.requestStart(ctx, inst) |
| 154 | } |
| 155 | |
| 156 | func (t *TemplateFileBasedManager) RequestStop(ctx context.Context, inst *limatype.Instance) (bool, error) { |
| 157 | if t.requestStop == nil { |
| 158 | return false, errors.New("no RequestStop function available") |
| 159 | } |
| 160 | return t.requestStop(ctx, inst) |
| 161 | } |