github.com/ablease/cli@v6.37.1-0.20180613014814-3adbb7d7fb19+incompatible/actor/pushaction/resource.go (about)

     1  package pushaction
     2  
     3  import (
     4  	"os"
     5  
     6  	"code.cloudfoundry.org/cli/actor/sharedaction"
     7  	"code.cloudfoundry.org/cli/actor/v2action"
     8  
     9  	log "github.com/sirupsen/logrus"
    10  )
    11  
    12  func (actor Actor) CreateArchive(config ApplicationConfig) (string, error) {
    13  	log.Info("creating archive")
    14  
    15  	var archivePath string
    16  	var err error
    17  
    18  	//change to look at unmatched
    19  	if config.Archive {
    20  		archivePath, err = actor.SharedActor.ZipArchiveResources(config.Path, actor.ConvertV2ResourcesToSharedResources(config.UnmatchedResources))
    21  	} else {
    22  		archivePath, err = actor.SharedActor.ZipDirectoryResources(config.Path, actor.ConvertV2ResourcesToSharedResources(config.UnmatchedResources))
    23  	}
    24  	if err != nil {
    25  		log.WithField("path", config.Path).Errorln("archiving resources:", err)
    26  		return archivePath, err
    27  	}
    28  	log.WithField("archivePath", archivePath).Debug("archive created")
    29  	return archivePath, nil
    30  }
    31  
    32  func (actor Actor) ConvertSharedResourcesToV2Resources(resources []sharedaction.Resource) []v2action.Resource {
    33  	newResources := make([]v2action.Resource, 0, len(resources)) // Explicitly done to prevent nils
    34  
    35  	for _, resource := range resources {
    36  		newResources = append(newResources, v2action.Resource(resource))
    37  	}
    38  	return newResources
    39  }
    40  
    41  func (actor Actor) ConvertV2ResourcesToSharedResources(resources []v2action.Resource) []sharedaction.Resource {
    42  	newResources := make([]sharedaction.Resource, 0, len(resources)) // Explicitly done to prevent nils
    43  
    44  	for _, resource := range resources {
    45  		newResources = append(newResources, sharedaction.Resource(resource))
    46  	}
    47  	return newResources
    48  }
    49  
    50  func (actor Actor) SetMatchedResources(config ApplicationConfig) (ApplicationConfig, Warnings) {
    51  	matched, unmatched, warnings, err := actor.V2Actor.ResourceMatch(config.AllResources)
    52  	if err != nil {
    53  		log.Error("uploading all resources instead of resource matching")
    54  		config.UnmatchedResources = config.AllResources
    55  		return config, Warnings(warnings)
    56  	}
    57  
    58  	config.MatchedResources = matched
    59  	config.UnmatchedResources = unmatched
    60  
    61  	return config, Warnings(warnings)
    62  }
    63  
    64  func (actor Actor) UploadPackage(config ApplicationConfig) (Warnings, error) {
    65  	job, warnings, err := actor.V2Actor.UploadApplicationPackage(config.DesiredApplication.GUID, config.MatchedResources, nil, 0)
    66  	if err != nil {
    67  		return Warnings(warnings), err
    68  	}
    69  
    70  	pollWarnings, err := actor.V2Actor.PollJob(job)
    71  	return append(Warnings(warnings), pollWarnings...), err
    72  }
    73  
    74  func (actor Actor) UploadPackageWithArchive(config ApplicationConfig, archivePath string, progressbar ProgressBar, eventStream chan<- Event) (Warnings, error) {
    75  	log.Info("uploading archive")
    76  	archive, err := os.Open(archivePath)
    77  	if err != nil {
    78  		log.WithField("archivePath", archivePath).Errorln("opening temp archive:", err)
    79  		return nil, err
    80  	}
    81  	defer archive.Close()
    82  
    83  	archiveInfo, err := archive.Stat()
    84  	if err != nil {
    85  		log.WithField("archivePath", archivePath).Errorln("stat temp archive:", err)
    86  		return nil, err
    87  	}
    88  
    89  	log.WithFields(log.Fields{
    90  		"appGUID":     config.DesiredApplication.GUID,
    91  		"archiveSize": archiveInfo.Size(),
    92  	}).Debug("uploading app bits")
    93  
    94  	eventStream <- UploadingApplicationWithArchive
    95  	reader := progressbar.NewProgressBarWrapper(archive, archiveInfo.Size())
    96  
    97  	var allWarnings Warnings
    98  	// change to look at matched resoruces
    99  	job, warnings, err := actor.V2Actor.UploadApplicationPackage(config.DesiredApplication.GUID, config.MatchedResources, reader, archiveInfo.Size())
   100  	allWarnings = append(allWarnings, Warnings(warnings)...)
   101  
   102  	if err != nil {
   103  		log.WithField("archivePath", archivePath).Errorln("streaming archive:", err)
   104  		return allWarnings, err
   105  	}
   106  	eventStream <- UploadWithArchiveComplete
   107  	warnings, err = actor.V2Actor.PollJob(job)
   108  	allWarnings = append(allWarnings, Warnings(warnings)...)
   109  
   110  	return allWarnings, err
   111  }
   112  
   113  func (actor Actor) UploadDroplet(config ApplicationConfig, dropletPath string, progressbar ProgressBar, eventStream chan<- Event) (Warnings, error) {
   114  	log.Info("uploading droplet")
   115  	droplet, err := os.Open(dropletPath)
   116  	if err != nil {
   117  		log.WithField("dropletPath", dropletPath).WithError(err).Errorln("opening droplet")
   118  		return nil, err
   119  	}
   120  	defer droplet.Close()
   121  
   122  	dropletInfo, err := droplet.Stat()
   123  	if err != nil {
   124  		log.WithField("dropletPath", dropletPath).WithError(err).Errorln("stat droplet")
   125  		return nil, err
   126  	}
   127  
   128  	log.WithFields(log.Fields{
   129  		"app_guid":     config.DesiredApplication.GUID,
   130  		"droplet_size": dropletInfo.Size(),
   131  	}).Debug("uploading droplet")
   132  
   133  	eventStream <- UploadingDroplet
   134  	reader := progressbar.NewProgressBarWrapper(droplet, dropletInfo.Size())
   135  
   136  	var allWarnings Warnings
   137  	job, warnings, err := actor.V2Actor.UploadDroplet(config.DesiredApplication.GUID, reader, dropletInfo.Size())
   138  	allWarnings = append(allWarnings, warnings...)
   139  
   140  	if err != nil {
   141  		return allWarnings, err
   142  	}
   143  
   144  	eventStream <- UploadDropletComplete
   145  	pollWarnings, err := actor.V2Actor.PollJob(job)
   146  	allWarnings = append(allWarnings, pollWarnings...)
   147  
   148  	return allWarnings, err
   149  }