ServeProvisionerDaemon returns the gRPC service for a provisioner daemon implementation. The context is during dial, not during the lifetime of the client. Client should be closed after use.
(ctx context.Context, req ServeProvisionerDaemonRequest)
| 292 | // implementation. The context is during dial, not during the lifetime of the |
| 293 | // client. Client should be closed after use. |
| 294 | func (c *Client) ServeProvisionerDaemon(ctx context.Context, req ServeProvisionerDaemonRequest) (proto.DRPCProvisionerDaemonClient, error) { |
| 295 | orgParam := req.Organization.String() |
| 296 | if req.Organization == uuid.Nil { |
| 297 | orgParam = DefaultOrganization |
| 298 | } |
| 299 | |
| 300 | serverURL, err := c.URL.Parse(fmt.Sprintf("/api/v2/organizations/%s/provisionerdaemons/serve", orgParam)) |
| 301 | if err != nil { |
| 302 | return nil, xerrors.Errorf("parse url: %w", err) |
| 303 | } |
| 304 | query := serverURL.Query() |
| 305 | query.Add("version", proto.CurrentVersion.String()) |
| 306 | query.Add("name", req.Name) |
| 307 | query.Add("version", proto.CurrentVersion.String()) |
| 308 | |
| 309 | for _, provisioner := range req.Provisioners { |
| 310 | query.Add("provisioner", string(provisioner)) |
| 311 | } |
| 312 | for key, value := range req.Tags { |
| 313 | query.Add("tag", fmt.Sprintf("%s=%s", key, value)) |
| 314 | } |
| 315 | serverURL.RawQuery = query.Encode() |
| 316 | httpClient := &http.Client{ |
| 317 | Transport: c.HTTPClient.Transport, |
| 318 | } |
| 319 | headers := http.Header{} |
| 320 | |
| 321 | headers.Set(BuildVersionHeader, buildinfo.Version()) |
| 322 | |
| 323 | if req.ProvisionerKey != "" { |
| 324 | headers.Set(ProvisionerDaemonKey, req.ProvisionerKey) |
| 325 | } |
| 326 | if req.PreSharedKey != "" { |
| 327 | headers.Set(ProvisionerDaemonPSK, req.PreSharedKey) |
| 328 | } |
| 329 | if req.ProvisionerKey == "" && req.PreSharedKey == "" { |
| 330 | // Use session token if we don't have a PSK or provisioner key. |
| 331 | headers.Set(SessionTokenHeader, c.SessionToken()) |
| 332 | } |
| 333 | |
| 334 | conn, res, err := websocket.Dial(ctx, serverURL.String(), &websocket.DialOptions{ |
| 335 | HTTPClient: httpClient, |
| 336 | // Need to disable compression to avoid a data-race. |
| 337 | CompressionMode: websocket.CompressionDisabled, |
| 338 | HTTPHeader: headers, |
| 339 | }) |
| 340 | if err != nil { |
| 341 | if res == nil { |
| 342 | return nil, err |
| 343 | } |
| 344 | return nil, ReadBodyAsError(res) |
| 345 | } |
| 346 | // Align with the frame size of yamux. |
| 347 | conn.SetReadLimit(256 * 1024) |
| 348 | |
| 349 | config := yamux.DefaultConfig() |
| 350 | config.LogOutput = io.Discard |
| 351 | // Use background context because caller should close the client. |