github.com/randomtask1155/cli@v6.41.1-0.20181227003417-a98eed78cbde+incompatible/actor/pushaction/actualize.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  	log "github.com/sirupsen/logrus"
     9  )
    10  
    11  func (actor Actor) Actualize(state PushState, progressBar ProgressBar) (
    12  	<-chan PushState, <-chan Event, <-chan Warnings, <-chan error,
    13  ) {
    14  	log.Debugf("Starting to Actualize Push State: %#v\n", state)
    15  	stateStream := make(chan PushState)
    16  	eventStream := make(chan Event)
    17  	warningsStream := make(chan Warnings)
    18  	errorStream := make(chan error)
    19  
    20  	go func() {
    21  		log.Debug("starting actualize go routine")
    22  		defer close(stateStream)
    23  		defer close(eventStream)
    24  		defer close(warningsStream)
    25  		defer close(errorStream)
    26  
    27  		if state.Application.GUID != "" {
    28  			log.WithField("Name", state.Application.Name).Info("skipping app creation as it has a GUID")
    29  			eventStream <- SkippingApplicationCreation
    30  		} else {
    31  			log.WithField("Name", state.Application.Name).Info("creating app")
    32  			createdApp, createWarnings, err := actor.V3Actor.CreateApplicationInSpace(state.Application, state.SpaceGUID)
    33  			warningsStream <- Warnings(createWarnings)
    34  			if err != nil {
    35  				errorStream <- err
    36  				return
    37  			}
    38  
    39  			state.Application = createdApp
    40  			eventStream <- CreatedApplication
    41  		}
    42  		stateStream <- state
    43  
    44  		log.WithField("Path", state.BitsPath).Info(string(CreatingArchive))
    45  
    46  		eventStream <- CreatingArchive
    47  		archivePath, err := actor.SharedActor.ZipDirectoryResources(state.BitsPath, state.AllResources)
    48  		if err != nil {
    49  			errorStream <- err
    50  			return
    51  		}
    52  		defer os.RemoveAll(archivePath)
    53  
    54  		eventStream <- CreatingPackage
    55  		log.WithField("GUID", state.Application.GUID).Info("creating package")
    56  		pkg, warnings, err := actor.V3Actor.CreateBitsPackageByApplication(state.Application.GUID)
    57  		warningsStream <- Warnings(warnings)
    58  		if err != nil {
    59  			errorStream <- err
    60  			return
    61  		}
    62  
    63  		for count := 0; count < PushRetries; count++ {
    64  			eventStream <- ReadingArchive
    65  			log.WithField("GUID", state.Application.GUID).Info("creating package")
    66  			file, size, readErr := actor.SharedActor.ReadArchive(archivePath)
    67  			if readErr != nil {
    68  				errorStream <- readErr
    69  				return
    70  			}
    71  			defer file.Close()
    72  
    73  			eventStream <- UploadingApplicationWithArchive
    74  			progressReader := progressBar.NewProgressBarWrapper(file, size)
    75  			pkg, warnings, err = actor.V3Actor.UploadBitsPackage(pkg, state.MatchedResources, progressReader, size)
    76  			warningsStream <- Warnings(warnings)
    77  
    78  			if _, ok := err.(ccerror.PipeSeekError); ok {
    79  				eventStream <- RetryUpload
    80  				continue
    81  			}
    82  			break
    83  		}
    84  
    85  		if err != nil {
    86  			if e, ok := err.(ccerror.PipeSeekError); ok {
    87  				errorStream <- actionerror.UploadFailedError{Err: e.Err}
    88  				return
    89  			}
    90  			errorStream <- err
    91  			return
    92  		}
    93  
    94  		eventStream <- UploadWithArchiveComplete
    95  
    96  		polledPackage, warnings, err := actor.V3Actor.PollPackage(pkg)
    97  		warningsStream <- Warnings(warnings)
    98  		if err != nil {
    99  			errorStream <- err
   100  			return
   101  		}
   102  
   103  		eventStream <- StartingStaging
   104  
   105  		build, warnings, err := actor.V3Actor.StageApplicationPackage(polledPackage.GUID)
   106  		warningsStream <- Warnings(warnings)
   107  		if err != nil {
   108  			errorStream <- err
   109  			return
   110  		}
   111  
   112  		eventStream <- PollingBuild
   113  
   114  		droplet, warnings, err := actor.V3Actor.PollBuild(build.GUID, state.Application.Name)
   115  		warningsStream <- Warnings(warnings)
   116  		if err != nil {
   117  			errorStream <- err
   118  			return
   119  		}
   120  
   121  		eventStream <- StagingComplete
   122  		eventStream <- SettingDroplet
   123  
   124  		warnings, err = actor.V3Actor.SetApplicationDroplet(state.Application.GUID, droplet.GUID)
   125  		warningsStream <- Warnings(warnings)
   126  		if err != nil {
   127  			errorStream <- err
   128  			return
   129  		}
   130  
   131  		eventStream <- SetDropletComplete
   132  
   133  		log.Debug("completed apply")
   134  		eventStream <- Complete
   135  	}()
   136  	return stateStream, eventStream, warningsStream, errorStream
   137  }