Skip to content

Commit

Permalink
Updated yb to support creating an image based on a (#73)
Browse files Browse the repository at this point in the history
* Updated yb to support creating an image based on a container definition+files

* separated tar archive and docker subcommand

* go mod update

Co-authored-by: Ridai Govinda Pombo <[email protected]>
  • Loading branch information
jamesnaftel and Ridai Govinda Pombo authored Mar 11, 2020
1 parent 07ad49e commit 3b5d222
Show file tree
Hide file tree
Showing 5 changed files with 178 additions and 60 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ yb
.DS_Store
release
.env
.vscode
139 changes: 118 additions & 21 deletions cli/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ import (
"context"
"flag"
"fmt"
"github.com/johnewart/archiver"
"github.com/johnewart/subcommands"
"os"
"path/filepath"
"strings"

"github.com/johnewart/archiver"
"github.com/johnewart/subcommands"

"github.com/yourbase/narwhal"
. "github.com/yourbase/yb/plumbing"
"github.com/yourbase/yb/plumbing/log"
. "github.com/yourbase/yb/workspace"
Expand All @@ -21,7 +24,7 @@ type PackageCmd struct {
func (*PackageCmd) Name() string { return "package" }
func (*PackageCmd) Synopsis() string { return "Create a package artifact" }
func (*PackageCmd) Usage() string {
return `package [--target pkg]`
return ``
}

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

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

func (b *PackageCmd) ArchiveWorkspace() subcommands.ExitStatus {
targetPackage := Package{}
workspace, err := LoadWorkspace()

if err == nil {
if b.target != "" {
targetPackage, _ = workspace.PackageByName(b.target)
} else {
targetPackage, _ = workspace.TargetPackage()
b.target = targetPackage.Name
}
}

// Fallback for no workspace
if targetPackage.Name == "" {
targetPackage, err = GetTargetPackage()
if err != nil {
log.Errorf("Unable to load package: %v\n", err)
return subcommands.ExitFailure
}
}

cmdr.Register(&DockerArchiveCmd{targetPackage: targetPackage}, "")
cmdr.Register(&TarArchiveCmd{targetPackage: targetPackage}, "")

return (cmdr.Execute(ctx))
}

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")
}

//Execute will create a container image matching the file format below
//
// 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: instructions.Docker.BaseImage,
WorkDir: instructions.Docker.WorkingDir,
Command: instructions.Docker.Exec,
}

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)

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

if b.target != "" {
targetPackage, _ = workspace.PackageByName(b.target)
err = narwhal.BuildImageWithFile(cd, repository, d.dockerTag, tmpArchiveFile, "tmp.tar", instructions.Docker.WorkingDir)
if err != nil {
log.Error(err)
return subcommands.ExitFailure
}

instructions := targetPackage.Manifest
return subcommands.ExitSuccess
}

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 ``
}

buildDir := workspace.BuildRoot()
outputDir := filepath.Join(buildDir, "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.Package.Artifacts, pkgFile)
err := tar.Archive(t.targetPackage.Manifest.Package.Artifacts, t.tar_file)
_ = os.Chdir(oldCwd)

if err != nil {
Expand Down
29 changes: 15 additions & 14 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ require (
github.com/frankban/quicktest v1.5.0 // indirect
github.com/gobwas/httphead v0.0.0-20180130184737-2c6c146eadee // indirect
github.com/gobwas/pool v0.2.0 // indirect
github.com/gobwas/ws v1.0.2
github.com/gobwas/ws v1.0.3
github.com/gogo/protobuf v1.3.1 // indirect
github.com/golang/protobuf v1.3.4 // 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 +32,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-20200302210943-78000ba7a073
golang.org/x/net v0.0.0-20200301022130-244492dfa37a // 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
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
golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527 // indirect
google.golang.org/genproto v0.0.0-20200311144346-b662892dd51b // indirect
google.golang.org/grpc v1.28.0 // indirect
gopkg.in/ini.v1 v1.54.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 3b5d222

Please sign in to comment.