52 lines
1.3 KiB
Go
52 lines
1.3 KiB
Go
package services
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"git.cesium.pw/niku/virteen/internal/podman/models"
|
|
"git.cesium.pw/niku/virteen/internal/podman/repositories"
|
|
"github.com/containers/podman/v4/pkg/domain/entities"
|
|
)
|
|
|
|
type PodmanContainerService struct {
|
|
podman *repositories.PodmanRepository
|
|
}
|
|
|
|
func (pcs *PodmanContainerService) List() ([]entities.ListContainer, error) {
|
|
return pcs.podman.ListContainers()
|
|
}
|
|
|
|
func (pcs *PodmanContainerService) Create(blueprint models.ContainerBlueprint) error {
|
|
return pcs.podman.CreateContainer(blueprint)
|
|
}
|
|
|
|
func (pcs *PodmanContainerService) Delete(name string) error {
|
|
return pcs.podman.DeleteContainer(name)
|
|
}
|
|
|
|
func (pcs *PodmanContainerService) UpdateStatus(name, status string) error {
|
|
var state models.ContainerStatus
|
|
if status == "started" {
|
|
state = models.StartContainer
|
|
} else if status == "stopped" {
|
|
state = models.StopContainer
|
|
} else if status == "restarting" {
|
|
state = models.RestartContainer
|
|
} else {
|
|
return fmt.Errorf("invalid state, should be started, stopped or restarting")
|
|
}
|
|
|
|
return pcs.podman.UpdateContainerStatus(name, state)
|
|
}
|
|
|
|
func NewPodmanContainerService() (*PodmanContainerService, error) {
|
|
podmanRepo, err := repositories.NewPodmanRepository()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &PodmanContainerService{
|
|
podman: podmanRepo,
|
|
}, nil
|
|
}
|