-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added service manager and support for systemd and sysvinit service ma…
…nagement
- Loading branch information
Showing
13 changed files
with
1,362 additions
and
230 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/darki73/goflaresync/pkg/log" | ||
"github.com/darki73/goflaresync/pkg/service" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// stopCmd represents the stop command. | ||
var stopCmd = &cobra.Command{ | ||
Use: "stop", | ||
Short: "Stops the service", | ||
Long: "Stops the service", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
if err := initializeConfiguration(); err != nil { | ||
log.Fatal(err.Error()) | ||
} | ||
|
||
manager := service.NewManager() | ||
systemManager := manager.GetSystemManager() | ||
if systemManager != nil { | ||
if err := systemManager.GetProcessIdentifierHandler().HandleApplicationStop(); err != nil { | ||
log.Fatal(err.Error()) | ||
} | ||
} | ||
}, | ||
} | ||
|
||
// init initializes the command. | ||
func init() { | ||
rootCmd.AddCommand(stopCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package debug | ||
|
||
import ( | ||
"github.com/Code-Hex/dd/p" | ||
"github.com/darki73/goflaresync/pkg/log" | ||
"os" | ||
) | ||
|
||
// Dump dumps the arguments. | ||
func Dump(args ...interface{}) { | ||
if _, err := p.P(args...); err != nil { | ||
log.Fatalf("failed to dump: %s", err.Error()) | ||
} | ||
} | ||
|
||
// DieAndDump dumps the arguments and exits. | ||
func DieAndDump(args ...interface{}) { | ||
Dump(args...) | ||
os.Exit(0) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package helpers | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
// GetExecutableDirectory returns the directory containing the executable. | ||
func GetExecutableDirectory() (string, error) { | ||
executablePath, err := os.Executable() | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
executableDirectory := filepath.Dir(executablePath) | ||
return executableDirectory, nil | ||
} | ||
|
||
// GetExecutableName returns the name of the executable. | ||
func GetExecutableName() string { | ||
executableName := filepath.Base(os.Args[0]) | ||
if strings.Contains(executableName, ".") { | ||
executableName = strings.Split(executableName, ".")[0] | ||
} | ||
return executableName | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package helpers | ||
|
||
import "os" | ||
|
||
// IsDirectoryExists returns a boolean value indicating whether the directory exists. | ||
func IsDirectoryExists(directoryPath string) bool { | ||
info, err := os.Stat(directoryPath) | ||
if os.IsNotExist(err) { | ||
return false | ||
} | ||
return info.IsDir() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
package service | ||
|
||
import ( | ||
"github.com/darki73/goflaresync/pkg/helpers" | ||
"github.com/darki73/goflaresync/pkg/log" | ||
"os" | ||
"path" | ||
"strings" | ||
) | ||
|
||
// InitializationSystemManager is the initialization system manager. | ||
type InitializationSystemManager interface { | ||
// GetServiceTemplate returns the service template. | ||
GetServiceTemplate() (string, error) | ||
// GetServicePath returns the service path. | ||
GetServicePath() string | ||
// GetProcessIdentifierHandler returns the process identifier handler. | ||
GetProcessIdentifierHandler() *ProcessIdentifierHandler | ||
// CreateService creates the service. | ||
CreateService() error | ||
// DeleteService deletes the service. | ||
DeleteService() error | ||
// EnableService enables the service. | ||
EnableService() error | ||
// DisableService disables the service. | ||
DisableService() error | ||
// StartService starts the service. | ||
StartService() error | ||
// StopService stops the service. | ||
StopService() error | ||
// RestartService restarts the service. | ||
RestartService() error | ||
// ReloadManager reloads the manager. | ||
ReloadManager() error | ||
// IsRunningAsRoot checks if the application is running as root. | ||
IsRunningAsRoot() | ||
// IsServiceExists checks if the service exists. | ||
IsServiceExists() bool | ||
// IsServiceEnabled checks if the service is enabled. | ||
IsServiceEnabled() (bool, error) | ||
// IsServiceRunning checks if the service is running. | ||
IsServiceRunning() (bool, error) | ||
} | ||
|
||
// mergeLogFields merges the log fields. | ||
func mergeLogFields(fields log.FieldsMap) log.FieldsMap { | ||
defaultFields := log.FieldsMap{ | ||
"source": "service", | ||
} | ||
|
||
if fields == nil { | ||
return defaultFields | ||
} | ||
|
||
for key, value := range defaultFields { | ||
fields[key] = value | ||
} | ||
|
||
return fields | ||
} | ||
|
||
// logDebug logs a debug message. | ||
func logDebug(message string, fields log.FieldsMap) { | ||
log.DebugWithFields(message, mergeLogFields(fields)) | ||
} | ||
|
||
// logDebugf logs a debug message. | ||
func logDebugf(message string, fields log.FieldsMap, args ...interface{}) { | ||
log.DebugfWithFields(message, mergeLogFields(fields), args...) | ||
} | ||
|
||
// logInfo logs an info message. | ||
func logInfo(message string, fields log.FieldsMap) { | ||
log.InfoWithFields(message, mergeLogFields(fields)) | ||
} | ||
|
||
// logInfof logs an info message. | ||
func logInfof(message string, fields log.FieldsMap, args ...interface{}) { | ||
log.InfofWithFields(message, mergeLogFields(fields), args...) | ||
} | ||
|
||
// applicationName returns the name of the application. | ||
func applicationName() string { | ||
return helpers.GetExecutableName() | ||
} | ||
|
||
// applicationFullPath returns the full path of the application. | ||
func applicationFullPath() (string, error) { | ||
directory, err := helpers.GetExecutableDirectory() | ||
if err != nil { | ||
return "", err | ||
} | ||
return path.Join(directory, applicationName()), nil | ||
} | ||
|
||
// combineSlices combines the slices. | ||
func combineSlices(slices ...[]string) string { | ||
var combined []string | ||
|
||
for _, slice := range slices { | ||
combined = append(combined, slice...) | ||
} | ||
|
||
return strings.Join(combined, "\n") | ||
} | ||
|
||
// isRunningAsRoot checks if the application is running as root. | ||
func isRunningAsRoot() { | ||
if !helpers.IsRoot() { | ||
log.Fatal("this command must be run as root") | ||
} | ||
} | ||
|
||
// isServiceExists checks if the service exists. | ||
func isServiceExists(baseDirectory string, serviceName string) bool { | ||
if _, err := os.Stat(path.Join(baseDirectory, serviceName)); err != nil { | ||
return false | ||
} | ||
return true | ||
} |
Oops, something went wrong.