Compare commits

...

19 commits
v0.0.4 ... main

Author SHA1 Message Date
98fda3b404
refactor(deadcode): unused custom errors
All checks were successful
Pull-Request / tests (pull_request) Successful in 1m9s
Pull-Request / static-analysis (pull_request) Successful in 1m38s
Pull-Request / post-run (pull_request) Successful in 30s
Push / pre-run (push) Successful in 26s
Push / tests (push) Successful in 1m4s
Push / static-analysis (push) Successful in 1m28s
Push / post-run (push) Successful in 40s
2024-11-13 23:59:55 -05:00
7cc11af378
refactor: split fetcher by type, reorganize fetcher tests 2024-11-13 23:56:19 -05:00
5a18a5bf44
refactor: isolate + inject DefinitionFetcher
All checks were successful
Pull-Request / tests (pull_request) Successful in 1m6s
Pull-Request / static-analysis (pull_request) Successful in 1m31s
Pull-Request / post-run (pull_request) Successful in 24s
Push / pre-run (push) Successful in 28s
Push / tests (push) Successful in 1m3s
Push / static-analysis (push) Successful in 1m29s
Push / post-run (push) Successful in 38s
docs: add documentation to core functions and structs
2024-11-10 23:05:57 -05:00
8bc33870e6
feat: fetch service def from git
All checks were successful
Pull-Request / tests (pull_request) Successful in 1m6s
Pull-Request / static-analysis (pull_request) Successful in 1m31s
Pull-Request / post-run (pull_request) Successful in 24s
2024-11-10 10:52:06 -05:00
c580c0c433
refactor: generalize service definition fetch 2024-11-10 10:37:46 -05:00
24e795e1aa
ci: run pre-commit on pr + push
All checks were successful
Pull-Request / tests (pull_request) Successful in 1m8s
Pull-Request / static-analysis (pull_request) Successful in 1m33s
Pull-Request / post-run (pull_request) Successful in 26s
Push / pre-run (push) Successful in 28s
Push / tests (push) Successful in 1m7s
Push / static-analysis (push) Successful in 1m33s
Push / post-run (push) Successful in 36s
2024-11-10 10:25:33 -05:00
8f0489a622
build: add pre-commit tooling + basic go/shell/yaml hooks
docs: mention pre-commit
2024-11-10 10:25:30 -05:00
91094b9360
ci: update runs-on to runner-latest
All checks were successful
/ build (push) Successful in 2m5s
2024-11-09 18:20:47 -05:00
62eb27a0a8
feat: allow network, pid in container definitions
Some checks failed
/ build (push) Has been cancelled
2024-11-09 18:08:32 -05:00
0b2e222124
refactor: remove extraneous Post webclient flow, use Do instead 2024-10-04 20:31:38 -04:00
4cad5e875d
feat: add stop-remote to cli 2024-10-04 20:28:09 -04:00
a6c937b565
refactor: unify service management under manager struct 2024-10-04 20:05:33 -04:00
daa19167a7
feat: start cmd for remote hosts 2024-10-04 00:03:12 -04:00
de56bc9d0d
feat: add request-level logging 2024-10-03 00:33:08 -04:00
d51031a875
feat: add service start/stop via daemon api 2024-10-02 23:47:33 -04:00
01df6c4dd5
refactor: inject service client / podman in daemon 2024-10-02 23:03:57 -04:00
824ba8ba0b
feat: daemon command stub 2024-10-01 22:56:25 -04:00
da0b3dd240
build: update go -> 1.23.1 2024-09-29 15:34:11 -04:00
17221855cc
feat: add explicit flag / arg definitions
Some checks failed
/ build (push) Has been cancelled
2024-09-28 21:49:46 -04:00
35 changed files with 1132 additions and 111 deletions

View file

@ -0,0 +1,41 @@
name: Pull-Request
on: [pull_request]
jobs:
static-analysis:
runs-on: runner-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: 1.23
- name: Validate Yaml
run: pipx run pre-commit run check-yaml -a
- name: Validate shell scripts
run: pipx run pre-commit run shellcheck -a
- name: Check formatting
run: pipx run pre-commit run go-fmt -a
- name: Check code patterns
run: pipx run pre-commit run go-vet-mod -a
tests:
runs-on: runner-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: 1.23
- name: Tests
run: pipx run pre-commit run go-test-mod -a
post-run:
runs-on: runner-latest
needs: [static-analysis,tests]
if: ${{ always() }}
steps:
- name: Export trace
uses: https://forge.karnov.club/marc/opentelemetry-trace-export-forgejo-action@main
with:
otlp-endpoint: "http://otel.home.karnov.club:4318"
forgejo-token: ${{ secrets.GITHUB_TOKEN }}
forgejo-base-url: ${{ env.GITHUB_SERVER_URL }}
run-id: ${{ env.GITHUB_RUN_NUMBER }}
repo-name: ${{ env.GITHUB_REPOSITORY }}

View file

