Skip to content

Commit e381576

Browse files
authored
Replace login_before_ready with startup_script_behavior (#125)
1 parent 547f1b6 commit e381576

File tree

3 files changed

+120
-11
lines changed

3 files changed

+120
-11
lines changed

docs/resources/agent.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,13 @@ resource "kubernetes_pod" "dev" {
5050
- `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.
5151
- `dir` (String) The starting directory when a user creates a shell session. Defaults to $HOME.
5252
- `env` (Map of String) A mapping of environment variables to set inside the workspace.
53-
- `login_before_ready` (Boolean) This option defines whether or not the user can (by default) login to the workspace before it is ready. Ready means that e.g. the startup_script is done and has exited. When enabled, users may see an incomplete workspace when logging in.
53+
- `login_before_ready` (Boolean, Deprecated) This option defines whether or not the user can (by default) login to the workspace before it is ready. Ready means that e.g. the startup_script is done and has exited. When enabled, users may see an incomplete workspace when logging in.
5454
- `metadata` (Block List) Each "metadata" block defines a single item consisting of a key/value pair. This feature is in alpha and may break in future releases. (see [below for nested schema](#nestedblock--metadata))
5555
- `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.
5656
- `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.
5757
- `shutdown_script_timeout` (Number) Time in seconds until the agent lifecycle status is marked as timed out during shutdown, this happens when the shutdown script has not completed (exited) in the given time.
5858
- `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.
59+
- `startup_script_behavior` (String) This option sets the behavior of the `startup_script`. When set to "blocking", the startup_script must exit before the workspace is ready. When set to "non-blocking", the startup_script may run in the background and the workspace will be ready immediately. Default is "non-blocking", although "blocking" is recommended.
5960
- `startup_script_timeout` (Number) Time in seconds until the agent lifecycle status is marked as timed out during start, this happens when the startup script has not completed (exited) in the given time.
6061
- `troubleshooting_url` (String) A URL to a document with instructions for troubleshooting problems with the agent.
6162

provider/agent.go

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
func agentResource() *schema.Resource {
1717
return &schema.Resource{
1818
Description: "Use this resource to associate an agent.",
19-
CreateContext: func(c context.Context, resourceData *schema.ResourceData, i interface{}) diag.Diagnostics {
19+
CreateContext: func(ctx context.Context, resourceData *schema.ResourceData, i interface{}) diag.Diagnostics {
2020
// This should be a real authentication token!
2121
resourceData.SetId(uuid.NewString())
2222
err := resourceData.Set("token", uuid.NewString())
@@ -25,14 +25,14 @@ func agentResource() *schema.Resource {
2525
}
2626
return updateInitScript(resourceData, i)
2727
},
28-
ReadWithoutTimeout: func(c context.Context, resourceData *schema.ResourceData, i interface{}) diag.Diagnostics {
28+
ReadWithoutTimeout: func(ctx context.Context, resourceData *schema.ResourceData, i interface{}) diag.Diagnostics {
2929
err := resourceData.Set("token", uuid.NewString())
3030
if err != nil {
3131
return diag.FromErr(err)
3232
}
3333
return updateInitScript(resourceData, i)
3434
},
35-
DeleteContext: func(c context.Context, rd *schema.ResourceData, i interface{}) diag.Diagnostics {
35+
DeleteContext: func(ctx context.Context, resourceData *schema.ResourceData, i interface{}) diag.Diagnostics {
3636
return nil
3737
},
3838
Schema: map[string]*schema.Schema{
@@ -131,11 +131,29 @@ func agentResource() *schema.Resource {
131131
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.",
132132
},
133133
"login_before_ready": {
134-
Type: schema.TypeBool,
135-
Default: true, // Change default value to false in a future release.
136-
ForceNew: true,
137-
Optional: true,
138-
Description: "This option defines whether or not the user can (by default) login to the workspace before it is ready. Ready means that e.g. the startup_script is done and has exited. When enabled, users may see an incomplete workspace when logging in.",
134+
// Note: When this is removed, "startup_script_behavior" should
135+
// be set to "non-blocking" by default (instead of empty string).
136+
Type: schema.TypeBool,
137+
Default: true,
138+
ForceNew: true,
139+
Optional: true,
140+
Description: "This option defines whether or not the user can (by default) login to the workspace before it is ready. Ready means that e.g. the startup_script is done and has exited. When enabled, users may see an incomplete workspace when logging in.",
141+
Deprecated: "Configure startup_script_behavior instead. This attribute will be removed in a future version of the provider.",
142+
ConflictsWith: []string{"startup_script_behavior"},
143+
},
144+
"startup_script_behavior": {
145+
// Note: Our default value is "non-blocking" but we do not set
146+
// it here because we want to be able to differentiate between
147+
// the user setting this or "login_before_ready". For all
148+
// intents and purposes, until deprecation, setting
149+
// "login_before_ready = false" is equivalent to setting
150+
// "startup_script_behavior = blocking".
151+
Type: schema.TypeString,
152+
ForceNew: true,
153+
Optional: true,
154+
Description: "This option sets the behavior of the `startup_script`. When set to \"blocking\", the startup_script must exit before the workspace is ready. When set to \"non-blocking\", the startup_script may run in the background and the workspace will be ready immediately. Default is \"non-blocking\", although \"blocking\" is recommended.",
155+
ValidateFunc: validation.StringInSlice([]string{"blocking", "non-blocking"}, false),
156+
ConflictsWith: []string{"login_before_ready"},
139157
},
140158
"metadata": {
141159
Type: schema.TypeList,

provider/agent_test.go

Lines changed: 92 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package provider_test
22

33
import (
4+
"regexp"
45
"testing"
56

67
"github.com/coder/terraform-provider-coder/provider"
@@ -36,7 +37,6 @@ func TestAgent(t *testing.T) {
3637
motd_file = "/etc/motd"
3738
shutdown_script = "echo bye bye"
3839
shutdown_script_timeout = 120
39-
login_before_ready = false
4040
}
4141
`,
4242
Check: func(state *terraform.State) error {
@@ -58,7 +58,6 @@ func TestAgent(t *testing.T) {
5858
"motd_file",
5959
"shutdown_script",
6060
"shutdown_script_timeout",
61-
"login_before_ready",
6261
} {
6362
value := resource.Primary.Attributes[key]
6463
t.Logf("%q = %q", key, value)
@@ -71,6 +70,97 @@ func TestAgent(t *testing.T) {
7170
})
7271
}
7372

73+
func TestAgent_StartupScriptBehavior(t *testing.T) {
74+
t.Parallel()
75+
76+
for _, tc := range []struct {
77+
Name string
78+
Config string
79+
ExpectError *regexp.Regexp
80+
Check func(state *terraform.ResourceState)
81+
}{
82+
{
83+
Name: "blocking",
84+
Config: `
85+
resource "coder_agent" "new" {
86+
os = "linux"
87+
arch = "amd64"
88+
startup_script_behavior = "blocking"
89+
}
90+
`,
91+
Check: func(state *terraform.ResourceState) {
92+
require.Equal(t, "blocking", state.Primary.Attributes["startup_script_behavior"])
93+
},
94+
},
95+
{
96+
Name: "non-blocking",
97+
Config: `
98+
resource "coder_agent" "new" {
99+
os = "linux"
100+
arch = "amd64"
101+
startup_script_behavior = "non-blocking"
102+
}
103+
`,
104+
Check: func(state *terraform.ResourceState) {
105+
require.Equal(t, "non-blocking", state.Primary.Attributes["startup_script_behavior"])
106+
},
107+
},
108+
{
109+
Name: "login_before_ready (deprecated)",
110+
Config: `
111+
resource "coder_agent" "new" {
112+
os = "linux"
113+
arch = "amd64"
114+
login_before_ready = false
115+
}
116+
`,
117+
Check: func(state *terraform.ResourceState) {
118+
require.Equal(t, "false", state.Primary.Attributes["login_before_ready"])
119+
// startup_script_behavior must be empty, this indicates that
120+
// login_before_ready should be used instead.
121+
require.Equal(t, "", state.Primary.Attributes["startup_script_behavior"])
122+
},
123+
},
124+
{
125+
Name: "no login_before_ready with startup_script_behavior",
126+
Config: `
127+
resource "coder_agent" "new" {
128+
os = "linux"
129+
arch = "amd64"
130+
login_before_ready = false
131+
startup_script_behavior = "blocking"
132+
}
133+
`,
134+
ExpectError: regexp.MustCompile("conflicts with"),
135+
},
136+
} {
137+
tc := tc
138+
t.Run(tc.Name, func(t *testing.T) {
139+
t.Parallel()
140+
resource.Test(t, resource.TestCase{
141+
Providers: map[string]*schema.Provider{
142+
"coder": provider.New(),
143+
},
144+
IsUnitTest: true,
145+
Steps: []resource.TestStep{{
146+
Config: tc.Config,
147+
ExpectError: tc.ExpectError,
148+
Check: func(state *terraform.State) error {
149+
require.Len(t, state.Modules, 1)
150+
require.Len(t, state.Modules[0].Resources, 1)
151+
resource := state.Modules[0].Resources["coder_agent.new"]
152+
require.NotNil(t, resource)
153+
if tc.Check != nil {
154+
tc.Check(resource)
155+
}
156+
return nil
157+
},
158+
}},
159+
})
160+
})
161+
}
162+
}
163+
74164
func TestAgent_Instance(t *testing.T) {
75165
t.Parallel()
76166
resource.Test(t, resource.TestCase{

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy