Skip to content

Commit

Permalink
tiltfile: add echo_off to live update run (#6351)
Browse files Browse the repository at this point in the history
* tiltfile: add echo_off to live update run

Signed-off-by: Muriel Picone Farias <m.picone.farias@catawiki.nl>

* Add files generated by codegen

Signed-off-by: Muriel Picone Farias <m.picone.farias@catawiki.nl>

---------

Signed-off-by: Muriel Picone Farias <m.picone.farias@catawiki.nl>
  • Loading branch information
muripic authored Apr 11, 2024
1 parent 50a7b5f commit 05beffd
Show file tree
Hide file tree
Showing 11 changed files with 699 additions and 604 deletions.
4 changes: 3 additions & 1 deletion internal/containerupdate/docker_container_updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ func (cu *DockerUpdater) UpdateContainer(ctx context.Context, cInfo liveupdates.

// Exec run's on container
for i, cmd := range cmds {
l.Infof("[CMD %d/%d] %s", i+1, len(cmds), strings.Join(cmd.Argv, " "))
if !cmd.EchoOff {
l.Infof("[CMD %d/%d] %s", i+1, len(cmds), strings.Join(cmd.Argv, " "))
}
err = cu.dCli.ExecInContainer(ctx, cInfo.ContainerID, cmd, nil, l.Writer(logger.InfoLvl))
if err != nil {
return fmt.Errorf(
Expand Down
4 changes: 3 additions & 1 deletion internal/containerupdate/exec_updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ func (cu *ExecUpdater) UpdateContainer(ctx context.Context, cInfo liveupdates.Co

// run commands
for i, c := range cmds {
l.Infof("[CMD %d/%d] %s", i+1, len(cmds), strings.Join(c.Argv, " "))
if !c.EchoOff {
l.Infof("[CMD %d/%d] %s", i+1, len(cmds), strings.Join(c.Argv, " "))
}
err := cu.kCli.Exec(ctx, cInfo.PodID, cInfo.ContainerName, cInfo.Namespace,
c.Argv, nil, w, w)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion internal/controllers/apis/liveupdate/liveupdate.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ func RunSteps(spec v1alpha1.LiveUpdateSpec) []model.Run {
for _, exec := range spec.Execs {
runs = append(runs, model.Run{
Cmd: model.Cmd{
Argv: exec.Args,
Argv: exec.Args,
EchoOff: exec.EchoOff,
},
Triggers: model.NewPathSet(exec.TriggerPaths, spec.BasePath),
})
Expand Down
4 changes: 3 additions & 1 deletion internal/tiltfile/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ def sync(local_path: str, remote_path: str) -> LiveUpdateStep:
"""
pass

def run(cmd: Union[str, List[str]], trigger: Union[List[str], str] = []) -> LiveUpdateStep:
def run(cmd: Union[str, List[str]], trigger: Union[List[str], str] = [], echo_off: bool = False) -> LiveUpdateStep:
"""Specify that the given `cmd` should be executed when updating an image's container
May not precede any `sync` steps in a `live_update`.
Expand All @@ -160,6 +160,8 @@ def run(cmd: Union[str, List[str]], trigger: Union[List[str], str] = []) -> Live
as program name and args.
trigger: If the ``trigger`` argument is specified, the build step is only run when there are changes to the given file(s). Paths relative to Tiltfile. (Note that in addition to matching the trigger, file changes must also match at least one of this Live Update's syncs in order to trigger this run. File changes that do not match any syncs will be ignored.)
bool: If ``echo_off`` is set to ``True``, the command's output will not be echoed to the Tilt UI.
"""
pass

Expand Down
7 changes: 6 additions & 1 deletion internal/tiltfile/live_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ func (l liveUpdateSyncStep) declarationPos() string { return l.position.String()
type liveUpdateRunStep struct {
command model.Cmd
triggers []string
echoOff bool
position syntax.Position
}

Expand Down Expand Up @@ -164,9 +165,11 @@ func (s *tiltfileState) liveUpdateSync(thread *starlark.Thread, fn *starlark.Bui
func (s *tiltfileState) liveUpdateRun(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var commandVal starlark.Value
var triggers starlark.Value
echoOff := false
if err := s.unpackArgs(fn.Name(), args, kwargs,
"cmd", &commandVal,
"trigger?", &triggers); err != nil {
"trigger?", &triggers,
"echo_off", &echoOff); err != nil {
return nil, err
}

Expand All @@ -189,6 +192,7 @@ func (s *tiltfileState) liveUpdateRun(thread *starlark.Thread, fn *starlark.Buil
ret := liveUpdateRunStep{
command: command,
triggers: triggerStrings,
echoOff: echoOff,
position: thread.CallFrame(1).Pos,
}
s.recordLiveUpdateStep(ret)
Expand Down Expand Up @@ -277,6 +281,7 @@ func (s *tiltfileState) liveUpdateFromSteps(t *starlark.Thread, maybeSteps starl
spec.Execs = append(spec.Execs, v1alpha1.LiveUpdateExec{
Args: x.command.Argv,
TriggerPaths: x.triggers,
EchoOff: x.echoOff,
})

case liveUpdateRestartContainerStep:
Expand Down
45 changes: 43 additions & 2 deletions internal/tiltfile/live_update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func TestLiveUpdateNonStringInRunTriggers(t *testing.T) {
k8s_yaml('foo.yaml')
docker_build('gcr.io/foo', 'foo',
live_update=[
run('bar', [4]),
run('bar', trigger=[4]),
]
)`)
f.loadErrString("run", "triggers", "'bar'", "contained value '4' of type 'int'. it may only contain strings")
Expand Down Expand Up @@ -275,6 +275,46 @@ k8s_yaml('foo.yaml')
}
}

func TestLiveUpdateRunEchoOff(t *testing.T) {
for _, tc := range []struct {
name string
tiltfileText string
expectedValue bool
}{
{"echoOff True", `echo_off=True`, true},
{"echoOff False", `echo_off=False`, false},
{"echoOff default", ``, false},
} {
t.Run(tc.name, func(t *testing.T) {
f := newFixture(t)

f.gitInit("")
f.yaml("foo.yaml", deployment("foo", image("gcr.io/image-a")))
f.file("imageA.dockerfile", `FROM golang:1.10`)
f.file("Tiltfile", fmt.Sprintf(`
docker_build('gcr.io/image-a', 'a', dockerfile='imageA.dockerfile',
live_update=[
run("echo hi", %s)
])
k8s_yaml('foo.yaml')
`, tc.tiltfileText))
f.load()

lu := v1alpha1.LiveUpdateSpec{
BasePath: f.Path(),
Execs: []v1alpha1.LiveUpdateExec{
{
Args: []string{"sh", "-c", "echo hi"},
EchoOff: tc.expectedValue,
},
},
}
f.assertNextManifest("foo",
db(image("gcr.io/image-a"), lu))
})
}
}

func TestLiveUpdateFallBackTriggersOutsideOfDockerBuildContext(t *testing.T) {
f := newFixture(t)

Expand Down Expand Up @@ -420,7 +460,7 @@ func (f *liveUpdateFixture) init() {
luSteps := `[
fall_back_on(['foo/i', 'foo/j']),
sync('foo/b', '/c'),
run('f', ['g', 'h']),
run('f', trigger=['g', 'h']),
]`
codeToInsert := fmt.Sprintf(f.tiltfileCode, luSteps)

Expand Down Expand Up @@ -450,6 +490,7 @@ func newLiveUpdateFixture(t *testing.T) *liveUpdateFixture {
v1alpha1.LiveUpdateExec{
Args: []string{"sh", "-c", "f"},
TriggerPaths: []string{"g", "h"},
EchoOff: false,
},
},
}
Expand Down
Loading

0 comments on commit 05beffd

Please sign in to comment.