Skip to content

Commit

Permalink
separated tar archive and docker subcommand, go version bump
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesnaftel authored and Ridai Govinda Pombo committed Feb 26, 2020
1 parent eaac52e commit c422acb
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 96 deletions.
2 changes: 1 addition & 1 deletion .yourbase.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
dependencies:
build:
- go:1.13.7
- go:1.13.8
- python:3.6

build_targets:
Expand Down
132 changes: 80 additions & 52 deletions cli/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ import (
)

type PackageCmd struct {
target string
dockerRepository string
dockerTag string
target string
}

func (*PackageCmd) Name() string { return "package" }
Expand All @@ -31,8 +29,6 @@ func (*PackageCmd) Usage() string {

func (p *PackageCmd) SetFlags(f *flag.FlagSet) {
f.StringVar(&p.target, "target", "", "Target package, if not the default")
f.StringVar(&p.dockerRepository, "repository", "docker-registry.yourbase.io", "Repository base name")
f.StringVar(&p.dockerTag, "tag", "latest", "Container tag")
}

/*
Expand All @@ -41,7 +37,9 @@ Executing the target involves:
2. Run any dependent components
3. Start target
*/
func (b *PackageCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
func (b *PackageCmd) Execute(ctx context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {

cmdr := subcommands.NewCommander(f, "packages")

targetPackage := Package{}
workspace, err := LoadWorkspace()
Expand All @@ -64,55 +62,68 @@ func (b *PackageCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{
}
}

instructions := targetPackage.Manifest.Package
cmdr.Register(&DockerArchiveCmd{targetPackage: targetPackage}, "")
cmdr.Register(&TarArchiveCmd{targetPackage: targetPackage}, "")

if len(instructions.Artifacts) > 0 {
if workspace.Path != "" {
b.ArchiveWorkspace(targetPackage, instructions.Artifacts)
}
}
if instructions.DockerArtifact.BaseImage != "" {
b.PackageDockerImage(targetPackage, instructions.DockerArtifact)
}
return (cmdr.Execute(ctx))
}

return subcommands.ExitSuccess
type DockerArchiveCmd struct {
targetPackage Package
dockerRepository string
dockerTag string
}

func (*DockerArchiveCmd) Name() string { return "docker" }
func (*DockerArchiveCmd) Synopsis() string { return "Create a docker image" }
func (*DockerArchiveCmd) Usage() string {
return ``
}

func (d *DockerArchiveCmd) SetFlags(f *flag.FlagSet) {
f.StringVar(&d.dockerRepository, "repository", "docker-registry.yourbase.io", "Repository base name")
f.StringVar(&d.dockerTag, "tag", "latest", "Container tag")
}

//PackageDockerImage will create a push a container image matching the file format below
//Execute will create a container image matching the file format below
//
//artifacts:
// docker:
// base_image: ubuntu
// image: dispatcher
// files:
// - dispatcher:dispatcher
// working_dir: /
// exec: /dispatcher
func (b *PackageCmd) PackageDockerImage(targetPackage Package, artifact DockerArtifact) subcommands.ExitStatus {
// package:
// docker:
// base_image: ubuntu
// image: dispatcher
// working_dir: /
// exec: /dispatcher
// artifacts:
// - dispatcher
func (d *DockerArchiveCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {

instructions := d.targetPackage.Manifest.Package
if instructions.Docker.BaseImage == "" {
log.Errorf("BaseImage is a required field in the configuration")
return subcommands.ExitFailure
}
cd := narwhal.ContainerDefinition{
Image: artifact.BaseImage,
Label: artifact.Image,
WorkDir: artifact.WorkingDir,
Command: artifact.Exec,
Image: instructions.Docker.BaseImage,
WorkDir: instructions.Docker.WorkingDir,
Command: instructions.Docker.Exec,
}

file := ""
localFile := ""
// TODO: expand after talking John
// UploadArchive for something like the API - How do we know which files?
if len(artifact.Files) > 0 {
files := strings.SplitN(artifact.Files[0], ":", 2)
localFile = filepath.Join(targetPackage.Path(), files[0])
file = files[0]
tmpArchiveFile := filepath.Join(d.targetPackage.Path(), fmt.Sprintf("%s-package.tar", d.targetPackage.Name))
tar := archiver.Tar{MkdirAll: true}
err := tar.Archive(instructions.Artifacts, tmpArchiveFile)
if err != nil {
log.Errorf("Could not create archive: %v\n", err)
return subcommands.ExitFailure
}
defer os.Remove(tmpArchiveFile)

// Default to yourbase container registry. User can pass in an empty string for local registry
repository := targetPackage.Name
if b.dockerRepository != "" {
repository = fmt.Sprintf("%s/%s", strings.TrimRight(b.dockerRepository, "/"), targetPackage.Name)
repository := d.targetPackage.Name
if d.dockerRepository != "" {
repository = fmt.Sprintf("%s/%s", strings.TrimRight(d.dockerRepository, "/"), d.targetPackage.Name)
}

err := narwhal.BuildImageWithFile(cd, repository, b.dockerTag, localFile, file, artifact.WorkingDir)
err = narwhal.BuildImageWithFile(cd, repository, d.dockerTag, tmpArchiveFile, "tmp.tar", instructions.Docker.WorkingDir)
if err != nil {
log.Error(err)
return subcommands.ExitFailure
Expand All @@ -121,28 +132,45 @@ func (b *PackageCmd) PackageDockerImage(targetPackage Package, artifact DockerAr
return subcommands.ExitSuccess
}

func (b *PackageCmd) ArchiveWorkspace(targetPackage Package, instructions []string) subcommands.ExitStatus {
type TarArchiveCmd struct {
targetPackage Package
tar_file string
}

func (*TarArchiveCmd) Name() string { return "tar" }
func (*TarArchiveCmd) Synopsis() string { return "Create a tar file" }
func (*TarArchiveCmd) Usage() string {
return ``
}

outputDir := filepath.Join(targetPackage.BuildRoot(), "output")
MkdirAsNeeded(outputDir)
archiveFile := fmt.Sprintf("%s-package.tar", targetPackage.Name)
pkgFile := filepath.Join(outputDir, archiveFile)
func (t *TarArchiveCmd) SetFlags(f *flag.FlagSet) {
f.StringVar(&t.tar_file, "tar_file", "", "tar file path and name")
}

func (t *TarArchiveCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {

if t.tar_file == "" {
outputDir := filepath.Join(t.targetPackage.BuildRoot(), "output")
MkdirAsNeeded(outputDir)
archiveFile := fmt.Sprintf("%s-package.tar", t.targetPackage.Name)
t.tar_file = filepath.Join(outputDir, archiveFile)
}

if PathExists(pkgFile) {
os.Remove(pkgFile)
if PathExists(t.tar_file) {
os.Remove(t.tar_file)
}

log.Infof("Generating package file %s...\n", pkgFile)
log.Infof("Generating package file %s...\n", t.tar_file)

tar := archiver.Tar{
MkdirAll: true,
}

packageDir := targetPackage.Path()
packageDir := t.targetPackage.Path()

oldCwd, _ := os.Getwd()
_ = os.Chdir(packageDir)
err := tar.Archive(instructions, pkgFile)
err := tar.Archive(t.targetPackage.Manifest.Package.Artifacts, t.tar_file)
_ = os.Chdir(oldCwd)

if err != nil {
Expand Down
28 changes: 12 additions & 16 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
module github.com/yourbase/yb

replace github.com/Sirupsen/logrus => github.com/sirupsen/logrus v1.4.2

//replace github.com/yourbase/narwhal => /Users/jewart/Work/narwhal

require (
github.com/Microsoft/hcsshim v0.8.7 // indirect
github.com/beholders-eye/diffparser v0.0.0-20190814025112-fcedf0a097ba
Expand All @@ -17,7 +13,7 @@ require (
github.com/gobwas/ws v1.0.2
github.com/gogo/protobuf v1.3.1 // indirect
github.com/golang/snappy v0.0.1 // indirect
github.com/google/shlex v0.0.0-20181106134648-c34317bd91bf
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
github.com/gopherjs/gopherjs v0.0.0-20190915194858-d3ddacdb130f // indirect
github.com/johnewart/archiver v3.1.4+incompatible
github.com/johnewart/subcommands v0.0.0-20181012225330-46f0354f6315
Expand All @@ -31,28 +27,28 @@ require (
github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect
github.com/matishsiao/goInfo v0.0.0-20170803142006-617e6440957e
github.com/morikuni/aec v1.0.0 // indirect
github.com/nwaples/rardecode v1.0.0 // indirect
github.com/pierrec/lz4 v2.3.0+incompatible // indirect
github.com/nwaples/rardecode v1.1.0 // indirect
github.com/pierrec/lz4 v2.4.1+incompatible // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/sergi/go-diff v1.1.0 // indirect
github.com/sirupsen/logrus v1.4.2
github.com/smartystreets/assertions v1.0.1 // indirect
github.com/smartystreets/goconvey v0.0.0-20190731233626-505e41936337 // indirect
github.com/ulikunitz/xz v0.5.6
github.com/ulikunitz/xz v0.5.7
github.com/waigani/diffparser v0.0.0-20190828052634-7391f219313d // indirect
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 // indirect
github.com/yourbase/narwhal v0.0.0-20200207175229-fecb0f220671
golang.org/x/crypto v0.0.0-20191112222119-e1110fd1c708
golang.org/x/net v0.0.0-20191112182307-2180aed22343 // indirect
github.com/yourbase/narwhal v0.0.0-20200226183157-748bc33c70c8
golang.org/x/crypto v0.0.0-20200221231518-2aa609cf4a9d
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b // indirect
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e // indirect
golang.org/x/sys v0.0.0-20200202164722-d101bd2416d5 // indirect
google.golang.org/genproto v0.0.0-20200205142000-a86caf926a67 // indirect
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae // indirect
google.golang.org/genproto v0.0.0-20200225123651-fc8f55426688 // indirect
google.golang.org/grpc v1.27.1 // indirect
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
gopkg.in/ini.v1 v1.51.0
gopkg.in/ini.v1 v1.52.0
gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22 // indirect
gopkg.in/src-d/go-billy.v4 v4.3.2
gopkg.in/src-d/go-git.v4 v4.13.1
gopkg.in/yaml.v2 v2.2.5
gopkg.in/yaml.v2 v2.2.8
)

go 1.13
Loading

0 comments on commit c422acb

Please sign in to comment.