Skip to content

feat: add support for coder_script #9584

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 68 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
68 commits
Select commit Hold shift + click to select a range
7199651
Add basic migrations
kylecarbs Sep 3, 2023
51b0079
Merge branch 'main' into execscripts
kylecarbs Sep 5, 2023
c18a401
Improve schema
kylecarbs Sep 5, 2023
9ae6e62
Merge branch 'main' into execscripts
kylecarbs Sep 5, 2023
70ebaf3
Refactor agent scripts into it's own package
kylecarbs Sep 7, 2023
89c7af1
Support legacy start and stop script format
kylecarbs Sep 7, 2023
d5df133
Pipe the scripts!
kylecarbs Sep 7, 2023
58964c9
Finish the piping
kylecarbs Sep 7, 2023
00a4e73
Fix context usage
kylecarbs Sep 7, 2023
942fde6
It works!
kylecarbs Sep 7, 2023
92dedad
Fix sql query
kylecarbs Sep 7, 2023
7cf6f0c
Fix SQL query
kylecarbs Sep 7, 2023
5b6f264
Rename `LogSourceID` -> `SourceID`
kylecarbs Sep 7, 2023
e2c9f91
Fix the FE
kylecarbs Sep 7, 2023
6fab755
Merge branch 'main' into execscripts
kylecarbs Sep 7, 2023
51e08f4
fmt
kylecarbs Sep 7, 2023
9a38131
Rename migrations
kylecarbs Sep 7, 2023
c0fac6b
Fix log tests
kylecarbs Sep 8, 2023
f7f1c7a
Fix lint err
kylecarbs Sep 8, 2023
f0a8f53
Fix gen
kylecarbs Sep 8, 2023
66f9185
Fix story type
kylecarbs Sep 8, 2023
78f01d1
Rename source to script
kylecarbs Sep 13, 2023
75388f7
Fix schema jank
kylecarbs Sep 13, 2023
8810326
Uncomment test
kylecarbs Sep 13, 2023
45b395e
Rename proto to TimeoutSeconds
kylecarbs Sep 13, 2023
40bcd9d
Fix comments
kylecarbs Sep 17, 2023
b744c9f
Fix comments
kylecarbs Sep 17, 2023
c844462
Fix legacy endpoint without specified log_source
kylecarbs Sep 18, 2023
139bbf9
Merge branch 'main' into execscripts
kylecarbs Sep 18, 2023
a21f085
Fix non-blocking by default in agent
kylecarbs Sep 18, 2023
49808b1
Fix resources tests
kylecarbs Sep 18, 2023
a47fe28
Fix dbfake
kylecarbs Sep 18, 2023
da40c79
Fix resources
kylecarbs Sep 18, 2023
3631cfa
Fix linting I think
kylecarbs Sep 18, 2023
77bc6e1
Add fixtures
kylecarbs Sep 18, 2023
5b2bd86
fmt
kylecarbs Sep 18, 2023
9d1a4fe
Fix startup script behavior
kylecarbs Sep 18, 2023
ad0d678
Fix comments
kylecarbs Sep 19, 2023
39fb9d3
Fix context
kylecarbs Sep 21, 2023
444decb
Merge branch 'main' into execscripts
kylecarbs Sep 21, 2023
c4d3cb8
Fix cancel
kylecarbs Sep 21, 2023
4c17a8b
Fix SQL tests
kylecarbs Sep 21, 2023
9591e34
Fix e2e tests
kylecarbs Sep 21, 2023
a4a0270
Interrupt on Windows
kylecarbs Sep 21, 2023
35c1c11
Merge branch 'main' into execscripts
matifali Sep 22, 2023
dd5abdf
Fix agent leaking script process
kylecarbs Sep 24, 2023
9e85d7b
Fix migrations
kylecarbs Sep 24, 2023
c26a01b
Merge branch 'main' into execscripts
kylecarbs Sep 24, 2023
9513acf
Fix stories
kylecarbs Sep 24, 2023
2e3611b
Merge branch 'main' into execscripts
matifali Sep 25, 2023
e8b1e43
Fix duplicate logs appearing
kylecarbs Sep 25, 2023
ee1fe11
Merge branch 'execscripts' of https://github.com/coder/coder into exe…
kylecarbs Sep 25, 2023
b837aac
Gen
kylecarbs Sep 25, 2023
f1ff5cc
Fix log location
kylecarbs Sep 25, 2023
4ec3a87
Fix tests
kylecarbs Sep 25, 2023
d36ab53
Fix tests
kylecarbs Sep 25, 2023
eeddb52
Fix log output
kylecarbs Sep 25, 2023
aa5540b
Show display name in output
kylecarbs Sep 25, 2023
f866a92
Fix print
kylecarbs Sep 25, 2023
a99b6dd
Return timeout on start context
kylecarbs Sep 25, 2023
1865590
Gen
kylecarbs Sep 25, 2023
3b26aa0
Fix fixture
kylecarbs Sep 25, 2023
f2f69bb
Fix the agent status
kylecarbs Sep 25, 2023
aa68796
Fix startup timeout msg
kylecarbs Sep 25, 2023
73a7a78
Fix command using shared context
kylecarbs Sep 25, 2023
d1f4963
Fix timeout draining
kylecarbs Sep 25, 2023
784e616
Change signal type
kylecarbs Sep 25, 2023
7ac782b
Add deterministic colors to startup script logs
kylecarbs Sep 25, 2023
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
Prev Previous commit
Next Next commit
Support legacy start and stop script format
  • Loading branch information
