refactor: split into modules
This commit is contained in:
parent
a811dd8927
commit
e51648801f
6 changed files with 91 additions and 36 deletions
13
main.go
13
main.go
|
@ -2,6 +2,10 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
"log"
|
||||||
|
|
||||||
|
service "spud/service"
|
||||||
|
service_definition "spud/service_definition"
|
||||||
)
|
)
|
||||||
|
|
||||||
var cli = &cobra.Command{
|
var cli = &cobra.Command{
|
||||||
|
@ -14,8 +18,13 @@ var start = &cobra.Command{
|
||||||
Short: "Creates or updates a service based on the provided definition.",
|
Short: "Creates or updates a service based on the provided definition.",
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
pathProvided := args[0]
|
pathProvided := args[0]
|
||||||
def := GetServiceDefinitionFromFile(pathProvided)
|
def, err := service_definition.GetServiceDefinitionFromFile(pathProvided)
|
||||||
CreateService(def)
|
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
service.CreateService(def)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,10 @@
|
||||||
package main
|
package podman
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
|
||||||
|
"spud/service_definition"
|
||||||
)
|
)
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -53,7 +55,7 @@ func CreatePod(name string) error {
|
||||||
/*
|
/*
|
||||||
* Creates individual containers.
|
* Creates individual containers.
|
||||||
*/
|
*/
|
||||||
func CreateContainer(definition ContainerDefinition, knownVolumes map[string]string, service string) error {
|
func CreateContainer(definition service_definition.ContainerDefinition, knownVolumes map[string]string, service string) error {
|
||||||
namespacedContainerName := service + "_" + definition.Name
|
namespacedContainerName := service + "_" + definition.Name
|
||||||
|
|
||||||
args := []string{
|
args := []string{
|
32
service/service.go
Normal file
32
service/service.go
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
package service
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
|
||||||
|
podman "spud/podman"
|
||||||
|
service_definition "spud/service_definition"
|
||||||
|
)
|
||||||
|
|
||||||
|
func CreateService(definition service_definition.ServiceDefinition) {
|
||||||
|
var err error
|
||||||
|
|
||||||
|
err = podman.CreatePod(definition.Name)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("%s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
knownVolumes := map[string]string{}
|
||||||
|
|
||||||
|
for _, volume := range definition.Volumes {
|
||||||
|
namespacedName := definition.Name + "_" + volume.Name
|
||||||
|
podman.CreateVolume(namespacedName, true)
|
||||||
|
knownVolumes[volume.Name] = namespacedName
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, container := range definition.Containers {
|
||||||
|
if err = podman.CreateContainer(container, knownVolumes, definition.Name); err != nil {
|
||||||
|
log.Fatalf("%s", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
24
service_definition/errors.go
Normal file
24
service_definition/errors.go
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
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
|
||||||
|
}
|
|
@ -1,9 +1,9 @@
|
||||||
package main
|
package service_definition
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/goccy/go-yaml"
|
|
||||||
"log"
|
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
"github.com/goccy/go-yaml"
|
||||||
)
|
)
|
||||||
|
|
||||||
type VolumeDefinition struct {
|
type VolumeDefinition struct {
|
||||||
|
@ -29,43 +29,18 @@ type ServiceDefinition struct {
|
||||||
Containers []ContainerDefinition `yaml:"containers"`
|
Containers []ContainerDefinition `yaml:"containers"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetServiceDefinitionFromFile(path string) ServiceDefinition {
|
func GetServiceDefinitionFromFile(path string) (ServiceDefinition, error) {
|
||||||
var definition ServiceDefinition
|
var definition ServiceDefinition
|
||||||
|
|
||||||
defData, err := os.ReadFile(path)
|
defData, err := os.ReadFile(path)
|
||||||
|
|
||||||
// TODO: Bubble up error?
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Could not parse service configuration at %s: %s", path, err)
|
return ServiceDefinition{}, &FileDoesNotExistError{Message: "Could not find service configuration file", ExpectedPath: path}
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = yaml.Unmarshal(defData, &definition); err != nil {
|
if err = yaml.Unmarshal(defData, &definition); err != nil {
|
||||||
log.Fatalf("Could not unpack service configuration: %s", err)
|
return ServiceDefinition{}, &InvalidServiceDefinitionError{Path: path}
|
||||||
}
|
}
|
||||||
|
|
||||||
return definition
|
return definition, nil
|
||||||
}
|
|
||||||
|
|
||||||
func CreateService(definition ServiceDefinition) {
|
|
||||||
var err error
|
|
||||||
|
|
||||||
err = CreatePod(definition.Name)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalf("%s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
knownVolumes := map[string]string{}
|
|
||||||
|
|
||||||
for _, volume := range definition.Volumes {
|
|
||||||
namespacedName := definition.Name + "_" + volume.Name
|
|
||||||
CreateVolume(namespacedName, true)
|
|
||||||
knownVolumes[volume.Name] = namespacedName
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, container := range definition.Containers {
|
|
||||||
if err = CreateContainer(container, knownVolumes, definition.Name); err != nil {
|
|
||||||
log.Fatalf("%s", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
13
service_definition/service_definition_test.go
Normal file
13
service_definition/service_definition_test.go
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
package service_definition
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestGetServiceDefinitionFromFileDoesNotExist(t *testing.T) {
|
||||||
|
_, err := GetServiceDefinitionFromFile(t.TempDir() + "/not-a-file.yml")
|
||||||
|
|
||||||
|
if err == nil {
|
||||||
|
t.Errorf("Expected error, got nil.")
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue