github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/actor/pushaction/apply.go (about)

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