From add1d31c06344bdd08f0e2d95a5f5649cd6a9929 Mon Sep 17 00:00:00 2001 From: Mathias Fredriksson Date: Wed, 18 Jan 2023 13:10:45 +0000 Subject: [PATCH 1/3] feat: Add `startup_script_timeout` and `allow_login_before_ready` Ref: https://github.com/coder/coder/issues/5749 --- docs/resources/agent.md | 6 ++++-- provider/agent.go | 23 +++++++++++++++++++---- provider/agent_test.go | 4 ++++ 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/docs/resources/agent.md b/docs/resources/agent.md index a064585c..5cd87ee2 100644 --- a/docs/resources/agent.md +++ b/docs/resources/agent.md @@ -46,13 +46,15 @@ resource "kubernetes_pod" "dev" { ### Optional +- `allow_login_before_ready` (Boolean) Allow users to login to the workspace before the agent is ready. Note that, when enabled, the agent could still be executing the startup script and the workspace in an incomplete state when logging in. - `auth` (String) The authentication type the agent will use. Must be one of: "token", "google-instance-identity", "aws-instance-identity", "azure-instance-identity". - `connection_timeout` (Number) Time in seconds until the agent is marked as timed out when a connection with the server cannot be established. A value of zero never marks the agent as timed out. - `dir` (String) The starting directory when a user creates a shell session. Defaults to $HOME. - `env` (Map of String) A mapping of environment variables to set inside the workspace. - `motd_file` (String) The path to a file within the workspace containing a message to display to users when they login via SSH. A typical value would be /etc/motd. -- `shutdown_script` (String) A script to run before the agent is stopped. -- `startup_script` (String) A script to run after the agent starts. +- `shutdown_script` (String) A script to run before the agent is stopped. The script should exit when it is done to signal that the workspace can be stopped. +- `startup_script` (String) A script to run after the agent starts. The script should exit when it is done to signal that the agent is ready to accept connections. +- `startup_script_timeout` (Number) Time in seconds until the agent ready status is marked as timed out, this happens when the startup script has not completed (exited) in the given time. - `troubleshooting_url` (String) A URL to a document with instructions for troubleshooting problems with the agent. ### Read-Only diff --git a/provider/agent.go b/provider/agent.go index 0216d8dd..3f4c8f3e 100644 --- a/provider/agent.go +++ b/provider/agent.go @@ -77,10 +77,24 @@ func agentResource() *schema.Resource { }, "startup_script": { ForceNew: true, - Description: "A script to run after the agent starts.", + Description: "A script to run after the agent starts. The script should exit when it is done to signal that the agent is ready to accept connections.", Type: schema.TypeString, Optional: true, }, + "startup_script_timeout": { + Type: schema.TypeInt, + Default: 300, + ForceNew: true, + Optional: true, + Description: "Time in seconds until the agent ready status is marked as timed out, this happens when the startup script has not completed (exited) in the given time.", + ValidateFunc: validation.IntAtLeast(1), + }, + "shutdown_script": { + Type: schema.TypeString, + ForceNew: true, + Optional: true, + Description: "A script to run before the agent is stopped. The script should exit when it is done to signal that the workspace can be stopped.", + }, "token": { ForceNew: true, Sensitive: true, @@ -108,11 +122,12 @@ func agentResource() *schema.Resource { Optional: true, Description: "The path to a file within the workspace containing a message to display to users when they login via SSH. A typical value would be /etc/motd.", }, - "shutdown_script": { + "allow_login_before_ready": { + Type: schema.TypeBool, + Default: true, // TODO(mafredri): Change default to false in a future version. ForceNew: true, - Description: "A script to run before the agent is stopped.", - Type: schema.TypeString, Optional: true, + Description: "Allow users to login to the workspace before the agent is ready. Note that, when enabled, the agent could still be executing the startup script and the workspace in an incomplete state when logging in.", }, }, } diff --git a/provider/agent_test.go b/provider/agent_test.go index ae3be7af..26c6c89d 100644 --- a/provider/agent_test.go +++ b/provider/agent_test.go @@ -31,9 +31,11 @@ func TestAgent(t *testing.T) { hi = "test" } startup_script = "echo test" + startup_script_timeout = 120 troubleshooting_url = "https://example.com/troubleshoot" motd_file = "/etc/motd" shutdown_script = "echo bye bye" + allow_login_before_ready = false } `, Check: func(state *terraform.State) error { @@ -49,10 +51,12 @@ func TestAgent(t *testing.T) { "dir", "env.hi", "startup_script", + "startup_script_timeout", "connection_timeout", "troubleshooting_url", "motd_file", "shutdown_script", + "allow_login_before_ready", } { value := resource.Primary.Attributes[key] t.Logf("%q = %q", key, value) From 163bba0ea2ce5e88695a906dc9e2bc3e457ee68e Mon Sep 17 00:00:00 2001 From: Mathias Fredriksson Date: Thu, 19 Jan 2023 12:16:24 +0000 Subject: [PATCH 2/3] Rename `allow_login_before_ready` to `delay_login_until_ready` --- provider/agent.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/provider/agent.go b/provider/agent.go index 3f4c8f3e..5d4af4f1 100644 --- a/provider/agent.go +++ b/provider/agent.go @@ -77,7 +77,7 @@ func agentResource() *schema.Resource { }, "startup_script": { ForceNew: true, - Description: "A script to run after the agent starts. The script should exit when it is done to signal that the agent is ready to accept connections.", + Description: "A script to run after the agent starts. The script should exit when it is done to signal that the agent is ready.", Type: schema.TypeString, Optional: true, }, @@ -122,12 +122,12 @@ func agentResource() *schema.Resource { Optional: true, Description: "The path to a file within the workspace containing a message to display to users when they login via SSH. A typical value would be /etc/motd.", }, - "allow_login_before_ready": { + "delay_login_until_ready": { Type: schema.TypeBool, - Default: true, // TODO(mafredri): Change default to false in a future version. + Default: false, // Change default value to true in a future release. ForceNew: true, Optional: true, - Description: "Allow users to login to the workspace before the agent is ready. Note that, when enabled, the agent could still be executing the startup script and the workspace in an incomplete state when logging in.", + Description: "This option defines whether or not user logins to the workspace agent are delayed until the agent is ready. When disabled, users may see an incomplete workspace upon logging in.", }, }, } From ebc4d6a05219ad287e69aa42d310d471d2ce64c2 Mon Sep 17 00:00:00 2001 From: Mathias Fredriksson Date: Thu, 19 Jan 2023 12:55:56 +0000 Subject: [PATCH 3/3] fixup! Rename `allow_login_before_ready` to `delay_login_until_ready` --- docs/resources/agent.md | 4 ++-- provider/agent_test.go | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/resources/agent.md b/docs/resources/agent.md index 5cd87ee2..1415cc04 100644 --- a/docs/resources/agent.md +++ b/docs/resources/agent.md @@ -46,14 +46,14 @@ resource "kubernetes_pod" "dev" { ### Optional -- `allow_login_before_ready` (Boolean) Allow users to login to the workspace before the agent is ready. Note that, when enabled, the agent could still be executing the startup script and the workspace in an incomplete state when logging in. - `auth` (String) The authentication type the agent will use. Must be one of: "token", "google-instance-identity", "aws-instance-identity", "azure-instance-identity". - `connection_timeout` (Number) Time in seconds until the agent is marked as timed out when a connection with the server cannot be established. A value of zero never marks the agent as timed out. +- `delay_login_until_ready` (Boolean) This option defines whether or not user logins to the workspace agent are delayed until the agent is ready. When disabled, users may see an incomplete workspace upon logging in. - `dir` (String) The starting directory when a user creates a shell session. Defaults to $HOME. - `env` (Map of String) A mapping of environment variables to set inside the workspace. - `motd_file` (String) The path to a file within the workspace containing a message to display to users when they login via SSH. A typical value would be /etc/motd. - `shutdown_script` (String) A script to run before the agent is stopped. The script should exit when it is done to signal that the workspace can be stopped. -- `startup_script` (String) A script to run after the agent starts. The script should exit when it is done to signal that the agent is ready to accept connections. +- `startup_script` (String) A script to run after the agent starts. The script should exit when it is done to signal that the agent is ready. - `startup_script_timeout` (Number) Time in seconds until the agent ready status is marked as timed out, this happens when the startup script has not completed (exited) in the given time. - `troubleshooting_url` (String) A URL to a document with instructions for troubleshooting problems with the agent. diff --git a/provider/agent_test.go b/provider/agent_test.go index 26c6c89d..9efa15bf 100644 --- a/provider/agent_test.go +++ b/provider/agent_test.go @@ -35,7 +35,7 @@ func TestAgent(t *testing.T) { troubleshooting_url = "https://example.com/troubleshoot" motd_file = "/etc/motd" shutdown_script = "echo bye bye" - allow_login_before_ready = false + delay_login_until_ready = false } `, Check: func(state *terraform.State) error { @@ -56,7 +56,7 @@ func TestAgent(t *testing.T) { "troubleshooting_url", "motd_file", "shutdown_script", - "allow_login_before_ready", + "delay_login_until_ready", } { value := resource.Primary.Attributes[key] t.Logf("%q = %q", key, value)