How does the coder_script terraform resource work?
I have a script that I need to run when the workspace has stopped/deleted to delete some resources from another k8s cluster.
I add it as a resource to the template and set “run_on_stop” to true and set the agent id to the one of the workspace but when I stop/delete a workspace it doesn’t run. Am I missing something in the setup?
30 Replies
<#1210530942246985740>
Category
Help needed
Product
Coder OSS (v2)
Platform
Linux
Logs
Please post any relevant logs/error messages.
This only runs on stop transition.
i.e. when you press the stop button and not on deleting it.
Each start/stop/update/create are actually
terraform apply
with the difference of data.coder_workspace.me.start_count
being 0 for the stop case.
And the delete operation is terraform destroy
So it will just delete all resources.
You should be able to run a script at stop transition when you use run_at_stop = true
So I have the script with run_on_stop set to true and when I press stop on the workspace it doesn’t run. Also would there be a way to run a script before the deletion of a workspace?
For that I'd recommend using the
local-exec
resource instead.@Phorcys closed the thread.
I have the same use case; I need to manually stop some external processes on shutdown.
How can
local-exec
help in this case?hey @mingfang,
local-exec
runs a command on the provisioner (not within the workspace), you can use it to run a script on workspace deletion
you can do something like this
see https://developer.hashicorp.com/terraform/language/resources/provisioners/local-exec#example-usage for more info
let me know if you need anything else@Phorcys
thanks for the explanation.
for my use case I need the script to run on the workspace.
i was hoping that I can use
coder_script
https://registry.terraform.io/providers/coder/coder/latest/docs/resources/script#run_on_stop-1
but i have not been able to get a simple echo hello
to worksadly you can't have the script run on deletion
yoh can have it run on start or stop only
if a workspace is not started up and gets deleted, it only gets deleted and not started up, so your scripts can't run inside of it
may I as what the use-case is?
This is a good feature request, we can invetsigate to add to the provider.
Coul you open a feature request issue on GitHub?
is it technically feasible though?
a delete script won't be able to run inside the workspace because the workspace would have to be running for it to work
We can run it just before deleting.
Similar to how a script can run on stop.
Take for example the case of docker template where the compute(container) gets deleted on stop but can still run the script before getting destroyed.
Makes sense?
i know but what if the workspace is deleted when stopped
Sorry I was not being clear.
I'm trying to run a script on stop and not on delete.
I've tried these and they are not working.
and
Question is...why is this not working?
good question, could you send your full template over please?
My template is made up of several Terraform files.
https://github.com/mingfang/coder-templates/tree/master/catalog
The shutdow script is here https://github.com/mingfang/coder-templates/blob/master/catalog/main.tf#L108
The purpose of this template is to provide a catalog of services to try out.
The service should start and shutdown along of the workspace.
You can click on the link to the service(e.g. Kestra) from github to try it yourself.
GitHub
coder-templates/catalog at master · mingfang/coder-templates
Contribute to mingfang/coder-templates development by creating an account on GitHub.
GitHub
coder-templates/catalog/main.tf at master · mingfang/coder-templates
Contribute to mingfang/coder-templates development by creating an account on GitHub.
can you check the version of the
coder/coder
Terraform provider?
it should be in .terraform.lock.hcl
or maybe you can find it via the TF CLIhttps://github.com/mingfang/coder-templates/blob/master/catalog/providers.tf
I left it blank so it should be the "latest", correct?
GitHub
coder-templates/catalog/providers.tf at master · mingfang/coder-tem...
Contribute to mingfang/coder-templates development by creating an account on GitHub.
I saw this, if you do not have a
.terraform.lock.hcl
lockfile then yes, it'll be latestI don't have a lock file.
Here is the build log
thanks
this is indeed the latest
i will take a deeper look in a moment, this behavior from the provider is pretty weird
It turns out the shutdown script is running but it didn't get a chance to finish.
I'm guessing it is because the ssh server got killed before the script gets a chance to finish.
Here is the part of the agent log that shows the shutdown sequence.
oh yeah that makes sense
what if you add
start_blocks_login = true
to the coder_script
?that caused a validation error
should this https://github.com/coder/coder/blame/f6e990ed875f7e7f273b8a6f5c03c3475a7bb026/agent/agent.go#L1552
run before the ssh server shutdown here https://github.com/coder/coder/blame/f6e990ed875f7e7f273b8a6f5c03c3475a7bb026/agent/agent.go#L1537 ?
GitHub
Blaming coder/agent/agent.go at f6e990ed875f7e7f273b8a6f5c03c3475a7...
Provision remote development environments via Terraform - Blaming coder/agent/agent.go at f6e990ed875f7e7f273b8a6f5c03c3475a7bb026 · coder/coder
hey @mingfang, have you figured this out, or is this issue still happening?
it's still a problem. I think the coder above may be the problem;
i think the stop scripts should run before the ssh server is shutdown
@mingfang apologies for not getting back to you sooner, would you be able to open up an issue on our GitHub?
GitHub
Issues · coder/coder
Provision remote development environments via Terraform - Issues · coder/coder
make sure to mention the Coder version and your terraform provider version as well as the analysis you've provided