From 69815b2469f40aac78b2b59f4c04bb3521ef17b3 Mon Sep 17 00:00:00 2001 From: Chad Thielen Date: Fri, 27 Dec 2024 13:15:50 -0700 Subject: [PATCH 1/9] save off changes --- system/service_windows.go | 63 +++++++++++++++++++++++++++++++++++++++ system/system.go | 6 ++++ 2 files changed, 69 insertions(+) create mode 100644 system/service_windows.go diff --git a/system/service_windows.go b/system/service_windows.go new file mode 100644 index 000000000..569a7bdf0 --- /dev/null +++ b/system/service_windows.go @@ -0,0 +1,63 @@ +package system + +import ( + "context" + "fmt" + "strings" + + "github.com/goss-org/goss/util" +) + +type ServiceWindows struct { + service string +} + +func NewServiceWindows(_ context.Context, service string, system *System, config util.Config) Service { + return &ServiceWindows{ + service: service, + } +} + +func (s *ServiceWindows) Service() string { + return s.service +} + +func (s *ServiceWindows) Exists() (bool, error) { + if invalidService(s.service) { + return false, nil + } + cmd := util.NewCommand("systemctl", "-q", "list-unit-files", "--type=service") + cmd.Run() + if strings.Contains(cmd.Stdout.String(), fmt.Sprintf("%s.service", s.service)) { + return true, cmd.Err + } + return false, nil +} + +func (s *ServiceWindows) Enabled() (bool, error) { + if invalidService(s.service) { + return false, nil + } + cmd := util.NewCommand("systemctl", "-q", "is-enabled", s.service) + cmd.Run() + if cmd.Status == 0 { + return true, cmd.Err + } + return false, nil +} + +func (s *ServiceWindows) Running() (bool, error) { + if invalidService(s.service) { + return false, nil + } + cmd := util.NewCommand("systemctl", "-q", "is-active", s.service) + cmd.Run() + if cmd.Status == 0 { + return true, cmd.Err + } + return false, nil +} + +func (s *ServiceWindows) RunLevels() ([]string, error) { + return nil, nil +} diff --git a/system/system.go b/system/system.go index 6c6083558..287717903 100644 --- a/system/system.go +++ b/system/system.go @@ -5,6 +5,7 @@ import ( "context" "os" "os/exec" + "runtime" "strconv" "sync" @@ -109,6 +110,8 @@ func (sys *System) detectService() { sys.NewService = NewServiceSystemdLegacy case "alpineinit": sys.NewService = NewAlpineServiceInit + case "windows": + sys.NewService = NewServiceWindows default: sys.NewService = NewServiceInit } @@ -166,6 +169,9 @@ func DetectService() string { } return "systemd" } + if runtime.GOOS == "windows" { + return "windows" + } // Centos Docker container doesn't run systemd, so we detect it or use init. switch DetectDistro() { case "ubuntu": From 568ef796e59dab9af2789085a5018f888142a477 Mon Sep 17 00:00:00 2001 From: Chad Thielen Date: Tue, 14 Jan 2025 11:48:28 -0700 Subject: [PATCH 2/9] windows file name preventing file from building --- system/{service_windows.go => service_win.go} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename system/{service_windows.go => service_win.go} (100%) diff --git a/system/service_windows.go b/system/service_win.go similarity index 100% rename from system/service_windows.go rename to system/service_win.go From dc7eb8a14b1c542995fae529d47340ec5ecc9cae Mon Sep 17 00:00:00 2001 From: Chad Thielen <48074114+cthiel42@users.noreply.github.com> Date: Sat, 18 Jan 2025 05:14:03 +0000 Subject: [PATCH 3/9] release builds work when compiled on linux and ran on windows. can start implementing service logic now --- system/service_win.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/system/service_win.go b/system/service_win.go index 569a7bdf0..d6634cfcf 100644 --- a/system/service_win.go +++ b/system/service_win.go @@ -26,7 +26,7 @@ func (s *ServiceWindows) Exists() (bool, error) { if invalidService(s.service) { return false, nil } - cmd := util.NewCommand("systemctl", "-q", "list-unit-files", "--type=service") + cmd := util.NewCommand("systemctltest", "-q", "list-unit-files", "--type=service") cmd.Run() if strings.Contains(cmd.Stdout.String(), fmt.Sprintf("%s.service", s.service)) { return true, cmd.Err @@ -38,7 +38,7 @@ func (s *ServiceWindows) Enabled() (bool, error) { if invalidService(s.service) { return false, nil } - cmd := util.NewCommand("systemctl", "-q", "is-enabled", s.service) + cmd := util.NewCommand("systemctltest", "-q", "is-enabled", s.service) cmd.Run() if cmd.Status == 0 { return true, cmd.Err @@ -50,7 +50,7 @@ func (s *ServiceWindows) Running() (bool, error) { if invalidService(s.service) { return false, nil } - cmd := util.NewCommand("systemctl", "-q", "is-active", s.service) + cmd := util.NewCommand("systemctltest", "-q", "is-active", s.service) cmd.Run() if cmd.Status == 0 { return true, cmd.Err From 053e1760f3e0c868249a0fa87a004d61664c0e6f Mon Sep 17 00:00:00 2001 From: Chad Thielen <48074114+cthiel42@users.noreply.github.com> Date: Sat, 18 Jan 2025 05:40:27 +0000 Subject: [PATCH 4/9] testing something --- system/service_win.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/system/service_win.go b/system/service_win.go index d6634cfcf..ada752ecc 100644 --- a/system/service_win.go +++ b/system/service_win.go @@ -61,3 +61,5 @@ func (s *ServiceWindows) Running() (bool, error) { func (s *ServiceWindows) RunLevels() ([]string, error) { return nil, nil } + +// test \ No newline at end of file From c720e04f3cbbed67cf780a4ad948e305b886372f Mon Sep 17 00:00:00 2001 From: Chad Thielen <48074114+cthiel42@users.noreply.github.com> Date: Sat, 18 Jan 2025 05:42:07 +0000 Subject: [PATCH 5/9] removing --- system/service_win.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/system/service_win.go b/system/service_win.go index ada752ecc..d6634cfcf 100644 --- a/system/service_win.go +++ b/system/service_win.go @@ -61,5 +61,3 @@ func (s *ServiceWindows) Running() (bool, error) { func (s *ServiceWindows) RunLevels() ([]string, error) { return nil, nil } - -// test \ No newline at end of file From 14cb7fff48b00ee9c08acfa77583bbb8272021ba Mon Sep 17 00:00:00 2001 From: Chad Thielen <48074114+cthiel42@users.noreply.github.com> Date: Tue, 21 Jan 2025 03:40:26 +0000 Subject: [PATCH 6/9] windows services working --- system/service_win.go | 29 ++++++++++------------------- 1 file changed, 10 insertions(+), 19 deletions(-) diff --git a/system/service_win.go b/system/service_win.go index d6634cfcf..020786b93 100644 --- a/system/service_win.go +++ b/system/service_win.go @@ -23,39 +23,30 @@ func (s *ServiceWindows) Service() string { } func (s *ServiceWindows) Exists() (bool, error) { - if invalidService(s.service) { - return false, nil - } - cmd := util.NewCommand("systemctltest", "-q", "list-unit-files", "--type=service") + cmd := util.NewCommand(fmt.Sprintf("Get-Service -Name %s", s.service)) cmd.Run() - if strings.Contains(cmd.Stdout.String(), fmt.Sprintf("%s.service", s.service)) { - return true, cmd.Err + if strings.Contains(cmd.Stderr.String(), "Cannot find any service with service name") { + return false, nil } - return false, nil + return true, cmd.Err } func (s *ServiceWindows) Enabled() (bool, error) { - if invalidService(s.service) { - return false, nil - } - cmd := util.NewCommand("systemctltest", "-q", "is-enabled", s.service) + cmd := util.NewCommand("powershell", "-command", fmt.Sprintf("$(Get-Service -Name %s).StartType", s.service), "-eq", "\"Automatic\"") cmd.Run() - if cmd.Status == 0 { + if strings.Contains(cmd.Stdout.String(), "True") { return true, cmd.Err } - return false, nil + return false, cmd.Err } func (s *ServiceWindows) Running() (bool, error) { - if invalidService(s.service) { - return false, nil - } - cmd := util.NewCommand("systemctltest", "-q", "is-active", s.service) + cmd := util.NewCommand("powershell", "-command", fmt.Sprintf("$(Get-Service -Name %s).Status", s.service), "-eq", "\"Running\"") cmd.Run() - if cmd.Status == 0 { + if strings.Contains(cmd.Stdout.String(), "True") { return true, cmd.Err } - return false, nil + return false, cmd.Err } func (s *ServiceWindows) RunLevels() ([]string, error) { From 93260dbde8e03f573f1c01698cb63d80266bb997 Mon Sep 17 00:00:00 2001 From: Chad Thielen <48074114+cthiel42@users.noreply.github.com> Date: Mon, 27 Jan 2025 02:25:34 +0000 Subject: [PATCH 7/9] remove skip in integration test --- integration-tests/goss/windows/tests/service.goss.yaml | 3 --- 1 file changed, 3 deletions(-) diff --git a/integration-tests/goss/windows/tests/service.goss.yaml b/integration-tests/goss/windows/tests/service.goss.yaml index f29e31286..1fe258ce4 100644 --- a/integration-tests/goss/windows/tests/service.goss.yaml +++ b/integration-tests/goss/windows/tests/service.goss.yaml @@ -3,6 +3,3 @@ service: MSDTC: enabled: true running: true - - # needs implementation - skip: true From 24368fff4ffde4cb6b011110c43e79a5e0438f6f Mon Sep 17 00:00:00 2001 From: Chad Thielen <48074114+cthiel42@users.noreply.github.com> Date: Mon, 27 Jan 2025 03:30:17 +0000 Subject: [PATCH 8/9] update exists method --- system/service_win.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/service_win.go b/system/service_win.go index 020786b93..14e617cd0 100644 --- a/system/service_win.go +++ b/system/service_win.go @@ -23,7 +23,7 @@ func (s *ServiceWindows) Service() string { } func (s *ServiceWindows) Exists() (bool, error) { - cmd := util.NewCommand(fmt.Sprintf("Get-Service -Name %s", s.service)) + cmd := util.NewCommand("powershell", "-command", fmt.Sprintf("Get-Service -Name %s", s.service)) cmd.Run() if strings.Contains(cmd.Stderr.String(), "Cannot find any service with service name") { return false, nil From fb49c484ef770a80d86183e8b49629d3469f86d4 Mon Sep 17 00:00:00 2001 From: Chad Thielen <48074114+cthiel42@users.noreply.github.com> Date: Tue, 28 Jan 2025 00:54:19 +0000 Subject: [PATCH 9/9] another test update --- system/system_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/system_test.go b/system/system_test.go index f8644915c..b0622346c 100644 --- a/system/system_test.go +++ b/system/system_test.go @@ -37,7 +37,7 @@ func TestDetectService(t *testing.T) { t.Parallel() testOutputs( DetectService, - []string{"systemd", "init", "alpineinit", "upstart", ""}, + []string{"systemd", "init", "alpineinit", "upstart", "windows"}, t, ) }