Skip to content

Commit

Permalink
Workspace centric targets
Browse files Browse the repository at this point in the history
Adds fixes and features from `hcl-config`, but won't enable HCL itself
  • Loading branch information
Ridai Govinda Pombo committed Jun 1, 2020
1 parent bf9111e commit be189e7
Show file tree
Hide file tree
Showing 11 changed files with 436 additions and 290 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.14.2
- go:1.14.3
- python:3.6

build_targets:
Expand Down
37 changes: 17 additions & 20 deletions cli/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,8 @@ import (
"os"
"time"

"github.com/matishsiao/goInfo"

"github.com/johnewart/subcommands"
ybconfig "github.com/yourbase/yb/config"
. "github.com/yourbase/yb/plumbing"
"github.com/yourbase/yb/plumbing/log"
"github.com/yourbase/yb/workspace"
)
Expand Down Expand Up @@ -52,28 +49,28 @@ func (b *BuildCmd) SetFlags(f *flag.FlagSet) {
func (b *BuildCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
startTime := time.Now()

if InsideTheMatrix() {
log.StartSection("BUILD CONTAINER", "CONTAINER")
} else {
log.StartSection("BUILD HOST", "HOST")
}
gi := goInfo.GetInfo()
gi.VarDump()
log.EndSection()

log.StartSection("BUILD PACKAGE SETUP", "SETUP")
log.Infof("Build started at %s", startTime.Format(TIME_FORMAT))

targetPackage, err := GetTargetPackage()
ws, err := workspace.LoadWorkspace()
if err != nil {
log.Errorf("%v", err)
log.Errorf("Error loading workspace: %v", err)
return subcommands.ExitFailure
}
// Determine build target
buildTargetName := "default"

var pkg workspace.Package
if len(f.Args()) > 0 {
buildTargetName = f.Args()[0]
pkgName := f.Arg(0)
pkg, err = ws.PackageByName(pkgName)
if err != nil {
log.Errorf("Unable to find package named %s: %v", pkgName, err)
return subcommands.ExitFailure
}
} else {
pkg, err = ws.TargetPackage()
if err != nil {
log.Errorf("Unable to find default package: %v", err)
return subcommands.ExitFailure
}
}

var targetTimers []workspace.TargetTimer
Expand All @@ -83,14 +80,14 @@ func (b *BuildCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{})
CleanBuild: b.CleanBuild,
ExecPrefix: b.ExecPrefix,
}
stepTimers, buildError := targetPackage.BuildTarget(buildTargetName, buildFlags)
stepTimers, buildError := pkg.Build(buildFlags)

if err != nil {
log.Errorf("Failed to build target package: %v\n", err)
return subcommands.ExitFailure
}

targetTimers = append(targetTimers, workspace.TargetTimer{Name: buildTargetName, Timers: stepTimers})
targetTimers = append(targetTimers, workspace.TargetTimer{Name: pkg.Name, Timers: stepTimers})

endTime := time.Now()
buildTime := endTime.Sub(startTime)
Expand Down
47 changes: 20 additions & 27 deletions cli/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,9 @@ package cli
import (
"context"
"flag"
"fmt"
"os"
"os/signal"
"syscall"

"github.com/johnewart/subcommands"

"github.com/yourbase/yb/plumbing/log"
"github.com/yourbase/yb/workspace"
)

type ExecCmd struct {
Expand All @@ -37,36 +32,34 @@ Executing the target involves:
*/
func (b *ExecCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {

targetPackage, err := GetTargetPackage()
ws, err := workspace.LoadWorkspace()
if err != nil {
log.Errorf("%v", err)
log.Errorf("Error loading workspace: %v", err)
return subcommands.ExitFailure
}

log.ActiveSection("Dependencies")
if _, err := targetPackage.SetupRuntimeDependencies(); err != nil {
log.Infof("Couldn't configure dependencies: %v\n", err)
return subcommands.ExitFailure
var pkg workspace.Package
if len(f.Args()) > 0 {
pkgName := f.Arg(0)
pkg, err = ws.PackageByName(pkgName)
if err != nil {
log.Errorf("Unable to find package named %s: %v", pkgName, err)
return subcommands.ExitFailure
}
} else {
pkg, err = ws.TargetPackage()
if err != nil {
log.Errorf("Unable to find default package: %v", err)
return subcommands.ExitFailure
}
}

execRuntime, err := targetPackage.ExecutionRuntime(b.environment)

c := make(chan os.Signal, 2)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
fmt.Println("\r- Ctrl+C pressed in Terminal")
execRuntime.Shutdown()
os.Exit(0)
}()

log.Infof("Executing package '%s'...\n", targetPackage.Name)

err = targetPackage.Execute(execRuntime)
err = ws.ExecutePackage(pkg)
if err != nil {
log.Errorf("Unable to run command: %v", err)
log.Errorf("Unable to run '%s': %v", pkg.Name, err)
return subcommands.ExitFailure
}


return subcommands.ExitSuccess
}
7 changes: 3 additions & 4 deletions cli/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ 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"
Expand All @@ -24,7 +23,7 @@ type PackageCmd struct {
func (*PackageCmd) Name() string { return "package" }
func (*PackageCmd) Synopsis() string { return "Create a package artifact" }
func (*PackageCmd) Usage() string {
return ``
return `package [--target pkg]`
}

func (p *PackageCmd) SetFlags(f *flag.FlagSet) {
Expand Down
46 changes: 13 additions & 33 deletions cli/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import (
"context"
"flag"
"fmt"
"github.com/yourbase/yb/workspace"
"strings"

"github.com/johnewart/subcommands"
"github.com/yourbase/yb/plumbing/log"
"github.com/yourbase/yb/runtime"
//"path/filepath"
)

Expand Down Expand Up @@ -40,52 +40,32 @@ func (b *RunCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) s
return subcommands.ExitFailure
}

targetPackage, err := GetTargetPackage()
ws, err := workspace.LoadWorkspace()
if err != nil {
log.Errorf("%v", err)
log.Errorf("Error loading workspace: %v", err)
return subcommands.ExitFailure
}

runtimeEnv, err := targetPackage.ExecutionRuntime("default")

if err != nil {
log.Errorf("%v", err)
return subcommands.ExitFailure
}

argList := f.Args()

runInTarget := false
runtimeTarget := "default"
argList := f.Args()
workDir := "/workspace"

if strings.HasPrefix(argList[0], "@") {
runtimeTarget = argList[0][1:]
parts := strings.Split(runtimeTarget, ":")
if len(parts) == 2 {
workDir = parts[1]
runtimeTarget = parts[0]
}
argList = argList[1:]
runInTarget = true
}

cmdString := strings.Join(argList, " ")
workDir := "/workspace"

if runInTarget {
workDir = "/"
if runErr := ws.RunInTarget(cmdString, workDir, runtimeTarget); runErr != nil {
log.Errorf("Unable to run command: %v", runErr)
return subcommands.ExitFailure
}

log.Infof("Running %s in %s from %s", cmdString, runtimeTarget, workDir)

p := runtime.Process{Command: cmdString, Interactive: true, Directory: workDir}

if runInTarget {
if err := runtimeEnv.RunInTarget(p, runtimeTarget); err != nil {
log.Errorf("%v", err)
return subcommands.ExitFailure
}
} else {
if err := runtimeEnv.Run(p); err != nil {
log.Errorf("%v", err)
return subcommands.ExitFailure
}
}

return subcommands.ExitSuccess
}
95 changes: 69 additions & 26 deletions cli/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,17 @@ import (
"context"
"flag"
"fmt"
"github.com/johnewart/subcommands"
"gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing"
"gopkg.in/src-d/go-git.v4/plumbing/transport/http"
"io/ioutil"
"os"
"path/filepath"
"strings"

util "github.com/yourbase/yb/plumbing"
"github.com/johnewart/subcommands"
"gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing"
"gopkg.in/src-d/go-git.v4/plumbing/transport/http"

"github.com/yourbase/yb/plumbing/log"
ybtypes "github.com/yourbase/yb/types"
. "github.com/yourbase/yb/workspace"
)

Expand All @@ -37,10 +36,68 @@ func (w *WorkspaceCmd) Execute(ctx context.Context, f *flag.FlagSet, _ ...interf
cmdr.Register(&workspaceAddCmd{}, "")
cmdr.Register(&workspaceTargetCmd{}, "")
cmdr.Register(&workspaceLocationCmd{}, "")
cmdr.Register(&workspaceListCmd{}, "")
return (cmdr.Execute(ctx))
//return subcommands.ExitFailure
}

// LOCATION
type workspaceListCmd struct{}

func (*workspaceListCmd) Name() string { return "ls" }
func (*workspaceListCmd) Synopsis() string { return "List packages in workspace" }
func (*workspaceListCmd) Usage() string {
return `ls`
}

func (w *workspaceListCmd) SetFlags(f *flag.FlagSet) {
}

func (w *workspaceListCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
ws, err := LoadWorkspace()

if err != nil {
log.Errorf("No package here, and no workspace, nothing to do!")
return subcommands.ExitFailure
}

pkgs := ws.PackageList()

fmt.Printf("Packages in workspace:\n")
for _, pkg := range pkgs {
fmt.Printf(" * %s\n", pkg.Name)
}
fmt.Println()

fmt.Println("Containers in workspace:")
if containers, err := ws.RunningContainers(); err != nil {
log.Warnf("Unable to get running containers: %v", err)
} else {

for _, c := range containers {

running, err := c.Container.IsRunning()
if err != nil {
log.Warnf("Unable to determine if %s is running: %v", c.Container.Name, err)
}

status := "idle"
if running {
if address, err := c.Container.IPv4Address(); err == nil {
status = fmt.Sprintf("up @ %s", address)
} else {
status = "up @ unknown ip"
}
}


fmt.Printf(" * %s (%s)", c.Container.Name, status)
}
}

return subcommands.ExitSuccess
}

// LOCATION
type workspaceLocationCmd struct{}

Expand All @@ -54,28 +111,14 @@ func (w *workspaceLocationCmd) SetFlags(f *flag.FlagSet) {
}

func (w *workspaceLocationCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
// check if we're just a package
if util.PathExists(ybtypes.MANIFEST_FILE) {
currentPath, _ := filepath.Abs(".")
_, pkgName := filepath.Split(currentPath)
pkg, err := LoadPackage(pkgName, currentPath)
if err != nil {
log.Errorf("Error loading package '%s': %v", pkgName, err)
return subcommands.ExitFailure
}

log.Infoln(pkg.BuildRoot())
return subcommands.ExitSuccess
} else {
ws, err := LoadWorkspace()
ws, err := LoadWorkspace()

if err != nil {
log.Errorf("No package here, and no workspace, nothing to do!")
return subcommands.ExitFailure
}
fmt.Println(ws.Root()) // No logging used, because this can be used by scripts
return subcommands.ExitSuccess
if err != nil {
log.Errorf("No package here, and no workspace, nothing to do!")
return subcommands.ExitFailure
}
fmt.Println(ws.Root()) // No logging used, because this can be used by scripts
return subcommands.ExitSuccess
}

// CREATION
Expand Down
Loading

0 comments on commit be189e7

Please sign in to comment.