mirror of
https://github.com/outbackdingo/debos.git
synced 2026-01-27 10:18:47 +00:00
Enabled additional linters from fakemachine configuration: - errorlint: Error wrapping with %w - misspell: Spelling checks - revive: Code quality checks - whitespace: Formatting checks Fixed all issues including: - Error handling: Added proper error checks for all function returns - Error wrapping: Changed %v to %w for proper error wrapping - Type assertions: Used errors.As instead of direct type assertions - Unused parameters: Renamed to underscore where appropriate - Variable naming: Fixed ALL_CAPS constants and underscored names - Whitespace: Removed unnecessary leading/trailing newlines - Code flow: Removed unnecessary else blocks Renamed types (breaking internal API changes): - DebosState → State - DebosContext → Context - DownloadHttpUrl → DownloadHTTPURL Fixed struct field naming with proper YAML tags: - Url → URL (with yaml:"url" tag) - TlsClientCertPath → TLSClientCertPath (kept yaml:"tls-client-cert-path") - TlsClientKeyPath → TLSClientKeyPath (kept yaml:"tls-client-key-path") - validateUrl → validateURL method Co-authored-by: sjoerdsimons <22603932+sjoerdsimons@users.noreply.github.com>
73 lines
1.3 KiB
Go
73 lines
1.3 KiB
Go
package debos
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path"
|
|
)
|
|
|
|
const debianPolicyHelper = "/usr/sbin/policy-rc.d"
|
|
|
|
/*
|
|
ServiceHelper is used to manage services.
|
|
Currently supports only debian-based family.
|
|
*/
|
|
|
|
type ServiceHelper struct {
|
|
Rootdir string
|
|
}
|
|
|
|
type ServicesManager interface {
|
|
Allow() error
|
|
Deny() error
|
|
}
|
|
|
|
/*
|
|
Allow() allows to start/stop services on OS level.
|
|
*/
|
|
func (s *ServiceHelper) Allow() error {
|
|
helperFile := path.Join(s.Rootdir, debianPolicyHelper)
|
|
|
|
if _, err := os.Stat(helperFile); os.IsNotExist(err) {
|
|
return nil
|
|
}
|
|
if err := os.Remove(helperFile); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
/*
|
|
Deny() prohibits to start/stop services on OS level.
|
|
*/
|
|
func (s *ServiceHelper) Deny() error {
|
|
helperFile := path.Join(s.Rootdir, debianPolicyHelper)
|
|
var helper = []byte(`#!/bin/sh
|
|
|
|
exit 101
|
|
`)
|
|
|
|
if _, err := os.Stat(helperFile); os.IsExist(err) {
|
|
return fmt.Errorf("policy helper file '%s' exists already", debianPolicyHelper)
|
|
}
|
|
if _, err := os.Stat(path.Dir(helperFile)); os.IsNotExist(err) {
|
|
// do not try to do something if ".../usr/sbin" is not exists
|
|
return nil
|
|
}
|
|
pf, err := os.Create(helperFile)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer pf.Close()
|
|
|
|
if _, err := pf.Write(helper); err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := pf.Chmod(0755); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|