Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Query language runtime for options during “pulumi new” #16346

Merged
merged 18 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
changes:
- type: feat
scope: cli/new
description: Allow passing runtime options as args in pulumi new
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
changes:
- type: feat
scope: cli/new
description: Query language runtime for options during “pulumi new”
208 changes: 176 additions & 32 deletions pkg/cmd/pulumi/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"path/filepath"
"runtime"
"sort"
"strconv"
"strings"
"unicode"

Expand Down Expand Up @@ -59,26 +60,31 @@ type promptForValueFunc func(yes bool, valueType string, defaultValue string, se

type chooseTemplateFunc func(templates []workspace.Template, opts display.Options) (workspace.Template, error)

type runtimeOptionsFunc func(ctx *plugin.Context, info *workspace.ProjectRuntimeInfo, main string,
opts display.Options, yes, interactive bool, prompt promptForValueFunc) (map[string]interface{}, error)

type newArgs struct {
configArray []string
configPath bool
description string
dir string
force bool
generateOnly bool
interactive bool
name string
offline bool
prompt promptForValueFunc
chooseTemplate chooseTemplateFunc
secretsProvider string
stack string
templateNameOrURL string
yes bool
listTemplates bool
aiPrompt string
aiLanguage httpstate.PulumiAILanguage
templateMode bool
configArray []string
configPath bool
description string
dir string
force bool
generateOnly bool
interactive bool
name string
offline bool
prompt promptForValueFunc
promptRuntimeOptions runtimeOptionsFunc
chooseTemplate chooseTemplateFunc
secretsProvider string
stack string
templateNameOrURL string
yes bool
listTemplates bool
aiPrompt string
aiLanguage httpstate.PulumiAILanguage
templateMode bool
runtimeOptions []string
}

func runNew(ctx context.Context, args newArgs) error {
Expand Down Expand Up @@ -321,17 +327,6 @@ func runNew(ctx context.Context, args newArgs) error {
proj.Name = tokens.PackageName(args.name)
proj.Description = &args.description
proj.Template = nil
// TODO[pulumi/pulumi/issues/16309]: This should be handled by the language runtime.
// Workaround for python, most of our templates don't specify a venv but we want to use one
if proj.Runtime.Name() == "python" {
// If we are using pip (the default toolchain), backfill the virtualenv option.
tc, hasToolchain := proj.Runtime.Options()["toolchain"]
if !hasToolchain || tc == "pip" {
if _, has := proj.Runtime.Options()["virtualenv"]; !has {
proj.Runtime.SetOption("virtualenv", "venv")
}
}
}

// Set the pulumi:template tag to the template name or URL.
templateTag := template.Name
Expand All @@ -342,6 +337,14 @@ func runNew(ctx context.Context, args newArgs) error {
apitype.ProjectTemplateTag: templateTag,
})

for _, opt := range args.runtimeOptions {
parts := strings.Split(strings.TrimSpace(opt), "=")
if len(parts) != 2 {
return fmt.Errorf("invalid runtime option: %s", opt)
}
proj.Runtime.SetOption(parts[0], parts[1])
}

if err = workspace.SaveProject(proj); err != nil {
return fmt.Errorf("saving project: %w", err)
}
Expand All @@ -366,6 +369,39 @@ func runNew(ctx context.Context, args newArgs) error {
fmt.Println()
}

// Query the language runtime for additional options.
if args.promptRuntimeOptions != nil {
span := opentracing.SpanFromContext(ctx)
projinfo := &engine.Projinfo{Proj: proj, Root: root}
_, entryPoint, pluginCtx, err := engine.ProjectInfoContext(
projinfo,
nil,
cmdutil.Diag(),
cmdutil.Diag(),
false,
span,
nil,
)
if err != nil {
return err
}
defer pluginCtx.Close()
options, err := args.promptRuntimeOptions(pluginCtx, &proj.Runtime, entryPoint, opts,
args.yes, args.interactive, args.prompt)
if err != nil {
return err
}
if len(options) > 0 {
// Save the new options
for k, v := range options {
proj.Runtime.SetOption(k, v)
}
if err = workspace.SaveProject(proj); err != nil {
return fmt.Errorf("saving project: %w", err)
}
}
}

// Prompt for config values (if needed) and save.
if !args.generateOnly {
err = handleConfig(
Expand Down Expand Up @@ -456,14 +492,21 @@ func useSpecifiedDir(dir string) (string, error) {
return cwd, nil
}

// isInteractive lets us force interactive mode for testing by setting PULUMI_TEST_INTERACTIVE.
func isInteractive() bool {
test, ok := os.LookupEnv("PULUMI_TEST_INTERACTIVE")
return cmdutil.Interactive() || ok && cmdutil.IsTruthy(test)
}

// newNewCmd creates a New command with default dependencies.
// Intentionally disabling here for cleaner err declaration/assignment.
//
//nolint:vetshadow
func newNewCmd() *cobra.Command {
args := newArgs{
prompt: promptForValue,
chooseTemplate: chooseTemplate,
prompt: promptForValue,
chooseTemplate: chooseTemplate,
promptRuntimeOptions: runtimeOptions,
}

getTemplates := func() ([]workspace.Template, error) {
Expand Down Expand Up @@ -549,6 +592,7 @@ func newNewCmd() *cobra.Command {

args.interactive = cmdutil.Interactive()
julienp marked this conversation as resolved.
Show resolved Hide resolved
args.yes = args.yes || skipConfirmations()
args.interactive = isInteractive()
return runNew(ctx, args)
}),
}
Expand Down Expand Up @@ -619,6 +663,10 @@ func newNewCmd() *cobra.Command {
&args.templateMode, "template-mode", "t", false,
"Run in template mode, which will skip prompting for AI or Template functionality",
)
cmd.PersistentFlags().StringSliceVar(
&args.runtimeOptions, "runtime-options", []string{},
"Additional options for the language runtime (format: key1=value1,key2=value2)",
)

return cmd
}
Expand Down Expand Up @@ -813,6 +861,102 @@ func saveConfig(stack backend.Stack, c config.Map) error {
return saveProjectStack(stack, ps)
}

func runtimeOptions(ctx *plugin.Context, info *workspace.ProjectRuntimeInfo,
julienp marked this conversation as resolved.
Show resolved Hide resolved
main string, opts display.Options, yes, interactive bool, prompt promptForValueFunc,
) (map[string]interface{}, error) {
programInfo := plugin.NewProgramInfo(ctx.Root, ctx.Pwd, main, info.Options())
lang, err := ctx.Host.LanguageRuntime(info.Name(), programInfo)
julienp marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return nil, fmt.Errorf("failed to load language plugin %s: %w", info.Name(), err)
}
defer lang.Close()

options := make(map[string]interface{}, len(info.Options()))
for k, v := range info.Options() {
options[k] = v
}

// Keep querying for prompts until there are no more.
for {
// Update the program info with the latest runtime options.
programInfo := plugin.NewProgramInfo(ctx.Root, ctx.Pwd, main, options)
prompts, err := lang.RuntimeOptionsPrompts(programInfo)
if err != nil {
return nil, fmt.Errorf("failed to get runtime options prompts: %w", err)
}

if len(prompts) == 0 {
break
}

surveycore.DisableColor = true
for _, optionPrompt := range prompts {
if !interactive {
if optionPrompt.Default == nil {
return nil, fmt.Errorf("must provide a runtime option for '%s' in non-interactive mode", optionPrompt.Key)
}
options[optionPrompt.Key] = optionPrompt.Default.Value()
continue
}

if len(optionPrompt.Choices) == 1 {
// We got exactly one choice, so use that as default value without prompting the user.
choice := optionPrompt.Choices[0]
options[optionPrompt.Key] = choice.Value()
} else if len(optionPrompt.Choices) > 0 {
// Pick one among the choices
choices := make([]string, 0, len(optionPrompt.Choices))
// Map choice display string to the actual value
choiceMap := make(map[string]interface{}, len(optionPrompt.Choices))
for _, choice := range optionPrompt.Choices {
choices = append(choices, choice.String())
choiceMap[choice.String()] = choice.Value()
}

var response string
message := opts.Color.Colorize(colors.SpecPrompt + "\r" + optionPrompt.Description + colors.Reset)
if err := survey.AskOne(&survey.Select{
Message: message,
Options: choices,
}, &response, surveyIcons(opts.Color), nil); err != nil {
return nil, err
}
options[optionPrompt.Key] = choiceMap[response]
} else {
// Free form input
val, err := prompt(yes, optionPrompt.Description, "", false, makePromptValidator(optionPrompt), opts)
if err != nil {
return nil, err
}
r, err := plugin.RuntimeOptionValueFromString(optionPrompt.PromptType, val)
if err != nil {
return nil, err
}
options[optionPrompt.Key] = r.Value()
}
}
}

return options, nil
}

func makePromptValidator(prompt plugin.RuntimeOptionPrompt) func(string) error {
return func(value string) error {
switch prompt.PromptType {
case plugin.PromptTypeInt32:
_, err := strconv.ParseInt(value, 10, 32)
if err != nil {
return fmt.Errorf("expected an integer value, got %q", value)
}
case plugin.PromptTypeString:
// No validation needed for strings.
default:
return fmt.Errorf("unexpected prompt type: %v", prompt.PromptType)
}
return nil
}
}

// installDependencies will install dependencies for the project, e.g. by running `npm install` for nodejs projects.
func installDependencies(ctx *plugin.Context, runtime *workspace.ProjectRuntimeInfo, main string) error {
// First make sure the language plugin is present. We need this to load the required resource plugins.
Expand Down
58 changes: 49 additions & 9 deletions pkg/cmd/pulumi/new_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/pulumi/pulumi/pkg/v3/backend/display"
"github.com/pulumi/pulumi/sdk/v3/go/common/apitype"
"github.com/pulumi/pulumi/sdk/v3/go/common/resource/config"
"github.com/pulumi/pulumi/sdk/v3/go/common/resource/plugin"
"github.com/pulumi/pulumi/sdk/v3/go/common/tokens"
"github.com/pulumi/pulumi/sdk/v3/go/common/workspace"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -1097,16 +1098,23 @@ func TestPulumiNewSetsTemplateTag(t *testing.T) {
return workspace.Template{}, errors.New("template not found")
}

runtimeOptionsMock := func(ctx *plugin.Context, info *workspace.ProjectRuntimeInfo,
main string, opts display.Options, interactive, yes bool, prompt promptForValueFunc,
) (map[string]interface{}, error) {
return nil, nil
}

args := newArgs{
interactive: tt.prompted != "",
generateOnly: true,
yes: true,
templateMode: true,
name: projectName,
prompt: promptMock(uniqueProjectName, stackName),
chooseTemplate: chooseTemplateMock,
secretsProvider: "default",
templateNameOrURL: tt.argument,
interactive: tt.prompted != "",
generateOnly: true,
yes: true,
templateMode: true,
name: projectName,
prompt: promptMock(uniqueProjectName, stackName),
promptRuntimeOptions: runtimeOptionsMock,
chooseTemplate: chooseTemplateMock,
secretsProvider: "default",
templateNameOrURL: tt.argument,
}

err := runNew(context.Background(), args)
Expand Down Expand Up @@ -1145,3 +1153,35 @@ func TestSanitizeTemplate(t *testing.T) {
})
}
}

