tilt icon indicating copy to clipboard operation
tilt copied to clipboard

Tilt Lifecycle Hooks

Open jazzdan opened this issue 7 years ago • 18 comments

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’t using (so they still build the image, and Tilt complains in to the void that there’s an image that’s not used in any resource) but Tilt never removes the resource from their cluster so it’s 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.

jazzdan avatar May 31 '19 13:05 jazzdan

+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

maiamcc avatar Jun 13 '19 14:06 maiamcc

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

nicks avatar Jun 18 '19 15:06 nicks

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)

nicks avatar Jun 20 '19 15:06 nicks

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 avatar Jun 20 '19 16:06 JimPatterson

@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

nicks avatar Jun 20 '19 17:06 nicks

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

joeblubaugh avatar Aug 01 '19 17:08 joeblubaugh

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.

npuc85 avatar Sep 09 '19 20:09 npuc85

👍 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

rally25rs avatar Oct 28 '19 15:10 rally25rs

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

bitsofinfo avatar Feb 28 '20 19:02 bitsofinfo

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()

andymartin-sch avatar Feb 28 '20 20:02 andymartin-sch

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

bitsofinfo avatar Feb 28 '20 21:02 bitsofinfo

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

andymartin-sch avatar Feb 28 '20 21:02 andymartin-sch

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"

bitsofinfo avatar Feb 28 '20 21:02 bitsofinfo

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.

bitsofinfo avatar Feb 28 '20 21:02 bitsofinfo

Yes please. 👍

esatterwhite avatar Jun 23 '20 15:06 esatterwhite

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.

djcp avatar Jun 30 '20 18:06 djcp

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.

landism avatar Jul 01 '20 20:07 landism

for anyone passing by here and not having a clear idea how to use the solution, you simply need to do:

if config.tilt_subcommand == 'down':
  ...

rotty3000 avatar Sep 07 '22 17:09 rotty3000