gopkg.in/docker/libcompose.v0@v0.4.0/project/project_scale.go (about)

     1  package project
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"golang.org/x/net/context"
     7  
     8  	log "github.com/Sirupsen/logrus"
     9  )
    10  
    11  // Scale scales the specified services.
    12  func (p *Project) Scale(ctx context.Context, timeout int, servicesScale map[string]int) error {
    13  	// This code is a bit verbose but I wanted to parse everything up front
    14  	order := make([]string, 0, 0)
    15  	services := make(map[string]Service)
    16  
    17  	for name := range servicesScale {
    18  		if !p.ServiceConfigs.Has(name) {
    19  			return fmt.Errorf("%s is not defined in the template", name)
    20  		}
    21  
    22  		service, err := p.CreateService(name)
    23  		if err != nil {
    24  			return fmt.Errorf("Failed to lookup service: %s: %v", service, err)
    25  		}
    26  
    27  		order = append(order, name)
    28  		services[name] = service
    29  	}
    30  
    31  	for _, name := range order {
    32  		scale := servicesScale[name]
    33  		log.Infof("Setting scale %s=%d...", name, scale)
    34  		err := services[name].Scale(ctx, scale, timeout)
    35  		if err != nil {
    36  			return fmt.Errorf("Failed to set the scale %s=%d: %v", name, scale, err)
    37  		}
    38  	}
    39  	return nil
    40  }