github.com/joshdk/godel@v0.0.0-20170529232908-862138a45aee/apps/distgo/cmd/dist/bindist.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  	"bytes"
    19  	"fmt"
    20  	"io"
    21  	"io/ioutil"
    22  	"path"
    23  	"text/template"
    24  
    25  	"github.com/palantir/pkg/specdir"
    26  	"github.com/pkg/errors"
    27  
    28  	"github.com/palantir/godel/apps/distgo/params"
    29  	"github.com/palantir/godel/apps/distgo/pkg/binspec"
    30  	"github.com/palantir/godel/apps/distgo/templating"
    31  )
    32  
    33  const binDistInitSh = `#!/bin/bash
    34  set -euo pipefail
    35  
    36  BIN_DIR="$(cd "$(dirname "$0")" && pwd)"
    37  
    38  # determine OS
    39  OS=""
    40  case "$(uname)" in
    41    Darwin*)
    42      OS=darwin
    43      ;;
    44    Linux*)
    45      OS=linux
    46      ;;
    47    *)
    48      echo "Unsupported operating system: $(uname)"
    49      exit 1
    50      ;;
    51  esac
    52  
    53  # determine executable location based on OS
    54  CMD=$BIN_DIR/$OS-amd64/{{.ProductName}}
    55  
    56  # verify that executable exists
    57  if [ ! -e "$CMD" ]; then
    58      echo "Executable $CMD does not exist"
    59      exit 1
    60  fi
    61  
    62  # invoke appropriate executable
    63  $CMD "$@"
    64  `
    65  
    66  type binDister params.BinDistInfo
    67  
    68  func (b *binDister) NumArtifacts() int {
    69  	return 1
    70  }
    71  
    72  func (b *binDister) ArtifactPathsInOutputDir(buildSpec params.ProductBuildSpec) []string {
    73  	return []string{fmt.Sprintf("%v-%v.tgz", buildSpec.ProductName, buildSpec.ProductVersion)}
    74  }
    75  
    76  func (b *binDister) Dist(buildSpecWithDeps params.ProductBuildSpecWithDeps, distCfg params.Dist, outputProductDir string, spec specdir.LayoutSpec, values specdir.TemplateValues, stdout io.Writer) (Packager, error) {
    77  	buildSpec := buildSpecWithDeps.Spec
    78  	binSpec := binspec.New(buildSpec.Build.OSArchs, buildSpec.ProductName)
    79  	binDir := path.Join(outputProductDir, "bin")
    80  	binSpecDir, err := specdir.New(binDir, binSpec, nil, specdir.Create)
    81  	if err != nil {
    82  		return nil, errors.Wrapf(err, "failed to create directory structure for %v", binDir)
    83  	}
    84  	if err := copyBuildArtifactsToBinDir(buildSpecWithDeps, binSpecDir); err != nil {
    85  		return nil, errors.Wrapf(err, "failed to copy artifacts to bin dir")
    86  	}
    87  
    88  	if !b.OmitInitSh {
    89  		var initShTemplateBytes []byte
    90  		if b.InitShTemplateFile != "" {
    91  			initShTemplateFilePath := path.Join(buildSpec.ProjectDir, b.InitShTemplateFile)
    92  			var err error
    93  			initShTemplateBytes, err = ioutil.ReadFile(initShTemplateFilePath)
    94  			if err != nil {
    95  				return nil, errors.Wrapf(err, "failed to read init.sh template file %v", initShTemplateFilePath)
    96  			}
    97  		} else {
    98  			initShTemplateBytes = []byte(binDistInitSh)
    99  		}
   100  		initShBuf := bytes.Buffer{}
   101  		t := template.Must(template.New("init.sh").Parse(string(initShTemplateBytes)))
   102  		if err := t.Execute(&initShBuf, templating.ConvertSpec(buildSpec, distCfg)); err != nil {
   103  			return nil, errors.Wrapf(err, "failed to execute template %v on template %v", t, buildSpec)
   104  		}
   105  		if err := ioutil.WriteFile(path.Join(binDir, buildSpec.ProductName+".sh"), initShBuf.Bytes(), 0755); err != nil {
   106  			return nil, errors.Wrapf(err, "failed to write init.sh")
   107  		}
   108  	}
   109  
   110  	// known to only have 1 output path
   111  	dstPath := FullArtifactsPaths(b, buildSpec, distCfg)[0]
   112  	return singlePathTGZPackager(dstPath, outputProductDir), nil
   113  }
   114  
   115  func (b *binDister) DistPackageType() string {
   116  	return "tgz"
   117  }