Tilt: Tilt Lifecycle Hooks

Created on 31 May 2019  路  17Comments  路  Source: tilt-dev/tilt

There are two use case we've seen that could be solved by Tilt "lifecycle hooks", or bits of code that only run on tilt up or only run on tilt down.

One is someone who has a problem where they use Helm to disable resources that they aren鈥檛 using (so they still build the image, and Tilt complains in to the void that there鈥檚 an image that鈥檚 not used in any resource) but Tilt never _removes_ the resource from their cluster so it鈥檚 still sitting there taking up resources.

Another is that they also have a command in their Tiltfile that tells Helm to deploy persistent resources which are required by our core application, but not managed by Tilt. But what's annoying is that the helm upgrade command also runs on a Tilt down. It doesn't cause functionality problems, since Helm does some checks and decides to do nothing, but it adds time and is unnecessary.

It also might be desirable to have Tilt automatically run the code in the down hook whenever Tilt is exited.

enhancement

Most helpful comment

I wonder if we can so something simple here, like have a process.command variable, so you can do

if process.command == "up":
  # do things

then see how people use it

All 17 comments

+1 for this: I was just iterating on the tilt docs, the Tiltfile runs make api whenever it's executed, which we do NOT have to do when running tilt down

I wonder if we can so something simple here, like have a process.command variable, so you can do

if process.command == "up":
  # do things

then see how people use it

It's also worth pointing out that we've got this request like half a dozen times, and almost every one has been "I want to install a remote helm chart but the 'helm install' life-cycle doesn't make play well with Tilt's life-cycle" (the obvious exception is the make api in tilt.build)

which makes me wonder if there should be a separate spec for better helm primitives (see also the Tiltfile in: https://github.com/windmilleng/tilt/issues/1650)

If it helps, I have faked the process.command thing for use on my team. We use a script called start to start the app, this script ends up just running tilt up, but right before that, it does:

trap 'SHUTTING_DOWN=true tilt down' EXIT

There are some slow tasks that the Tiltfile calls and we check the environment variable SHUTTING_DOWN before hand.

@JimPatterson oh ya, good points!

the "I want Tilt to shutdown all my services on exit" is an important use case

i also like the idea of exposing more of things in env variables as an even quicker fix while we hammer out a Tiltfile api

Yes, this would be great as an optional entry in the tiltfile. Especially when developing locally, I'd appreciate an exit hook.

I have a use case for this that is not installing helm charts. I have a file that I want to create a configmap from and I cannot use k8s_yaml() to apply a resource file containing the configmap due to a size limitation of 256k on the annotations created by the kubectl apply command. My configmap can be created by calling local('kubectl create configmap ...') because the create command does not generate the same annotations that the apply command does, but then tilt down fails because the config map already exists.

i would need something like

if process.command == "up":
  local('kubectl create configmap ...')
if process.command == "down":
  local('kubectl delete configmap ...')

But also, if I put a watch on the file, I would want the delete and create to run again when the file changes. I'm not sure how this would play with that.

馃憤 for this.

My use case is that when my cluster comes up, I have to do things like create and seed my DB with kubectl exec some-rails-pod -- bundle exec rake db:migrate db:seed

and I don't need these to run on tilt down

Having the ability to either pass local([up|down],cmd) or some other way to qualify what gets run when is most definitely needed.

if on linux, a workaround until this gets implemented is to extract the command line arguments with something like:

command = str(local('cat /proc/$PPID/cmdline')).strip()

i don't think osx has a /proc fs, anyone have an osx version of that

will be a little more messy, but something built on local('ps -p $PPID') should generally work on macOS

thanks! for anyone else

myos = str(local('echo $OSTYPE')).strip()
tiltMode = "up"

if "darwin" in myos:
    if "tilt down" in str(local('ps -p $PPID')).strip():
        tiltMode = "down"
else:
    if "tilt down" in str(local('cat /proc/$PPID/cmdline')).strip():
        tiltMode = "down"

all we need is some variable that tells us the reason for why the code is running (i.e. resource changed (and what)) and we open up a world of possibilities.

Yes please. 馃憤

This should be:

myos = str(local('echo $OSTYPE')).strip()
tiltMode = "up"

if "darwin" in myos:
    if "tilt down" in str(local('ps -p $PPID')).strip():
        tiltMode = "down"
else:
    if "tilt down" in str(local("tr '\\0' ' ' < /proc/$PPID/cmdline")).strip():
        tiltMode = "down"

because /proc/PID/cmdline is separated with null byte characters.

The next release of Tilt (probably 0.15.2) adds config.tilt_subcommand:
https://docs.tilt.dev/api.html#modules.config.tilt_subcommand

If you have other use cases not covered by this, please open a new GitHub issue or talk to us on slack.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

majelbstoat picture majelbstoat  路  4Comments

bandrade picture bandrade  路  5Comments

matthiasak picture matthiasak  路  4Comments

jazzdan picture jazzdan  路  3Comments

ivansantosz picture ivansantosz  路  7Comments