Skip to content

bramz/systemgo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

34 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SystemGo

SystemGo is a comprehensive and extensible system management tool inspired by the capabilities of systemd. It is designed to manage and monitor system tasks and services with a focus on flexibility, reliability, and ease of use. SystemGo provides features for starting, stopping, restarting, and monitoring services, as well as an optional web interface for real-time management.

Features

  • Process Management: Start, stop, restart, and monitor system tasks and services.
  • Service Management: Manage services with support for different types of services.
  • Logging: Comprehensive logging for monitoring and debugging.
  • Configuration Management: Support for configuration files to define services and processes.
  • Web Interface: Optional web interface for monitoring and managing services.
  • Health Checks: Built-in health check mechanisms to ensure service health.
  • Service Dependencies: Define dependencies between services to manage their startup and shutdown order.
  • Security: Secure communication and authentication mechanisms.

Installation

Prerequisites

  • Go 1.22.3 or later
  • Git

Steps

  1. Clone the repository:

    git clone https://github.com/your-username/systemgo.git
    cd systemgo
    

Initialize the module and download dependencies

Build the project: ``go mod tidy ; go build -o systemgo main.go`

Configuration

SystemGo uses a YAML configuration file to define services. The configuration file should be named config.yaml and placed in the root directory of the project.

Example config.yaml yaml

services:

  • name: example-service
  • command: /path/to/your/executable

Usage

SystemGo can be managed via command-line interface. The basic usage pattern is:

systemgo <start|stop|restart|status> <service-name>

Examples

Start a service: ./systemgo start example-service

Stop a service: ./systemgo stop example-service

Restart a service: ./systemgo restart example-service

Get the status of a service: ./systemgo status example-service

Web Interface

SystemGo includes an optional web interface for monitoring and managing services. The web server runs on port 8080 by default.

Starting the Web Server

The web server starts automatically when you run the main application. To access the web interface, navigate to http://localhost:8080 in your web browser.

Web Interface Endpoints

Start a Service: GET /start?name=<service-name> Stop a Service: GET /stop?name=<service-name> Restart a Service: GET /restart?name=<service-name> Get Service Status: GET /status?name=<service-name> Health Check: GET /health?name=<service-name>

Health Checks

SystemGo includes built-in health checks to ensure the health of services. Health checks can be configured and executed using the web interface or programmatically.

Example Health Check Handler

In daemons/handlers.go, the health check handler is implemented as follows:

func HealthCheckHandler(w http.ResponseWriter, r *http.Request) {
    name := r.URL.Query().Get("name")
    svc, exists := services[name]
    if !exists {
        w.WriteHeader(http.StatusNotFound)
        json.NewEncoder(w).Encode(map[string]string{"status": "not found"})
        return
    }
    status, err := internal.CheckHealth(name, svc.Command)
    if err != nil {
        w.WriteHeader(http.StatusInternalServerError)
        json.NewEncoder(w).Encode(map[string]string{"status": status, "error": err.Error()})
        return
    }
    w.WriteHeader(http.StatusOK)
    json.NewEncoder(w).Encode(map[string]string{"status": status})
}

Logging

SystemGo provides comprehensive logging for monitoring and debugging. Logs are written to a file named systemgo.log in the root directory.

Log Levels

INFO: General information about the operations.
ERROR: Error messages when operations fail.

Log Initialization

In internal/logging.go, logging is initialized as follows:

package internal

import (
    "log"
    "os"
)

var (
    InfoLogger  *log.Logger
    ErrorLogger*log.Logger
)

func Init() {
    file, err := os.OpenFile("systemgo.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
    if err != nil {
        log.Fatalf("Failed to open log file: %v", err)
    }

    InfoLogger = log.New(file, "INFO: ", log.Ldate|log.Ltime|log.Lshortfile)
    ErrorLogger = log.New(file, "ERROR: ", log.Ldate|log.Ltime|log.Lshortfile)
}

Service Dependencies

SystemGo allows defining dependencies between services to ensure they start and stop in the correct order.

Example Dependency Configuration

To add dependencies, extend the ServiceConfig struct in internal/settings.go:

service "example-service" {
    name = "Example Service"
    command = "executable-name" # Executables should be built in systemgo/bin directory
    enabled = true
    retry_count = 3
    dependencies = "another-service, yet-another-service" # Leave blank if not required

Update the ServiceConfig struct and initialization logic to handle dependencies.

Security

SystemGo provides secure communication and authentication mechanisms for the web interface.

Enabling TLS

To enable TLS, modify the StartWebServer function in daemons/server.go:

func StartWebServer() {
    http.HandleFunc("/start", StartServiceHandler)
    http.HandleFunc("/stop", StopServiceHandler)
    http.HandleFunc("/restart", RestartServiceHandler)
    http.HandleFunc("/status", StatusServiceHandler)
    http.HandleFunc("/health", HealthCheckHandler)

    log.Fatal(http.ListenAndServeTLS(":8080", "server.crt", "server.key", nil))
}

Ensure you have the necessary certificate (server.crt) and key (server.key) files.

Contributing

We welcome contributions to improve SystemGo! To contribute:

Fork the repository

Create a new branch for your feature or bugfix. Make your changes and commit them with a clear message. Push your changes to your fork. Create a pull request describing your changes.

License

SystemGo is licensed under the MIT License. Feel free to use, modify, and distribute it according to the terms of the license.

About

Process management tool.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published