@ -0,0 +1,67 @@
name: Push
on:
push:
branches: [main]
jobs:
pre-run:
runs-on: runner-latest
steps:
- uses: https://forge.karnov.club/marc/push-status-to-discord-action@main
with:
webhook-url: ${{secrets.DISCORD_WEBHOOK_URL}}
status: "Started"
init: true
static-analysis:
runs-on: runner-latest
needs: [pre-run]
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: 1.23
- name: Validate Yaml
run: pipx run pre-commit run check-yaml -a
- name: Validate shell scripts
run: pipx run pre-commit run shellcheck -a
- name: Check formatting
run: pipx run pre-commit run go-fmt -a
- name: Check code patterns
run: pipx run pre-commit run go-vet-mod -a
tests:
runs-on: runner-latest
needs: [pre-run]
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: 1.23
- name: Tests
run: pipx run pre-commit run go-test-mod -a
post-run:
runs-on: runner-latest
needs: [static-analysis,tests]
steps:
- name: Export trace
uses: https://forge.karnov.club/marc/opentelemetry-trace-export-forgejo-action@main
with:
otlp-endpoint: "http://otel.home.karnov.club:4318"
forgejo-token: ${{ secrets.GITHUB_TOKEN }}
forgejo-base-url: ${{ env.GITHUB_SERVER_URL }}
run-id: ${{ env.GITHUB_RUN_NUMBER }}
repo-name: ${{ env.GITHUB_REPOSITORY }}
- name: Notify success
uses: https://forge.karnov.club/marc/push-status-to-discord-action@main
if: ${{always() && success()}}
with:
webhook-url: ${{secrets.DISCORD_WEBHOOK_URL}}
status: "Success"
variant: "success"
- name: Notify failure
uses: https://forge.karnov.club/marc/push-status-to-discord-action@main
if: ${{always() && failure()}}
with:
webhook-url: ${{secrets.DISCORD_WEBHOOK_URL}}
status: "Failure"
variant: "failure"

View file

@ -5,7 +5,7 @@ on:
jobs:
build:
runs-on: ubuntu-latest
runs-on: runner-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5

16
.pre-commit-config.yaml Normal file
View file

@ -0,0 +1,16 @@
---
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.6.0
hooks:
- id: check-yaml
- repo: https://github.com/shellcheck-py/shellcheck-py
rev: v0.10.0.1
hooks:
- id: shellcheck
- repo: https://github.com/tekwizely/pre-commit-golang
rev: master
hooks:
- id: go-test-mod
- id: go-fmt
- id: go-vet-mod

View file

@ -22,3 +22,7 @@ curl https://forge.karnov.club/spadinastan/spud/raw/branch/main/install.sh | bas
To pull a specific version, supply a `$SPUD_VERSION` that corresponds to an existing release tag, and to control where
the binary is unpacked and installed, supply `$SPUD_ROOT`.
## Development
The `bootstrap.sh` script should be run to prepare any pre-commit hooks and other required tooling for local development. Checks can be run manually via `pipx run pre-commit run -a`.

8
bootstrap.sh Executable file
View file

@ -0,0 +1,8 @@
#!/bin/bash
if [[ -z "$(command -v pipx)" ]]; then
echo "ERROR: Pre-commit tooling requires pipx to be available."
exit 1
fi
pipx run pre-commit install

View file

