forked from pulumi/pulumi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugins.go
185 lines (166 loc) · 6.8 KB
/
plugins.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// Copyright 2016-2019, 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"
"golang.org/x/sync/errgroup"
"github.com/pulumi/pulumi/pkg/resource/deploy"
"github.com/pulumi/pulumi/pkg/resource/deploy/providers"
"github.com/pulumi/pulumi/pkg/resource/plugin"
"github.com/pulumi/pulumi/pkg/util/contract"
"github.com/pulumi/pulumi/pkg/util/logging"
"github.com/pulumi/pulumi/pkg/workspace"
)
const (
preparePluginLog = 7
preparePluginVerboseLog = 8
)
// pluginSet represents a set of plugins.
type pluginSet map[string]workspace.PluginInfo
// Add adds a plugin to this plugin set.
func (p pluginSet) Add(plug workspace.PluginInfo) {
p[plug.String()] = plug
}
// Union returns the union of this pluginSet with another pluginSet.
func (p pluginSet) Union(other pluginSet) pluginSet {
newSet := newPluginSet()
for _, value := range p {
newSet.Add(value)
}
for _, value := range other {
newSet.Add(value)
}
return newSet
}
// Values returns a slice of all of the plugins contained within this set.
func (p pluginSet) Values() []workspace.PluginInfo {
var plugins []workspace.PluginInfo
for _, value := range p {
plugins = append(plugins, value)
}
return plugins
}
// newPluginSet creates a new empty pluginSet.
func newPluginSet() pluginSet {
return make(map[string]workspace.PluginInfo)
}
// gatherPluginsFromProgram inspects the given program and returns the set of plugins that the program requires to
// function. If the language host does not support this operation, the empty set is returned.
func gatherPluginsFromProgram(plugctx *plugin.Context, prog plugin.ProgInfo) (pluginSet, error) {
logging.V(preparePluginLog).Infof("gatherPluginsFromProgram(): gathering plugins from language host")
set := newPluginSet()
langhostPlugins, err := plugctx.Host.GetRequiredPlugins(prog, plugin.AllPlugins)
if err != nil {
return set, err
}
for _, plug := range langhostPlugins {
logging.V(preparePluginLog).Infof(
"gatherPluginsFromProgram(): plugin %s %s is required by language host", plug.Name, plug.Version)
set.Add(plug)
}
return set, nil
}
// gatherPluginsFromSnapshot inspects the snapshot associated with the given Target and returns the set of plugins
// required to operate on the snapshot. The set of plugins is derived from first-class providers saved in the snapshot
// and the plugins specified in the deployment manifest.
func gatherPluginsFromSnapshot(plugctx *plugin.Context, target *deploy.Target) (pluginSet, error) {
logging.V(preparePluginLog).Infof("gatherPluginsFromSnapshot(): gathering plugins from snapshot")
set := newPluginSet()
if target == nil || target.Snapshot == nil {
logging.V(preparePluginLog).Infof("gatherPluginsFromSnapshot(): no snapshot available, skipping")
return set, nil
}
for _, res := range target.Snapshot.Resources {
urn := res.URN
if !providers.IsProviderType(urn.Type()) {
logging.V(preparePluginVerboseLog).Infof("gatherPluginsFromSnapshot(): skipping %q, not a provider", urn)
continue
}
pkg := providers.GetProviderPackage(urn.Type())
version, err := providers.GetProviderVersion(res.Inputs)
if err != nil {
return set, err
}
logging.V(preparePluginLog).Infof(
"gatherPluginsFromSnapshot(): plugin %s %s is required by first-class provider %q", pkg, version, urn)
set.Add(workspace.PluginInfo{
Name: pkg.String(),
Kind: workspace.ResourcePlugin,
Version: version,
})
}
for _, plug := range target.Snapshot.Manifest.Plugins {
logging.V(preparePluginLog).Infof(
"gatherPluginsFromSnapshot(): plugin %s %s is required by snapshot manifest", plug.Name, plug.Version)
set.Add(plug)
}
return set, nil
}
// ensurePluginsAreInstalled inspects all plugins in the plugin set and, if any plugins are not currently installed,
// uses the given backend client to install them. Installations are processed in parallel, though
// ensurePluginsAreInstalled does not return until all installations are completed.
func ensurePluginsAreInstalled(client deploy.BackendClient, plugins pluginSet) error {
if client == nil {
logging.V(preparePluginLog).Infoln("ensurePluginsAreInstalled(): skipping due to nil client")
return nil
}
logging.V(preparePluginLog).Infof("ensurePluginsAreInstalled(): beginning")
var installTasks errgroup.Group
for _, plug := range plugins.Values() {
_, path, err := workspace.GetPluginPath(plug.Kind, plug.Name, plug.Version)
if err == nil && path != "" {
logging.V(preparePluginLog).Infof(
"ensurePluginsAreInstalled(): plugin %s %s already installed", plug.Name, plug.Version)
continue
}
// Launch an install task asynchronously and add it to the current error group.
info := plug // don't close over the loop induction variable
installTasks.Go(func() error {
logging.V(preparePluginLog).Infof(
"ensurePluginsAreInstalled(): plugin %s %s not installed, doing install", info.Name, info.Version)
return installPlugin(client, info)
})
}
err := installTasks.Wait()
logging.V(preparePluginLog).Infof("ensurePluginsAreInstalled(): completed")
return err
}
// ensurePluginsAreLoaded ensures that all of the plugins in the given plugin set that match the given plugin flags are
// loaded.
func ensurePluginsAreLoaded(plugctx *plugin.Context, plugins pluginSet, kinds plugin.Flags) error {
return plugctx.Host.EnsurePlugins(plugins.Values(), kinds)
}
// installPlugin installs a plugin from the given backend client.
func installPlugin(client deploy.BackendClient, plugin workspace.PluginInfo) error {
contract.Assert(client != nil)
logging.V(preparePluginLog).Infof("installPlugin(%s, %s): beginning install", plugin.Name, plugin.Version)
if plugin.Kind == workspace.LanguagePlugin {
logging.V(preparePluginLog).Infof(
"installPlugin(%s, %s): is a language plugin, skipping install", plugin.Name, plugin.Version)
return nil
}
logging.V(preparePluginVerboseLog).Infof(
"installPlugin(%s, %s): initiating download", plugin.Name, plugin.Version)
stream, err := client.DownloadPlugin(context.TODO(), plugin)
if err != nil {
return err
}
logging.V(preparePluginVerboseLog).Infof(
"installPlugin(%s, %s): extracting tarball to installation directory", plugin.Name, plugin.Version)
if err := plugin.Install(stream); err != nil {
return err
}
logging.V(7).Infof("installPlugin(%s, %s): successfully installed", plugin.Name, plugin.Version)
return nil
}