Compare commits
7 commits
Author | SHA1 | Date | |
---|---|---|---|
98fda3b404 | |||
7cc11af378 | |||
5a18a5bf44 | |||
8bc33870e6 | |||
c580c0c433 | |||
24e795e1aa | |||
8f0489a622 |
17 changed files with 353 additions and 54 deletions
41
.forgejo/workflows/pull-request.yml
Normal file
41
.forgejo/workflows/pull-request.yml
Normal 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 }}
|
67
.forgejo/workflows/push.yml
Normal file
67
.forgejo/workflows/push.yml
Normal 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"
|
||||||
|
|
16
.pre-commit-config.yaml
Normal file
16
.pre-commit-config.yaml
Normal 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
|
|
@ -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
|
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`.
|
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
8
bootstrap.sh
Executable 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
|
16
cli/build.go
16
cli/build.go
|
@ -13,14 +13,22 @@ func getBuildCommand() *cobra.Command {
|
||||||
Use: "build",
|
Use: "build",
|
||||||
Short: "Build service images.",
|
Short: "Build service images.",
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
pathProvided, err := cmd.PersistentFlags().GetString("definition")
|
var pathProvided string
|
||||||
|
var fetcher service_definition.DefinitionFetcher
|
||||||
|
var def service_definition.ServiceDefinition
|
||||||
|
var err error
|
||||||
|
|
||||||
if err != nil {
|
if pathProvided, err = cmd.PersistentFlags().GetString("definition"); err != nil {
|
||||||
return fmt.Errorf("%+v", err)
|
return fmt.Errorf("%+v", err)
|
||||||
}
|
}
|
||||||
def, err := service_definition.GetServiceDefinitionFromFile(pathProvided)
|
|
||||||
|
|
||||||
if err != nil {
|
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)
|
return fmt.Errorf("Failed to read service definition from file: %+v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -47,12 +47,22 @@ func getStartCommand() *cobra.Command {
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
var fetcher service_definition.DefinitionFetcher
|
||||||
|
var def service_definition.ServiceDefinition
|
||||||
|
var err error
|
||||||
|
|
||||||
ctx := cmd.Context()
|
ctx := cmd.Context()
|
||||||
flags := ctx.Value("flags").(ParsedFlags)
|
flags := ctx.Value("flags").(ParsedFlags)
|
||||||
def, err := service_definition.GetServiceDefinitionFromFile(flags.definitionPath)
|
|
||||||
|
|
||||||
if err != nil {
|
defPath := flags.definitionPath
|
||||||
return fmt.Errorf("Failed to read service definition from file: %+v", err)
|
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 {
|
if flags.daemonHost != "" && flags.daemonPort != 0 {
|
||||||
|
|
26
git/main.go
Normal file
26
git/main.go
Normal 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
|
||||||
|
}
|
|
@ -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
|
|
||||||
}
|
|
26
service_definition/fetcher.go
Normal file
26
service_definition/fetcher.go
Normal 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)
|
||||||
|
}
|
19
service_definition/fetcher_test.go
Normal file
19
service_definition/fetcher_test.go
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
34
service_definition/file_definition_fetcher.go
Normal file
34
service_definition/file_definition_fetcher.go
Normal 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
|
||||||
|
}
|
38
service_definition/git_fetcher.go
Normal file
38
service_definition/git_fetcher.go
Normal 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"))
|
||||||
|
}
|
34
service_definition/git_fetcher_test.go
Normal file
34
service_definition/git_fetcher_test.go
Normal 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])
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,11 +1,5 @@
|
||||||
package service_definition
|
package service_definition
|
||||||
|
|
||||||
import (
|
|
||||||
"os"
|
|
||||||
|
|
||||||
"github.com/goccy/go-yaml"
|
|
||||||
)
|
|
||||||
|
|
||||||
type BuildImage struct {
|
type BuildImage struct {
|
||||||
Path string `yaml:"path"`
|
Path string `yaml:"path"`
|
||||||
TagPrefix string `yaml:"tag"`
|
TagPrefix string `yaml:"tag"`
|
||||||
|
@ -49,19 +43,3 @@ type ServiceDefinition struct {
|
||||||
Containers []ContainerDefinition `yaml:"containers"`
|
Containers []ContainerDefinition `yaml:"containers"`
|
||||||
Ports []PortMapping `yaml:"ports"`
|
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
|
|
||||||
}
|
|
||||||
|
|
13
service_definition/paths.go
Normal file
13
service_definition/paths.go
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
package service_definition
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
func GetPathType(path string) string {
|
||||||
|
if strings.HasPrefix(path, "git+") {
|
||||||
|
return "git"
|
||||||
|
}
|
||||||
|
|
||||||
|
return "file"
|
||||||
|
}
|
|
@ -5,7 +5,8 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestGetServiceDefinitionFromFileDoesNotExist(t *testing.T) {
|
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 {
|
if err == nil {
|
||||||
t.Errorf("Expected error, got nil.")
|
t.Errorf("Expected error, got nil.")
|
||||||
|
|
Loading…
Reference in a new issue