@ -1,21 +1,35 @@
package cli
import (
"fmt"
"github.com/spf13/cobra"
"log"
podman "spud/podman"
service_definition "spud/service_definition"
)
var build = &cobra.Command{
func getBuildCommand() *cobra.Command {
build := &cobra.Command{
Use: "build",
Short: "Build service images.",
Run: func(cmd *cobra.Command, args []string) {
pathProvided := args[0]
def, err := service_definition.GetServiceDefinitionFromFile(pathProvided)
RunE: func(cmd *cobra.Command, args []string) error {
var pathProvided string
var fetcher service_definition.DefinitionFetcher
var def service_definition.ServiceDefinition
var err error
if err != nil {
log.Fatal(err)
if pathProvided, err = cmd.PersistentFlags().GetString("definition"); err != nil {
return fmt.Errorf("%+v", err)
}
defPathType := service_definition.GetPathType(pathProvided)
if fetcher, err = service_definition.NewDefinitionFetcher(defPathType); err != nil {
return fmt.Errorf("Couldn't set up fetcher from the path provided.")
}
if def, err = fetcher.GetDefinition(pathProvided); err != nil {
return fmt.Errorf("Failed to read service definition from file: %+v", err)
}
imagesToBuild := def.Build.Images
@ -27,5 +41,11 @@ var build = &cobra.Command{
for _, imageDef := range imagesToBuild {
podman.Build(imageDef)
}
return nil
},
}
build.PersistentFlags().StringP("definition", "d", "./service.yml", "Path to the service definition to use.")
return build
}

37
cli/build_test.go Normal file
View file

@ -0,0 +1,37 @@
package cli
import (
"github.com/spf13/cobra"
"testing"
)
func TestCliBuildServiceDefinitionPathMustExist(t *testing.T) {
cli := GetCli()
cli.SetArgs([]string{"build", "-d", "./not-a-file.yml"})
outcome := cli.Execute()
if outcome == nil {
t.Errorf("Expected error, got nil.")
}
}
func TestCliBuildDefaultsServiceDefinitionPath(t *testing.T) {
cli := GetCli()
cli.SetArgs([]string{"build"})
startCommand, _, _ := cli.Find([]string{"build"})
startCommand.RunE = func(cmd *cobra.Command, args []string) error {
actual, _ := cmd.PersistentFlags().GetString("definition")
if actual != "./service.yml" {
t.Errorf("Unexpected default value for 'definition' / 'd' arg: %s", actual)
}
return nil
}
cli.Execute()
}

View file

@ -5,15 +5,6 @@ import (
"github.com/spf13/cobra"
)
var allCommands = []*cobra.Command{
// cli/start_service.go
start,
// cli/stop_service.go
stop,
// cli/build.go
build,
}
// Creates the root of the CLI, with all available commands
// added to it.
func GetCli() *cobra.Command {
@ -22,6 +13,17 @@ func GetCli() *cobra.Command {
Short: "A not-entirely-terrible-way to manage self-hosted services.",
}
allCommands := []*cobra.Command{
// cli/start_service.go
getStartCommand(),
// cli/stop_service.go
getStopCommand(),
// cli/build.go
getBuildCommand(),
// cli/daemon.go
getDaemonCommand(),
}
for _, command := range allCommands {
cli.AddCommand(command)
}

21
cli/daemon.go Normal file
View file

@ -0,0 +1,21 @@
package cli
import (
"github.com/spf13/cobra"
daemon "spud/daemon"
)
func getDaemonCommand() *cobra.Command {
startDaemon := &cobra.Command{
Use: "daemon",
Short: "Starts a daemon instance.",
RunE: func(cmd *cobra.Command, args []string) error {
d := daemon.NewDaemon("", 8000, nil)
d.Start()
return nil
},
}
return startDaemon
}

View file

@ -5,23 +5,78 @@
package cli
import (
"context"
"fmt"
"github.com/spf13/cobra"
"log"
service "spud/service"
service_definition "spud/service_definition"
webclient "spud/webclient"
)
var start = &cobra.Command{
func getStartCommand() *cobra.Command {
type ParsedFlags struct {
definitionPath string
daemonHost string
daemonPort int
}
start := &cobra.Command{
Use: "start",
Short: "Creates or updates a service based on the provided definition.",
Run: func(cmd *cobra.Command, args []string) {
pathProvided := args[0]
def, err := service_definition.GetServiceDefinitionFromFile(pathProvided)
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
var pathProvided, host string
var port int
var err error
if err != nil {
log.Fatal(err)
if pathProvided, err = cmd.PersistentFlags().GetString("definition"); err != nil {
return err
}
service.CreateService(def)
if host, err = cmd.PersistentFlags().GetString("host"); err != nil {
return err
}
if port, err = cmd.PersistentFlags().GetInt("port"); err != nil {
return err
}
if host != "" && port == 0 || host == "" && port != 0 {
return fmt.Errorf("Invalid flags: host and port must be defined together or not at all.")
}
cmd.SetContext(context.WithValue(cmd.Context(), "flags", ParsedFlags{definitionPath: pathProvided, daemonHost: host, daemonPort: port}))
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
var fetcher service_definition.DefinitionFetcher
var def service_definition.ServiceDefinition
var err error
ctx := cmd.Context()
flags := ctx.Value("flags").(ParsedFlags)
defPath := flags.definitionPath
defPathType := service_definition.GetPathType(defPath)
if fetcher, err = service_definition.NewDefinitionFetcher(defPathType); err != nil {
return fmt.Errorf("Couldn't set up fetcher from the path provided.")
}
if def, err = fetcher.GetDefinition(defPath); err != nil {
return fmt.Errorf("Failed to read service definition: %+v", err)
}
if flags.daemonHost != "" && flags.daemonPort != 0 {
webclient.NewWebClient(flags.daemonHost, flags.daemonPort).CreateService(def)
return nil
}
return service.NewPodmanServiceManager().Create(def)
},
}
start.PersistentFlags().StringP("definition", "d", "./service.yml", "Path to the service definition to use.")
start.PersistentFlags().StringP("host", "H", "", "If specified, host where the daemon lives.")
start.PersistentFlags().IntP("port", "p", 0, "Port on the daemon host.")
return start
}

71
cli/start_service_test.go Normal file
View file

@ -0,0 +1,71 @@
package cli
import (
"fmt"
"github.com/spf13/cobra"
"testing"
)
func Test_StartServiceCli_StartServiceDefinitionPathMustExist(t *testing.T) {
cli := GetCli()
cli.SetArgs([]string{"start", "-d", "./not-a-file.yml"})
outcome := cli.Execute()
if outcome == nil {
t.Errorf("Expected error, got nil.")
}
}
func Test_StartServiceCli_StartDefaultsServiceDefinitionPath(t *testing.T) {
cli := GetCli()
cli.SetArgs([]string{"start"})
startCommand, _, _ := cli.Find([]string{"start"})
startCommand.RunE = func(cmd *cobra.Command, args []string) error {
actual, _ := cmd.PersistentFlags().GetString("definition")
if actual != "./service.yml" {
t.Errorf("Unexpected default value for 'definition' / 'd' arg: %s", actual)
}
return nil
}
cli.Execute()
}
func Test_StartServiceCli_ErrorIfHostAndPortNotProvidedTogether(t *testing.T) {
inputs := [][]string{
{"start", "-H", "host"},
{"start", "-p", "9999"},
}
for _, input := range inputs {
t.Run(fmt.Sprintf("%+v", input), func(t *testing.T) {
cli := GetCli()
cli.SetArgs(input)
startCommand, _, _ := cli.Find([]string{"start"})
previousPreRun := startCommand.PersistentPreRunE
startCommand.PersistentPreRunE = func(cmd *cobra.Command, args []string) error {
if err := previousPreRun(cmd, args); err == nil {
t.Errorf("Expected error, got nil.")
}
return nil
}
startCommand.RunE = func(cmd *cobra.Command, args []string) error {
return nil
}
cli.Execute()
})
}
}

View file

@ -4,15 +4,63 @@
package cli
import (
"context"
"fmt"
"github.com/spf13/cobra"
service "spud/service"
webclient "spud/webclient"
)
var stop = &cobra.Command{
Use: "stop",
func getStopCommand() *cobra.Command {
type ParsedFlags struct {
serviceName string
daemonHost string
daemonPort int
}
stop := &cobra.Command{
Use: "stop [service-name]",
Short: "Stops a running service and all of its containers.",
Run: func(cmd *cobra.Command, args []string) {
serviceName := args[0]
service.StopService(serviceName)
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
var serviceName, host string
var port int
var err error
if host, err = cmd.PersistentFlags().GetString("host"); err != nil {
return err
}
if port, err = cmd.PersistentFlags().GetInt("port"); err != nil {
return err
}
if host != "" && port == 0 || host == "" && port != 0 {
return fmt.Errorf("Invalid flags: host and port must be defined together or not at all.")
}
if len(args) == 0 {
return fmt.Errorf("Must provide a service name.")
}
serviceName = args[0]
cmd.SetContext(context.WithValue(cmd.Context(), "flags", ParsedFlags{serviceName: serviceName, daemonHost: host, daemonPort: port}))
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
flags := cmd.Context().Value("flags").(ParsedFlags)
if flags.daemonHost != "" && flags.daemonPort != 0 {
webclient.NewWebClient(flags.daemonHost, flags.daemonPort).StopService(flags.serviceName)
return nil
}
return service.NewPodmanServiceManager().Stop(flags.serviceName)
},
}
stop.PersistentFlags().StringP("host", "H", "", "If specified, host where the daemon lives.")
stop.PersistentFlags().IntP("port", "p", 0, "Port on the daemon host.")
return stop
}

52
cli/stop_service_test.go Normal file
View file

@ -0,0 +1,52 @@
package cli
import (
"fmt"
"github.com/spf13/cobra"
"testing"
)
func Test_StopServiceCli_StopRequiresAServiceName(t *testing.T) {
cli := GetCli()
cli.SetArgs([]string{"stop"})
outcome := cli.Execute()
if outcome == nil {
t.Error("Expected error, got nil.")
}
}
func Test_StopServiceCli_ErrorIfHostAndPortNotProvidedTogether(t *testing.T) {
inputs := [][]string{
{"stop", "-H", "host"},
{"stop", "-p", "9999"},
}
for _, input := range inputs {
t.Run(fmt.Sprintf("%+v", input), func(t *testing.T) {
cli := GetCli()
cli.SetArgs(input)
stopCommand, _, _ := cli.Find([]string{"stop"})
previousPreRun := stopCommand.PersistentPreRunE
stopCommand.PersistentPreRunE = func(cmd *cobra.Command, args []string) error {
if err := previousPreRun(cmd, args); err == nil {
t.Errorf("Expected error, got nil.")
}
return nil
}
stopCommand.RunE = func(cmd *cobra.Command, args []string) error {
return nil
}
cli.Execute()
})
}
}

59
daemon/api.go Normal file
View file

@ -0,0 +1,59 @@
package daemon
import (
"context"
"encoding/json"
"net/http"
service "spud/service"
service_definition "spud/service_definition"
)
func GetApiRoutes() map[string]HandlerFuncWithContext {
return map[string]HandlerFuncWithContext{
"/service/": ServiceList,
"/service/{serviceName}/": ServiceDetails,
}
}
type ServiceListPayload struct {
Definition service_definition.ServiceDefinition `json:"definition"`
}
func handleServiceListPost(w http.ResponseWriter, r *http.Request, c context.Context) {
client := c.Value("client").(service.ServiceClient)
var p ServiceListPayload
json.NewDecoder(r.Body).Decode(&p)
client.Create(p.Definition)
w.WriteHeader(201)
json.NewEncoder(w).Encode(p.Definition)
}
func handleServiceDetailDelete(w http.ResponseWriter, r *http.Request, c context.Context) {
client := c.Value("client").(service.ServiceClient)
serviceName := r.PathValue("serviceName")
client.Stop(serviceName)
w.WriteHeader(204)
}
func handleNotImplemented(w http.ResponseWriter) {
w.WriteHeader(501)
}
func ServiceList(w http.ResponseWriter, r *http.Request, c context.Context) {
switch r.Method {
case http.MethodPost:
handleServiceListPost(w, r, c)
default:
handleNotImplemented(w)
}
}
func ServiceDetails(w http.ResponseWriter, r *http.Request, c context.Context) {
switch r.Method {
case http.MethodDelete:
handleServiceDetailDelete(w, r, c)
default:
handleNotImplemented(w)
}
}

100
daemon/api_test.go Normal file
View file

@ -0,0 +1,100 @@
package daemon
import (
"context"
"net/http"
"net/http/httptest"
service_definition "spud/service_definition"
"testing"
)
type MockClient struct {
calls struct {
Create []service_definition.ServiceDefinition
Stop []string
}
}
func (c *MockClient) Create(s service_definition.ServiceDefinition) error {
c.calls.Create = append(c.calls.Create, s)
return nil
}
func (c *MockClient) Stop(n string) error {
c.calls.Stop = append(c.calls.Stop, n)
return nil
}
func TestServiceListPostCreatesService(t *testing.T) {
mockClient := &MockClient{}
daemonContext := context.WithValue(context.Background(), "client", mockClient)
req := httptest.NewRequest(http.MethodPost, "/service/", nil)
resp := httptest.NewRecorder()
ServiceList(resp, req, daemonContext)
response := resp.Result()
if response.StatusCode != 201 {
t.Errorf("Expected status 201, got %d", response.StatusCode)
}
if len(mockClient.calls.Create) != 1 {
t.Error("Expected a call to Create")
}
}
func TestServiceListUnsupportedMethods(t *testing.T) {
for _, method := range []string{http.MethodGet, http.MethodPut, http.MethodHead, http.MethodDelete} {
t.Run(method, func(t *testing.T) {
req := httptest.NewRequest(method, "/service/", nil)
resp := httptest.NewRecorder()
ServiceList(resp, req, context.Background())
response := resp.Result()
if response.StatusCode != 501 {
t.Errorf("Expected status 501, got %d.", response.StatusCode)
}
})
}
}
func TestServiceDetailsDeleteStopsService(t *testing.T) {
mockClient := &MockClient{}
daemonContext := context.WithValue(context.Background(), "client", mockClient)
req := httptest.NewRequest(http.MethodDelete, "/service/service-name/", nil)
resp := httptest.NewRecorder()
ServiceDetails(resp, req, daemonContext)
response := resp.Result()
if response.StatusCode != 204 {
t.Errorf("Expected status 204, got %d", response.StatusCode)
}
if len(mockClient.calls.Stop) != 1 {
t.Error("Expected a call to Stop")
}
}
func TestServiceDetailUnsupportedMethods(t *testing.T) {
for _, method := range []string{http.MethodGet, http.MethodPost, http.MethodPut, http.MethodHead} {
t.Run(method, func(t *testing.T) {
req := httptest.NewRequest(method, "/service/service-name/", nil)
resp := httptest.NewRecorder()
ServiceDetails(resp, req, context.Background())
response := resp.Result()
if response.StatusCode != 501 {
t.Errorf("Expected status 501, got %d.", response.StatusCode)
}
})
}
}

71
daemon/daemon.go Normal file
View file

@ -0,0 +1,71 @@
package daemon
import (
"context"
"fmt"
"log/slog"
"net/http"
service "spud/service"
"strconv"
)
type Daemon struct {
Host string
Port int
Services service.ServiceClient
Routes map[string]http.HandlerFunc
}
type HandlerFuncWithContext = func(w http.ResponseWriter, r *http.Request, c context.Context)
type RecordingResponseWriter struct {
responseWriter http.ResponseWriter
StatusCode int
}
func (r *RecordingResponseWriter) WriteHeader(statusCode int) {
r.StatusCode = statusCode
r.responseWriter.WriteHeader(statusCode)
}
func (r RecordingResponseWriter) Write(b []byte) (int, error) {
return r.responseWriter.Write(b)
}
func (r RecordingResponseWriter) Header() http.Header {
return r.responseWriter.Header()
}
func handleFuncWithContext(h HandlerFuncWithContext, c context.Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
respWriter := &RecordingResponseWriter{responseWriter: w}
h(respWriter, r, c)
slog.Info("Request", "Method", r.Method, "Path", r.URL.Path, "Status", strconv.Itoa(respWriter.StatusCode))
}
}
func NewDaemon(host string, port int, serviceClient service.ServiceClient) *Daemon {
d := &Daemon{Host: host, Port: port}
if serviceClient == nil {
d.Services = service.NewPodmanServiceManager()
} else {
d.Services = serviceClient
}
return d
}
func (d Daemon) GetListenAddress() string {
return fmt.Sprintf("%s:%d", d.Host, d.Port)
}
func (d Daemon) Start() {
daemonContext := context.WithValue(context.Background(), "client", d.Services)
for route, handler := range GetApiRoutes() {
http.HandleFunc(route, handleFuncWithContext(handler, daemonContext))
}
http.ListenAndServe(d.GetListenAddress(), nil)
}

31
daemon/daemon_test.go Normal file
View file

@ -0,0 +1,31 @@
package daemon
import (
"reflect"
service "spud/service"
service_definition "spud/service_definition"
"testing"
)
type DummyClient struct{}
func (c DummyClient) Create(d service_definition.ServiceDefinition) error { return nil }
func (c DummyClient) Stop(d string) error { return nil }
func TestDaemonDefaultsToPodmanClient(t *testing.T) {
d := NewDaemon("host", 0, nil)
clientType := reflect.TypeOf(d.Services).String()
if clientType != reflect.TypeOf(service.NewPodmanServiceManager()).String() {
t.Errorf("Expected podman client, got %s instead.", clientType)
}
}
func TestDaemonUsesInjectedClientIfProvided(t *testing.T) {
d := NewDaemon("host", 0, DummyClient{})
clientType := reflect.TypeOf(d.Services).String()
if clientType != reflect.TypeOf(DummyClient{}).String() {
t.Errorf("Expected dummy client, got %s instead.", clientType)
}
}

26
git/main.go Normal file
View file

@ -0,0 +1,26 @@
// Git wrapper
//
// Facilitates the usage of `git` commands when dealing with
// data living in repositories.
package git
import (
"os/exec"
)
type GitClient interface {
Clone(path string, destination string) (string, error)
}
type Git struct{}
func (g Git) Clone(path string, destination string) (string, error) {
cloneCmd := exec.Command("git", "clone", path, destination)
if err := cloneCmd.Run(); err != nil {
return "", err
}
return path, nil
}

2
go.mod
View file

@ -1,6 +1,6 @@
module spud
go 1.22.2
go 1.23.1
require (
github.com/goccy/go-yaml v1.11.3

View file

@ -1,9 +1,12 @@
package main
import (
"os"
cli "spud/cli"
)
func main() {
cli.GetCli().Execute()
if err := cli.GetCli().Execute(); err != nil {
os.Exit(1)
}
}

View file

@ -96,6 +96,14 @@ func CreateContainer(definition service_definition.ContainerDefinition, knownVol
"--replace",
}
if definition.Network != "" {
args = append(args, []string{"--network", definition.Network}...)
}
if definition.PIDNamespace != "" {
args = append(args, []string{"--pid", definition.PIDNamespace}...)
}
if definition.EnvFile != "" {
args = append(args, []string{"--env-file", definition.EnvFile}...)
}

10
service/client.go Normal file
View file

@ -0,0 +1,10 @@
package service
import (
service_definition "spud/service_definition"
)
type ServiceClient interface {
Create(service_definition.ServiceDefinition) error
Stop(string) error
}

View file

@ -1,19 +1,28 @@
package service
import (
"log"
podman "spud/podman"
service_definition "spud/service_definition"
)
func CreateService(definition service_definition.ServiceDefinition) {
type ServiceManager interface {
Create(definition service_definition.ServiceDefinition)
Stop(name string)
}
type PodmanServiceManager struct{}
func NewPodmanServiceManager() *PodmanServiceManager {
return &PodmanServiceManager{}
}
func (c PodmanServiceManager) Create(definition service_definition.ServiceDefinition) error {
var err error
err = podman.CreatePod(definition.Name, definition.Ports)
if err != nil {
log.Fatalf("%s", err)
return err
}
knownVolumes := map[string]string{}
@ -26,9 +35,11 @@ func CreateService(definition service_definition.ServiceDefinition) {
for _, container := range definition.Containers {
if err = podman.CreateContainer(container, knownVolumes, definition.Name); err != nil {
log.Fatalf("%s", err)
return err
}
}
return nil
}
/*
@ -36,10 +47,6 @@ func CreateService(definition service_definition.ServiceDefinition) {
*
* The service and all its containers are stopped but not deleted.
*/
func StopService(name string) {
err := podman.StopPod(name)
if err != nil {
log.Fatalf("%s", err)
}
func (c PodmanServiceManager) Stop(name string) error {
return podman.StopPod(name)
}

View file

@ -1,24 +0,0 @@
package service_definition
type FileDoesNotExistError struct {
Message string
ExpectedPath string
}
func (r *FileDoesNotExistError) Error() string {
prefix := "File not found"
if r.Message != "" {
prefix = r.Message
}
return prefix + ": " + r.ExpectedPath
}
type InvalidServiceDefinitionError struct {
Path string
}
func (r *InvalidServiceDefinitionError) Error() string {
return "Service definition does not satisfy expected schema: " + r.Path
}

View file

@ -0,0 +1,26 @@
// Definition fetcher
//
// Handles fetching and building ServiceDefinition structs from different
// data sources.
package service_definition
import (
"fmt"
)
type DefinitionFetcher interface {
GetDefinition(path string) (ServiceDefinition, error)
}
func NewDefinitionFetcher(fetcher_type string) (DefinitionFetcher, error) {
if fetcher_type == "git" {
return NewGitDefinitionFetcher(), nil
}
if fetcher_type == "file" {
return NewFileDefinitionFetcher(), nil
}
return nil, fmt.Errorf("Unrecognized fetcher type: %s", fetcher_type)
}

View file

@ -0,0 +1,19 @@
package service_definition
import (
"testing"
)
func TestGetPathTypeDetectsGitPathPrefix(t *testing.T) {
actual := GetPathType("git+https://test.com")
if actual != "git" {
t.Errorf("Expected 'git' type, got %s", actual)
}
}
func TestGetPathTypeDetectsFileNoPrefix(t *testing.T) {
actual := GetPathType("file/path.yml")
if actual != "file" {
t.Errorf("Expected 'file' type, got %s", actual)
}
}

View file

@ -0,0 +1,34 @@
// File Definition fetcher
//
// Handles extracting service definitions from local files.
package service_definition
import (
"fmt"
"github.com/goccy/go-yaml"
"os"
)
type FileDefinitionFetcher struct{}
func NewFileDefinitionFetcher() *FileDefinitionFetcher {
return &FileDefinitionFetcher{}
}
// Retrieves a service definition from the given filepath.
func (f FileDefinitionFetcher) GetDefinition(path string) (ServiceDefinition, error) {
var definition ServiceDefinition
defData, err := os.ReadFile(path)
if err != nil {
return ServiceDefinition{}, fmt.Errorf("Could not find service configuration file: %s", path)
}
if err = yaml.Unmarshal(defData, &definition); err != nil {
return ServiceDefinition{}, fmt.Errorf("Service definition does not satisfy expected schema: %s", path)
}
return definition, nil
}

View file

@ -0,0 +1,38 @@
// Git Definition fetcher
//
// Handles fetching and building ServiceDefinition structs from git
// repositories.
package service_definition
import (
"os"
git "spud/git"
"strings"
)
type GitDefinitionFetcher struct {
Git git.GitClient
}
func NewGitDefinitionFetcher() *GitDefinitionFetcher {
return &GitDefinitionFetcher{
Git: git.Git{},
}
}
// Clones the target git repository and uses it as a basis to extract
// a service definition.
func (f GitDefinitionFetcher) GetDefinition(path string) (ServiceDefinition, error) {
dir, err := os.MkdirTemp("/tmp", "spud-service-")
if err != nil {
return ServiceDefinition{}, err
}
if _, err := f.Git.Clone(strings.TrimPrefix(path, "git+"), dir); err != nil {
return ServiceDefinition{}, err
}
return NewFileDefinitionFetcher().GetDefinition((dir + "/service.yml"))
}

View file

@ -0,0 +1,34 @@
package service_definition
import (
"strings"
"testing"
)
type MockGit struct {
calls []string
}
func (g *MockGit) Clone(path string, destination string) (string, error) {
g.calls = append(g.calls, path+":"+destination)
return path, nil
}
func TestGetDefinitionGetDefinitionFromGit(t *testing.T) {
var gitFetcher GitDefinitionFetcher
fetcher, _ := NewDefinitionFetcher("git")
gitFetcher, _ = fetcher.(GitDefinitionFetcher)
mockGit := MockGit{}
gitFetcher.Git = &mockGit
mockUrl := "https://git.com/owner/repo.git"
gitFetcher.GetDefinition("git+" + mockUrl)
if !strings.HasPrefix(mockGit.calls[0], mockUrl) {
t.Errorf("Expected git cloning for %s, got %s instead.", mockUrl, mockGit.calls[0])
}
}

View file

@ -1,11 +1,5 @@
package service_definition
import (
"os"
"github.com/goccy/go-yaml"
)
type BuildImage struct {
Path string `yaml:"path"`
TagPrefix string `yaml:"tag"`
@ -37,6 +31,8 @@ type ContainerDefinition struct {
Image string `yaml:"image"`
Volumes []VolumeConfiguration `yaml:"volumes"`
EnvFile string `yaml:"env-file"`
Network string `yaml:"network"`
PIDNamespace string `yaml:"pid-namespace"`
ExtraArgs []string `yaml:"extra-args"`
}
@ -47,19 +43,3 @@ type ServiceDefinition struct {
Containers []ContainerDefinition `yaml:"containers"`
Ports []PortMapping `yaml:"ports"`
}
func GetServiceDefinitionFromFile(path string) (ServiceDefinition, error) {
var definition ServiceDefinition
defData, err := os.ReadFile(path)
if err != nil {
return ServiceDefinition{}, &FileDoesNotExistError{Message: "Could not find service configuration file", ExpectedPath: path}
}
if err = yaml.Unmarshal(defData, &definition); err != nil {
return ServiceDefinition{}, &InvalidServiceDefinitionError{Path: path}
}
return definition, nil
}

View file

@ -0,0 +1,13 @@
package service_definition
import (
"strings"
)
func GetPathType(path string) string {
if strings.HasPrefix(path, "git+") {
return "git"
}
return "file"
}

View file

@ -5,7 +5,8 @@ import (
)
func TestGetServiceDefinitionFromFileDoesNotExist(t *testing.T) {
_, err := GetServiceDefinitionFromFile(t.TempDir() + "/not-a-file.yml")
fetcher, _ := NewDefinitionFetcher("file")
_, err := fetcher.GetDefinition(t.TempDir() + "/not-a-file.yml")
if err == nil {
t.Errorf("Expected error, got nil.")

91
webclient/client_test.go Normal file
View file

@ -0,0 +1,91 @@
package webclient
import (
"bytes"
"encoding/json"
"io"
"net/http"
daemon "spud/daemon"
service_definition "spud/service_definition"
"testing"
)
type RecordedRequest struct {
method string
url string
contentType string
data io.Reader
}
type DummyHttpClient struct {
requests []RecordedRequest
}
func (d *DummyHttpClient) Do(request *http.Request) (*http.Response, error) {
d.requests = append(d.requests, RecordedRequest{method: request.Method, url: request.URL.String(), contentType: "", data: request.Body})
return nil, nil
}
func Test_WebClient_GetBaseUrlGetsUrlFromHostPort(t *testing.T) {
c := NewWebClient("http://host", 9999)
actual := c.getBaseUrl()
expected := "http://host:9999"
if actual != expected {
t.Errorf("Expected %s, got %s.", expected, actual)
}
}
func Test_WebClient_CreateServicePostsToDaemon(t *testing.T) {
c := NewWebClient("http://host", 9999)
httpClient := &DummyHttpClient{}
c.httpClient = httpClient
def := service_definition.ServiceDefinition{Name: "test-service"}
c.CreateService(def)
if len(httpClient.requests) != 1 || httpClient.requests[0].method != http.MethodPost {
t.Errorf("Expected one POST requests, got none.")
}
}
func Test_WebClient_CreateServiceSendsDefinition(t *testing.T) {
c := NewWebClient("http://host", 9999)
httpClient := &DummyHttpClient{}
c.httpClient = httpClient
payload := daemon.ServiceListPayload{
Definition: service_definition.ServiceDefinition{Name: "test-service"},
}
c.CreateService(payload.Definition)
req := httpClient.requests[0]
actualDef := bytes.NewBuffer([]byte{})
actualDef.ReadFrom(req.data)
expectedDef, _ := json.Marshal(payload)
if actualDef.String() != string(expectedDef) {
t.Errorf("Unexpected data: %s != %s", actualDef.String(), string(expectedDef))
}
}
func Test_WebClient_StopServiceDeletesToDaemon(t *testing.T) {
c := NewWebClient("http://host", 9999)
httpClient := &DummyHttpClient{}
c.httpClient = httpClient
serviceName := "test-service"
c.StopService(serviceName)
if len(httpClient.requests) != 1 || httpClient.requests[0].method != http.MethodDelete {
t.Errorf("Expected one DELETE request, got none.")
}
expected := c.getBaseUrl() + "/service/" + serviceName + "/"
if httpClient.requests[0].url != expected {
t.Errorf("Expected url to be %s, got %s.", expected, httpClient.requests[0].url)
}
}

52
webclient/main.go Normal file
View file

@ -0,0 +1,52 @@
package webclient
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
daemon "spud/daemon"
service_definition "spud/service_definition"
)
type HttpClient interface {
Do(*http.Request) (*http.Response, error)
}
type WebClient struct {
httpClient HttpClient
Host string
Port int
}
func NewWebClient(host string, port int) *WebClient {
return &WebClient{
httpClient: &http.Client{},
Host: host,
Port: port,
}
}
func (c WebClient) getBaseUrl() string {
return fmt.Sprintf("%s:%d", c.Host, c.Port)
}
func (c WebClient) CreateService(def service_definition.ServiceDefinition) error {
payload := daemon.ServiceListPayload{
Definition: def,
}
serializedPayload, _ := json.Marshal(payload)
req, _ := http.NewRequest(http.MethodPost, c.getBaseUrl()+"/service/", bytes.NewBuffer(serializedPayload))
_, e := c.httpClient.Do(req)
return e
}
func (c WebClient) StopService(name string) error {
req, _ := http.NewRequest(http.MethodDelete, c.getBaseUrl()+"/service/"+name+"/", nil)
_, e := c.httpClient.Do(req)
return e
}