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
@@ -85,10 +87,20 @@ type CreateEnvironmentRequest struct {
85
87
// CreateEnvironment sends a request to create an environment.
86
88
func (c Client ) CreateEnvironment (ctx context.Context , orgID string , req CreateEnvironmentRequest ) (* Environment , error ) {
87
89
var env Environment
90
+ << << << < HEAD
88
91
if err := c .requestBody (ctx , http .MethodPost , "/api/orgs/" + orgID + "/environments" , req , & env ); err != nil {
89
92
return nil , err
90
93
}
91
94
return & env , nil
95
+ == == == =
96
+ err := c .requestBody (
97
+ ctx ,
98
+ http .MethodPost , "/api/orgs/" + orgID + "/environments" ,
99
+ req ,
100
+ & env ,
101
+ )
102
+ return & env , err
103
+ >> >> >> > master
92
104
}
93
105
94
106
// EnvironmentsByOrganization gets the list of environments owned by the given user.
@@ -111,7 +123,12 @@ func (c Client) DialWsep(ctx context.Context, env *Environment) (*websocket.Conn
111
123
return c .dialWebsocket (ctx , "/proxy/environments/" + env .ID + "/wsep" )
112
124
}
113
125
114
- // DialEnvironmentBuildLog opens a websocket connection for the environment build log messages.
126
+ // DialIDEStatus opens a websocket connection for cpu load metrics on the environment
127
+ func (c Client ) DialIDEStatus (ctx context.Context , envID string ) (* websocket.Conn , error ) {
128
+ return c .dialWs (ctx , "/proxy/environments/" + envID + "/ide/api/status" )
129
+ }
130
+
131
+ // DialEnvironmentBuildLog opens a websocket connection for the environment build log messages
115
132
func (c Client ) DialEnvironmentBuildLog (ctx context.Context , envID string ) (* websocket.Conn , error ) {
116
133
return c .dialWebsocket (ctx , "/api/environments/" + envID + "/watch-update" )
117
134
}
@@ -120,3 +137,52 @@ func (c Client) DialEnvironmentBuildLog(ctx context.Context, envID string) (*web
120
137
func (c Client ) DialEnvironmentStats (ctx context.Context , envID string ) (* websocket.Conn , error ) {
121
138
return c .dialWebsocket (ctx , "/api/environments/" + envID + "/watch-stats" )
122
139
}
140
+
141
+ // DialResourceLoad opens a websocket connection for cpu load metrics on the environment
142
+ func (c Client ) DialResourceLoad (ctx context.Context , envID string ) (* websocket.Conn , error ) {
143
+ return c .dialWs (ctx , "/api/environments/" + envID + "/watch-resource-load" )
144
+ }
145
+
146
+ // BuildLogType describes the type of an event.
147
+ type BuildLogType string
148
+
149
+ const (
150
+ // BuildLogTypeStart signals that a new build log has begun.
151
+ BuildLogTypeStart BuildLogType = "start"
152
+ // BuildLogTypeStage is a stage-level event for an environment.
153
+ // It can be thought of as a major step in the environment's
154
+ // lifecycle.
155
+ BuildLogTypeStage BuildLogType = "stage"
156
+ // BuildLogTypeError describes an error that has occurred.
157
+ BuildLogTypeError BuildLogType = "error"
158
+ // BuildLogTypeSubstage describes a subevent that occurs as
159
+ // part of a stage. This can be the output from a user's
160
+ // personalization script, or a long running command.
161
+ BuildLogTypeSubstage BuildLogType = "substage"
162
+ // BuildLogTypeDone signals that the build has completed.
163
+ BuildLogTypeDone BuildLogType = "done"
164
+ )
165
+
166
+ type buildLogMsg struct {
167
+ Type BuildLogType `json:"type"`
168
+ }
169
+
170
+ // WaitForEnvironmentReady will watch the build log and return when done
171
+ func (c Client ) WaitForEnvironmentReady (ctx context.Context , env * Environment ) error {
172
+ conn , err := c .DialEnvironmentBuildLog (ctx , env .ID )
173
+ if err != nil {
174
+ return xerrors .Errorf ("%s: dial build log: %w" , env .Name , err )
175
+ }
176
+
177
+ for {
178
+ msg := buildLogMsg {}
179
+ err := wsjson .Read (ctx , conn , & msg )
180
+ if err != nil {
181
+ return xerrors .Errorf ("%s: reading build log msg: %w" , env .Name , err )
182
+ }
183
+
184
+ if msg .Type == BuildLogTypeDone {
185
+ return nil
186
+ }
187
+ }
188
+ }
0 commit comments