forked from toolshed/abra
		
	feat: rudimentary deploy status checking
See coop-cloud/organising#209.
This commit is contained in:
		@ -3,10 +3,13 @@ package stack // https://github.com/docker/cli/blob/master/cli/command/stack/swa
 | 
			
		||||
import (
 | 
			
		||||
	"context"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"io"
 | 
			
		||||
	"io/ioutil"
 | 
			
		||||
	"strings"
 | 
			
		||||
 | 
			
		||||
	abraClient "coopcloud.tech/abra/pkg/client"
 | 
			
		||||
	"coopcloud.tech/abra/pkg/upstream/convert"
 | 
			
		||||
	"github.com/docker/cli/cli/command/service/progress"
 | 
			
		||||
	composetypes "github.com/docker/cli/cli/compose/types"
 | 
			
		||||
	"github.com/docker/docker/api/types"
 | 
			
		||||
	"github.com/docker/docker/api/types/container"
 | 
			
		||||
@ -346,6 +349,7 @@ func deployServices(
 | 
			
		||||
		existingServiceMap[service.Spec.Name] = service
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	var serviceIDs []string
 | 
			
		||||
	for internalName, serviceSpec := range services {
 | 
			
		||||
		var (
 | 
			
		||||
			name        = namespace.Scope(internalName)
 | 
			
		||||
@ -405,6 +409,8 @@ func deployServices(
 | 
			
		||||
				return errors.Wrapf(err, "failed to update service %s", name)
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			serviceIDs = append(serviceIDs, service.ID)
 | 
			
		||||
 | 
			
		||||
			for _, warning := range response.Warnings {
 | 
			
		||||
				logrus.Warn(warning)
 | 
			
		||||
			}
 | 
			
		||||
@ -418,11 +424,33 @@ func deployServices(
 | 
			
		||||
				createOpts.QueryRegistry = true
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			if _, err := cl.ServiceCreate(ctx, serviceSpec, createOpts); err != nil {
 | 
			
		||||
			serviceCreateResponse, err := cl.ServiceCreate(ctx, serviceSpec, createOpts)
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				return errors.Wrapf(err, "failed to create service %s", name)
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			serviceIDs = append(serviceIDs, serviceCreateResponse.ID)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	logrus.Infof("waiting for services to converge: %s", strings.Join(serviceIDs, ", "))
 | 
			
		||||
 | 
			
		||||
	ch := make(chan error, len(serviceIDs))
 | 
			
		||||
	for _, serviceID := range serviceIDs {
 | 
			
		||||
		logrus.Debugf("waiting on %s to converge", serviceID)
 | 
			
		||||
		go func(s string) {
 | 
			
		||||
			ch <- waitOnService(ctx, cl, s)
 | 
			
		||||
		}(serviceID)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	for _, serviceID := range serviceIDs {
 | 
			
		||||
		err := <-ch
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
		logrus.Debugf("assuming %s converged successfully", serviceID)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -437,3 +465,17 @@ func getStackSecrets(ctx context.Context, dockerclient client.APIClient, namespa
 | 
			
		||||
func getStackConfigs(ctx context.Context, dockerclient client.APIClient, namespace string) ([]swarm.Config, error) {
 | 
			
		||||
	return dockerclient.ConfigList(ctx, types.ConfigListOptions{Filters: getStackFilter(namespace)})
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// https://github.com/docker/cli/blob/master/cli/command/service/helpers.go
 | 
			
		||||
// https://github.com/docker/cli/blob/master/cli/command/service/progress/progress.go
 | 
			
		||||
func waitOnService(ctx context.Context, cl *dockerclient.Client, serviceID string) error {
 | 
			
		||||
	errChan := make(chan error, 1)
 | 
			
		||||
	pipeReader, pipeWriter := io.Pipe()
 | 
			
		||||
 | 
			
		||||
	go func() {
 | 
			
		||||
		errChan <- progress.ServiceProgress(ctx, cl, serviceID, pipeWriter)
 | 
			
		||||
	}()
 | 
			
		||||
 | 
			
		||||
	go io.Copy(ioutil.Discard, pipeReader)
 | 
			
		||||
	return <-errChan
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user