Skip to content

feat: add coder_script #154

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
36 changes: 36 additions & 0 deletions docs/resources/script.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "coder_script Resource - terraform-provider-coder"
subcategory: ""
description: |-
Use this resource to run a script from an agent.
---

# coder_script (Resource)

Use this resource to run a script from an agent.



<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `agent_id` (String) The "id" property of a "coder_agent" resource to associate with.
- `display_name` (String) The display name of the script to display logs in the dashboard.
Copy link
Member

Choose a reason for hiding this comment

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

Same for display_name. I am wondering how we will display them in a better way without overcrowding the dashboard.

IMO, a coder_app provides interactive access to a service, but a coder_script will not, so I am not convinced about how and if we should display them.

Copy link
Member Author

Choose a reason for hiding this comment

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

I have a nice UI mock that displays this!

- `script` (String) The script to run.

### Optional

- `cron` (String) The cron schedule to run the script on. This is a cron expression.
- `icon` (String) A URL to an icon that will display in the dashboard. View built-in icons here: https://github.com/coder/coder/tree/main/site/static/icon. Use a built-in icon with `data.coder_workspace.me.access_url + "/icon/<path>"`.
Copy link
Member

Choose a reason for hiding this comment

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

Why do we have icon? Do we want to display all of them on the dashboard?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes. These scripts will be displayed in the dashboard.

- `log_path` (String) The path of a file to write the logs to. If relative, it will be appended to tmp.
- `run_on_start` (Boolean) This option defines whether or not the script should run when the agent starts.
- `run_on_stop` (Boolean) This option defines whether or not the script should run when the agent stops.
- `start_blocks_login` (Boolean) This option defines whether or not the user can (by default) login to the workspace before this script completes running on start. When enabled, users may see an incomplete workspace when logging in.
- `timeout` (Number) Time in seconds until the agent lifecycle status is marked as timed out, this happens when the script has not completed (exited) in the given time.
Copy link
Member

Choose a reason for hiding this comment

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

Can you document what you set this to if it's a long-running process? Or I guess we want people to ampersand things &

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm going to allow long-running operations that consistently log, so I'll update this.


### Read-Only

- `id` (String) The ID of this resource.
1 change: 1 addition & 0 deletions provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ func New() *schema.Provider {
"coder_agent_instance": agentInstanceResource(),
"coder_app": appResource(),
"coder_metadata": metadataResource(),
"coder_script": scriptResource(),
},
}
}
Expand Down
91 changes: 91 additions & 0 deletions provider/script.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package provider

import (
"context"

"github.com/google/uuid"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)

func scriptResource() *schema.Resource {
return &schema.Resource{
Description: "Use this resource to run a script from an agent.",
CreateContext: func(ctx context.Context, rd *schema.ResourceData, i interface{}) diag.Diagnostics {
rd.SetId(uuid.NewString())
return nil
},
ReadContext: schema.NoopContext,
DeleteContext: schema.NoopContext,
Schema: map[string]*schema.Schema{
"agent_id": {
Type: schema.TypeString,
Description: `The "id" property of a "coder_agent" resource to associate with.`,
ForceNew: true,
Required: true,
},
"display_name": {
Type: schema.TypeString,
Description: "The display name of the script to display logs in the dashboard.",
ForceNew: true,
Required: true,
},
"log_path": {
Type: schema.TypeString,
Description: "The path of a file to write the logs to. If relative, it will be appended to tmp.",
ForceNew: true,
Optional: true,
},
"icon": {
Type: schema.TypeString,
ForceNew: true,
Optional: true,
Description: "A URL to an icon that will display in the dashboard. View built-in " +
"icons here: https://github.com/coder/coder/tree/main/site/static/icon. Use a " +
"built-in icon with `data.coder_workspace.me.access_url + \"/icon/<path>\"`.",
},
"script": {
ForceNew: true,
Type: schema.TypeString,
Required: true,
Description: "The script to run.",
},
"cron": {
ForceNew: true,
Type: schema.TypeString,
Optional: true,
Description: "The cron schedule to run the script on. This is a cron expression.",
},
"start_blocks_login": {
Type: schema.TypeBool,
Default: false,
ForceNew: true,
Optional: true,
Description: "This option defines whether or not the user can (by default) login to the workspace before this script completes running on start. When enabled, users may see an incomplete workspace when logging in.",
},
"run_on_start": {
Type: schema.TypeBool,
Default: false,
ForceNew: true,
Optional: true,
Description: "This option defines whether or not the script should run when the agent starts.",
},
"run_on_stop": {
Type: schema.TypeBool,
Default: false,
ForceNew: true,
Optional: true,
Description: "This option defines whether or not the script should run when the agent stops.",
},
"timeout": {
Copy link
Member

Choose a reason for hiding this comment

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

For cron jobs that use rclone to do a backup every 5 minutes, will this timeout cancel them? Backup time is usually variable and is a function of activity in the workspace directory. e.g., number of files, size of files, etc.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes. You'd want to add a very lengthy timeout in that case, or not have one at all.

Right now, when two CRONs overlap, the prior process won't end.

Type: schema.TypeInt,
Default: 0,
ForceNew: true,
Optional: true,
Description: "Time in seconds until the agent lifecycle status is marked as timed out, this happens when the script has not completed (exited) in the given time.",
ValidateFunc: validation.IntAtLeast(1),
},
},
}
}