spud/service/service.go

53 lines
1.1 KiB
Go
Raw Normal View History

2024-06-02 06:03:47 +00:00
package service
import (
podman "spud/podman"
service_definition "spud/service_definition"
)
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 {
2024-06-02 06:03:47 +00:00
var err error
err = podman.CreatePod(definition.Name, definition.Ports)
2024-06-02 06:03:47 +00:00
if err != nil {
return err
2024-06-02 06:03:47 +00:00
}
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 {
return err
2024-06-02 06:03:47 +00:00
}
}
return nil
2024-06-02 06:03:47 +00:00
}
/*
* Stops a running service.
*
* The service and all its containers are stopped but not deleted.
*/
func (c PodmanServiceManager) Stop(name string) error {
return podman.StopPod(name)
}