File: service_init.go

package info (click to toggle)
goss 0.4.9-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,700 kB
  • sloc: sh: 984; makefile: 139
file content (112 lines) | stat: -rw-r--r-- 2,491 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package system

import (
	"context"
	"fmt"
	"os"
	"path/filepath"
	"regexp"

	"github.com/goss-org/goss/util"
)

type ServiceInit struct {
	service  string
	alpine   bool
	runlevel string
}

func NewServiceInit(_ context.Context, service string, system *System, config util.Config) Service {
	return &ServiceInit{service: service}
}

func NewAlpineServiceInit(_ context.Context, service string, system *System, config util.Config) Service {
	runlevel := config.RunLevel
	if runlevel == "" {
		runlevel = "sysinit"
	}
	return &ServiceInit{service: service, alpine: true, runlevel: runlevel}
}

func (s *ServiceInit) Service() string {
	return s.service
}

func (s *ServiceInit) Exists() (bool, error) {
	if invalidService(s.service) {
		return false, nil
	}
	if _, err := os.Stat(fmt.Sprintf("/etc/init.d/%s", s.service)); err == nil {
		return true, err
	}
	return false, nil
}

func (s *ServiceInit) Enabled() (bool, error) {
	if invalidService(s.service) {
		return false, nil
	}
	var runLevels []string
	var err error
	if s.alpine {
		runLevels, err = alpineServiceRunLevels(s.service)
	} else {
		runLevels, err = initServiceRunLevels(s.service)
	}
	return len(runLevels) != 0, err
}

func (s *ServiceInit) RunLevels() ([]string, error) {
	if invalidService(s.service) {
		return nil, nil
	}
	if s.alpine {
		return alpineServiceRunLevels(s.service)
	} else {
		return initServiceRunLevels(s.service)
	}
}

func (s *ServiceInit) Running() (bool, error) {
	if invalidService(s.service) {
		return false, nil
	}
	cmd := util.NewCommand("service", s.service, "status")
	cmd.Run()
	if cmd.Status == 0 {
		return true, cmd.Err
	}
	return false, nil
}

func initServiceRunLevels(service string) ([]string, error) {
	var runLevels []string
	matches, err := filepath.Glob(fmt.Sprintf("/etc/rc*.d/S[0-9][0-9]%s", service))
	if err != nil {
		return nil, err
	}
	re := regexp.MustCompile("/etc/rc([0-9]+).d/")
	for _, m := range matches {
		matches := re.FindStringSubmatch(m)
		if matches != nil {
			runLevels = append(runLevels, matches[1])
		}
	}
	return runLevels, nil
}

func alpineServiceRunLevels(service string) ([]string, error) {
	var runLevels []string
	matches, err := filepath.Glob(fmt.Sprintf("/etc/runlevels/*/%s", service))
	if err != nil {
		return nil, err
	}
	re := regexp.MustCompile("/etc/runlevels/([^/]+)")
	for _, m := range matches {
		matches := re.FindStringSubmatch(m)
		if matches != nil {
			runLevels = append(runLevels, matches[1])
		}
	}
	return runLevels, nil
}