github.com/cloudfoundry-attic/cli-with-i18n@v6.32.1-0.20171002233121-7401370d3b85+incompatible/actor/pushaction/apply.go (about)

     1  package pushaction
     2  
     3  import (
     4  	"os"
     5  
     6  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
     7  
     8  	log "github.com/sirupsen/logrus"
     9  )
    10  
    11  const PushRetries = 3
    12  
    13  type UploadFailedError struct {
    14  	Err error
    15  }
    16  
    17  func (UploadFailedError) Error() string {
    18  	return "upload failed"
    19  }
    20  
    21  func (actor Actor) Apply(config ApplicationConfig, progressBar ProgressBar) (<-chan ApplicationConfig, <-chan Event, <-chan Warnings, <-chan error) {
    22  	configStream := make(chan ApplicationConfig)
    23  	eventStream := make(chan Event)
    24  	warningsStream := make(chan Warnings)
    25  	errorStream := make(chan error)
    26  
    27  	go func() {
    28  		log.Debug("starting apply go routine")
    29  		defer close(configStream)
    30  		defer close(eventStream)
    31  		defer close(warningsStream)
    32  		defer close(errorStream)
    33  
    34  		var event Event
    35  		var warnings Warnings
    36  		var err error
    37  
    38  		eventStream <- SettingUpApplication
    39  		config, event, warnings, err = actor.CreateOrUpdateApp(config)
    40  		warningsStream <- warnings
    41  		if err != nil {
    42  			errorStream <- err
    43  			return
    44  		}
    45  		eventStream <- event
    46  		log.Debugf("desired application: %#v", config.DesiredApplication)
    47  
    48  		eventStream <- ConfiguringRoutes
    49  
    50  		var createdRoutes bool
    51  		config, createdRoutes, warnings, err = actor.CreateRoutes(config)
    52  		warningsStream <- warnings
    53  		if err != nil {
    54  			errorStream <- err
    55  			return
    56  		}
    57  		if createdRoutes {
    58  			log.Debugf("updated desired routes: %#v", config.DesiredRoutes)
    59  			eventStream <- CreatedRoutes
    60  		}
    61  
    62  		var boundRoutes bool
    63  		config, boundRoutes, warnings, err = actor.BindRoutes(config)
    64  		warningsStream <- warnings
    65  		if err != nil {
    66  			errorStream <- err
    67  			return
    68  		}
    69  		if boundRoutes {
    70  			log.Debugf("updated desired routes: %#v", config.DesiredRoutes)
    71  			eventStream <- BoundRoutes
    72  		}
    73  
    74  		if len(config.CurrentServices) != len(config.DesiredServices) {
    75  			eventStream <- ConfiguringServices
    76  			var boundServices bool
    77  			config, boundServices, warnings, err = actor.BindServices(config)
    78  			warningsStream <- warnings
    79  			if err != nil {
    80  				errorStream <- err
    81  				return
    82  			}
    83  			if boundServices {
    84  				log.Debugf("bound desired services: %#v", config.DesiredServices)
    85  				eventStream <- BoundServices
    86  			}
    87  		}
    88  
    89  		if config.DesiredApplication.DockerImage == "" {
    90  			eventStream <- ResourceMatching
    91  			config, warnings = actor.SetMatchedResources(config)
    92  			warningsStream <- warnings
    93  
    94  			archivePath, err := actor.CreateArchive(config)
    95  			if err != nil {
    96  				errorStream <- err
    97  				return
    98  			}
    99  			eventStream <- CreatingArchive
   100  			defer os.Remove(archivePath)
   101  
   102  			for count := 0; count < PushRetries; count++ {
   103  				warnings, err = actor.UploadPackage(config, archivePath, progressBar, eventStream)
   104  				warningsStream <- warnings
   105  				if _, ok := err.(ccerror.PipeSeekError); !ok {
   106  					break
   107  				}
   108  				eventStream <- RetryUpload
   109  			}
   110  
   111  			if err != nil {
   112  				if _, ok := err.(ccerror.PipeSeekError); ok {
   113  					errorStream <- UploadFailedError{}
   114  					return
   115  				}
   116  				errorStream <- err
   117  				return
   118  			}
   119  		} else {
   120  			log.WithField("docker_image", config.DesiredApplication.DockerImage).Debug("skipping file upload")
   121  		}
   122  
   123  		configStream <- config
   124  
   125  		log.Debug("completed apply")
   126  		eventStream <- Complete
   127  	}()
   128  
   129  	return configStream, eventStream, warningsStream, errorStream
   130  }