kylecarbs committed Sep 7, 2023
commit 89c7af1118053e3445b1abcfbeee9b8aa94291d4
6 changes: 3 additions & 3 deletions agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -976,13 +976,13 @@ func (a *agent) runDERPMapSubscriber(ctx context.Context, network *tailnet.Conn)
}

func (a *agent) runScript(ctx context.Context, script codersdk.WorkspaceAgentScript) (err error) {
if script.Script == "" {
if script.Source == "" {
return nil
}

logger := a.logger.With(slog.F("log_source", script.LogSourceDisplayName))

logger.Info(ctx, "running script", slog.F("script", script.Script))
logger.Info(ctx, "running script", slog.F("script", script.Source))
fileWriter, err := a.filesystem.OpenFile(filepath.Join(a.logDir, fmt.Sprintf("coder-%s-script.log", script.LogSourceDisplayName)), os.O_CREATE|os.O_RDWR, 0o600)
if err != nil {
return xerrors.Errorf("open %s script log file: %w", script.LogSourceDisplayName, err)
Expand All @@ -994,7 +994,7 @@ func (a *agent) runScript(ctx context.Context, script codersdk.WorkspaceAgentScr
}
}()

cmdPty, err := a.sshServer.CreateCommand(ctx, script.Script, nil)
cmdPty, err := a.sshServer.CreateCommand(ctx, script.Source, nil)
if err != nil {
return xerrors.Errorf("%s script: create command: %w", script.LogSourceDisplayName, err)
}
Expand Down
16 changes: 8 additions & 8 deletions agent/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1209,7 +1209,7 @@ func TestAgent_Lifecycle(t *testing.T) {

_, client, _, _, _ := setupAgent(t, agentsdk.Manifest{
Scripts: []codersdk.WorkspaceAgentScript{{
Script: "sleep 3",
Source: "sleep 3",
Timeout: time.Nanosecond,
RunOnStart: true,
}},
Expand All @@ -1234,7 +1234,7 @@ func TestAgent_Lifecycle(t *testing.T) {

_, client, _, _, _ := setupAgent(t, agentsdk.Manifest{
Scripts: []codersdk.WorkspaceAgentScript{{
Script: "false",
Source: "false",
Timeout: 30 * time.Second,
RunOnStart: true,
}},
Expand All @@ -1259,7 +1259,7 @@ func TestAgent_Lifecycle(t *testing.T) {

_, client, _, _, _ := setupAgent(t, agentsdk.Manifest{
Scripts: []codersdk.WorkspaceAgentScript{{
Script: "true",
Source: "true",
Timeout: 30 * time.Second,
RunOnStart: true,
}},
Expand All @@ -1284,7 +1284,7 @@ func TestAgent_Lifecycle(t *testing.T) {

_, client, _, _, closer := setupAgent(t, agentsdk.Manifest{
Scripts: []codersdk.WorkspaceAgentScript{{
Script: "sleep 3",
Source: "sleep 3",
Timeout: 30 * time.Second,
RunOnStop: true,
}},
Expand Down Expand Up @@ -1325,7 +1325,7 @@ func TestAgent_Lifecycle(t *testing.T) {

_, client, _, _, closer := setupAgent(t, agentsdk.Manifest{
Scripts: []codersdk.WorkspaceAgentScript{{
Script: "sleep 3",
Source: "sleep 3",
Timeout: time.Nanosecond,
RunOnStop: true,
}},
Expand Down Expand Up @@ -1367,7 +1367,7 @@ func TestAgent_Lifecycle(t *testing.T) {

_, client, _, _, closer := setupAgent(t, agentsdk.Manifest{
Scripts: []codersdk.WorkspaceAgentScript{{
Script: "false",
Source: "false",
Timeout: 30 * time.Second,
RunOnStop: true,
}},
Expand Down Expand Up @@ -1417,11 +1417,11 @@ func TestAgent_Lifecycle(t *testing.T) {
DERPMap: derpMap,
Scripts: []codersdk.WorkspaceAgentScript{{
LogSourceDisplayName: "startup",
Script: "echo 1",
Source: "echo 1",
RunOnStart: true,
}, {
LogSourceDisplayName: "shutdown",
Script: "echo " + expected,
Source: "echo " + expected,
RunOnStop: true,
}},
},
Expand Down
8 changes: 4 additions & 4 deletions agent/agentscripts/agentscripts.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@ func (r *Runner) Init(scripts []codersdk.WorkspaceAgentScript) error {
r.scripts = scripts

for _, script := range scripts {
if script.Schedule == "" {
if script.CRON == "" {
continue
}
script := script
_, err := r.cron.AddFunc(script.Schedule, func() {
_, err := r.cron.AddFunc(script.CRON, func() {
err := r.run(script)
if err != nil {
r.Logger.Warn(r.ctx, "run agent script on schedule", slog.Error(err))
Expand Down Expand Up @@ -114,7 +114,7 @@ func (r *Runner) Execute(filter func(script codersdk.WorkspaceAgentScript) bool)
func (r *Runner) run(script codersdk.WorkspaceAgentScript) error {
logger := r.Logger.With(slog.F("log_source", script.LogSourceDisplayName))
ctx := r.ctx
logger.Info(ctx, "running agent script", slog.F("script", script.Script))
logger.Info(ctx, "running agent script", slog.F("script", script.Source))
fileWriter, err := r.Filesystem.OpenFile(filepath.Join(r.LogDir, fmt.Sprintf("coder-%s-script.log", script.LogSourceDisplayName)), os.O_CREATE|os.O_RDWR, 0o600)
if err != nil {
return xerrors.Errorf("open %s script log file: %w", script.LogSourceDisplayName, err)
Expand All @@ -134,7 +134,7 @@ func (r *Runner) run(script codersdk.WorkspaceAgentScript) error {
defer cancel()
}

cmdPty, err := r.SSHServer.CreateCommand(ctx, script.Script, nil)
cmdPty, err := r.SSHServer.CreateCommand(ctx, script.Source, nil)
if err != nil {
return xerrors.Errorf("%s script: create command: %w", script.LogSourceDisplayName, err)
}
Expand Down
4 changes: 2 additions & 2 deletions agent/agentscripts/agentscripts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestExecuteBasic(t *testing.T) {
defer runner.Close()
err := runner.Init([]codersdk.WorkspaceAgentScript{{
LogSourceDisplayName: "test",
Script: "echo hello",
Source: "echo hello",
}})
require.NoError(t, err)
require.NoError(t, runner.Execute(func(script codersdk.WorkspaceAgentScript) bool {
Expand All @@ -47,7 +47,7 @@ func TestTimeout(t *testing.T) {
runner := setup(t, nil)
defer runner.Close()
err := runner.Init([]codersdk.WorkspaceAgentScript{{
Script: "sleep 3",
Source: "sleep 3",
Timeout: time.Nanosecond,
}})
require.NoError(t, err)
Expand Down
9 changes: 4 additions & 5 deletions cli/cliui/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,11 +288,10 @@ func TestAgent(t *testing.T) {

var buf bytes.Buffer
agent := codersdk.WorkspaceAgent{
ID: uuid.New(),
Status: codersdk.WorkspaceAgentConnecting,
StartupScriptBehavior: codersdk.WorkspaceAgentStartupScriptBehaviorNonBlocking,
CreatedAt: time.Now(),
LifecycleState: codersdk.WorkspaceAgentLifecycleCreated,
ID: uuid.New(),
Status: codersdk.WorkspaceAgentConnecting,
CreatedAt: time.Now(),
LifecycleState: codersdk.WorkspaceAgentLifecycleCreated,
}
logs := make(chan []codersdk.WorkspaceAgentLog, 1)

Expand Down
12 changes: 5 additions & 7 deletions cli/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,11 @@ func (r *RootCmd) ssh() *clibase.Cmd {
case "no":
wait = false
case "auto":
switch workspaceAgent.StartupScriptBehavior {
case codersdk.WorkspaceAgentStartupScriptBehaviorBlocking:
wait = true
case codersdk.WorkspaceAgentStartupScriptBehaviorNonBlocking:
wait = false
default:
return xerrors.Errorf("unknown startup script behavior %q", workspaceAgent.StartupScriptBehavior)
for _, script := range workspaceAgent.Scripts {
Copy link
Member

Choose a reason for hiding this comment

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

Seeing this change, I believe old clients connecting to coderd will be broken by this since the API no longer includes the startup script behavior.

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'll fix this!

Copy link
Member Author

Choose a reason for hiding this comment

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

@mafredri do you think I should just include the field and leave it as a static value to not break old clients?

Copy link
Member

Choose a reason for hiding this comment

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

@kylecarbs I'm assuming static would be non-blocking? For anyone using blocking I suppose it would still be a breaking change. But I'm not sure it's worth including so much legacy to keep piping it to the DB, so I think it's acceptable. But if we leave it like that, I think we should mention it in the release notes.

if script.StartBlocksLogin {
wait = true
break
}
}
default:
return xerrors.Errorf("unknown wait value %q", waitEnum)
Expand Down
41 changes: 15 additions & 26 deletions coderd/provisionerdserver/provisionerdserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -1250,32 +1250,21 @@ func InsertWorkspaceResource(ctx context.Context, db database.Store, jobID uuid.

agentID := uuid.New()
dbAgent, err := db.InsertWorkspaceAgent(ctx, database.InsertWorkspaceAgentParams{
ID: agentID,
CreatedAt: dbtime.Now(),
UpdatedAt: dbtime.Now(),
ResourceID: resource.ID,
Name: prAgent.Name,
AuthToken: authToken,
AuthInstanceID: instanceID,
Architecture: prAgent.Architecture,
EnvironmentVariables: env,
Directory: prAgent.Directory,
OperatingSystem: prAgent.OperatingSystem,
StartupScript: sql.NullString{
String: prAgent.StartupScript,
Valid: prAgent.StartupScript != "",
},
ConnectionTimeoutSeconds: prAgent.GetConnectionTimeoutSeconds(),
TroubleshootingURL: prAgent.GetTroubleshootingUrl(),
MOTDFile: prAgent.GetMotdFile(),
StartupScriptBehavior: database.StartupScriptBehavior(prAgent.GetStartupScriptBehavior()),
StartupScriptTimeoutSeconds: prAgent.GetStartupScriptTimeoutSeconds(),
ShutdownScript: sql.NullString{
String: prAgent.ShutdownScript,
Valid: prAgent.ShutdownScript != "",
},
ShutdownScriptTimeoutSeconds: prAgent.GetShutdownScriptTimeoutSeconds(),
DisplayApps: convertDisplayApps(prAgent.GetDisplayApps()),
ID: agentID,
CreatedAt: dbtime.Now(),
UpdatedAt: dbtime.Now(),
ResourceID: resource.ID,
Name: prAgent.Name,
AuthToken: authToken,
AuthInstanceID: instanceID,
Architecture: prAgent.Architecture,
EnvironmentVariables: env,
Directory: prAgent.Directory,
OperatingSystem: prAgent.OperatingSystem,
ConnectionTimeoutSeconds: prAgent.GetConnectionTimeoutSeconds(),
TroubleshootingURL: prAgent.GetTroubleshootingUrl(),
MOTDFile: prAgent.GetMotdFile(),
DisplayApps: convertDisplayApps(prAgent.GetDisplayApps()),
})
if err != nil {
return xerrors.Errorf("insert agent: %w", err)
Expand Down
15 changes: 8 additions & 7 deletions codersdk/workspaceagents.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,11 +190,11 @@ type WorkspaceAgentLogSource struct {
type WorkspaceAgentScript struct {
LogSourceDisplayName string `json:"log_source_display_name"`
LogSourceID uuid.UUID `json:"log_source_id" format:"uuid"`
Script string `json:"script"`
Schedule string `json:"schedule"`
Source string `json:"source"`
CRON string `json:"cron"`
RunOnStart bool `json:"run_on_start"`
RunOnStop bool `json:"run_on_stop"`
LoginBeforeReady bool `json:"login_before_ready"`
StartBlocksLogin bool `json:"start_blocks_login"`
Timeout time.Duration `json:"timeout_seconds"`
}

Expand Down Expand Up @@ -767,10 +767,11 @@ const (
)

type WorkspaceAgentLog struct {
ID int64 `json:"id"`
CreatedAt time.Time `json:"created_at" format:"date-time"`
Output string `json:"output"`
Level LogLevel `json:"level"`
ID int64 `json:"id"`
CreatedAt time.Time `json:"created_at" format:"date-time"`
Output string `json:"output"`
Level LogLevel `json:"level"`
LogSourceID uuid.UUID `json:"log_source_id" format:"uuid"`
}

type AgentSubsystem string
Expand Down
Loading