Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Replacelogin_before_ready withstartup_script_behavior#125

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletiondocs/resources/agent.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -50,12 +50,13 @@ resource "kubernetes_pod" "dev" {
- `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.
- `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.
- `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.
- `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))
- `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.
- `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.
- `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_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.
- `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.
- `troubleshooting_url` (String) A URL to a document with instructions for troubleshooting problems with the agent.

Expand Down
34 changes: 26 additions & 8 deletionsprovider/agent.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -16,7 +16,7 @@ import (
func agentResource() *schema.Resource {
return &schema.Resource{
Description: "Use this resource to associate an agent.",
CreateContext: func(c context.Context, resourceData *schema.ResourceData, i interface{}) diag.Diagnostics {
CreateContext: func(ctx context.Context, resourceData *schema.ResourceData, i interface{}) diag.Diagnostics {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

nit: I'm curious if there is a way to check ifCheckContext calledupdateStartupScriptBehaviorIfLoginBeforeReady or if it wasReadWithoutTimeout.

BTW It would be nice to write a comment on why we need to call it in both contexts. I think that we usually stick withReadContext.

Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Both are called, but I never got this method working because the value reverts at the end resulting inStep 1/1 error: After applying this test step, the plan was not empty..

This function was actually supposed to be removed in favor of empty default forstartup_script_behavior, so it's good you asked.

mtojek reacted with thumbs up emoji
// This should be a real authentication token!
resourceData.SetId(uuid.NewString())
err := resourceData.Set("token", uuid.NewString())
Expand All@@ -25,14 +25,14 @@ func agentResource() *schema.Resource {
}
return updateInitScript(resourceData, i)
},
ReadWithoutTimeout: func(c context.Context, resourceData *schema.ResourceData, i interface{}) diag.Diagnostics {
ReadWithoutTimeout: func(ctx context.Context, resourceData *schema.ResourceData, i interface{}) diag.Diagnostics {
err := resourceData.Set("token", uuid.NewString())
if err != nil {
return diag.FromErr(err)
}
return updateInitScript(resourceData, i)
},
DeleteContext: func(c context.Context,rd *schema.ResourceData, i interface{}) diag.Diagnostics {
DeleteContext: func(ctx context.Context,resourceData *schema.ResourceData, i interface{}) diag.Diagnostics {
return nil
},
Schema: map[string]*schema.Schema{
Expand DownExpand Up@@ -131,11 +131,29 @@ func agentResource() *schema.Resource {
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.",
},
"login_before_ready": {
Type: schema.TypeBool,
Default: true, // Change default value to false in a future release.
ForceNew: true,
Optional: true,
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.",
// Note: When this is removed, "startup_script_behavior" should
// be set to "non-blocking" by default (instead of empty string).
Type: schema.TypeBool,
Default: true,
ForceNew: true,
Optional: true,
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.",
Deprecated: "Configure startup_script_behavior instead. This attribute will be removed in a future version of the provider.",
ConflictsWith: []string{"startup_script_behavior"},
},
"startup_script_behavior": {
// Note: Our default value is "non-blocking" but we do not set
// it here because we want to be able to differentiate between
// the user setting this or "login_before_ready". For all
// intents and purposes, until deprecation, setting
// "login_before_ready = false" is equivalent to setting
// "startup_script_behavior = blocking".
Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

We will handle this case in coder/coder, ifstartup_script_behavior is empty string, we take the value fromlogin_before_ready.

mtojek reacted with thumbs up emoji
Type: schema.TypeString,
ForceNew: true,
Optional: true,
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.",
ValidateFunc: validation.StringInSlice([]string{"blocking", "non-blocking"}, false),
ConflictsWith: []string{"login_before_ready"},
},
"metadata": {
Type: schema.TypeList,
Expand Down
94 changes: 92 additions & 2 deletionsprovider/agent_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
package provider_test

import (
"regexp"
"testing"

"github.com/coder/terraform-provider-coder/provider"
Expand DownExpand Up@@ -36,7 +37,6 @@ func TestAgent(t *testing.T) {
motd_file = "/etc/motd"
shutdown_script = "echo bye bye"
shutdown_script_timeout = 120
login_before_ready = false
}
`,
Check: func(state *terraform.State) error {
Expand All@@ -58,7 +58,6 @@ func TestAgent(t *testing.T) {
"motd_file",
"shutdown_script",
"shutdown_script_timeout",
"login_before_ready",
} {
value := resource.Primary.Attributes[key]
t.Logf("%q = %q", key, value)
Expand All@@ -71,6 +70,97 @@ func TestAgent(t *testing.T) {
})
}

func TestAgent_StartupScriptBehavior(t *testing.T) {
t.Parallel()

for _, tc := range []struct {
Name string
Config string
ExpectError *regexp.Regexp
Check func(state *terraform.ResourceState)
}{
{
Name: "blocking",
Config: `
resource "coder_agent" "new" {
os = "linux"
arch = "amd64"
startup_script_behavior = "blocking"
}
`,
Check: func(state *terraform.ResourceState) {
require.Equal(t, "blocking", state.Primary.Attributes["startup_script_behavior"])
},
},
{
Name: "non-blocking",
Config: `
resource "coder_agent" "new" {
os = "linux"
arch = "amd64"
startup_script_behavior = "non-blocking"
}
`,
Check: func(state *terraform.ResourceState) {
require.Equal(t, "non-blocking", state.Primary.Attributes["startup_script_behavior"])
},
},
{
Name: "login_before_ready (deprecated)",
Config: `
resource "coder_agent" "new" {
os = "linux"
arch = "amd64"
login_before_ready = false
}
`,
Check: func(state *terraform.ResourceState) {
require.Equal(t, "false", state.Primary.Attributes["login_before_ready"])
// startup_script_behavior must be empty, this indicates that
// login_before_ready should be used instead.
require.Equal(t, "", state.Primary.Attributes["startup_script_behavior"])
},
},
{
Name: "no login_before_ready with startup_script_behavior",
Config: `
resource "coder_agent" "new" {
os = "linux"
arch = "amd64"
login_before_ready = false
startup_script_behavior = "blocking"
}
`,
ExpectError: regexp.MustCompile("conflicts with"),
},
} {
tc := tc
t.Run(tc.Name, func(t *testing.T) {
t.Parallel()
resource.Test(t, resource.TestCase{
Providers: map[string]*schema.Provider{
"coder": provider.New(),
},
IsUnitTest: true,
Steps: []resource.TestStep{{
Config: tc.Config,
ExpectError: tc.ExpectError,
Check: func(state *terraform.State) error {
require.Len(t, state.Modules, 1)
require.Len(t, state.Modules[0].Resources, 1)
resource := state.Modules[0].Resources["coder_agent.new"]
require.NotNil(t, resource)
if tc.Check != nil {
tc.Check(resource)
}
return nil
},
}},
})
})
}
}

func TestAgent_Instance(t *testing.T) {
t.Parallel()
resource.Test(t, resource.TestCase{
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp