6
6
"time"
7
7
8
8
"cdr.dev/coder-cli/internal/x/xjson"
9
+ "golang.org/x/xerrors"
9
10
"nhooyr.io/websocket"
11
+ "nhooyr.io/websocket/wsjson"
10
12
)
11
13
12
14
// Environment describes a Coder environment
@@ -81,14 +83,14 @@ type CreateEnvironmentRequest struct {
81
83
82
84
// CreateEnvironment sends a request to create an environment.
83
85
func (c Client ) CreateEnvironment (ctx context.Context , orgID string , req CreateEnvironmentRequest ) (* Environment , error ) {
84
- var env * Environment
86
+ var env Environment
85
87
err := c .requestBody (
86
88
ctx ,
87
89
http .MethodPost , "/api/orgs/" + orgID + "/environments" ,
88
90
req ,
89
- env ,
91
+ & env ,
90
92
)
91
- return env , err
93
+ return & env , err
92
94
}
93
95
94
96
// EnvironmentsByOrganization gets the list of environments owned by the given user.
@@ -119,6 +121,11 @@ func (c Client) DialWsep(ctx context.Context, env *Environment) (*websocket.Conn
119
121
return c .dialWs (ctx , "/proxy/environments/" + env .ID + "/wsep" )
120
122
}
121
123
124
+ // DialIDEStatus opens a websocket connection for cpu load metrics on the environment
125
+ func (c Client ) DialIDEStatus (ctx context.Context , envID string ) (* websocket.Conn , error ) {
126
+ return c .dialWs (ctx , "/proxy/environments/" + envID + "/ide/api/status" )
127
+ }
128
+
122
129
// DialEnvironmentBuildLog opens a websocket connection for the environment build log messages
123
130
func (c Client ) DialEnvironmentBuildLog (ctx context.Context , envID string ) (* websocket.Conn , error ) {
124
131
return c .dialWs (ctx , "/api/environments/" + envID + "/watch-update" )
@@ -128,3 +135,52 @@ func (c Client) DialEnvironmentBuildLog(ctx context.Context, envID string) (*web
128
135
func (c Client ) DialEnvironmentStats (ctx context.Context , envID string ) (* websocket.Conn , error ) {
129
136
return c .dialWs (ctx , "/api/environments/" + envID + "/watch-stats" )
130
137
}
138
+
139
+ // DialResourceLoad opens a websocket connection for cpu load metrics on the environment
140
+ func (c Client ) DialResourceLoad (ctx context.Context , envID string ) (* websocket.Conn , error ) {
141
+ return c .dialWs (ctx , "/api/environments/" + envID + "/watch-resource-load" )
142
+ }
143
+
144
+ // BuildLogType describes the type of an event.
145
+ type BuildLogType string
146
+
147
+ const (
148
+ // BuildLogTypeStart signals that a new build log has begun.
149
+ BuildLogTypeStart BuildLogType = "start"
150
+ // BuildLogTypeStage is a stage-level event for an environment.
151
+ // It can be thought of as a major step in the environment's
152
+ // lifecycle.
153
+ BuildLogTypeStage BuildLogType = "stage"
154
+ // BuildLogTypeError describes an error that has occurred.
155
+ BuildLogTypeError BuildLogType = "error"
156
+ // BuildLogTypeSubstage describes a subevent that occurs as
157
+ // part of a stage. This can be the output from a user's
158
+ // personalization script, or a long running command.
159
+ BuildLogTypeSubstage BuildLogType = "substage"
160
+ // BuildLogTypeDone signals that the build has completed.
161
+ BuildLogTypeDone BuildLogType = "done"
162
+ )
163
+
164
+ type buildLogMsg struct {
165
+ Type BuildLogType `json:"type"`
166
+ }
167
+
168
+ // WaitForEnvironmentReady will watch the build log and return when done
169
+ func (c Client ) WaitForEnvironmentReady (ctx context.Context , env * Environment ) error {
170
+ conn , err := c .DialEnvironmentBuildLog (ctx , env .ID )
171
+ if err != nil {
172
+ return xerrors .Errorf ("%s: dial build log: %w" , env .Name , err )
173
+ }
174
+
175
+ for {
176
+ msg := buildLogMsg {}
177
+ err := wsjson .Read (ctx , conn , & msg )
178
+ if err != nil {
179
+ return xerrors .Errorf ("%s: reading build log msg: %w" , env .Name , err )
180
+ }
181
+
182
+ if msg .Type == BuildLogTypeDone {
183
+ return nil
184
+ }
185
+ }
186
+ }
0 commit comments