github.com/jghiloni/cli@v6.28.1-0.20170628223758-0ce05fe032a2+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 config.DesiredApplication.DockerImage == "" {
    75  			archivePath, err := actor.CreateArchive(config)
    76  			if err != nil {
    77  				errorStream <- err
    78  				return
    79  			}
    80  			eventStream <- CreatingArchive
    81  			defer os.Remove(archivePath)
    82  
    83  			for count := 0; count < PushRetries; count++ {
    84  				warnings, err = actor.UploadPackage(config, archivePath, progressBar, eventStream)
    85  				warningsStream <- warnings
    86  				if _, ok := err.(ccerror.PipeSeekError); !ok {
    87  					break
    88  				}
    89  				eventStream <- RetryUpload
    90  			}
    91  
    92  			if err != nil {
    93  				if _, ok := err.(ccerror.PipeSeekError); ok {
    94  					errorStream <- UploadFailedError{}
    95  					return
    96  				}
    97  				errorStream <- err
    98  				return
    99  			}
   100  		} else {
   101  			log.WithField("docker_image", config.DesiredApplication.DockerImage).Debug("skipping file upload")
   102  		}
   103  
   104  		configStream <- config
   105  
   106  		log.Debug("completed apply")
   107  		eventStream <- Complete
   108  	}()
   109  
   110  	return configStream, eventStream, warningsStream, errorStream
   111  }