pulumi/pkg/resource/plugin/context.go
joeduffy 97deabb9bd Finish interface for reading configuration¬
This continues the previous commit and establishes the interpreter
context so that we can use the new host interface.  In summary:

    * Instead of using the NullSource for destructions -- which
      doesn't hook up an interpreter and so any reads of configuration
      variables will fail -- we will enlighten the EvalSource to know
      how to orchestrate destruction interpretation.  The primary
      difference is that we don't actually run the code, but *we do*
      perform all of the necessary configuration and variable init.

    * Associate the active interpreter with the plugin context as
      we are executing, so that the host object can actually read the
      state from the heap as requested to do so by attached plugins.

    * Rename anything "engine" related to use the term "host"; this
      avoids introducing unnecesarily new terminology.

    * Add a new pkg/resource/provider/ package where we can begin
      consolidating helper functionality for resource providers.
      Right now, this includes a wrapper interface atop the gRPC
      machinery necessary to contact the host, in addition to a
      Main function that hides some boilerplate entrypoint code.

    * Add a rpcutil.IsBenignCloseErr routine to let us ignore
      "benign" gRPC errors that are knowingly returned at shutdown.

This commit completes pulumi/lumi#117.
2017-06-21 10:31:06 -07:00

97 lines
3.9 KiB
Go

// Licensed to Pulumi Corporation ("Pulumi") under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership.
// Pulumi licenses this file to You under the Apache License, Version 2.0
// (the "License"); you may not use this file except in compliance with
// the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package plugin
import (
"context"
"github.com/golang/glog"
"github.com/pulumi/lumi/pkg/diag"
"github.com/pulumi/lumi/pkg/eval"
"github.com/pulumi/lumi/pkg/eval/rt"
"github.com/pulumi/lumi/pkg/resource"
"github.com/pulumi/lumi/pkg/util/contract"
"github.com/pulumi/lumi/pkg/util/rpcutil"
)
// Context is used to group related operations together so that associated OS resources can be cached, shared, and
// reclaimed as appropriate.
type Context struct {
Diag diag.Sink // the diagnostics sink to use for messages.
Host Host // the host that can be used to fetch providers.
E eval.Interpreter // the interpreter shared amongst all planning in this context.
ObjRes objectResourceMap // the resources held inside of this snapshot.
ObjURN objectURNMap // a convenient lookup map for object to URN.
IDURN idURNMap // a convenient lookup map for ID to URN.
URNRes urnResourceMap // a convenient lookup map for URN to resource.
URNOldIDs urnIDMap // a convenient lookup map for URNs to old IDs.
}
type objectURNMap map[*rt.Object]resource.URN
type objectResourceMap map[*rt.Object]resource.Resource
type idURNMap map[resource.ID]resource.URN
type urnResourceMap map[resource.URN]resource.Resource
type urnIDMap map[resource.URN]resource.ID
// NewContext allocates a new context with a given sink and host. Note that the host is "owned" by this context from
// here forwards, such that when the context's resources are reclaimed, so too are the host's.
func NewContext(d diag.Sink, host Host) (*Context, error) {
ctx := &Context{
Diag: d,
Host: host,
ObjRes: make(objectResourceMap),
ObjURN: make(objectURNMap),
IDURN: make(idURNMap),
URNRes: make(urnResourceMap),
URNOldIDs: make(urnIDMap),
}
if host == nil {
h, err := NewDefaultHost(ctx)
if err != nil {
return nil, err
}
ctx.Host = h
}
return ctx, nil
}
// SetCurrentInterpreter changes the current interpreter context. Only one interpreter may be in use at any given time
// for a single context. This allows the heap state to be read by plugins remotely over an RPC interface.
func (ctx *Context) SetCurrentInterpreter(e eval.Interpreter) {
contract.Assertf(e == nil || ctx.E == nil, "Only one active interpreter permitted at one time")
// IDEA: we could refactor the way contexts are used so that they are unique per *iteration* inside of a plan,
// rather than shared amongst all possible activations of a single plan. I wouldn't say we have a good
// philosophy on whether we want to support concurrent iteration of the same plan, however, so doing this right
// now seems like overkill. Perhaps down the road, the scenarios will become clearer and we can take action.
ctx.E = e
glog.V(9).Infof("Set current plugctx interpreter to: %v", e)
}
// Request allocates a request sub-context.
func (ctx *Context) Request() context.Context {
// TODO[pulumi/lumi#143]: support cancellation.
return context.TODO()
}
// Close reclaims all resources associated with this context.
func (ctx *Context) Close() error {
err := ctx.Host.Close()
if err != nil && !rpcutil.IsBenignCloseErr(err) {
return err
}
return nil
}