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