-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
policy_new.go
366 lines (318 loc) · 12 KB
/
policy_new.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
// Copyright 2016-2022, 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 main
import (
"context"
"errors"
"fmt"
"os"
"sort"
"strings"
survey "github.com/AlecAivazis/survey/v2"
surveycore "github.com/AlecAivazis/survey/v2/core"
"github.com/opentracing/opentracing-go"
"github.com/pulumi/pulumi/pkg/v3/backend/display"
"github.com/pulumi/pulumi/pkg/v3/engine"
"github.com/pulumi/pulumi/sdk/v3/go/common/diag/colors"
"github.com/pulumi/pulumi/sdk/v3/go/common/resource/plugin"
"github.com/pulumi/pulumi/sdk/v3/go/common/util/cmdutil"
"github.com/pulumi/pulumi/sdk/v3/go/common/util/contract"
"github.com/pulumi/pulumi/sdk/v3/go/common/workspace"
"github.com/spf13/cobra"
)
type newPolicyArgs struct {
dir string
force bool
generateOnly bool
interactive bool
offline bool
templateNameOrURL string
yes bool
}
func newPolicyNewCmd() *cobra.Command {
args := newPolicyArgs{
interactive: cmdutil.Interactive(),
}
cmd := &cobra.Command{
Use: "new [template|url]",
SuggestFor: []string{"init", "create"},
Short: "Create a new Pulumi Policy Pack",
Long: "Create a new Pulumi Policy Pack from a template.\n" +
"\n" +
"To create a Policy Pack from a specific template, pass the template name (such as `aws-typescript`\n" +
"or `azure-python`). If no template name is provided, a list of suggested templates will be presented\n" +
"which can be selected interactively.\n" +
"\n" +
"Once you're done authoring the Policy Pack, you will need to publish the pack to your organization.\n" +
"Only organization administrators can publish a Policy Pack.",
Args: cmdutil.MaximumNArgs(1),
Run: cmdutil.RunFunc(func(cmd *cobra.Command, cliArgs []string) error {
ctx := commandContext()
if len(cliArgs) > 0 {
args.templateNameOrURL = cliArgs[0]
}
return runNewPolicyPack(ctx, args)
}),
}
cmd.PersistentFlags().StringVar(
&args.dir, "dir", "",
"The location to place the generated Policy Pack; if not specified, the current directory is used")
cmd.PersistentFlags().BoolVarP(
&args.force, "force", "f", false,
"Forces content to be generated even if it would change existing files")
cmd.PersistentFlags().BoolVarP(
&args.generateOnly, "generate-only", "g", false,
"Generate the Policy Pack only; do not install dependencies")
cmd.PersistentFlags().BoolVarP(
&args.offline, "offline", "o", false,
"Use locally cached templates without making any network requests")
return cmd
}
func runNewPolicyPack(ctx context.Context, args newPolicyArgs) error {
if !args.interactive && !args.yes {
return errors.New("--yes must be passed in to proceed when running in non-interactive mode")
}
// Prepare options.
opts := display.Options{
Color: cmdutil.GetGlobalColorization(),
IsInteractive: args.interactive,
}
// Get the current working directory.
cwd, err := os.Getwd()
if err != nil {
return fmt.Errorf("getting the working directory: %w", err)
}
// If dir was specified, ensure it exists and use it as the
// current working directory.
if args.dir != "" {
cwd, err = useSpecifiedDir(args.dir)
if err != nil {
return err
}
}
// Return an error if the directory isn't empty.
if !args.force {
if err = errorIfNotEmptyDirectory(cwd); err != nil {
return err
}
}
// Retrieve the templates-policy repo.
repo, err := workspace.RetrieveTemplates(args.templateNameOrURL, args.offline, workspace.TemplateKindPolicyPack)
if err != nil {
return err
}
defer func() {
contract.IgnoreError(repo.Delete())
}()
// List the templates from the repo.
templates, err := repo.PolicyTemplates()
if err != nil {
return err
}
var template workspace.PolicyPackTemplate
if len(templates) == 0 {
return errors.New("no templates")
} else if len(templates) == 1 {
template = templates[0]
} else {
if template, err = choosePolicyPackTemplate(templates, opts); err != nil {
return err
}
}
if template.Errored() {
return fmt.Errorf("template '%s' is currently broken: %w", template.Name, template.Error)
}
// Do a dry run, if we're not forcing files to be overwritten.
if !args.force {
if err = workspace.CopyTemplateFilesDryRun(template.Dir, cwd, ""); err != nil {
if os.IsNotExist(err) {
return fmt.Errorf("template '%s' not found: %w", args.templateNameOrURL, err)
}
return err
}
}
// Actually copy the files.
if err = workspace.CopyTemplateFiles(template.Dir, cwd, args.force, "", ""); err != nil {
if os.IsNotExist(err) {
return fmt.Errorf("template '%s' not found: %w", args.templateNameOrURL, err)
}
return err
}
fmt.Println("Created Policy Pack!")
proj, projPath, root, err := readPolicyProject()
if err != nil {
return err
}
// Workaround for python, most of our templates don't specify a venv but we want to use one
if proj.Runtime.Name() == "python" {
// If the template does give virtualenv use it, else default to "venv"
if _, has := proj.Runtime.Options()["virtualenv"]; !has {
proj.Runtime.SetOption("virtualenv", "venv")
}
}
if err = proj.Save(projPath); err != nil {
return fmt.Errorf("saving project: %w", err)
}
// Install dependencies.
if !args.generateOnly {
span := opentracing.SpanFromContext(ctx)
// Bit of a hack here. Creating a plugin context requires a "program project", but we've only got a
// policy project. Ideally we should be able to make a plugin context without any related project. But
// fow now this works.
projinfo := &engine.Projinfo{Proj: &workspace.Project{
Main: proj.Main,
Runtime: proj.Runtime}, Root: root}
pwd, _, pluginCtx, err := engine.ProjectInfoContext(
projinfo,
nil,
cmdutil.Diag(),
cmdutil.Diag(),
false,
span,
)
if err != nil {
return err
}
defer pluginCtx.Close()
if err := installPolicyPackDependencies(pluginCtx, proj, pwd); err != nil {
return err
}
}
fmt.Println(
opts.Color.Colorize(
colors.BrightGreen+colors.Bold+"Your new Policy Pack is ready to go!"+colors.Reset) +
" " + cmdutil.EmojiOr("✨", ""))
fmt.Println()
printPolicyPackNextSteps(proj, root, args.generateOnly, opts)
return nil
}
func installPolicyPackDependencies(ctx *plugin.Context,
proj *workspace.PolicyPackProject, directory string) error {
// First make sure the language plugin is present. We need this to load the required resource plugins.
// TODO: we need to think about how best to version this. For now, it always picks the latest.
lang, err := ctx.Host.LanguageRuntime(ctx.Root, ctx.Pwd, proj.Runtime.Name(), proj.Runtime.Options())
if err != nil {
return fmt.Errorf("failed to load language plugin %s: %w", proj.Runtime.Name(), err)
}
if err = lang.InstallDependencies(directory); err != nil {
return fmt.Errorf("installing dependencies failed; rerun manually to try again, "+
"then run `pulumi up` to perform an initial deployment: %w", err)
}
return nil
}
func printPolicyPackNextSteps(proj *workspace.PolicyPackProject, root string, generateOnly bool, opts display.Options) {
var commands []string
if generateOnly {
// We didn't install dependencies, so instruct the user to do so.
if strings.EqualFold(proj.Runtime.Name(), "nodejs") {
commands = append(commands, "npm install")
} else if strings.EqualFold(proj.Runtime.Name(), "python") {
commands = append(commands, pythonCommands()...)
}
}
if len(commands) == 1 {
installMsg := fmt.Sprintf("To install dependencies for the Policy Pack, run `%s`", commands[0])
installMsg = colors.Highlight(installMsg, commands[0], colors.BrightBlue+colors.Bold)
fmt.Println(opts.Color.Colorize(installMsg))
fmt.Println()
}
if len(commands) > 1 {
fmt.Println("To install dependencies for the Policy Pack, run the following commands:")
fmt.Println()
for i, cmd := range commands {
cmdColors := colors.BrightBlue + colors.Bold + cmd + colors.Reset
fmt.Printf(" %d. %s\n", i+1, opts.Color.Colorize(cmdColors))
}
fmt.Println()
}
usageCommandPreambles :=
[]string{"run the Policy Pack against a Pulumi program, in the directory of the Pulumi program run"}
usageCommands := []string{fmt.Sprintf("pulumi up --policy-pack %s", root)}
if strings.EqualFold(proj.Runtime.Name(), "nodejs") || strings.EqualFold(proj.Runtime.Name(), "python") {
usageCommandPreambles = append(usageCommandPreambles, "publish the Policy Pack, run")
usageCommands = append(usageCommands, "pulumi policy publish [org-name]")
}
contract.Assert(len(usageCommandPreambles) == len(usageCommands))
if len(usageCommands) == 1 {
usageMsg := fmt.Sprintf("Once you're done editing your Policy Pack, to %s `%s`", usageCommandPreambles[0],
usageCommands[0])
usageMsg = colors.Highlight(usageMsg, usageCommands[0], colors.BrightBlue+colors.Bold)
fmt.Println(opts.Color.Colorize(usageMsg))
fmt.Println()
} else {
fmt.Println("Once you're done editing your Policy Pack:")
fmt.Println()
for i, cmd := range usageCommands {
cmdColors := colors.BrightBlue + colors.Bold + cmd + colors.Reset
fmt.Printf(" * To %s `%s`\n", usageCommandPreambles[i], opts.Color.Colorize(cmdColors))
}
fmt.Println()
}
}
// choosePolicyPackTemplate will prompt the user to choose amongst the available templates.
func choosePolicyPackTemplate(templates []workspace.PolicyPackTemplate,
opts display.Options) (workspace.PolicyPackTemplate, error) {
const chooseTemplateErr = "no template selected; please use `pulumi policy new` to choose one"
if !opts.IsInteractive {
return workspace.PolicyPackTemplate{}, errors.New(chooseTemplateErr)
}
// Customize the prompt a little bit (and disable color since it doesn't match our scheme).
surveycore.DisableColor = true
message := "\rPlease choose a template:"
message = opts.Color.Colorize(colors.SpecPrompt + message + colors.Reset)
options, optionToTemplateMap := policyTemplatesToOptionArrayAndMap(templates)
var option string
if err := survey.AskOne(&survey.Select{
Message: message,
Options: options,
PageSize: optimalPageSize(optimalPageSizeOpts{nopts: len(options)}),
}, &option, surveyIcons(opts.Color)); err != nil {
return workspace.PolicyPackTemplate{}, errors.New(chooseTemplateErr)
}
return optionToTemplateMap[option], nil
}
// policyTemplatesToOptionArrayAndMap returns an array of option strings and a map of option strings to policy
// templates. Each option string is made up of the template name and description with some padding in between.
func policyTemplatesToOptionArrayAndMap(
templates []workspace.PolicyPackTemplate) ([]string, map[string]workspace.PolicyPackTemplate) {
// Find the longest name length. Used to add padding between the name and description.
maxNameLength := 0
for _, template := range templates {
if len(template.Name) > maxNameLength {
maxNameLength = len(template.Name)
}
}
// Build the array and map.
var options []string
var brokenOptions []string
nameToTemplateMap := make(map[string]workspace.PolicyPackTemplate)
for _, template := range templates {
// If template is broken, indicate it in the project description.
if template.Errored() {
template.Description = brokenTemplateDescription
}
// Create the option string that combines the name, padding, and description.
option := fmt.Sprintf(fmt.Sprintf("%%%ds %%s", -maxNameLength), template.Name, template.Description)
nameToTemplateMap[option] = template
if template.Errored() {
brokenOptions = append(brokenOptions, option)
} else {
options = append(options, option)
}
}
// After sorting the options, add the broken templates to the end
sort.Strings(options)
options = append(options, brokenOptions...)
return options, nameToTemplateMap
}