diff --git a/NOTICE.txt b/NOTICE.txt index 726d355445c..2acc00f3420 100644 --- a/NOTICE.txt +++ b/NOTICE.txt @@ -2220,6 +2220,36 @@ Contents of probable licence file $GOMODCACHE/github.com/elastic/go-licenser@v0. limitations under the License. +-------------------------------------------------------------------------------- +Dependency : github.com/elastic/go-service +Version: v0.0.0-20240611154109-f44f756f194f +Licence type (autodetected): Zlib +-------------------------------------------------------------------------------- + +Contents of probable licence file $GOMODCACHE/github.com/elastic/go-service@v0.0.0-20240611154109-f44f756f194f/LICENSE: + +Copyright (c) 2015 Daniel Theophanes + +This software is provided 'as-is', without any express or implied +warranty. In no event will the authors be held liable for any damages +arising from the use of this software. + +Permission is granted to anyone to use this software for any purpose, +including commercial applications, and to alter it and redistribute it +freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + + 3. This notice may not be removed or altered from any source + distribution. + + -------------------------------------------------------------------------------- Dependency : github.com/elastic/go-sysinfo Version: v1.14.0 @@ -3759,36 +3789,6 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. --------------------------------------------------------------------------------- -Dependency : github.com/kardianos/service -Version: v1.2.1-0.20210728001519-a323c3813bc7 -Licence type (autodetected): Zlib --------------------------------------------------------------------------------- - -Contents of probable licence file $GOMODCACHE/github.com/kardianos/service@v1.2.1-0.20210728001519-a323c3813bc7/LICENSE: - -Copyright (c) 2015 Daniel Theophanes - -This software is provided 'as-is', without any express or implied -warranty. In no event will the authors be held liable for any damages -arising from the use of this software. - -Permission is granted to anyone to use this software for any purpose, -including commercial applications, and to alter it and redistribute it -freely, subject to the following restrictions: - - 1. The origin of this software must not be misrepresented; you must not - claim that you wrote the original software. If you use this software - in a product, an acknowledgment in the product documentation would be - appreciated but is not required. - - 2. Altered source versions must be plainly marked as such, and must not be - misrepresented as being the original software. - - 3. This notice may not be removed or altered from any source - distribution. - - -------------------------------------------------------------------------------- Dependency : github.com/magefile/mage Version: v1.15.0 diff --git a/go.mod b/go.mod index ef6871f6c51..a30d84e6658 100644 --- a/go.mod +++ b/go.mod @@ -21,6 +21,7 @@ require ( github.com/elastic/elastic-transport-go/v8 v8.5.0 github.com/elastic/go-elasticsearch/v8 v8.13.1 github.com/elastic/go-licenser v0.4.1 + github.com/elastic/go-service v0.0.0-20240611154109-f44f756f194f github.com/elastic/go-sysinfo v1.14.0 github.com/elastic/go-ucfg v0.8.8 github.com/fatih/color v1.15.0 @@ -37,7 +38,6 @@ require ( github.com/jedib0t/go-pretty/v6 v6.4.6 github.com/joeshaw/multierror v0.0.0-20140124173710-69b34d4ec901 github.com/josephspurrier/goversioninfo v0.0.0-20190209210621-63e6d1acd3dd - github.com/kardianos/service v1.2.1-0.20210728001519-a323c3813bc7 github.com/magefile/mage v1.15.0 github.com/mitchellh/gox v1.0.1 github.com/mitchellh/hashstructure v1.1.0 diff --git a/go.sum b/go.sum index fffe8b439c6..96380a6f68e 100644 --- a/go.sum +++ b/go.sum @@ -813,6 +813,8 @@ github.com/elastic/go-licenser v0.3.1/go.mod h1:D8eNQk70FOCVBl3smCGQt/lv7meBeQno github.com/elastic/go-licenser v0.4.0/go.mod h1:V56wHMpmdURfibNBggaSBfqgPxyT1Tldns1i87iTEvU= github.com/elastic/go-licenser v0.4.1 h1:1xDURsc8pL5zYT9R29425J3vkHdt4RT5TNEMeRN48x4= github.com/elastic/go-licenser v0.4.1/go.mod h1:V56wHMpmdURfibNBggaSBfqgPxyT1Tldns1i87iTEvU= +github.com/elastic/go-service v0.0.0-20240611154109-f44f756f194f h1:kMtsJ3zfcBBR6wfbf5yUExmvWLru6R7zSLaJIfSjU3g= +github.com/elastic/go-service v0.0.0-20240611154109-f44f756f194f/go.mod h1:3lVNFcuNMdWJrSpGF5SMPUVuC+qOYobfPIrMQUoizrk= github.com/elastic/go-structform v0.0.10 h1:oy08o/Ih2hHTkNcRY/1HhaYvIp5z6t8si8gnCJPDo1w= github.com/elastic/go-structform v0.0.10/go.mod h1:CZWf9aIRYY5SuKSmOhtXScE5uQiLZNqAFnwKR4OrIM4= github.com/elastic/go-sysinfo v1.1.1/go.mod h1:i1ZYdU10oLNfRzq4vq62BEwD2fH8KaWh6eh0ikPT9F0= @@ -1271,8 +1273,6 @@ github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfV github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w= github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM= github.com/k0kubun/go-ansi v0.0.0-20180517002512-3bf9e2903213/go.mod h1:vNUNkEQ1e29fT/6vq2aBdFsgNPmy8qMdSay1npru+Sw= -github.com/kardianos/service v1.2.1-0.20210728001519-a323c3813bc7 h1:oohm9Rk9JAxxmp2NLZa7Kebgz9h4+AJDcc64txg3dQ0= -github.com/kardianos/service v1.2.1-0.20210728001519-a323c3813bc7/go.mod h1:CIMRFEJVL+0DS1a3Nx06NaMn4Dz63Ng6O7dl0qH0zVM= github.com/karrick/godirwalk v1.15.6/go.mod h1:j4mkqPuvaLI8mp1DroR3P6ad7cyYd4c1qeJ3RV7ULlk= github.com/karrick/godirwalk v1.16.1 h1:DynhcF+bztK8gooS0+NDJFrdNZjJ3gzVzC545UNA9iw= github.com/karrick/godirwalk v1.16.1/go.mod h1:j4mkqPuvaLI8mp1DroR3P6ad7cyYd4c1qeJ3RV7ULlk= diff --git a/internal/pkg/agent/application/upgrade/service_update_linux.go b/internal/pkg/agent/application/upgrade/service_update_linux.go index 618b632f93a..84e9c80292b 100644 --- a/internal/pkg/agent/application/upgrade/service_update_linux.go +++ b/internal/pkg/agent/application/upgrade/service_update_linux.go @@ -12,7 +12,7 @@ import ( "gopkg.in/ini.v1" - "github.com/kardianos/service" + "github.com/elastic/go-service" "github.com/elastic/elastic-agent/internal/pkg/agent/application/paths" ) diff --git a/internal/pkg/agent/install/install.go b/internal/pkg/agent/install/install.go index 922bb85d8b6..60f7f1ed6d4 100644 --- a/internal/pkg/agent/install/install.go +++ b/internal/pkg/agent/install/install.go @@ -12,8 +12,9 @@ import ( "runtime" "strings" + "github.com/elastic/go-service" + "github.com/jaypipes/ghw" - "github.com/kardianos/service" "github.com/otiai10/copy" "github.com/schollz/progressbar/v3" diff --git a/internal/pkg/agent/install/installed.go b/internal/pkg/agent/install/installed.go index c415d4e1abe..b2390d81efa 100644 --- a/internal/pkg/agent/install/installed.go +++ b/internal/pkg/agent/install/installed.go @@ -8,7 +8,7 @@ import ( "os" "path/filepath" - "github.com/kardianos/service" + "github.com/elastic/go-service" "github.com/elastic/elastic-agent/internal/pkg/agent/application/paths" "github.com/elastic/elastic-agent/internal/pkg/agent/install/pkgmgr" diff --git a/internal/pkg/agent/install/svc.go b/internal/pkg/agent/install/svc.go index 3fc599fb80a..aadc7f7c190 100644 --- a/internal/pkg/agent/install/svc.go +++ b/internal/pkg/agent/install/svc.go @@ -9,7 +9,7 @@ import ( "path/filepath" "runtime" - "github.com/kardianos/service" + "github.com/elastic/go-service" "github.com/elastic/elastic-agent/internal/pkg/agent/application/paths" ) diff --git a/internal/pkg/agent/install/switch.go b/internal/pkg/agent/install/switch.go new file mode 100644 index 00000000000..4e732645037 --- /dev/null +++ b/internal/pkg/agent/install/switch.go @@ -0,0 +1,107 @@ +// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one +// or more contributor license agreements. Licensed under the Elastic License; +// you may not use this file except in compliance with the Elastic License. + +package install + +import ( + "fmt" + + "github.com/schollz/progressbar/v3" + + "github.com/elastic/go-service" + + "github.com/elastic/elastic-agent/internal/pkg/agent/application/paths" + "github.com/elastic/elastic-agent/internal/pkg/agent/perms" + "github.com/elastic/elastic-agent/pkg/utils" +) + +// SwitchExecutingMode switches the executing mode of the installed Elastic Agent. +// +// When username and groupName are blank then it switched back to root/Administrator and when a username/groupName is +// provided then it switched to running with that username and groupName. +func SwitchExecutingMode(topPath string, pt *progressbar.ProgressBar, username string, groupName string) error { + // ensure service is stopped + status, err := EnsureStoppedService(topPath, pt) + if err != nil { + // context for the error already provided in the EnsureStoppedService function + return err + } + + // ensure that upon exit of this function that the service is always placed back to running, in the case + // that it was running when the command was executed + defer func() { + if err != nil && status == service.StatusRunning { + _ = StartService(topPath) + } + }() + + // ensure user/group are created + var ownership utils.FileOwner + if username != "" && groupName != "" { + ownership, err = EnsureUserAndGroup(username, groupName, pt) + if err != nil { + // context for the error already provided in the EnsureUserAndGroup function + return err + } + } + + // **start critical section** + // after this point changes will be made that can leave the installed Elastic Agent broken if they do not + // complete successfully + + // perform platform specific work + err = switchPlatformMode(pt, ownership) + + // fix all permissions to use the new ownership + pt.Describe("Adjusting permissions") + err = perms.FixPermissions(topPath, perms.WithOwnership(ownership)) + if err != nil { + return fmt.Errorf("failed to perform permission changes on path %s: %w", topPath, err) + } + if paths.ShellWrapperPath != "" { + err = perms.FixPermissions(paths.ShellWrapperPath, perms.WithOwnership(ownership)) + if err != nil { + return fmt.Errorf("failed to perform permission changes on path %s: %w", paths.ShellWrapperPath, err) + } + } + + // the service has to be uninstalled + pt.Describe("Removing service") + // error is ignored because it's possible that its already uninstalled + // + // this can happen if this action failed in the middle of this critical section, so to allow the + // command to be called again we don't error on the uninstall + // + // the install error below will include an error about the service still existing if this failed + // to uninstall (really this should never fail, but the unexpected can happen) + _ = UninstallService(topPath) + + // re-install service + pt.Describe("Installing service") + err = InstallService(topPath, ownership, username, groupName) + if err != nil { + pt.Describe("Failed to install service") + // error context already added by InstallService + + // this is now in a bad state, because the service is uninstall and now the service failed to install + return err + } + pt.Describe("Installed service") + + // start the service + pt.Describe("Starting service") + err = StartService(topPath) + if err != nil { + pt.Describe("Failed to start service") + // error context already added by InstallService + + // this is now in a bad state, because the service is not running and failed to install + return err + } + + // **end critical section** + // service is now re-created and started + + return nil +} diff --git a/internal/pkg/agent/install/uninstall.go b/internal/pkg/agent/install/uninstall.go index f333e8c49a5..5508b1f8fa3 100644 --- a/internal/pkg/agent/install/uninstall.go +++ b/internal/pkg/agent/install/uninstall.go @@ -15,9 +15,10 @@ import ( "strings" "time" - "github.com/kardianos/service" "github.com/schollz/progressbar/v3" + "github.com/elastic/go-service" + "github.com/elastic/elastic-agent-libs/logp" "github.com/elastic/elastic-agent/internal/pkg/agent/application/paths" "github.com/elastic/elastic-agent/internal/pkg/agent/application/secret" diff --git a/pkg/component/runtime/service.go b/pkg/component/runtime/service.go index c12e5cae569..7ff2073df48 100644 --- a/pkg/component/runtime/service.go +++ b/pkg/component/runtime/service.go @@ -10,7 +10,7 @@ import ( "fmt" "time" - "github.com/kardianos/service" + "github.com/elastic/go-service" "github.com/elastic/elastic-agent-client/v7/pkg/client" "github.com/elastic/elastic-agent-client/v7/pkg/proto" diff --git a/testing/integration/upgrade_rollback_test.go b/testing/integration/upgrade_rollback_test.go index 37b845fec85..6c1a041ffad 100644 --- a/testing/integration/upgrade_rollback_test.go +++ b/testing/integration/upgrade_rollback_test.go @@ -15,7 +15,7 @@ import ( "testing" "time" - "github.com/kardianos/service" + "github.com/elastic/go-service" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require"