Skip to content

Commit

Permalink
Merge pull request #72 from HilkopterBob/generate-systemd-unit-file
Browse files Browse the repository at this point in the history
Generate systemd unit file
  • Loading branch information
HilkopterBob authored Oct 10, 2024
2 parents ad08ed7 + f432068 commit 1159b10
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ import (
"packagelock/logger"
"packagelock/server"
"packagelock/structs"
"path/filepath"
"strconv"
"syscall"
"text/template"
"time"

"github.com/fsnotify/fsnotify"
Expand Down Expand Up @@ -129,9 +131,77 @@ func setup() {
}
pp.Println("Generated certs directory")

generateUnitFile()
pp.Println("Generated Unit File")

pp.Println("Setup finished successfully!")
}

func generateUnitFile() {
// SystemdTemplate defines the systemd unit file structure
const SystemdTemplate = `[Unit]
Description=PackageLock Management Server
After=network.target
[Service]
ExecStart={{.ExecStart}} start
Restart=always
User={{.User}}
Group={{.Group}}
[Install]
WantedBy=multi-user.target
`

// UnitFileData holds the dynamic data for the systemd unit file
type UnitFileData struct {
ExecStart string
User string
Group string
}

// Get the current executable path
execPath, err := os.Executable()
if err != nil {
logger.Logger.Panicf("failed to get executable path: %w", err)
}
// Convert to an absolute path
execPath, err = filepath.Abs(execPath)
if err != nil {
logger.Logger.Panicf("failed to get absolute executable path: %w", err)
}

// Define the data to be injected into the unit file
data := UnitFileData{
ExecStart: execPath, // The path of the Go binary
User: "your-user", // Replace with your actual user
Group: "your-group", // Replace with your actual group
}

// Open the systemd unit file for writing (requires sudo permission)
filePath := "/etc/systemd/system/packagelock.service"
file, err := os.Create(filePath)
if err != nil {
pp.Println("Seems like you cant generate the Unit File...")
pp.Println("Did you ran this with 'sudo'?🚀")
logger.Logger.Panicf("failed to create systemd unit file: %w", err)
}
defer file.Close()

// Parse and execute the systemd template
tmpl, err := template.New("systemd").Parse(SystemdTemplate)
if err != nil {
logger.Logger.Panicf("failed to parse systemd template: %w", err)
}

err = tmpl.Execute(file, data)
if err != nil {
logger.Logger.Panicf("failed to execute template: %w", err)
}

pp.Printf("Systemd unit file created at %s\n", filePath)
}

// INFO: init is ran everytime a cobra comand gets used.
// It does not init the Server!
// It only inits cobra!
Expand Down

0 comments on commit 1159b10

Please sign in to comment.