mirror of
https://github.com/steveiliop56/tinyauth.git
synced 2025-10-28 04:35:40 +00:00
refactor: move docker connection check to start up
This commit is contained in:
@@ -12,8 +12,9 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type DockerService struct {
|
type DockerService struct {
|
||||||
client *client.Client
|
client *client.Client
|
||||||
context context.Context
|
context context.Context
|
||||||
|
isConnected bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDockerService() *DockerService {
|
func NewDockerService() *DockerService {
|
||||||
@@ -31,10 +32,24 @@ func (docker *DockerService) Init() error {
|
|||||||
|
|
||||||
docker.client = client
|
docker.client = client
|
||||||
docker.context = ctx
|
docker.context = ctx
|
||||||
|
|
||||||
|
_, err = docker.client.Ping(docker.context)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Debug().Err(err).Msg("Docker not connected")
|
||||||
|
docker.isConnected = false
|
||||||
|
docker.client = nil
|
||||||
|
docker.context = nil
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
docker.isConnected = true
|
||||||
|
log.Debug().Msg("Docker connected")
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (docker *DockerService) GetContainers() ([]container.Summary, error) {
|
func (docker *DockerService) getContainers() ([]container.Summary, error) {
|
||||||
containers, err := docker.client.ContainerList(docker.context, container.ListOptions{})
|
containers, err := docker.client.ContainerList(docker.context, container.ListOptions{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -42,7 +57,7 @@ func (docker *DockerService) GetContainers() ([]container.Summary, error) {
|
|||||||
return containers, nil
|
return containers, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (docker *DockerService) InspectContainer(containerId string) (container.InspectResponse, error) {
|
func (docker *DockerService) inspectContainer(containerId string) (container.InspectResponse, error) {
|
||||||
inspect, err := docker.client.ContainerInspect(docker.context, containerId)
|
inspect, err := docker.client.ContainerInspect(docker.context, containerId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return container.InspectResponse{}, err
|
return container.InspectResponse{}, err
|
||||||
@@ -50,26 +65,19 @@ func (docker *DockerService) InspectContainer(containerId string) (container.Ins
|
|||||||
return inspect, nil
|
return inspect, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (docker *DockerService) DockerConnected() bool {
|
|
||||||
_, err := docker.client.Ping(docker.context)
|
|
||||||
return err == nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (docker *DockerService) GetLabels(appDomain string) (config.App, error) {
|
func (docker *DockerService) GetLabels(appDomain string) (config.App, error) {
|
||||||
isConnected := docker.DockerConnected()
|
if !docker.isConnected {
|
||||||
|
|
||||||
if !isConnected {
|
|
||||||
log.Debug().Msg("Docker not connected, returning empty labels")
|
log.Debug().Msg("Docker not connected, returning empty labels")
|
||||||
return config.App{}, nil
|
return config.App{}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
containers, err := docker.GetContainers()
|
containers, err := docker.getContainers()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return config.App{}, err
|
return config.App{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, ctr := range containers {
|
for _, ctr := range containers {
|
||||||
inspect, err := docker.InspectContainer(ctr.ID)
|
inspect, err := docker.inspectContainer(ctr.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return config.App{}, err
|
return config.App{}, err
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user