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

     1  package v3action
     2  
     3  import (
     4  	"os"
     5  	"time"
     6  
     7  	"code.cloudfoundry.org/cli/actor/actionerror"
     8  	"code.cloudfoundry.org/cli/actor/sharedaction"
     9  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
    10  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant"
    11  )
    12  
    13  const (
    14  	DefaultFolderPermissions      = 0755
    15  	DefaultArchiveFilePermissions = 0744
    16  )
    17  
    18  type Package ccv3.Package
    19  
    20  type DockerImageCredentials struct {
    21  	Path     string
    22  	Username string
    23  	Password string
    24  }
    25  
    26  func (actor Actor) CreateDockerPackageByApplicationNameAndSpace(appName string, spaceGUID string, dockerImageCredentials DockerImageCredentials) (Package, Warnings, error) {
    27  	app, allWarnings, err := actor.GetApplicationByNameAndSpace(appName, spaceGUID)
    28  	if err != nil {
    29  		return Package{}, allWarnings, err
    30  	}
    31  	inputPackage := ccv3.Package{
    32  		Type: constant.PackageTypeDocker,
    33  		Relationships: ccv3.Relationships{
    34  			constant.ApplicationRelationship: ccv3.Relationship{GUID: app.GUID},
    35  		},
    36  		DockerImage:    dockerImageCredentials.Path,
    37  		DockerUsername: dockerImageCredentials.Username,
    38  		DockerPassword: dockerImageCredentials.Password,
    39  	}
    40  	pkg, warnings, err := actor.CloudControllerClient.CreatePackage(inputPackage)
    41  	allWarnings = append(allWarnings, warnings...)
    42  	if err != nil {
    43  		return Package{}, allWarnings, err
    44  	}
    45  	return Package(pkg), allWarnings, err
    46  }
    47  
    48  func (actor Actor) CreateAndUploadBitsPackageByApplicationNameAndSpace(appName string, spaceGUID string, bitsPath string) (Package, Warnings, error) {
    49  	app, allWarnings, err := actor.GetApplicationByNameAndSpace(appName, spaceGUID)
    50  	if err != nil {
    51  		return Package{}, allWarnings, err
    52  	}
    53  
    54  	if bitsPath == "" {
    55  		bitsPath, err = os.Getwd()
    56  		if err != nil {
    57  			return Package{}, allWarnings, err
    58  		}
    59  	}
    60  
    61  	info, err := os.Stat(bitsPath)
    62  	if err != nil {
    63  		return Package{}, allWarnings, err
    64  	}
    65  
    66  	var resources []sharedaction.Resource
    67  	if info.IsDir() {
    68  		resources, err = actor.SharedActor.GatherDirectoryResources(bitsPath)
    69  	} else {
    70  		resources, err = actor.SharedActor.GatherArchiveResources(bitsPath)
    71  	}
    72  	if err != nil {
    73  		return Package{}, allWarnings, err
    74  	}
    75  
    76  	// potentially match resources here in the future
    77  
    78  	var archivePath string
    79  	if info.IsDir() {
    80  		archivePath, err = actor.SharedActor.ZipDirectoryResources(bitsPath, resources)
    81  	} else {
    82  		archivePath, err = actor.SharedActor.ZipArchiveResources(bitsPath, resources)
    83  	}
    84  	if err != nil {
    85  		return Package{}, allWarnings, err
    86  	}
    87  
    88  	inputPackage := ccv3.Package{
    89  		Type: constant.PackageTypeBits,
    90  		Relationships: ccv3.Relationships{
    91  			constant.ApplicationRelationship: ccv3.Relationship{GUID: app.GUID},
    92  		},
    93  	}
    94  
    95  	pkg, warnings, err := actor.CloudControllerClient.CreatePackage(inputPackage)
    96  	allWarnings = append(allWarnings, warnings...)
    97  	if err != nil {
    98  		return Package{}, allWarnings, err
    99  	}
   100  
   101  	_, warnings, err = actor.CloudControllerClient.UploadPackage(pkg, archivePath)
   102  	allWarnings = append(allWarnings, warnings...)
   103  	if err != nil {
   104  		return Package{}, allWarnings, err
   105  	}
   106  
   107  	for pkg.State != constant.PackageReady &&
   108  		pkg.State != constant.PackageFailed &&
   109  		pkg.State != constant.PackageExpired {
   110  		time.Sleep(actor.Config.PollingInterval())
   111  		pkg, warnings, err = actor.CloudControllerClient.GetPackage(pkg.GUID)
   112  		allWarnings = append(allWarnings, warnings...)
   113  		if err != nil {
   114  			return Package{}, allWarnings, err
   115  		}
   116  	}
   117  
   118  	if pkg.State == constant.PackageFailed {
   119  		return Package{}, allWarnings, actionerror.PackageProcessingFailedError{}
   120  	} else if pkg.State == constant.PackageExpired {
   121  		return Package{}, allWarnings, actionerror.PackageProcessingExpiredError{}
   122  	}
   123  
   124  	return Package(pkg), allWarnings, err
   125  }
   126  
   127  // GetApplicationPackages returns a list of package of an app.
   128  func (actor *Actor) GetApplicationPackages(appName string, spaceGUID string) ([]Package, Warnings, error) {
   129  	app, allWarnings, err := actor.GetApplicationByNameAndSpace(appName, spaceGUID)
   130  	if err != nil {
   131  		return nil, allWarnings, err
   132  	}
   133  
   134  	ccv3Packages, warnings, err := actor.CloudControllerClient.GetPackages(
   135  		ccv3.Query{Key: ccv3.AppGUIDFilter, Values: []string{app.GUID}},
   136  	)
   137  	allWarnings = append(allWarnings, warnings...)
   138  	if err != nil {
   139  		return nil, allWarnings, err
   140  	}
   141  
   142  	var packages []Package
   143  	for _, ccv3Package := range ccv3Packages {
   144  		packages = append(packages, Package(ccv3Package))
   145  	}
   146  
   147  	return packages, allWarnings, nil
   148  }