github.com/koron/hk@v0.0.0-20150303213137-b8aeaa3ab34c/hkdist/main.go (about)

     1  // Command hkdist provides services for distributing hk binaries and updates.
     2  //
     3  // It has three sub-commands: build, web, and gen.
     4  //
     5  //   $ hkdist build [platforms]
     6  //
     7  // This command builds cross-compiled binaries. The tool builds all known
     8  // platforms by default, but will optionally build for a specified list of
     9  // platforms instead.  It first fetches the source code and termines the latest
    10  // git tag on BUILDBRANCH.  Then, for each platform, it builds a binary
    11  // executable, uploads the binary to an S3 bucket, and posts its SHA-256 hash
    12  // to the hk distribution server (hk.heroku.com in production).
    13  //
    14  //   $ hkdist web
    15  //
    16  // This command provides directory service for hk binary hashes.
    17  //
    18  //   $ hkdist gen
    19  //
    20  // This command polls the distribution server to learn about new releases,
    21  // then generates byte-sequence patches between each pair of releases on
    22  // each platform. It puts these patches in an S3 bucket so the hk client
    23  // can use them for self-update instead of downloading a (much larger) full
    24  // release.
    25  package main
    26  
    27  import (
    28  	"fmt"
    29  	"log"
    30  	"os"
    31  	"path/filepath"
    32  
    33  	"github.com/heroku/hk/Godeps/_workspace/src/github.com/kr/s3"
    34  )
    35  
    36  var (
    37  	distURL      = os.Getenv("DISTURL")
    38  	s3DistURL    = os.Getenv("S3DISTURL")
    39  	s3PatchURL   = os.Getenv("S3PATCHURL")
    40  	buildName    = os.Getenv("BUILDNAME")
    41  	netrcPath    = filepath.Join(os.Getenv("HOME"), ".netrc")
    42  	buildbranch  = os.Getenv("BUILDBRANCH")
    43  	hkgenAppName = os.Getenv("HKGENAPPNAME")
    44  	s3keys       = s3.Keys{
    45  		AccessKey: os.Getenv("S3_ACCESS_KEY"),
    46  		SecretKey: os.Getenv("S3_SECRET_KEY"),
    47  	}
    48  )
    49  
    50  type release struct {
    51  	Plat   string `json:"platform"`
    52  	Ver    string `json:"version"`
    53  	Cmd    string `json:"cmd"`
    54  	Sha256 []byte `json:"sha256"`
    55  }
    56  
    57  func (r release) Name() string {
    58  	return r.Cmd + "/" + r.Ver + "/" + r.Plat
    59  }
    60  
    61  func (r release) Gzname() string {
    62  	return r.Name() + ".gz"
    63  }
    64  
    65  var subcmds = map[string]func([]string){
    66  	"gen":   gen,
    67  	"build": build,
    68  	"web":   web,
    69  }
    70  
    71  func usage() {
    72  	fmt.Fprintln(os.Stderr, "Usage: hkdist (web|gen|build [platforms])")
    73  	os.Exit(2)
    74  }
    75  
    76  func main() {
    77  	log.SetFlags(log.Lshortfile)
    78  	if len(os.Args) < 2 {
    79  		usage()
    80  	} else if os.Args[1] == "web" && len(os.Args) != 2 {
    81  		usage()
    82  	} else if os.Args[1] == "gen" && len(os.Args) != 6 {
    83  		usage()
    84  	}
    85  	f := subcmds[os.Args[1]]
    86  	if f == nil {
    87  		usage()
    88  	}
    89  	f(os.Args[2:])
    90  }