- Notifications
You must be signed in to change notification settings - Fork22
feat: allow presets to define prebuilds#363
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
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -3,10 +3,13 @@ package provider | ||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"reflect" | ||
"strings" | ||
"github.com/hashicorp/terraform-plugin-log/tflog" | ||
"github.com/google/uuid" | ||
"github.com/hashicorp/go-cty/cty" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
@@ -22,10 +25,54 @@ func agentResource() *schema.Resource { | ||
SchemaVersion: 1, | ||
Description: "Use this resource to associate an agent.", | ||
CreateContext: func(ctx context.Context, resourceData *schema.ResourceData, i interface{}) diag.Diagnostics { | ||
// This should be a real authentication token! | ||
resourceData.SetId(uuid.NewString()) | ||
// CODER_RUNNING_WORKSPACE_AGENT_TOKEN is *only* used for prebuilds. We pass it down when we want to rebuild a prebuilt workspace | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Reminder: we agreed to split the agent part out of this PR since it's logically separate. | ||
// but not generate a new agent token. The provisionerdserver will retrieve this token and push it down to | ||
// here where it will be reused. | ||
// Context: the agent token is often used in immutable attributes of workspace resource (e.g. VM/container) | ||
// to initialize the agent, so if that value changes it will necessitate a replacement of that resource, thus | ||
// obviating the whole point of the prebuild. | ||
// | ||
// The default path is for a new token to be generated on each new resource creation. | ||
// TODO: add logging when the running token is actually used. | ||
var token string | ||
isPrebuild := helpers.OptionalEnv(IsPrebuildEnvironmentVariable()) == "true" | ||
if !isPrebuild { | ||
token = os.Getenv(RunningAgentTokenEnvironmentVariable()) | ||
} | ||
allEnv := make(map[string]interface{}) | ||
for _, v := range os.Environ() { | ||
split := strings.Split(v, "=") | ||
var key, val string | ||
if len(split) > 0 { | ||
key = split[0] | ||
} | ||
if len(split) > 1 { | ||
val = split[1] | ||
} | ||
allEnv[key] = val | ||
} | ||
allEnv["is_prebuild"] = fmt.Sprintf("%v", isPrebuild) | ||
if token == "" { | ||
token = uuid.NewString() | ||
if !isPrebuild { | ||
tflog.Warn(ctx, "NOT USING EXISTING AGENT TOKEN", allEnv) | ||
} | ||
} else { | ||
if !isPrebuild { | ||
tflog.Info(ctx, "IS USING EXISTING AGENT TOKEN", allEnv) | ||
} | ||
} | ||
err := resourceData.Set("token", token) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
@@ -469,3 +516,7 @@ func updateInitScript(resourceData *schema.ResourceData, i interface{}) diag.Dia | ||
} | ||
return nil | ||
} | ||
func RunningAgentTokenEnvironmentVariable() string { | ||
return "CODER_RUNNING_WORKSPACE_AGENT_TOKEN" | ||
} |
Uh oh!
There was an error while loading.Please reload this page.