Skip to content

Commit 8701e00

Browse files
authored
feat: Update Terraform provider to support "dir" in "coder_agent" (#1219)
This allows users to specify a starting directory for shell sessions.
1 parent a79aa64 commit 8701e00

File tree

17 files changed

+233
-145
lines changed

17 files changed

+233
-145
lines changed

agent/agent.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ type Metadata struct {
4949
OwnerUsername string `json:"owner_username"`
5050
EnvironmentVariables map[string]string `json:"environment_variables"`
5151
StartupScript string `json:"startup_script"`
52+
Directory string `json:"directory"`
5253
}
5354

5455
type Dialer func(ctx context.Context, logger slog.Logger) (Metadata, *peerbroker.Listener, error)
@@ -340,6 +341,11 @@ func (a *agent) createCommand(ctx context.Context, rawCommand string, env []stri
340341
caller = "/c"
341342
}
342343
cmd := exec.CommandContext(ctx, shell, caller, command)
344+
cmd.Dir = metadata.Directory
345+
if cmd.Dir == "" {
346+
// Default to $HOME if a directory is not set!
347+
cmd.Dir = os.Getenv("HOME")
348+
}
343349
cmd.Env = append(os.Environ(), env...)
344350
executablePath, err := os.Executable()
345351
if err != nil {

coderd/database/databasefake/databasefake.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1173,6 +1173,7 @@ func (q *fakeQuerier) InsertWorkspaceAgent(_ context.Context, arg database.Inser
11731173
Name: arg.Name,
11741174
Architecture: arg.Architecture,
11751175
OperatingSystem: arg.OperatingSystem,
1176+
Directory: arg.Directory,
11761177
StartupScript: arg.StartupScript,
11771178
InstanceMetadata: arg.InstanceMetadata,
11781179
ResourceMetadata: arg.ResourceMetadata,

coderd/database/dump.sql

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ALTER TABLE ONLY workspace_agents
2+
DROP COLUMN IF EXISTS directory;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
ALTER TABLE ONLY workspace_agents
2+
-- UNIX paths are a maximum length of 4096.
3+
ADD COLUMN IF NOT EXISTS directory varchar(4096) DEFAULT '' NOT NULL;

coderd/database/models.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries.sql.go

Lines changed: 13 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries/workspaceagents.sql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,12 @@ INSERT INTO
4848
environment_variables,
4949
operating_system,
5050
startup_script,
51+
directory,
5152
instance_metadata,
5253
resource_metadata
5354
)
5455
VALUES
55-
($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13) RETURNING *;
56+
($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14) RETURNING *;
5657

5758
-- name: UpdateWorkspaceAgentConnectionByID :exec
5859
UPDATE

coderd/provisionerdaemons.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,8 @@ func (server *provisionerdServer) AcquireJob(ctx context.Context, _ *proto.Empty
243243
WorkspaceTransition: transition,
244244
WorkspaceName: workspace.Name,
245245
WorkspaceOwner: owner.Username,
246+
WorkspaceId: workspace.ID.String(),
247+
WorkspaceOwnerId: owner.ID.String(),
246248
},
247249
},
248250
}
@@ -633,6 +635,7 @@ func insertWorkspaceResource(ctx context.Context, db database.Store, jobID uuid.
633635
AuthInstanceID: instanceID,
634636
Architecture: agent.Architecture,
635637
EnvironmentVariables: env,
638+
Directory: agent.Directory,
636639
OperatingSystem: agent.OperatingSystem,
637640
StartupScript: sql.NullString{
638641
String: agent.StartupScript,

coderd/workspaceagents.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ func (api *api) workspaceAgentMetadata(rw http.ResponseWriter, r *http.Request)
132132
OwnerUsername: owner.Username,
133133
EnvironmentVariables: apiAgent.EnvironmentVariables,
134134
StartupScript: apiAgent.StartupScript,
135+
Directory: apiAgent.Directory,
135136
})
136137
}
137138

@@ -469,6 +470,7 @@ func convertWorkspaceAgent(dbAgent database.WorkspaceAgent, agentUpdateFrequency
469470
OperatingSystem: dbAgent.OperatingSystem,
470471
StartupScript: dbAgent.StartupScript.String,
471472
EnvironmentVariables: envs,
473+
Directory: dbAgent.Directory,
472474
}
473475
if dbAgent.FirstConnectedAt.Valid {
474476
workspaceAgent.FirstConnectedAt = &dbAgent.FirstConnectedAt.Time

0 commit comments

Comments
 (0)