github.com/arunkumar7540/cli@v6.45.0+incompatible/actor/v3action/package.go (about) 1 package v3action 2 3 import ( 4 "io" 5 "os" 6 "time" 7 8 "code.cloudfoundry.org/cli/actor/actionerror" 9 "code.cloudfoundry.org/cli/actor/sharedaction" 10 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3" 11 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant" 12 log "github.com/sirupsen/logrus" 13 ) 14 15 const ( 16 DefaultFolderPermissions = 0755 17 DefaultArchiveFilePermissions = 0744 18 ) 19 20 type Package ccv3.Package 21 22 type DockerImageCredentials struct { 23 Path string 24 Username string 25 Password string 26 } 27 28 func (actor Actor) CreateDockerPackageByApplicationNameAndSpace(appName string, spaceGUID string, dockerImageCredentials DockerImageCredentials) (Package, Warnings, error) { 29 app, allWarnings, err := actor.GetApplicationByNameAndSpace(appName, spaceGUID) 30 if err != nil { 31 return Package{}, allWarnings, err 32 } 33 inputPackage := ccv3.Package{ 34 Type: constant.PackageTypeDocker, 35 Relationships: ccv3.Relationships{ 36 constant.RelationshipTypeApplication: ccv3.Relationship{GUID: app.GUID}, 37 }, 38 DockerImage: dockerImageCredentials.Path, 39 DockerUsername: dockerImageCredentials.Username, 40 DockerPassword: dockerImageCredentials.Password, 41 } 42 pkg, warnings, err := actor.CloudControllerClient.CreatePackage(inputPackage) 43 allWarnings = append(allWarnings, warnings...) 44 if err != nil { 45 return Package{}, allWarnings, err 46 } 47 return Package(pkg), allWarnings, err 48 } 49 50 func (actor Actor) CreateAndUploadBitsPackageByApplicationNameAndSpace(appName string, spaceGUID string, bitsPath string) (Package, Warnings, error) { 51 app, allWarnings, err := actor.GetApplicationByNameAndSpace(appName, spaceGUID) 52 if err != nil { 53 return Package{}, allWarnings, err 54 } 55 56 if bitsPath == "" { 57 bitsPath, err = os.Getwd() 58 if err != nil { 59 return Package{}, allWarnings, err 60 } 61 } 62 63 info, err := os.Stat(bitsPath) 64 if err != nil { 65 return Package{}, allWarnings, err 66 } 67 68 var resources []sharedaction.Resource 69 if info.IsDir() { 70 resources, err = actor.SharedActor.GatherDirectoryResources(bitsPath) 71 } else { 72 resources, err = actor.SharedActor.GatherArchiveResources(bitsPath) 73 } 74 if err != nil { 75 return Package{}, allWarnings, err 76 } 77 78 // potentially match resources here in the future 79 80 var archivePath string 81 if info.IsDir() { 82 archivePath, err = actor.SharedActor.ZipDirectoryResources(bitsPath, resources) 83 } else { 84 archivePath, err = actor.SharedActor.ZipArchiveResources(bitsPath, resources) 85 } 86 if err != nil { 87 os.RemoveAll(archivePath) 88 return Package{}, allWarnings, err 89 } 90 defer os.RemoveAll(archivePath) 91 92 inputPackage := ccv3.Package{ 93 Type: constant.PackageTypeBits, 94 Relationships: ccv3.Relationships{ 95 constant.RelationshipTypeApplication: ccv3.Relationship{GUID: app.GUID}, 96 }, 97 } 98 99 pkg, warnings, err := actor.CloudControllerClient.CreatePackage(inputPackage) 100 allWarnings = append(allWarnings, warnings...) 101 if err != nil { 102 return Package{}, allWarnings, err 103 } 104 105 _, warnings, err = actor.CloudControllerClient.UploadPackage(pkg, archivePath) 106 allWarnings = append(allWarnings, warnings...) 107 if err != nil { 108 return Package{}, allWarnings, err 109 } 110 111 for pkg.State != constant.PackageReady && 112 pkg.State != constant.PackageFailed && 113 pkg.State != constant.PackageExpired { 114 time.Sleep(actor.Config.PollingInterval()) 115 pkg, warnings, err = actor.CloudControllerClient.GetPackage(pkg.GUID) 116 allWarnings = append(allWarnings, warnings...) 117 if err != nil { 118 return Package{}, allWarnings, err 119 } 120 } 121 122 if pkg.State == constant.PackageFailed { 123 return Package{}, allWarnings, actionerror.PackageProcessingFailedError{} 124 } else if pkg.State == constant.PackageExpired { 125 return Package{}, allWarnings, actionerror.PackageProcessingExpiredError{} 126 } 127 128 updatedPackage, updatedWarnings, err := actor.PollPackage(Package(pkg)) 129 return updatedPackage, append(allWarnings, updatedWarnings...), err 130 } 131 132 // GetApplicationPackages returns a list of package of an app. 133 func (actor *Actor) GetApplicationPackages(appName string, spaceGUID string) ([]Package, Warnings, error) { 134 app, allWarnings, err := actor.GetApplicationByNameAndSpace(appName, spaceGUID) 135 if err != nil { 136 return nil, allWarnings, err 137 } 138 139 ccv3Packages, warnings, err := actor.CloudControllerClient.GetPackages( 140 ccv3.Query{Key: ccv3.AppGUIDFilter, Values: []string{app.GUID}}, 141 ) 142 allWarnings = append(allWarnings, warnings...) 143 if err != nil { 144 return nil, allWarnings, err 145 } 146 147 var packages []Package 148 for _, ccv3Package := range ccv3Packages { 149 packages = append(packages, Package(ccv3Package)) 150 } 151 152 return packages, allWarnings, nil 153 } 154 155 func (actor Actor) CreateBitsPackageByApplication(appGUID string) (Package, Warnings, error) { 156 inputPackage := ccv3.Package{ 157 Type: constant.PackageTypeBits, 158 Relationships: ccv3.Relationships{ 159 constant.RelationshipTypeApplication: ccv3.Relationship{GUID: appGUID}, 160 }, 161 } 162 163 pkg, warnings, err := actor.CloudControllerClient.CreatePackage(inputPackage) 164 if err != nil { 165 return Package{}, Warnings(warnings), err 166 } 167 168 return Package(pkg), Warnings(warnings), err 169 } 170 171 func (actor Actor) UploadBitsPackage(pkg Package, existingResources []sharedaction.Resource, newResources io.Reader, newResourcesLength int64) (Package, Warnings, error) { 172 apiResources := make([]ccv3.Resource, 0, len(existingResources)) // Explicitly done to prevent nils 173 174 for _, resource := range existingResources { 175 apiResources = append(apiResources, ccv3.Resource(resource.ToV3Resource())) 176 } 177 178 appPkg, warnings, err := actor.CloudControllerClient.UploadBitsPackage(ccv3.Package(pkg), apiResources, newResources, newResourcesLength) 179 return Package(appPkg), Warnings(warnings), err 180 } 181 182 // PollPackage returns a package of an app. 183 func (actor Actor) PollPackage(pkg Package) (Package, Warnings, error) { 184 var allWarnings Warnings 185 186 for pkg.State != constant.PackageReady && pkg.State != constant.PackageFailed && pkg.State != constant.PackageExpired { 187 time.Sleep(actor.Config.PollingInterval()) 188 ccPkg, warnings, err := actor.CloudControllerClient.GetPackage(pkg.GUID) 189 log.WithFields(log.Fields{ 190 "package_guid": pkg.GUID, 191 "state": pkg.State, 192 }).Debug("polling package state") 193 194 allWarnings = append(allWarnings, warnings...) 195 if err != nil { 196 return Package{}, allWarnings, err 197 } 198 199 pkg = Package(ccPkg) 200 } 201 202 if pkg.State == constant.PackageFailed { 203 return Package{}, allWarnings, actionerror.PackageProcessingFailedError{} 204 } else if pkg.State == constant.PackageExpired { 205 return Package{}, allWarnings, actionerror.PackageProcessingExpiredError{} 206 } 207 208 return pkg, allWarnings, nil 209 }