github.com/joshdk/godel@v0.0.0-20170529232908-862138a45aee/apps/distgo/cmd/dist/osarchbindist.go (about) 1 // Copyright 2016 Palantir Technologies, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package dist 16 17 import ( 18 "fmt" 19 "io" 20 "os" 21 "path" 22 "sort" 23 24 "github.com/palantir/pkg/specdir" 25 "github.com/pkg/errors" 26 "github.com/termie/go-shutil" 27 28 "github.com/palantir/godel/apps/distgo/cmd/build" 29 "github.com/palantir/godel/apps/distgo/params" 30 "github.com/palantir/godel/apps/distgo/pkg/osarch" 31 ) 32 33 type osArchsBinDister params.OSArchsBinDistInfo 34 35 func (o *osArchsBinDister) NumArtifacts() int { 36 return 1 37 } 38 39 func (o *osArchsBinDister) ArtifactPathsInOutputDir(buildSpec params.ProductBuildSpec) []string { 40 var outPaths []string 41 for _, osArch := range o.OSArchs { 42 outPaths = append(outPaths, fmt.Sprintf("%s-%s-%s.tgz", buildSpec.ProductName, buildSpec.ProductVersion, osArch.String())) 43 } 44 return outPaths 45 } 46 47 func (o *osArchsBinDister) Dist(buildSpecWithDeps params.ProductBuildSpecWithDeps, distCfg params.Dist, outputProductDir string, spec specdir.LayoutSpec, values specdir.TemplateValues, stdout io.Writer) (Packager, error) { 48 buildSpec := buildSpecWithDeps.Spec 49 for _, osArch := range o.OSArchs { 50 if err := verifyDistTargetSupported(osArch, buildSpecWithDeps); err != nil { 51 return nil, err 52 } 53 } 54 55 // each index holds all of the files required for the OS/Arch at the corresponding index 56 outputPathsForOSArchs := make([][]string, len(o.OSArchs)) 57 58 for i, osArch := range o.OSArchs { 59 // copy executable for current product 60 dst, err := copyArtifactForOSArch(outputProductDir, buildSpec, osArch) 61 if err != nil { 62 return nil, err 63 } 64 outputPathsForOSArchs[i] = append(outputPathsForOSArchs[i], dst) 65 } 66 67 for i, osArch := range o.OSArchs { 68 // copy executables for dependent products 69 for _, currDepSpec := range buildSpecWithDeps.Deps { 70 dst, err := copyArtifactForOSArch(outputProductDir, currDepSpec, osArch) 71 if err != nil { 72 return nil, err 73 } 74 outputPathsForOSArchs[i] = append(outputPathsForOSArchs[i], dst) 75 } 76 } 77 78 outputArtifactPaths := FullArtifactsPaths(o, buildSpec, distCfg) 79 artifactToInputPaths := make(map[string][]string) 80 for i, currPaths := range outputPathsForOSArchs { 81 artifactToInputPaths[outputArtifactPaths[i]] = currPaths 82 } 83 return tgzPackager(outputArtifactPaths, artifactToInputPaths), nil 84 } 85 86 func (o *osArchsBinDister) DistPackageType() string { 87 return "tgz" 88 } 89 90 func verifyDistTargetSupported(osArch osarch.OSArch, buildSpecWithDeps params.ProductBuildSpecWithDeps) error { 91 spec := buildSpecWithDeps.Spec 92 if !osArchInBuildSpec(osArch, spec) { 93 return errors.Errorf("The OS/Arch specified for the distribution of a product must be specified as a build target for the product, "+ 94 "but product %s does not specify %s as one of its build targets. Current build targets: %v", spec.ProductName, osArch, spec.Build.OSArchs) 95 } 96 97 var keys []string 98 for k := range buildSpecWithDeps.Deps { 99 keys = append(keys, k) 100 } 101 sort.Strings(keys) 102 for _, currKey := range keys { 103 currSpec := buildSpecWithDeps.Deps[currKey] 104 if !osArchInBuildSpec(osArch, spec) { 105 return errors.Errorf("The OS/Arch specified for the distribution of a product must be specified as a build target for the product, "+ 106 "but product %s (which is a dependent product of %s) does not specify %s as one of its build targets. Current build targets: %v", currSpec.ProductName, buildSpecWithDeps.Spec.ProductName, osArch, currSpec.Build.OSArchs) 107 } 108 } 109 return nil 110 } 111 112 func osArchInBuildSpec(osArch osarch.OSArch, spec params.ProductBuildSpec) bool { 113 found := false 114 for _, currBuildOSArch := range spec.Build.OSArchs { 115 if currBuildOSArch == osArch { 116 found = true 117 break 118 } 119 } 120 return found 121 } 122 123 func copyArtifactForOSArch(outputProductDir string, buildSpec params.ProductBuildSpec, osArch osarch.OSArch) (string, error) { 124 artifactPath, ok := build.ArtifactPaths(buildSpec)[osArch] 125 if !ok { 126 return "", errors.Errorf("no build artifacts exist for %s", osArch) 127 } 128 dst := path.Join(outputProductDir, osArch.String(), build.ExecutableName(buildSpec.ProductName, osArch.OS)) 129 130 if err := os.MkdirAll(path.Dir(dst), 0755); err != nil { 131 return "", errors.Wrapf(err, "failed to create output directory for artifact") 132 } 133 if _, err := shutil.Copy(artifactPath, dst, false); err != nil { 134 return "", errors.Wrapf(err, "failed to copy build artifact from %s to %s", artifactPath, dst) 135 } 136 return dst, nil 137 }