//nolint:paralleltest // changes directory for process
func TestPulumiPromptRuntimeOptions(t *testing.T) {
tempdir := tempProjectDir(t)
chdir(t, tempdir)

runtimeOptionsMock := func(ctx *plugin.Context, info *workspace.ProjectRuntimeInfo,
main string, opts display.Options, interactive, yes bool, prompt promptForValueFunc,
) (map[string]interface{}, error) {
return map[string]interface{}{"someOption": "someValue"}, nil
}

args := newArgs{
interactive: false,
generateOnly: true,
yes: true,
templateMode: true,
name: projectName,
prompt: promptForValue,
promptRuntimeOptions: runtimeOptionsMock,
secretsProvider: "default",
templateNameOrURL: "python",
}

err := runNew(context.Background(), args)
assert.NoError(t, err)

require.NoError(t, err)
proj := loadProject(t, tempdir)
require.Equal(t, 1, len(proj.Runtime.Options()))
require.Equal(t, "someValue", proj.Runtime.Options()["someOption"])
}
7 changes: 7 additions & 0 deletions pkg/resource/deploy/deploytest/languageruntime.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ func (p *languageRuntime) InstallDependencies(info plugin.ProgramInfo) error {
return nil
}

func (p *languageRuntime) RuntimeOptionsPrompts(info plugin.ProgramInfo) ([]plugin.RuntimeOptionPrompt, error) {
if p.closed {
return []plugin.RuntimeOptionPrompt{}, ErrLanguageRuntimeIsClosed
}
return []plugin.RuntimeOptionPrompt{}, nil
}

func (p *languageRuntime) About(info plugin.ProgramInfo) (plugin.AboutInfo, error) {
if p.closed {
return plugin.AboutInfo{}, ErrLanguageRuntimeIsClosed
Expand Down
Loading
Loading