From d91faf5636ce9014c2350ea6de17e690ba363a57 Mon Sep 17 00:00:00 2001 From: Philippe Caseiro Date: Wed, 29 Jun 2022 17:26:10 +0200 Subject: [PATCH] Try 07 --- data/config/test-services.json | 1 + pkg/templater/files.go | 6 ++++++ pkg/templater/services.go | 2 +- pkg/templater/system_services.go | 13 +++++++++++++ 4 files changed, 21 insertions(+), 1 deletion(-) diff --git a/data/config/test-services.json b/data/config/test-services.json index 07afabc..6a73f4f 100644 --- a/data/config/test-services.json +++ b/data/config/test-services.json @@ -20,6 +20,7 @@ "source": "loki-local-config.pktpl.hcl", "mode": "600", "owner": "loki", + "service": "loki", "group": "grafana" } ], diff --git a/pkg/templater/files.go b/pkg/templater/files.go index 511c2d7..af14220 100644 --- a/pkg/templater/files.go +++ b/pkg/templater/files.go @@ -25,6 +25,7 @@ type ConfigFile struct { TemplateType string `json:"type"` // The template file type (hcl or gotemplate) Mode string `form:"mod" json:"mode"` // The configuration file final permissions (mode) Owner string `json:"owner"` // The configuration file owner + Service string `json:"service"` // Service to restart after configuration generation Group string `json:"group"` // The configuration file group owner } @@ -52,6 +53,11 @@ func (cf *ConfigFile) Generate(root string, templateDir string, values []byte) e return fmt.Errorf("Process templates failed with error: %v", err) } log.Printf("\tFile %s generated\n", dest) + + if cf.Service != "" { + sv := SystemService{Name: fmt.Sprintf("%s")} + return sv.Restart() + } return nil } diff --git a/pkg/templater/services.go b/pkg/templater/services.go index f4da44d..9a17278 100644 --- a/pkg/templater/services.go +++ b/pkg/templater/services.go @@ -65,7 +65,7 @@ func (s *Service) Manage(templateDir string) error { return fmt.Errorf("ProcessingTemplatesFailed with error: %v", err) } - log.Printf("Managing services\n") + log.Printf("Managing services:\n") for _, daemon := range s.Daemons { err = daemon.Manage() if err != nil { diff --git a/pkg/templater/system_services.go b/pkg/templater/system_services.go index c344f4b..c0faa8d 100644 --- a/pkg/templater/system_services.go +++ b/pkg/templater/system_services.go @@ -99,6 +99,19 @@ func (sys *SystemService) Stop() error { return nil } +func (sys *SystemService) Restart() error { + if sys.Type == "" || sys.Type == "auto" { + sys.SetType() + } + if err := sys.Stop(); err != nil { + return err + } + if err := sys.Start(); err != nil { + return err + } + return nil +} + func (sys *SystemService) Enable() error { if sys.Type == "systemd" { _, stdErr, err := utils.RunSystemCommand("systemctl", "enable", sys.Name)