123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- package initsystem
- import (
- "fmt"
- "os/exec"
- "strings"
- )
- type OpenRCInitSystem struct{}
- func (openrc OpenRCInitSystem) ServiceStart(service string) error {
- args := []string{service, "start"}
- return exec.Command("rc-service", args...).Run()
- }
- func (openrc OpenRCInitSystem) ServiceStop(service string) error {
- args := []string{service, "stop"}
- return exec.Command("rc-service", args...).Run()
- }
- func (openrc OpenRCInitSystem) ServiceRestart(service string) error {
- args := []string{service, "restart"}
- return exec.Command("rc-service", args...).Run()
- }
- func (openrc OpenRCInitSystem) ServiceExists(service string) bool {
- args := []string{service, "status"}
- outBytes, _ := exec.Command("rc-service", args...).CombinedOutput()
- if strings.Contains(string(outBytes), "does not exist") {
- return false
- }
- return true
- }
- func (openrc OpenRCInitSystem) ServiceIsEnabled(service string) bool {
- args := []string{"show", "default"}
- outBytes, _ := exec.Command("rc-update", args...).Output()
- if strings.Contains(string(outBytes), service) {
- return true
- }
- return false
- }
- func (openrc OpenRCInitSystem) ServiceIsActive(service string) bool {
- args := []string{service, "status"}
- outBytes, _ := exec.Command("rc-service", args...).Output()
- if strings.Contains(string(outBytes), "stopped") {
- return false
- }
- return true
- }
- func (openrc OpenRCInitSystem) EnableCommand(service string) string {
- return fmt.Sprintf("rc-update add %s default", service)
- }
- type SystemdInitSystem struct{}
- func (sysd SystemdInitSystem) EnableCommand(service string) string {
- return fmt.Sprintf("systemctl enable %s.service", service)
- }
- func (sysd SystemdInitSystem) reloadSystemd() error {
- if err := exec.Command("systemctl", "daemon-reload").Run(); err != nil {
- return fmt.Errorf("failed to reload systemd: %v", err)
- }
- return nil
- }
- func (sysd SystemdInitSystem) ServiceStart(service string) error {
-
- if err := sysd.reloadSystemd(); err != nil {
- return err
- }
- args := []string{"start", service}
- return exec.Command("systemctl", args...).Run()
- }
- func (sysd SystemdInitSystem) ServiceRestart(service string) error {
-
- if err := sysd.reloadSystemd(); err != nil {
- return err
- }
- args := []string{"restart", service}
- return exec.Command("systemctl", args...).Run()
- }
- func (sysd SystemdInitSystem) ServiceStop(service string) error {
- args := []string{"stop", service}
- return exec.Command("systemctl", args...).Run()
- }
- func (sysd SystemdInitSystem) ServiceExists(service string) bool {
- args := []string{"status", service}
- outBytes, _ := exec.Command("systemctl", args...).Output()
- output := string(outBytes)
- if strings.Contains(output, "Loaded: not-found") {
- return false
- }
- return true
- }
- func (sysd SystemdInitSystem) ServiceIsEnabled(service string) bool {
- args := []string{"is-enabled", service}
- err := exec.Command("systemctl", args...).Run()
- if err != nil {
- return false
- }
- return true
- }
- func (sysd SystemdInitSystem) ServiceIsActive(service string) bool {
- args := []string{"is-active", service}
-
- outBytes, _ := exec.Command("systemctl", args...).Output()
- output := strings.TrimSpace(string(outBytes))
- if output == "active" || output == "activating" {
- return true
- }
- return false
- }
- func GetInitSystem() (InitSystem, error) {
-
- _, err := exec.LookPath("systemctl")
- if err == nil {
- return &SystemdInitSystem{}, nil
- }
- _, err = exec.LookPath("openrc")
- if err == nil {
- return &OpenRCInitSystem{}, nil
- }
- return nil, fmt.Errorf("no supported init system detected, skipping checking for services")
- }
|