Compare commits
7 Commits
v0.0.1
...
880023c68b
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
880023c68b | ||
|
|
168b81f104 | ||
|
|
d8c90e4fff | ||
|
|
e4d6f3a4a2 | ||
|
|
cb9187bfbd | ||
|
|
e6743e12fd | ||
|
|
e5a61bb364 |
@@ -4,6 +4,11 @@ on:
|
||||
push:
|
||||
tags:
|
||||
- 'v*'
|
||||
workflow_dispatch:
|
||||
inputs:
|
||||
tag:
|
||||
description: 'Tag to release (e.g. v1.2.3)'
|
||||
required: true
|
||||
|
||||
jobs:
|
||||
test:
|
||||
@@ -35,7 +40,7 @@ jobs:
|
||||
|
||||
- name: Build release binaries
|
||||
run: |
|
||||
VERSION="${GITHUB_REF_NAME}"
|
||||
VERSION="${{ github.event.inputs.tag || github.ref_name }}"
|
||||
for target in "linux/amd64" "linux/arm64" "darwin/amd64" "darwin/arm64" "windows/amd64"; do
|
||||
GOOS="${target%/*}"
|
||||
GOARCH="${target#*/}"
|
||||
@@ -49,9 +54,40 @@ jobs:
|
||||
done
|
||||
|
||||
- name: Create release and upload assets
|
||||
uses: softprops/action-gh-release@v2
|
||||
with:
|
||||
files: unitdore-*
|
||||
generate_release_notes: true
|
||||
run: |
|
||||
TAG="${{ github.event.inputs.tag || github.ref_name }}"
|
||||
API="${GITHUB_API_URL}/repos/${GITHUB_REPOSITORY}/releases"
|
||||
|
||||
# Collect commits since the previous tag (or last 20 if no prior tag)
|
||||
PREV_TAG=$(git tag --sort=-version:refname | grep -v "^${TAG}$" | head -1)
|
||||
if [ -n "$PREV_TAG" ]; then
|
||||
RANGE="${PREV_TAG}..${TAG}"
|
||||
else
|
||||
RANGE="HEAD~20..HEAD"
|
||||
fi
|
||||
NOTES=$(git log "$RANGE" --pretty=format:"- %s" --no-merges)
|
||||
BODY="## What's changed"$'\n'"${NOTES}"
|
||||
|
||||
# Escape for JSON
|
||||
BODY_JSON=$(printf '%s' "$BODY" | python3 -c 'import json,sys; print(json.dumps(sys.stdin.read()))')
|
||||
|
||||
RELEASE=$(curl -s -X POST "$API" \
|
||||
-H "Authorization: token ${GITHUB_TOKEN}" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d "{\"tag_name\":\"${TAG}\",\"name\":\"${TAG}\",\"body\":${BODY_JSON}}")
|
||||
|
||||
UPLOAD_URL=$(echo "$RELEASE" | grep -o '"upload_url":"[^"]*"' | cut -d'"' -f4)
|
||||
if [ -z "$UPLOAD_URL" ]; then
|
||||
echo "Failed to create release: $RELEASE"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
for f in unitdore-*; do
|
||||
echo "Uploading $f..."
|
||||
curl -s -X POST "${UPLOAD_URL}?name=${f}" \
|
||||
-H "Authorization: token ${GITHUB_TOKEN}" \
|
||||
-H "Content-Type: application/octet-stream" \
|
||||
--data-binary "@${f}" > /dev/null
|
||||
done
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
|
||||
10
cmd/edit.go
10
cmd/edit.go
@@ -21,7 +21,15 @@ func init() {
|
||||
func runEdit(cmd *cobra.Command, args []string) error {
|
||||
editor := os.Getenv("EDITOR")
|
||||
if editor == "" {
|
||||
editor = "vi"
|
||||
for _, e := range []string{"nvim", "nano", "vi"} {
|
||||
if _, err := exec.LookPath(e); err == nil {
|
||||
editor = e
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
if editor == "" {
|
||||
return fmt.Errorf("no editor found; set $EDITOR")
|
||||
}
|
||||
c := exec.Command(editor, configPath)
|
||||
c.Stdin = os.Stdin
|
||||
|
||||
@@ -31,7 +31,7 @@ func runInstall(cmd *cobra.Command, args []string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
prefix, suffix := cfg.Prefix, cfg.Suffix
|
||||
prefix, suffix, serviceUser := cfg.Prefix, cfg.Suffix, cfg.EffectiveServiceUser()
|
||||
installed := 0
|
||||
skipped := 0
|
||||
removed := 0
|
||||
@@ -57,7 +57,7 @@ func runInstall(cmd *cobra.Command, args []string) error {
|
||||
}
|
||||
|
||||
if dryRun {
|
||||
content, err := systemd.Generate(u, prefix, suffix)
|
||||
content, err := systemd.Generate(u, prefix, suffix, serviceUser)
|
||||
if err != nil {
|
||||
fmt.Printf(" ✗ %s: %v\n", u.Name, err)
|
||||
continue
|
||||
@@ -68,7 +68,7 @@ func runInstall(cmd *cobra.Command, args []string) error {
|
||||
continue
|
||||
}
|
||||
|
||||
if err := systemd.Install(u, prefix, suffix); err != nil {
|
||||
if err := systemd.Install(u, prefix, suffix, serviceUser); err != nil {
|
||||
fmt.Printf(" ✗ failed: %s: %v\n", u.Name, err)
|
||||
} else {
|
||||
path, _ := systemd.UnitPath(u, prefix, suffix)
|
||||
|
||||
89
cmd/list.go
Normal file
89
cmd/list.go
Normal file
@@ -0,0 +1,89 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/warkanum/unitdore/config"
|
||||
"github.com/warkanum/unitdore/systemd"
|
||||
)
|
||||
|
||||
var listCmd = &cobra.Command{
|
||||
Use: "list",
|
||||
Short: "List all tracked units with service file locations",
|
||||
RunE: runList,
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(listCmd)
|
||||
}
|
||||
|
||||
func runList(cmd *cobra.Command, args []string) error {
|
||||
cfg, err := config.Load(configPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if len(cfg.Units) == 0 {
|
||||
fmt.Println("No units tracked. Run 'unitdore syncup' to discover containers.")
|
||||
return nil
|
||||
}
|
||||
|
||||
units := make([]config.Unit, len(cfg.Units))
|
||||
copy(units, cfg.Units)
|
||||
sort.Slice(units, func(i, j int) bool {
|
||||
if units[i].Order != units[j].Order {
|
||||
return units[i].Order < units[j].Order
|
||||
}
|
||||
return units[i].Name < units[j].Name
|
||||
})
|
||||
|
||||
prefix, suffix := cfg.Prefix, cfg.Suffix
|
||||
|
||||
type row struct{ name, runtime, enabled, installed, path string }
|
||||
|
||||
rows := make([]row, 0, len(units))
|
||||
for _, u := range units {
|
||||
enabled := "yes"
|
||||
if !u.Enabled {
|
||||
enabled = "no"
|
||||
}
|
||||
|
||||
installed := "no"
|
||||
unitPath := "—"
|
||||
if p, err := systemd.UnitPath(u, prefix, suffix); err == nil {
|
||||
unitPath = p
|
||||
if systemd.IsInstalled(u, prefix, suffix) {
|
||||
installed = "yes"
|
||||
}
|
||||
}
|
||||
|
||||
rows = append(rows, row{u.Name, u.Runtime, enabled, installed, unitPath})
|
||||
}
|
||||
|
||||
cols := []string{"NAME", "RUNTIME", "ENABLED", "INSTALLED", "SERVICE FILE"}
|
||||
widths := make([]int, len(cols))
|
||||
for i, c := range cols {
|
||||
widths[i] = len(c)
|
||||
}
|
||||
for _, r := range rows {
|
||||
vals := []string{r.name, r.runtime, r.enabled, r.installed, r.path}
|
||||
for i, v := range vals {
|
||||
if len(v) > widths[i] {
|
||||
widths[i] = len(v)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Println()
|
||||
printRow(cols, widths)
|
||||
fmt.Println(strings.Repeat("─", totalWidth(widths)))
|
||||
for _, r := range rows {
|
||||
printRow([]string{r.name, r.runtime, r.enabled, r.installed, r.path}, widths)
|
||||
}
|
||||
fmt.Println()
|
||||
|
||||
return nil
|
||||
}
|
||||
46
cmd/start.go
Normal file
46
cmd/start.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/warkanum/unitdore/config"
|
||||
"github.com/warkanum/unitdore/systemd"
|
||||
)
|
||||
|
||||
var startCmd = &cobra.Command{
|
||||
Use: "start <name>",
|
||||
Short: "Start a unit by name",
|
||||
Args: cobra.ExactArgs(1),
|
||||
RunE: runStart,
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(startCmd)
|
||||
}
|
||||
|
||||
func runStart(cmd *cobra.Command, args []string) error {
|
||||
cfg, err := config.Load(configPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
name := args[0]
|
||||
u := cfg.FindUnit(name)
|
||||
if u == nil {
|
||||
return fmt.Errorf("unit %q not found", name)
|
||||
}
|
||||
|
||||
prefix, suffix := cfg.Prefix, cfg.Suffix
|
||||
|
||||
if !systemd.IsInstalled(*u, prefix, suffix) {
|
||||
return fmt.Errorf("%s is not installed — run 'unitdore install' first", name)
|
||||
}
|
||||
|
||||
fmt.Printf(" ▶ starting %s...\n", systemd.ServiceName(*u, prefix, suffix))
|
||||
if err := systemd.Start(*u, prefix, suffix); err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Printf(" ✓ started: %s\n", name)
|
||||
return nil
|
||||
}
|
||||
@@ -9,19 +9,19 @@ import (
|
||||
"github.com/warkanum/unitdore/systemd"
|
||||
)
|
||||
|
||||
var activeCmd = &cobra.Command{
|
||||
Use: "active",
|
||||
var startallCmd = &cobra.Command{
|
||||
Use: "startall",
|
||||
Short: "Enable and start all installed, enabled units",
|
||||
Long: `Active runs 'systemctl enable --now' for all enabled units that have
|
||||
Long: `Startall runs 'systemctl enable --now' for all enabled units that have
|
||||
been installed. Units must be installed first via 'unitdore install'.`,
|
||||
RunE: runActive,
|
||||
RunE: runStartall,
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(activeCmd)
|
||||
rootCmd.AddCommand(startallCmd)
|
||||
}
|
||||
|
||||
func runActive(cmd *cobra.Command, args []string) error {
|
||||
func runStartall(cmd *cobra.Command, args []string) error {
|
||||
cfg, err := config.Load(configPath)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -29,7 +29,6 @@ func runActive(cmd *cobra.Command, args []string) error {
|
||||
|
||||
prefix, suffix := cfg.Prefix, cfg.Suffix
|
||||
|
||||
// Sort by order then name for deterministic startup sequence
|
||||
units := make([]config.Unit, len(cfg.Units))
|
||||
copy(units, cfg.Units)
|
||||
sort.Slice(units, func(i, j int) bool {
|
||||
@@ -50,7 +49,7 @@ func runActive(cmd *cobra.Command, args []string) error {
|
||||
fmt.Printf(" ! %s: not installed — run 'unitdore install' first\n", u.Name)
|
||||
continue
|
||||
}
|
||||
fmt.Printf(" ▶ enabling: %s...\n", systemd.ServiceName(u, prefix, suffix))
|
||||
fmt.Printf(" ▶ starting: %s...\n", systemd.ServiceName(u, prefix, suffix))
|
||||
if err := systemd.Enable(u, prefix, suffix); err != nil {
|
||||
fmt.Printf(" ✗ failed: %s: %v\n", u.Name, err)
|
||||
failed++
|
||||
46
cmd/stop.go
Normal file
46
cmd/stop.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/warkanum/unitdore/config"
|
||||
"github.com/warkanum/unitdore/systemd"
|
||||
)
|
||||
|
||||
var stopCmd = &cobra.Command{
|
||||
Use: "stop <name>",
|
||||
Short: "Stop a unit by name",
|
||||
Args: cobra.ExactArgs(1),
|
||||
RunE: runStop,
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(stopCmd)
|
||||
}
|
||||
|
||||
func runStop(cmd *cobra.Command, args []string) error {
|
||||
cfg, err := config.Load(configPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
name := args[0]
|
||||
u := cfg.FindUnit(name)
|
||||
if u == nil {
|
||||
return fmt.Errorf("unit %q not found", name)
|
||||
}
|
||||
|
||||
prefix, suffix := cfg.Prefix, cfg.Suffix
|
||||
|
||||
if !systemd.IsInstalled(*u, prefix, suffix) {
|
||||
return fmt.Errorf("%s is not installed", name)
|
||||
}
|
||||
|
||||
fmt.Printf(" ■ stopping %s...\n", systemd.ServiceName(*u, prefix, suffix))
|
||||
if err := systemd.Stop(*u, prefix, suffix); err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Printf(" ✓ stopped: %s\n", name)
|
||||
return nil
|
||||
}
|
||||
60
cmd/stopall.go
Normal file
60
cmd/stopall.go
Normal file
@@ -0,0 +1,60 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/warkanum/unitdore/config"
|
||||
"github.com/warkanum/unitdore/systemd"
|
||||
)
|
||||
|
||||
var stopallCmd = &cobra.Command{
|
||||
Use: "stopall",
|
||||
Short: "Stop and disable all running managed units",
|
||||
Long: `Stopall runs 'systemctl disable --now' for all enabled, installed units.`,
|
||||
RunE: runStopall,
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(stopallCmd)
|
||||
}
|
||||
|
||||
func runStopall(cmd *cobra.Command, args []string) error {
|
||||
cfg, err := config.Load(configPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
prefix, suffix := cfg.Prefix, cfg.Suffix
|
||||
|
||||
// Reverse order for shutdown
|
||||
units := make([]config.Unit, len(cfg.Units))
|
||||
copy(units, cfg.Units)
|
||||
sort.Slice(units, func(i, j int) bool {
|
||||
if units[i].Order != units[j].Order {
|
||||
return units[i].Order > units[j].Order
|
||||
}
|
||||
return units[i].Name > units[j].Name
|
||||
})
|
||||
|
||||
stopped := 0
|
||||
failed := 0
|
||||
|
||||
for _, u := range units {
|
||||
if !systemd.IsInstalled(u, prefix, suffix) {
|
||||
continue
|
||||
}
|
||||
fmt.Printf(" ■ stopping: %s...\n", systemd.ServiceName(u, prefix, suffix))
|
||||
if err := systemd.Disable(u, prefix, suffix); err != nil {
|
||||
fmt.Printf(" ✗ failed: %s: %v\n", u.Name, err)
|
||||
failed++
|
||||
} else {
|
||||
fmt.Printf(" ✓ stopped: %s\n", u.Name)
|
||||
stopped++
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Printf("\nDone. Stopped: %d Failed: %d\n", stopped, failed)
|
||||
return nil
|
||||
}
|
||||
@@ -31,6 +31,7 @@ func runSyncup(cmd *cobra.Command, args []string) error {
|
||||
}
|
||||
|
||||
added := 0
|
||||
unchanged := 0
|
||||
discovered := map[string]bool{}
|
||||
|
||||
// Discover running containers across all runtimes
|
||||
@@ -51,6 +52,8 @@ func runSyncup(cmd *cobra.Command, args []string) error {
|
||||
if cfg.AddUnit(unit) {
|
||||
fmt.Printf(" + added: %s (%s)\n", c.Name, c.Runtime)
|
||||
added++
|
||||
} else {
|
||||
unchanged++
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -86,6 +89,6 @@ func runSyncup(cmd *cobra.Command, args []string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
fmt.Printf("\nDone. Added: %d Disabled: %d Re-enabled: %d\n", added, disabled, reenabled)
|
||||
fmt.Printf("\nDone. Added: %d Unchanged: %d Disabled: %d Re-enabled: %d\n", added, unchanged, disabled, reenabled)
|
||||
return nil
|
||||
}
|
||||
|
||||
55
cmd/uninstall.go
Normal file
55
cmd/uninstall.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/warkanum/unitdore/config"
|
||||
"github.com/warkanum/unitdore/systemd"
|
||||
)
|
||||
|
||||
var uninstallCmd = &cobra.Command{
|
||||
Use: "uninstall <name>",
|
||||
Short: "Disable, stop, and remove the service file for a unit",
|
||||
Args: cobra.ExactArgs(1),
|
||||
RunE: runUninstall,
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(uninstallCmd)
|
||||
}
|
||||
|
||||
func runUninstall(cmd *cobra.Command, args []string) error {
|
||||
cfg, err := config.Load(configPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
name := args[0]
|
||||
u := cfg.FindUnit(name)
|
||||
if u == nil {
|
||||
return fmt.Errorf("unit %q not found", name)
|
||||
}
|
||||
|
||||
prefix, suffix := cfg.Prefix, cfg.Suffix
|
||||
|
||||
if !systemd.IsInstalled(*u, prefix, suffix) {
|
||||
return fmt.Errorf("%s is not installed", name)
|
||||
}
|
||||
|
||||
svcName := systemd.ServiceName(*u, prefix, suffix)
|
||||
|
||||
fmt.Printf(" ■ disabling %s...\n", svcName)
|
||||
if err := systemd.Disable(*u, prefix, suffix); err != nil {
|
||||
return fmt.Errorf("disabling unit: %w", err)
|
||||
}
|
||||
|
||||
path, _ := systemd.UnitPath(*u, prefix, suffix)
|
||||
fmt.Printf(" ✗ removing %s...\n", path)
|
||||
if err := systemd.Uninstall(*u, prefix, suffix); err != nil {
|
||||
return fmt.Errorf("removing service file: %w", err)
|
||||
}
|
||||
|
||||
fmt.Printf(" ✓ uninstalled: %s\n", name)
|
||||
return nil
|
||||
}
|
||||
77
cmd/update.go
Normal file
77
cmd/update.go
Normal file
@@ -0,0 +1,77 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/warkanum/unitdore/config"
|
||||
"github.com/warkanum/unitdore/systemd"
|
||||
)
|
||||
|
||||
var (
|
||||
updateOrder int
|
||||
updateEnable bool
|
||||
updateDisable bool
|
||||
)
|
||||
|
||||
var updateCmd = &cobra.Command{
|
||||
Use: "update <name>",
|
||||
Short: "Update a unit's config and recreate its service file",
|
||||
Args: cobra.ExactArgs(1),
|
||||
RunE: runUpdate,
|
||||
}
|
||||
|
||||
func init() {
|
||||
updateCmd.Flags().IntVar(&updateOrder, "order", 0, "set startup order")
|
||||
updateCmd.Flags().BoolVar(&updateEnable, "enable", false, "enable the unit")
|
||||
updateCmd.Flags().BoolVar(&updateDisable, "disable", false, "disable the unit")
|
||||
rootCmd.AddCommand(updateCmd)
|
||||
}
|
||||
|
||||
func runUpdate(cmd *cobra.Command, args []string) error {
|
||||
if updateEnable && updateDisable {
|
||||
return errors.New("--enable and --disable are mutually exclusive")
|
||||
}
|
||||
|
||||
cfg, err := config.Load(configPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
name := args[0]
|
||||
u := cfg.FindUnit(name)
|
||||
if u == nil {
|
||||
return fmt.Errorf("unit %q not found", name)
|
||||
}
|
||||
|
||||
if cmd.Flags().Changed("order") {
|
||||
u.Order = updateOrder
|
||||
fmt.Printf(" order → %d\n", u.Order)
|
||||
}
|
||||
if updateEnable {
|
||||
u.Enabled = true
|
||||
u.DisabledReason = ""
|
||||
fmt.Printf(" enabled\n")
|
||||
}
|
||||
if updateDisable {
|
||||
u.Enabled = false
|
||||
fmt.Printf(" disabled\n")
|
||||
}
|
||||
|
||||
if err := cfg.Save(configPath); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
prefix, suffix, serviceUser := cfg.Prefix, cfg.Suffix, cfg.EffectiveServiceUser()
|
||||
|
||||
if systemd.IsInstalled(*u, prefix, suffix) {
|
||||
if err := systemd.Install(*u, prefix, suffix, serviceUser); err != nil {
|
||||
return fmt.Errorf("recreating service file: %w", err)
|
||||
}
|
||||
path, _ := systemd.UnitPath(*u, prefix, suffix)
|
||||
fmt.Printf(" ✓ service file recreated: %s\n", path)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package config
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/user"
|
||||
"path/filepath"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
@@ -27,6 +28,18 @@ type Config struct {
|
||||
Units []Unit `yaml:"units"`
|
||||
Prefix string `yaml:"prefix,omitempty"` // prepended to generated service name, e.g. "prod-"
|
||||
Suffix string `yaml:"suffix,omitempty"` // appended to generated service name, e.g. "-svc"
|
||||
ServiceUser string `yaml:"service_user,omitempty"` // User= in [Service] section; defaults to "unitdore"
|
||||
}
|
||||
|
||||
// EffectiveServiceUser returns the configured service user, or the current OS user if unset.
|
||||
func (c *Config) EffectiveServiceUser() string {
|
||||
if c.ServiceUser != "" {
|
||||
return c.ServiceUser
|
||||
}
|
||||
if u, err := user.Current(); err == nil {
|
||||
return u.Username
|
||||
}
|
||||
return "root"
|
||||
}
|
||||
|
||||
// Load reads and parses the config file at path.
|
||||
|
||||
@@ -68,9 +68,9 @@ func (d *Docker) Exists(name string) (bool, error) {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
out, err := exec.Command(bin, "ps", "-a", "--format", "{{.Names}}").Output()
|
||||
out, err := exec.Command(bin, "ps", "--format", "{{.Names}}").Output()
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("docker ps -a: %w", err)
|
||||
return false, fmt.Errorf("docker ps: %w", err)
|
||||
}
|
||||
|
||||
for _, line := range strings.Split(string(out), "\n") {
|
||||
|
||||
@@ -15,7 +15,7 @@ type podmanContainer struct {
|
||||
ID string `json:"Id"`
|
||||
Names []string `json:"Names"`
|
||||
Image string `json:"Image"`
|
||||
Command string `json:"Command"`
|
||||
Command []string `json:"Command"`
|
||||
State string `json:"State"`
|
||||
}
|
||||
|
||||
@@ -55,7 +55,7 @@ func (p *Podman) ListRunning() ([]Container, error) {
|
||||
containers = append(containers, Container{
|
||||
Name: name,
|
||||
Image: c.Image,
|
||||
Command: c.Command,
|
||||
Command: strings.Join(c.Command, " "),
|
||||
Runtime: "podman",
|
||||
})
|
||||
}
|
||||
@@ -68,9 +68,9 @@ func (p *Podman) Exists(name string) (bool, error) {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
out, err := exec.Command(bin, "ps", "-a", "--format", "{{.Names}}").Output()
|
||||
out, err := exec.Command(bin, "ps", "--format", "{{.Names}}").Output()
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("podman ps -a: %w", err)
|
||||
return false, fmt.Errorf("podman ps: %w", err)
|
||||
}
|
||||
|
||||
for _, line := range strings.Split(string(out), "\n") {
|
||||
|
||||
@@ -17,6 +17,7 @@ After=network.target
|
||||
|
||||
[Service]
|
||||
Type=simple
|
||||
User={{.ServiceUser}}
|
||||
ExecStart={{.ExecStart}}
|
||||
ExecStop={{.ExecStop}}
|
||||
Restart=on-failure
|
||||
@@ -49,6 +50,7 @@ type templateData struct {
|
||||
Unit config.Unit
|
||||
ExecStart string
|
||||
ExecStop string
|
||||
ServiceUser string
|
||||
}
|
||||
|
||||
// ServiceName returns the systemd service name for a unit, with optional prefix/suffix.
|
||||
@@ -57,7 +59,7 @@ func ServiceName(u config.Unit, prefix, suffix string) string {
|
||||
}
|
||||
|
||||
// Generate produces the .service file content for a unit.
|
||||
func Generate(u config.Unit, prefix, suffix string) (string, error) {
|
||||
func Generate(u config.Unit, prefix, suffix, serviceUser string) (string, error) {
|
||||
execStart, execStop := buildExecCommands(u)
|
||||
|
||||
data := templateData{
|
||||
@@ -65,6 +67,7 @@ func Generate(u config.Unit, prefix, suffix string) (string, error) {
|
||||
Unit: u,
|
||||
ExecStart: execStart,
|
||||
ExecStop: execStop,
|
||||
ServiceUser: serviceUser,
|
||||
}
|
||||
|
||||
tmplStr := systemUnitTemplate
|
||||
|
||||
@@ -35,7 +35,7 @@ func TestGenerate_SystemUnit(t *testing.T) {
|
||||
Enabled: true,
|
||||
}
|
||||
|
||||
content, err := Generate(u, "", "")
|
||||
content, err := Generate(u, "", "", "unitdore")
|
||||
if err != nil {
|
||||
t.Fatalf("Generate() error: %v", err)
|
||||
}
|
||||
@@ -70,7 +70,7 @@ func TestGenerate_UserUnit(t *testing.T) {
|
||||
Enabled: true,
|
||||
}
|
||||
|
||||
content, err := Generate(u, "", "")
|
||||
content, err := Generate(u, "", "", "unitdore")
|
||||
if err != nil {
|
||||
t.Fatalf("Generate() error: %v", err)
|
||||
}
|
||||
@@ -101,7 +101,7 @@ func TestGenerate_CustomCommand(t *testing.T) {
|
||||
Enabled: true,
|
||||
}
|
||||
|
||||
content, err := Generate(u, "", "")
|
||||
content, err := Generate(u, "", "", "unitdore")
|
||||
if err != nil {
|
||||
t.Fatalf("Generate() error: %v", err)
|
||||
}
|
||||
@@ -118,7 +118,7 @@ func TestGenerate_DockerRuntime(t *testing.T) {
|
||||
Enabled: true,
|
||||
}
|
||||
|
||||
content, err := Generate(u, "", "")
|
||||
content, err := Generate(u, "", "", "unitdore")
|
||||
if err != nil {
|
||||
t.Fatalf("Generate() error: %v", err)
|
||||
}
|
||||
@@ -138,7 +138,7 @@ func TestGenerate_UnknownRuntime(t *testing.T) {
|
||||
Enabled: true,
|
||||
}
|
||||
|
||||
content, err := Generate(u, "", "")
|
||||
content, err := Generate(u, "", "", "unitdore")
|
||||
if err != nil {
|
||||
t.Fatalf("Generate() should not error on unknown runtime: %v", err)
|
||||
}
|
||||
@@ -155,7 +155,7 @@ func TestGenerate_WithPrefixSuffix(t *testing.T) {
|
||||
Enabled: true,
|
||||
}
|
||||
|
||||
content, err := Generate(u, "prod-", "-web")
|
||||
content, err := Generate(u, "prod-", "-web", "unitdore")
|
||||
if err != nil {
|
||||
t.Fatalf("Generate() error: %v", err)
|
||||
}
|
||||
|
||||
@@ -27,8 +27,8 @@ func UnitPath(u config.Unit, prefix, suffix string) (string, error) {
|
||||
}
|
||||
|
||||
// Install writes the .service file for a unit and reloads systemd.
|
||||
func Install(u config.Unit, prefix, suffix string) error {
|
||||
content, err := Generate(u, prefix, suffix)
|
||||
func Install(u config.Unit, prefix, suffix, serviceUser string) error {
|
||||
content, err := Generate(u, prefix, suffix, serviceUser)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -73,6 +73,16 @@ func Disable(u config.Unit, prefix, suffix string) error {
|
||||
return systemctl(u.User, "disable", "--now", ServiceName(u, prefix, suffix))
|
||||
}
|
||||
|
||||
// Start starts a unit without enabling it.
|
||||
func Start(u config.Unit, prefix, suffix string) error {
|
||||
return systemctl(u.User, "start", ServiceName(u, prefix, suffix))
|
||||
}
|
||||
|
||||
// Stop stops a unit without disabling it.
|
||||
func Stop(u config.Unit, prefix, suffix string) error {
|
||||
return systemctl(u.User, "stop", ServiceName(u, prefix, suffix))
|
||||
}
|
||||
|
||||
// Status returns the ActiveState of a unit ("active", "inactive", "failed", "unknown").
|
||||
func Status(u config.Unit, prefix, suffix string) string {
|
||||
args := []string{"show", "-p", "ActiveState", "--value", ServiceName(u, prefix, suffix)}
|
||||
|
||||
Reference in New Issue
Block a user