Skip to content

Commit

Permalink
fix: use image registry locally also (#417)
Browse files Browse the repository at this point in the history
  • Loading branch information
tanmoysrt authored Mar 23, 2024
1 parent 020f6d7 commit 9f81b71
Show file tree
Hide file tree
Showing 21 changed files with 632 additions and 218 deletions.
17 changes: 17 additions & 0 deletions container_manager/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,20 @@ func (m Manager) RunCommandInServiceContainers(serviceName string, command []str
}
return nil
}

// IsContainerRunning checks if a container is running
func (m Manager) IsContainerRunning(containerName string) (bool, error) {
containers, err := m.client.ContainerList(m.ctx, container.ListOptions{
All: true,
Filters: filters.NewArgs(
filters.Arg("name", containerName),
),
})
if err != nil {
return false, errors.New("Failed to list containers " + err.Error())
}
if len(containers) == 0 {
return false, nil
}
return containers[0].State == "running", nil
}
184 changes: 117 additions & 67 deletions container_manager/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@ package containermanger

import (
"errors"
"fmt"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/api/types/mount"
"github.com/docker/docker/api/types/swarm"
"io"
"strings"
"time"
)

const maxRetriesForVersionConflict = 5

func (m Manager) GetService(serviceName string) (Service, error) {
serviceData, _, err := m.client.ServiceInspectWithRaw(m.ctx, serviceName, types.ServiceInspectOptions{})
if err != nil {
Expand Down Expand Up @@ -102,62 +106,96 @@ func (m Manager) UpdateService(service Service, username string, password string
if err != nil {
return errors.New("failed to generate auth header")
}
serviceData, _, err := m.client.ServiceInspectWithRaw(m.ctx, service.Name, types.ServiceInspectOptions{})
if err != nil {
return errors.New("error getting swarm server version")
}
version := swarm.Version{
Index: serviceData.Version.Index,
}
if err != nil {
return errors.New("error getting swarm server version")
}
_, err = m.client.ServiceUpdate(m.ctx, service.Name, version, m.serviceToServiceSpec(service), types.ServiceUpdateOptions{
EncodedRegistryAuth: authHeader,
QueryRegistry: queryRegistry,
})
if err != nil {
return errors.New("error updating service")

maxRetries := maxRetriesForVersionConflict
for {
serviceData, _, err := m.client.ServiceInspectWithRaw(m.ctx, service.Name, types.ServiceInspectOptions{})
if err != nil {
return errors.New("error getting swarm server version")
}
version := swarm.Version{
Index: serviceData.Version.Index,
}
_, err = m.client.ServiceUpdate(m.ctx, service.Name, version, m.serviceToServiceSpec(service), types.ServiceUpdateOptions{
EncodedRegistryAuth: authHeader,
QueryRegistry: queryRegistry,
})
if err != nil {
if strings.Contains(err.Error(), "update out of sequence") {
if maxRetries == 0 {
return fmt.Errorf("error updating service due to version out of sync [retried %d times]", maxRetriesForVersionConflict)
}
<-time.After(3 * time.Second)
maxRetries--
continue
}
return errors.New("error updating service")
} else {
return nil
}
}
return nil
}

func (m Manager) RestartService(serviceName string) error {
serviceData, _, err := m.client.ServiceInspectWithRaw(m.ctx, serviceName, types.ServiceInspectOptions{})
if err != nil {
return errors.New("error getting swarm server version")
}
version := swarm.Version{
Index: serviceData.Version.Index,
}
if err != nil {
return errors.New("error getting swarm server version")
}
spec := serviceData.Spec
spec.TaskTemplate.ForceUpdate++
_, err = m.client.ServiceUpdate(m.ctx, serviceName, version, spec, types.ServiceUpdateOptions{})
if err != nil {
return errors.New("error updating service")
maxRetries := 5
for {
serviceData, _, err := m.client.ServiceInspectWithRaw(m.ctx, serviceName, types.ServiceInspectOptions{})
if err != nil {
return errors.New("error getting swarm server version")
}
version := swarm.Version{
Index: serviceData.Version.Index,
}
if err != nil {
return errors.New("error getting swarm server version")
}
spec := serviceData.Spec
spec.TaskTemplate.ForceUpdate++
_, err = m.client.ServiceUpdate(m.ctx, serviceName, version, spec, types.ServiceUpdateOptions{})
if err != nil {
if strings.Contains(err.Error(), "update out of sequence") {
if maxRetries == 0 {
return fmt.Errorf("error updating service due to version out of sync [retried %d times]", maxRetriesForVersionConflict)
}
<-time.After(3 * time.Second)
maxRetries--
continue
}
return errors.New("error updating service")
} else {
return nil
}
}
return nil
}

func (m Manager) RollbackService(serviceName string) error {
serviceData, _, err := m.client.ServiceInspectWithRaw(m.ctx, serviceName, types.ServiceInspectOptions{})
if err != nil {
return errors.New("error getting swarm server version")
}
version := swarm.Version{
Index: serviceData.Version.Index,
}
if err != nil {
return errors.New("error getting swarm server version")
}
_, err = m.client.ServiceUpdate(m.ctx, serviceName, version, *serviceData.PreviousSpec, types.ServiceUpdateOptions{})
if err != nil {
return errors.New("error updating service")
maxRetries := 5
for {
serviceData, _, err := m.client.ServiceInspectWithRaw(m.ctx, serviceName, types.ServiceInspectOptions{})
if err != nil {
return errors.New("error getting swarm server version")
}
version := swarm.Version{
Index: serviceData.Version.Index,
}
if err != nil {
return errors.New("error getting swarm server version")
}
_, err = m.client.ServiceUpdate(m.ctx, serviceName, version, *serviceData.PreviousSpec, types.ServiceUpdateOptions{})
if err != nil {
if strings.Contains(err.Error(), "update out of sequence") {
if maxRetries == 0 {
return fmt.Errorf("error updating service due to version out of sync [retried %d times]", maxRetriesForVersionConflict)
}
<-time.After(3 * time.Second)
maxRetries--
continue
}
return errors.New("error updating service")
} else {
return nil
}
}
return nil
}

func (m Manager) RemoveService(serviceName string) error {
Expand All @@ -169,27 +207,39 @@ func (m Manager) RemoveService(serviceName string) error {
}

func (m Manager) SetServiceReplicaCount(serviceName string, replicas int) error {
serviceData, _, err := m.client.ServiceInspectWithRaw(m.ctx, serviceName, types.ServiceInspectOptions{})
if err != nil {
return errors.New("error getting swarm server version")
}
version := swarm.Version{
Index: serviceData.Version.Index,
}
if err != nil {
return errors.New("error getting swarm server version")
}
spec := serviceData.Spec
if spec.Mode.Replicated == nil {
return errors.New("service is not a replicated service")
}
replicaCount := uint64(replicas)
spec.Mode.Replicated.Replicas = &replicaCount
_, err = m.client.ServiceUpdate(m.ctx, serviceName, version, spec, types.ServiceUpdateOptions{})
if err != nil {
return errors.New("error updating service")
maxRetries := 5
for {
serviceData, _, err := m.client.ServiceInspectWithRaw(m.ctx, serviceName, types.ServiceInspectOptions{})
if err != nil {
return errors.New("error getting swarm server version")
}
version := swarm.Version{
Index: serviceData.Version.Index,
}
if err != nil {
return errors.New("error getting swarm server version")
}
spec := serviceData.Spec
if spec.Mode.Replicated == nil {
return errors.New("service is not a replicated service")
}
replicaCount := uint64(replicas)
spec.Mode.Replicated.Replicas = &replicaCount
_, err = m.client.ServiceUpdate(m.ctx, serviceName, version, spec, types.ServiceUpdateOptions{})
if err != nil {
if strings.Contains(err.Error(), "update out of sequence") {
if maxRetries == 0 {
return fmt.Errorf("error updating service due to version out of sync [retried %d times]", maxRetriesForVersionConflict)
}
<-time.After(3 * time.Second)
maxRetries--
continue
}
return errors.New("error updating service")
} else {
return nil
}
}
return nil
}

func (m Manager) RealtimeInfoRunningServices() (map[string]ServiceRealtimeInfo, error) {
Expand Down
Loading

0 comments on commit 9f81b71

Please sign in to comment.