Implement query primitives in the engine
`pulumi query` is designed, essentially, as a souped-up `exec`. We
execute a query program, and add a few convenience constructs (e.g., the
default providers that give you access to things like `getStack`).
Early in the design process, we decided to not re-use the `up`/update
path, both to minimize risk to update operations, and to simplify the
implementation.
This commit will add this "parallel query universe" into the engine
package. In particular, this includes:
* `QuerySource`, which executes the language provider running the query
program, and providing it with some simple constructs, such as the
default provider, which provides access to `getStack`. This is much
like a very simplified `EvalSource`, though notably without any of the
planning/step execution machinery.
* `queryResmon`, which disallows all resource operations, except the
`Invoke` that retrieves the resource outputs of some stack's last
snapshot. This is much like a simplified `resmon`, but without any of
the provider resolution, and without and support for resource
operations generally.
* Various static functions that pull together miscellaneous things
needed to execute a query program. Notably, this includes gathering
language plugins.
2019-04-30 20:07:56 +02:00
|
|
|
// Copyright 2016-2018, Pulumi Corporation.
|
|
|
|
//
|
|
|
|
// Licensed 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 engine
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/opentracing/opentracing-go"
|
|
|
|
"github.com/pulumi/pulumi/pkg/diag"
|
|
|
|
"github.com/pulumi/pulumi/pkg/resource/deploy"
|
|
|
|
"github.com/pulumi/pulumi/pkg/resource/plugin"
|
|
|
|
"github.com/pulumi/pulumi/pkg/util/contract"
|
|
|
|
"github.com/pulumi/pulumi/pkg/util/fsutil"
|
|
|
|
"github.com/pulumi/pulumi/pkg/util/logging"
|
|
|
|
"github.com/pulumi/pulumi/pkg/util/result"
|
|
|
|
)
|
|
|
|
|
|
|
|
type QueryOptions struct {
|
|
|
|
Events eventEmitter // the channel to write events from the engine to.
|
|
|
|
Diag diag.Sink // the sink to use for diag'ing.
|
|
|
|
StatusDiag diag.Sink // the sink to use for diag'ing status messages.
|
|
|
|
host plugin.Host // the plugin host to use for this query.
|
|
|
|
pwd, main string
|
|
|
|
plugctx *plugin.Context
|
|
|
|
}
|
|
|
|
|
|
|
|
func Query(ctx *Context, u UpdateInfo, opts UpdateOptions) result.Result {
|
|
|
|
contract.Require(u != nil, "update")
|
|
|
|
contract.Require(ctx != nil, "ctx")
|
|
|
|
|
|
|
|
defer func() { ctx.Events <- cancelEvent() }()
|
|
|
|
|
|
|
|
tracingSpan := func(opName string, parentSpan opentracing.SpanContext) opentracing.Span {
|
|
|
|
// Create a root span for the operation
|
|
|
|
opts := []opentracing.StartSpanOption{}
|
|
|
|
if opName != "" {
|
|
|
|
opts = append(opts, opentracing.Tag{Key: "operation", Value: opName})
|
|
|
|
}
|
|
|
|
if parentSpan != nil {
|
|
|
|
opts = append(opts, opentracing.ChildOf(parentSpan))
|
|
|
|
}
|
|
|
|
return opentracing.StartSpan("pulumi-query", opts...)
|
|
|
|
}("query", ctx.ParentSpan)
|
|
|
|
defer tracingSpan.Finish()
|
|
|
|
|
|
|
|
emitter, err := makeEventEmitter(ctx.Events, u)
|
|
|
|
if err != nil {
|
|
|
|
return result.FromError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// First, load the package metadata and the deployment target in preparation for executing the package's program
|
|
|
|
// and creating resources. This includes fetching its pwd and main overrides.
|
|
|
|
diag := newEventSink(emitter, false)
|
|
|
|
statusDiag := newEventSink(emitter, true)
|
|
|
|
|
|
|
|
proj, target := u.GetProject(), u.GetTarget()
|
|
|
|
contract.Assert(proj != nil)
|
|
|
|
contract.Assert(target != nil)
|
|
|
|
|
|
|
|
pwd, main, plugctx, err := ProjectInfoContext(&Projinfo{Proj: proj, Root: u.GetRoot()}, opts.host,
|
2019-02-15 23:29:55 +01:00
|
|
|
target, diag, statusDiag, tracingSpan)
|
Implement query primitives in the engine
`pulumi query` is designed, essentially, as a souped-up `exec`. We
execute a query program, and add a few convenience constructs (e.g., the
default providers that give you access to things like `getStack`).
Early in the design process, we decided to not re-use the `up`/update
path, both to minimize risk to update operations, and to simplify the
implementation.
This commit will add this "parallel query universe" into the engine
package. In particular, this includes:
* `QuerySource`, which executes the language provider running the query
program, and providing it with some simple constructs, such as the
default provider, which provides access to `getStack`. This is much
like a very simplified `EvalSource`, though notably without any of the
planning/step execution machinery.
* `queryResmon`, which disallows all resource operations, except the
`Invoke` that retrieves the resource outputs of some stack's last
snapshot. This is much like a simplified `resmon`, but without any of
the provider resolution, and without and support for resource
operations generally.
* Various static functions that pull together miscellaneous things
needed to execute a query program. Notably, this includes gathering
language plugins.
2019-04-30 20:07:56 +02:00
|
|
|
if err != nil {
|
|
|
|
return result.FromError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return query(ctx, u, QueryOptions{
|
|
|
|
Events: emitter,
|
|
|
|
Diag: diag,
|
|
|
|
StatusDiag: statusDiag,
|
|
|
|
host: opts.host,
|
|
|
|
pwd: pwd,
|
|
|
|
main: main,
|
|
|
|
plugctx: plugctx,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func newQuerySource(cancel context.Context, client deploy.BackendClient, u UpdateInfo,
|
|
|
|
opts QueryOptions) (deploy.QuerySource, error) {
|
|
|
|
|
Add `--server` to `pulumi plugin install`
Previously, when the CLI wanted to install a plugin, it used a special
method, `DownloadPlugin` on the `httpstate` backend to actually fetch
the tarball that had the plugin. The reason for this is largely tied
to history, at one point during a closed beta, we required presenting
an API key to download plugins (as a way to enforce folks outside the
beta could not download them) and because of that it was natural to
bake that functionality into the part of the code that interfaced with
the rest of the API from the Pulumi Service.
The downside here is that it means we need to host all the plugins on
`api.pulumi.com` which prevents community folks from being able to
easily write resource providers, since they have to manually manage
the process of downloading a provider to a machine and getting it on
the `$PATH` or putting it in the plugin cache.
To make this easier, we add a `--server` argument you can pass to
`pulumi plugin install` to control the URL that it attempts to fetch
the tarball from. We still have perscriptive guidence on how the
tarball must be
named (`pulumi-[<type>]-[<provider-name>]-vX.Y.Z.tar.gz`) but the base
URL can now be configured.
Folks publishing packages can use install scripts to run `pulumi
plugin install` passing a custom `--server` argument, if needed.
There are two improvements we can make to provide a nicer end to end
story here:
- We can augment the GetRequiredPlugins method on the language
provider to also return information about an optional server to use
when downloading the provider.
- We can pass information about a server to download plugins from as
part of a resource registration or creation of a first class
provider.
These help out in cases where for one reason or another where `pulumi
plugin install` doesn't get run before an update takes place and would
allow us to either do the right thing ahead of time or provide better
error messages with the correct `--server` argument. But, for now,
this unblocks a majority of the cases we care about and provides a
path forward for folks that want to develop and host their own
resource providers.
2019-05-30 22:56:55 +02:00
|
|
|
allPlugins, _, err := installPlugins(u.GetProject(), opts.pwd,
|
Implement query primitives in the engine
`pulumi query` is designed, essentially, as a souped-up `exec`. We
execute a query program, and add a few convenience constructs (e.g., the
default providers that give you access to things like `getStack`).
Early in the design process, we decided to not re-use the `up`/update
path, both to minimize risk to update operations, and to simplify the
implementation.
This commit will add this "parallel query universe" into the engine
package. In particular, this includes:
* `QuerySource`, which executes the language provider running the query
program, and providing it with some simple constructs, such as the
default provider, which provides access to `getStack`. This is much
like a very simplified `EvalSource`, though notably without any of the
planning/step execution machinery.
* `queryResmon`, which disallows all resource operations, except the
`Invoke` that retrieves the resource outputs of some stack's last
snapshot. This is much like a simplified `resmon`, but without any of
the provider resolution, and without and support for resource
operations generally.
* Various static functions that pull together miscellaneous things
needed to execute a query program. Notably, this includes gathering
language plugins.
2019-04-30 20:07:56 +02:00
|
|
|
opts.main,
|
|
|
|
u.GetTarget(), opts.plugctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Once we've installed all of the plugins we need, make sure that all analyzers and language plugins are
|
|
|
|
// loaded up and ready to go. Provider plugins are loaded lazily by the provider registry and thus don't
|
|
|
|
// need to be loaded here.
|
|
|
|
const kinds = plugin.LanguagePlugins
|
|
|
|
if err := ensurePluginsAreLoaded(opts.plugctx, allPlugins, kinds); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// If that succeeded, create a new source that will perform interpretation of the compiled program.
|
|
|
|
// TODO[pulumi/pulumi#88]: we are passing `nil` as the arguments map; we need to allow a way to pass these.
|
|
|
|
return deploy.NewQuerySource(cancel, opts.plugctx, client, &deploy.EvalRunInfo{
|
|
|
|
Proj: u.GetProject(),
|
|
|
|
Pwd: opts.pwd,
|
|
|
|
Program: opts.main,
|
|
|
|
Target: u.GetTarget(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func query(ctx *Context, u UpdateInfo, opts QueryOptions) result.Result {
|
|
|
|
// Make the current working directory the same as the program's, and restore it upon exit.
|
|
|
|
done, chErr := fsutil.Chdir(opts.plugctx.Pwd)
|
|
|
|
if chErr != nil {
|
|
|
|
return result.FromError(chErr)
|
|
|
|
}
|
|
|
|
defer done()
|
|
|
|
|
|
|
|
if res := runQuery(ctx, u, opts); res != nil {
|
|
|
|
if res.IsBail() {
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
return result.Error("an error occurred while running the query")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func runQuery(cancelCtx *Context, u UpdateInfo, opts QueryOptions) result.Result {
|
|
|
|
ctx, cancelFunc := context.WithCancel(context.Background())
|
2019-05-01 00:21:26 +02:00
|
|
|
contract.Ignore(cancelFunc)
|
Implement query primitives in the engine
`pulumi query` is designed, essentially, as a souped-up `exec`. We
execute a query program, and add a few convenience constructs (e.g., the
default providers that give you access to things like `getStack`).
Early in the design process, we decided to not re-use the `up`/update
path, both to minimize risk to update operations, and to simplify the
implementation.
This commit will add this "parallel query universe" into the engine
package. In particular, this includes:
* `QuerySource`, which executes the language provider running the query
program, and providing it with some simple constructs, such as the
default provider, which provides access to `getStack`. This is much
like a very simplified `EvalSource`, though notably without any of the
planning/step execution machinery.
* `queryResmon`, which disallows all resource operations, except the
`Invoke` that retrieves the resource outputs of some stack's last
snapshot. This is much like a simplified `resmon`, but without any of
the provider resolution, and without and support for resource
operations generally.
* Various static functions that pull together miscellaneous things
needed to execute a query program. Notably, this includes gathering
language plugins.
2019-04-30 20:07:56 +02:00
|
|
|
|
|
|
|
src, err := newQuerySource(ctx, cancelCtx.BackendClient, u, opts)
|
|
|
|
if err != nil {
|
|
|
|
return result.FromError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set up a goroutine that will signal cancellation to the plan's plugins if the caller context
|
|
|
|
// is cancelled.
|
|
|
|
go func() {
|
2019-05-01 00:21:26 +02:00
|
|
|
<-cancelCtx.Cancel.Canceled()
|
|
|
|
|
|
|
|
logging.V(4).Infof("engine.runQuery(...): signalling cancellation to providers...")
|
|
|
|
cancelFunc()
|
|
|
|
cancelErr := opts.plugctx.Host.SignalCancellation()
|
|
|
|
if cancelErr != nil {
|
|
|
|
logging.V(4).Infof("engine.runQuery(...): failed to signal cancellation to providers: %v", cancelErr)
|
Implement query primitives in the engine
`pulumi query` is designed, essentially, as a souped-up `exec`. We
execute a query program, and add a few convenience constructs (e.g., the
default providers that give you access to things like `getStack`).
Early in the design process, we decided to not re-use the `up`/update
path, both to minimize risk to update operations, and to simplify the
implementation.
This commit will add this "parallel query universe" into the engine
package. In particular, this includes:
* `QuerySource`, which executes the language provider running the query
program, and providing it with some simple constructs, such as the
default provider, which provides access to `getStack`. This is much
like a very simplified `EvalSource`, though notably without any of the
planning/step execution machinery.
* `queryResmon`, which disallows all resource operations, except the
`Invoke` that retrieves the resource outputs of some stack's last
snapshot. This is much like a simplified `resmon`, but without any of
the provider resolution, and without and support for resource
operations generally.
* Various static functions that pull together miscellaneous things
needed to execute a query program. Notably, this includes gathering
language plugins.
2019-04-30 20:07:56 +02:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
done := make(chan result.Result)
|
|
|
|
go func() {
|
|
|
|
done <- src.Wait()
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Block until query completes.
|
|
|
|
select {
|
|
|
|
case <-cancelCtx.Cancel.Terminated():
|
|
|
|
return result.WrapIfNonNil(cancelCtx.Cancel.TerminateErr())
|
2019-05-11 04:29:42 +02:00
|
|
|
case res := <-done:
|
|
|
|
return res
|
Implement query primitives in the engine
`pulumi query` is designed, essentially, as a souped-up `exec`. We
execute a query program, and add a few convenience constructs (e.g., the
default providers that give you access to things like `getStack`).
Early in the design process, we decided to not re-use the `up`/update
path, both to minimize risk to update operations, and to simplify the
implementation.
This commit will add this "parallel query universe" into the engine
package. In particular, this includes:
* `QuerySource`, which executes the language provider running the query
program, and providing it with some simple constructs, such as the
default provider, which provides access to `getStack`. This is much
like a very simplified `EvalSource`, though notably without any of the
planning/step execution machinery.
* `queryResmon`, which disallows all resource operations, except the
`Invoke` that retrieves the resource outputs of some stack's last
snapshot. This is much like a simplified `resmon`, but without any of
the provider resolution, and without and support for resource
operations generally.
* Various static functions that pull together miscellaneous things
needed to execute a query program. Notably, this includes gathering
language plugins.
2019-04-30 20:07:56 +02:00
|
|
|
}
|
|
|
|
}
|