Ejemplo n.º 1
0
        /// <summary>
        /// Performs a dry-run update to a stack, returning pending changes.
        /// <para/>
        /// https://www.pulumi.com/docs/reference/cli/pulumi_preview/
        /// </summary>
        /// <param name="options">Options to customize the behavior of the update.</param>
        /// <param name="cancellationToken">A cancellation token.</param>
        public async Task <PreviewResult> PreviewAsync(
            PreviewOptions?options = null,
            CancellationToken cancellationToken = default)
        {
            var execKind = ExecKind.Local;
            var program  = this.Workspace.Program;
            var logger   = this.Workspace.Logger;
            var args     = new List <string>()
            {
                "preview"
            };

            if (options != null)
            {
                if (options.Program != null)
                {
                    program = options.Program;
                }

                if (options.Logger != null)
                {
                    logger = options.Logger;
                }

                if (options.ExpectNoChanges is true)
                {
                    args.Add("--expect-no-changes");
                }

                if (options.Diff is true)
                {
                    args.Add("--diff");
                }

                if (options.Plan != null)
                {
                    args.Add("--save-plan");
                    args.Add(options.Plan);
                }

                if (options.Replace?.Any() == true)
                {
                    foreach (var item in options.Replace)
                    {
                        args.Add("--replace");
                        args.Add(item);
                    }
                }

                if (options.TargetDependents is true)
                {
                    args.Add("--target-dependents");
                }


                ApplyUpdateOptions(options, args);
            }

            InlineLanguageHost?inlineHost = null;

            SummaryEvent?summaryEvent = null;

            var onEvent = options?.OnEvent;

            void OnPreviewEvent(EngineEvent @event)
            {
                if (@event.SummaryEvent != null)
                {
                    summaryEvent = @event.SummaryEvent;
                }

                onEvent?.Invoke(@event);
            }

            try
            {
                if (program != null)
                {
                    execKind   = ExecKind.Inline;
                    inlineHost = new InlineLanguageHost(program, logger, cancellationToken);
                    await inlineHost.StartAsync().ConfigureAwait(false);

                    var port = await inlineHost.GetPortAsync().ConfigureAwait(false);

                    args.Add($"--client=127.0.0.1:{port}");
                }

                args.Add("--exec-kind");
                args.Add(execKind);

                CommandResult result;
                try
                {
                    result = await this.RunCommandAsync(
                        args,
                        options?.OnStandardOutput,
                        options?.OnStandardError,
                        OnPreviewEvent,
                        cancellationToken).ConfigureAwait(false);
                }
                catch
                {
                    if (inlineHost != null && inlineHost.TryGetExceptionInfo(out var exceptionInfo))
                    {
                        exceptionInfo.Throw();
                    }

                    // this won't be hit if we have an inline
                    // program exception
                    throw;
                }

                if (summaryEvent is null)
                {
                    throw new NoSummaryEventException("No summary of changes for 'preview'");
                }

                return(new PreviewResult(
                           result.StandardOutput,
                           result.StandardError,
                           summaryEvent.ResourceChanges));
            }
            finally
            {
                if (inlineHost != null)
                {
                    await inlineHost.DisposeAsync().ConfigureAwait(false);
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Performs a dry-run update to a stack, returning pending changes.
        /// <para/>
        /// https://www.pulumi.com/docs/reference/cli/pulumi_preview/
        /// </summary>
        /// <param name="options">Options to customize the behavior of the update.</param>
        /// <param name="cancellationToken">A cancellation token.</param>
        public async Task <PreviewResult> PreviewAsync(
            PreviewOptions?options = null,
            CancellationToken cancellationToken = default)
        {
            await this.Workspace.SelectStackAsync(this.Name, cancellationToken).ConfigureAwait(false);

            var execKind = ExecKind.Local;
            var program  = this.Workspace.Program;
            var args     = new List <string>()
            {
                "preview"
            };

            if (options != null)
            {
                if (options.Program != null)
                {
                    program = options.Program;
                }

                if (!string.IsNullOrWhiteSpace(options.Message))
                {
                    args.Add("--message");
                    args.Add(options.Message);
                }

                if (options.ExpectNoChanges is true)
                {
                    args.Add("--expect-no-changes");
                }

                if (options.Diff is true)
                {
                    args.Add("--diff");
                }

                if (options.Replace?.Any() == true)
                {
                    foreach (var item in options.Replace)
                    {
                        args.Add("--replace");
                        args.Add(item);
                    }
                }

                if (options.Target?.Any() == true)
                {
                    foreach (var item in options.Target)
                    {
                        args.Add("--target");
                        args.Add(item);
                    }
                }

                if (options.TargetDependents is true)
                {
                    args.Add("--target-dependents");
                }

                if (options.Parallel.HasValue)
                {
                    args.Add("--parallel");
                    args.Add(options.Parallel.Value.ToString());
                }
            }

            InlineLanguageHost?inlineHost = null;

            SummaryEvent?summaryEvent = null;

            var onEvent = options?.OnEvent;

            void OnPreviewEvent(EngineEvent @event)
            {
                if (@event.SummaryEvent != null)
                {
                    summaryEvent = @event.SummaryEvent;
                }

                onEvent?.Invoke(@event);
            }

            try
            {
                if (program != null)
                {
                    execKind   = ExecKind.Inline;
                    inlineHost = new InlineLanguageHost(program, cancellationToken);
                    await inlineHost.StartAsync().ConfigureAwait(false);

                    var port = await inlineHost.GetPortAsync().ConfigureAwait(false);

                    args.Add($"--client=127.0.0.1:{port}");
                }

                args.Add("--exec-kind");
                args.Add(execKind);

                var result = await this.RunCommandAsync(args, options?.OnStandardOutput, options?.OnStandardError, OnPreviewEvent, cancellationToken).ConfigureAwait(false);

                if (inlineHost != null && inlineHost.TryGetExceptionInfo(out var exceptionInfo))
                {
                    exceptionInfo.Throw();
                }

                if (summaryEvent is null)
                {
                    throw new NoSummaryEventException("No summary of changes for 'preview'");
                }

                return(new PreviewResult(
                           result.StandardOutput,
                           result.StandardError,
                           summaryEvent.ResourceChanges));
            }
            finally
            {
                if (inlineHost != null)
                {
                    await inlineHost.DisposeAsync().ConfigureAwait(false);
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Creates or updates the resources in a stack by executing the program in the Workspace.
        /// <para/>
        /// https://www.pulumi.com/docs/reference/cli/pulumi_up/
        /// </summary>
        /// <param name="options">Options to customize the behavior of the update.</param>
        /// <param name="cancellationToken">A cancellation token.</param>
        public async Task <UpResult> UpAsync(
            UpOptions?options = null,
            CancellationToken cancellationToken = default)
        {
            var execKind = ExecKind.Local;
            var program  = this.Workspace.Program;
            var logger   = this.Workspace.Logger;
            var args     = new List <string>()
            {
                "up",
                "--yes",
                "--skip-preview",
            };

            if (options != null)
            {
                if (options.Program != null)
                {
                    program = options.Program;
                }

                if (options.Logger != null)
                {
                    logger = options.Logger;
                }

                if (options.ExpectNoChanges is true)
                {
                    args.Add("--expect-no-changes");
                }

                if (options.Diff is true)
                {
                    args.Add("--diff");
                }

                if (options.Plan != null)
                {
                    args.Add("--plan");
                    args.Add(options.Plan);
                }

                if (options.Replace?.Any() == true)
                {
                    foreach (var item in options.Replace)
                    {
                        args.Add("--replace");
                        args.Add(item);
                    }
                }

                if (options.TargetDependents is true)
                {
                    args.Add("--target-dependents");
                }

                ApplyUpdateOptions(options, args);
            }

            InlineLanguageHost?inlineHost = null;

            try
            {
                if (program != null)
                {
                    execKind   = ExecKind.Inline;
                    inlineHost = new InlineLanguageHost(program, logger, cancellationToken);
                    await inlineHost.StartAsync().ConfigureAwait(false);

                    var port = await inlineHost.GetPortAsync().ConfigureAwait(false);

                    args.Add($"--client=127.0.0.1:{port}");
                }

                args.Add("--exec-kind");
                args.Add(execKind);

                CommandResult upResult;
                try
                {
                    upResult = await this.RunCommandAsync(
                        args,
                        options?.OnStandardOutput,
                        options?.OnStandardError,
                        options?.OnEvent,
                        cancellationToken).ConfigureAwait(false);
                }
                catch
                {
                    if (inlineHost != null && inlineHost.TryGetExceptionInfo(out var exceptionInfo))
                    {
                        exceptionInfo.Throw();
                    }

                    // this won't be hit if we have an inline
                    // program exception
                    throw;
                }

                var output = await this.GetOutputsAsync(cancellationToken).ConfigureAwait(false);

                var summary = await this.GetInfoAsync(cancellationToken, options?.ShowSecrets).ConfigureAwait(false);

                return(new UpResult(
                           upResult.StandardOutput,
                           upResult.StandardError,
                           summary !,
                           output));
            }
            finally
            {
                if (inlineHost != null)
                {
                    await inlineHost.DisposeAsync().ConfigureAwait(false);
                }
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Creates or updates the resources in a stack by executing the program in the Workspace.
        /// <para/>
        /// https://www.pulumi.com/docs/reference/cli/pulumi_up/
        /// </summary>
        /// <param name="options">Options to customize the behavior of the update.</param>
        /// <param name="cancellationToken">A cancellation token.</param>
        public async Task <UpResult> UpAsync(
            UpOptions?options = null,
            CancellationToken cancellationToken = default)
        {
            await this.Workspace.SelectStackAsync(this.Name, cancellationToken).ConfigureAwait(false);

            var execKind = ExecKind.Local;
            var program  = this.Workspace.Program;
            var args     = new List <string>()
            {
                "up",
                "--yes",
                "--skip-preview",
            };

            if (options != null)
            {
                if (options.Program != null)
                {
                    program = options.Program;
                }

                if (!string.IsNullOrWhiteSpace(options.Message))
                {
                    args.Add("--message");
                    args.Add(options.Message);
                }

                if (options.ExpectNoChanges is true)
                {
                    args.Add("--expect-no-changes");
                }

                if (options.Diff is true)
                {
                    args.Add("--diff");
                }

                if (options.Replace?.Any() == true)
                {
                    foreach (var item in options.Replace)
                    {
                        args.Add("--replace");
                        args.Add(item);
                    }
                }

                if (options.Target?.Any() == true)
                {
                    foreach (var item in options.Target)
                    {
                        args.Add("--target");
                        args.Add(item);
                    }
                }

                if (options.TargetDependents is true)
                {
                    args.Add("--target-dependents");
                }

                if (options.Parallel.HasValue)
                {
                    args.Add("--parallel");
                    args.Add(options.Parallel.Value.ToString());
                }
            }

            InlineLanguageHost?inlineHost = null;

            try
            {
                if (program != null)
                {
                    execKind   = ExecKind.Inline;
                    inlineHost = new InlineLanguageHost(program, cancellationToken);
                    await inlineHost.StartAsync().ConfigureAwait(false);

                    var port = await inlineHost.GetPortAsync().ConfigureAwait(false);

                    args.Add($"--client=127.0.0.1:{port}");
                }

                args.Add("--exec-kind");
                args.Add(execKind);

                var upResult = await this.RunCommandAsync(args, options?.OnStandardOutput, options?.OnStandardError, options?.OnEvent, cancellationToken).ConfigureAwait(false);

                if (inlineHost != null && inlineHost.TryGetExceptionInfo(out var exceptionInfo))
                {
                    exceptionInfo.Throw();
                }

                var output = await this.GetOutputsAsync(cancellationToken).ConfigureAwait(false);

                var summary = await this.GetInfoAsync(cancellationToken).ConfigureAwait(false);

                return(new UpResult(
                           upResult.StandardOutput,
                           upResult.StandardError,
                           summary !,
                           output));
            }
            finally
            {
                if (inlineHost != null)
                {
                    await inlineHost.DisposeAsync().ConfigureAwait(false);
                }
            }
        }