Skip to content

Commit

Permalink
run function (#38)
Browse files Browse the repository at this point in the history
* run command

* fix run location
  • Loading branch information
maxmcd authored Oct 9, 2021
1 parent 38541df commit 9cf1d9a
Show file tree
Hide file tree
Showing 16 changed files with 238 additions and 55 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ go_test:
go test -race -v ./...

install:
go mod tidy
go install

build: install
Expand Down
1 change: 1 addition & 0 deletions bramble.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
[module]
name = "github.com/maxmcd/bramble"
version = "0.0.2"
# default_read_only_paths?
read_only_paths = ["./"]
hidden_paths = ["./lib"]

Expand Down
10 changes: 10 additions & 0 deletions default.bramble
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
load("github.com/maxmcd/bramble/tests/simple/simple")
load(nix_seed="github.com/maxmcd/bramble/lib/nix-seed")


def print_simple():
return run(simple.simple(), "simple", hidden_paths=["/"])


def bash():
return run(nix_seed.stdenv(), "bash", read_only_paths=["./"])
2 changes: 0 additions & 2 deletions pkg/chunkedarchive/chunkedarchive.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,6 @@ func Archive(location string, bw BodyWriter) (toc []TOCEntry, err error) {
if err != nil {
return fmt.Errorf("%s: opening: %w", path, err)
}
fmt.Println("file.NewChunk")
promise, err := bw.NewChunk(file)
if err != nil {
return fmt.Errorf("%s: reading: %w", path, err)
Expand All @@ -173,7 +172,6 @@ func Archive(location string, bw BodyWriter) (toc []TOCEntry, err error) {
}
for _, ep := range queue {
if ep.promise != nil {
fmt.Println("reap promise")
ep.entry.Body, err = ep.promise()
if err != nil {
return nil, err
Expand Down
1 change: 0 additions & 1 deletion pkg/sandbox/sandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ func (ee ExitError) Error() string {

// Run runs the sandbox until execution has been completed
func (s Sandbox) Run(ctx context.Context) (err error) {
// fmt.Printf("%+v\n", s)
container, err := newContainer(s)
if err != nil {
return err
Expand Down
3 changes: 3 additions & 0 deletions pkg/starutil/conversions.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import (
)

func IterableToStringSlice(list starlark.Iterable) (out []string, err error) {
if list == nil {
return nil, nil
}
iterator := list.Iterate()
defer iterator.Done()
var val starlark.Value
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ A `builder` can be on of the default built-ins: `["fetch_url", "fetch_git", "der
The run function defines the attributes for running a program from a derivation output. If a call to a bramble function returns a run command that run command and parameters will be executed.

```python
run(derivation, args=[], paths=[], write_paths=[], hidden_paths=[], network=False)
run(derivation, cmd, args=[], paths=[], read_only_paths=[], hidden_paths=[], network=False)
```

#### test()
Expand Down
1 change: 0 additions & 1 deletion src/build/cache_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,6 @@ func (s *Store) CacheServer() http.Handler {
return unprocessable(errors.New("chunk size can't be larger than 4MB"))
}

fmt.Println("chunk", hash)
fmt.Fprint(c.ResponseWriter, hash)
return nil
}))
Expand Down
12 changes: 6 additions & 6 deletions src/build/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,12 @@ func (s *Store) checkForBuiltDerivationOutputs(drv Derivation) (outputs []Output
}

type RunDerivationOptions struct {
Args []string
Mounts []string

Stdin io.Reader
Dir string
Args []string
Network bool
Stdin io.Reader
Dir string

Mounts []string
HiddenPaths []string
ReadOnlyPaths []string
}
Expand All @@ -94,7 +94,7 @@ func (s *Store) RunDerivation(ctx context.Context, drv Derivation, opts RunDeriv
HiddenPaths: opts.HiddenPaths,
ReadOnlyPaths: opts.ReadOnlyPaths,

Network: false,
Network: opts.Network,
}
return sbx.Run(ctx)
}
Expand Down
53 changes: 52 additions & 1 deletion src/command/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"log"
"os"
"os/signal"
"path/filepath"
"strings"
"syscall"

Expand Down Expand Up @@ -133,12 +134,60 @@ bramble build ./tests
Name: "run",
Usage: "Run an executable in a derivation output",
UsageText: "bramble run [options] [module]:<function> [args...]",
Flags: []cli.Flag{
&cli.StringSliceFlag{
Name: "paths",
Usage: "paths that the process has access to, to pass multiple paths use this flag multiple times",
},
&cli.StringSliceFlag{
Name: "read_only_paths",
Usage: "paths the process can't write to, to pass multiple paths use this flag multiple times",
},
&cli.StringSliceFlag{
Name: "hidden_paths",
Usage: "paths that are hidden from the process, to pass multiple paths use this flag multiple times",
},
&cli.BoolFlag{
Name: "network",
Usage: "allow network access",
},
},
Action: func(c *cli.Context) error {
b, err := newBramble()
if err != nil {
return err
}
return b.run(c.Context, c.Args().Slice())
absoluteSlicePaths := func(v []string) error {
for i, p := range v {
a, err := filepath.Abs(p)
if err != nil {
return err
}
v[i] = a
}
return nil
}

paths := c.StringSlice("paths")
readOnlyPaths := c.StringSlice("read_only_paths")
hiddenPaths := c.StringSlice("hidden_paths")
network := c.Bool("network")
for _, err := range []error{
absoluteSlicePaths(paths),
absoluteSlicePaths(readOnlyPaths),
absoluteSlicePaths(hiddenPaths),
} {
if err != nil {
return err
}
}

return b.run(c.Context, c.Args().Slice(), runOptions{
paths: paths,
readOnlyPaths: readOnlyPaths,
hiddenPaths: hiddenPaths,
network: network,
})
},
},
{
Expand Down Expand Up @@ -255,6 +304,8 @@ their public functions with documentation. If an immediate subdirectory has a
c.Usage = formatFlag(c.Usage, longest)
case *cli.StringFlag:
c.Usage = formatFlag(c.Usage, longest)
case *cli.StringSliceFlag:
c.Usage = formatFlag(c.Usage, longest)
}
}
}
Expand Down
49 changes: 40 additions & 9 deletions src/command/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,17 @@ import (
"os"

"github.com/maxmcd/bramble/src/build"
project "github.com/maxmcd/bramble/src/project"
)

func (b bramble) run(ctx context.Context, args []string) (err error) {
type runOptions struct {
paths []string
readOnlyPaths []string
hiddenPaths []string
network bool
}

func (b bramble) run(ctx context.Context, args []string, ro runOptions) (err error) {
output, err := b.execModule(ctx, "run", args, execModuleOptions{})
if err != nil {
return
Expand All @@ -17,18 +25,41 @@ func (b bramble) run(ctx context.Context, args []string) (err error) {
if err != nil {
return err
}

var run *project.Run
if len(output.Run) > 1 {
return errors.New("multiple run commands, not sure how to proceed")
}
if len(output.Run) == 1 {
run = &output.Run[0]
}

if len(outputDerivations) != 1 {
return errors.New("can't run a starlark function if it doesn't return a single derivation")
}

// use args after the module location
args = args[1:]

if run != nil {
ro.paths = run.Paths
ro.readOnlyPaths = run.ReadOnlyPaths
ro.hiddenPaths = run.HiddenPaths
ro.network = run.Network
args = append([]string{run.Cmd}, run.Args...)
}
if len(ro.paths) == 0 {
ro.paths = []string{b.project.Location()}
}
return b.store.RunDerivation(ctx, outputDerivations[0], build.RunDerivationOptions{
// Stdin: io.MultiReader(os.Stdin),
Stdin: os.Stdin,
Args: args[1:],
Dir: b.project.WD(),
Mounts: []string{b.project.Location()},

HiddenPaths: b.project.HiddenPaths(),
ReadOnlyPaths: b.project.ReadOnlyPaths(),
Stdin: os.Stdin,
Args: args,
Dir: b.project.WD(),

Network: ro.network,

Mounts: ro.paths,
HiddenPaths: ro.hiddenPaths,
ReadOnlyPaths: ro.readOnlyPaths,
})
}
7 changes: 7 additions & 0 deletions src/project/exec_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type ExecModuleOutput struct {
AllDerivations map[string]Derivation
Globals []string
Tests map[string][]Test
Run []Run
}

func (p *Project) ExecModule(ctx context.Context, input ExecModuleInput) (output ExecModuleOutput, err error) {
Expand Down Expand Up @@ -86,6 +87,12 @@ func (p *Project) ExecModule(ctx context.Context, input ExecModuleInput) (output
if err != nil {
return output, errors.Wrap(err, "error running")
}

// Add calls to run() to the output
if run, ok := values.(Run); ok {
output.Run = append(output.Run, run)
output.Output[run.Derivation.hash()] = run.Derivation
}
// The function must return a single derivation or a list of derivations, or
// a tuple of derivations. We turn them into an array.
for _, d := range valuesToDerivations(values) {
Expand Down
1 change: 1 addition & 0 deletions src/project/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func newRuntime(workingDirectory, projectLocation, moduleName string) *runtime {
rt.predeclared = starlark.StringDict{
"derivation": derivation,
"test": starlark.NewBuiltin("test", rt.testBuiltin),
"run": starlark.NewBuiltin("run", rt.runBuiltin),
"assert": assertGlobals["assert"],
"sys": starlarkSys,
"files": starlark.NewBuiltin("files", filesBuiltin{
Expand Down
32 changes: 0 additions & 32 deletions src/project/test.go

This file was deleted.

Loading

0 comments on commit 9cf1d9a

Please sign in to comment.