99 "io/ioutil"
1010 "os"
1111 "path/filepath"
12+ "runtime"
1213
1314 "github.com/otiai10/copy"
1415
@@ -17,6 +18,10 @@ import (
1718 "github.com/elastic/elastic-agent/internal/pkg/agent/errors"
1819)
1920
21+ const (
22+ darwin = "darwin"
23+ )
24+
2025// Install installs Elastic Agent persistently on the system including creating and starting its service.
2126func Install (cfgFile string ) error {
2227 dir , err := findDirectory ()
@@ -53,15 +58,36 @@ func Install(cfgFile string) error {
5358
5459 // place shell wrapper, if present on platform
5560 if paths .ShellWrapperPath != "" {
56- err = os .MkdirAll (filepath .Dir (paths .ShellWrapperPath ), 0755 )
57- if err == nil {
58- err = ioutil .WriteFile (paths .ShellWrapperPath , []byte (paths .ShellWrapper ), 0755 )
59- }
60- if err != nil {
61- return errors .New (
62- err ,
63- fmt .Sprintf ("failed to write shell wrapper (%s)" , paths .ShellWrapperPath ),
64- errors .M ("destination" , paths .ShellWrapperPath ))
61+ // Install symlink for darwin instead
62+ if runtime .GOOS == darwin {
63+ // Check if previous shell wrapper or symlink exists and remove it so it can be overwritten
64+ if _ , err := os .Lstat (paths .ShellWrapperPath ); err == nil {
65+ if err := os .Remove (paths .ShellWrapperPath ); err != nil {
66+ return errors .New (
67+ err ,
68+ fmt .Sprintf ("failed to remove (%s)" , paths .ShellWrapperPath ),
69+ errors .M ("destination" , paths .ShellWrapperPath ))
70+ }
71+ }
72+ err = os .Symlink ("/Library/Elastic/Agent/elastic-agent" , paths .ShellWrapperPath )
73+ if err != nil {
74+ return errors .New (
75+ err ,
76+ fmt .Sprintf ("failed to create elastic-agent symlink (%s)" , paths .ShellWrapperPath ),
77+ errors .M ("destination" , paths .ShellWrapperPath ))
78+ }
79+ } else {
80+ err = os .MkdirAll (filepath .Dir (paths .ShellWrapperPath ), 0755 )
81+ if err == nil {
82+ //nolint: gosec // this is intended to be an executable shell script, not chaning the permissions for the linter
83+ err = ioutil .WriteFile (paths .ShellWrapperPath , []byte (paths .ShellWrapper ), 0755 )
84+ }
85+ if err != nil {
86+ return errors .New (
87+ err ,
88+ fmt .Sprintf ("failed to write shell wrapper (%s)" , paths .ShellWrapperPath ),
89+ errors .M ("destination" , paths .ShellWrapperPath ))
90+ }
6591 }
6692 }
6793
@@ -151,6 +177,9 @@ func findDirectory() (string, error) {
151177 // executable path is being reported as being down inside of data path
152178 // move up to directories to perform the copy
153179 sourceDir = filepath .Dir (filepath .Dir (sourceDir ))
180+ if runtime .GOOS == darwin {
181+ sourceDir = filepath .Dir (filepath .Dir (filepath .Dir (sourceDir )))
182+ }
154183 }
155184 err = verifyDirectory (sourceDir )
156185 if err != nil {
0 commit comments