示例#1
0
        private Lambda.PackageType DeterminePackageType()
        {
            var strPackageType = this.GetStringValueOrDefault(this.PackageType, LambdaDefinedCommandOptions.ARGUMENT_PACKAGE_TYPE, false);

            return(LambdaUtilities.DeterminePackageType(strPackageType));
        }
        protected override async Task <bool> PerformActionAsync()
        {
            EnsureInProjectDirectory();

            // Disable interactive since this command is intended to be run as part of a pipeline.
            this.DisableInteractive = true;

            var projectLocation = this.GetStringValueOrDefault(this.ProjectLocation, CommonDefinedCommandOptions.ARGUMENT_PROJECT_LOCATION, false);

            Lambda.PackageType packageType = LambdaUtilities.DeterminePackageType(this.GetStringValueOrDefault(this.PackageType, LambdaDefinedCommandOptions.ARGUMENT_PACKAGE_TYPE, false));
            if (packageType == Lambda.PackageType.Image)
            {
                var pushResults = await PushLambdaImageAsync();

                if (!pushResults.Success)
                {
                    if (pushResults.LastToolsException != null)
                    {
                        throw pushResults.LastToolsException;
                    }

                    return(false);
                }
            }
            else
            {
                var layerVersionArns = this.GetStringValuesOrDefault(this.LayerVersionArns, LambdaDefinedCommandOptions.ARGUMENT_FUNCTION_LAYERS, false);
                LayerPackageInfo layerPackageInfo = null;
                if (layerVersionArns != null)
                {
                    if (!this.DisableRegionAndCredentialsCheck)
                    {
                        // Region and credentials are only required if using layers. This is new behavior so do a preemptive check when there are layers to
                        // see if region and credentials are set. If they are not set give a specific error message about region and credentials required
                        // when using layers.
                        try
                        {
                            base.DetermineAWSRegion();
                        }
                        catch (Exception)
                        {
                            throw new ToolsException("Region is required for the package command when layers are specified. The layers must be inspected to see how they affect packaging.", ToolsException.CommonErrorCode.RegionNotConfigured);
                        }
                        try
                        {
                            base.DetermineAWSCredentials();
                        }
                        catch (Exception)
                        {
                            throw new ToolsException("AWS credentials are required for the package command when layers are specified. The layers must be inspected to see how they affect packaging.", ToolsException.CommonErrorCode.InvalidCredentialConfiguration);
                        }
                    }

                    layerPackageInfo = await LambdaUtilities.LoadLayerPackageInfos(this.Logger, this.LambdaClient, this.S3Client, layerVersionArns);
                }
                else
                {
                    layerPackageInfo = new LayerPackageInfo();
                }

                // Release will be the default configuration if nothing set.
                var configuration = this.GetStringValueOrDefault(this.Configuration, CommonDefinedCommandOptions.ARGUMENT_CONFIGURATION, false);

                var targetFramework = this.GetStringValueOrDefault(this.TargetFramework, CommonDefinedCommandOptions.ARGUMENT_FRAMEWORK, false);
                if (string.IsNullOrEmpty(targetFramework))
                {
                    targetFramework = Utilities.LookupTargetFrameworkFromProjectFile(Utilities.DetermineProjectLocation(this.WorkingDirectory, projectLocation));

                    // If we still don't know what the target framework is ask the user what targetframework to use.
                    // This is common when a project is using multi targeting.
                    if (string.IsNullOrEmpty(targetFramework))
                    {
                        targetFramework = this.GetStringValueOrDefault(this.TargetFramework, CommonDefinedCommandOptions.ARGUMENT_FRAMEWORK, true);
                    }
                }

                var msbuildParameters   = this.GetStringValueOrDefault(this.MSBuildParameters, CommonDefinedCommandOptions.ARGUMENT_MSBUILD_PARAMETERS, false);
                var disableVersionCheck = this.GetBoolValueOrDefault(this.DisableVersionCheck, LambdaDefinedCommandOptions.ARGUMENT_DISABLE_VERSION_CHECK, false).GetValueOrDefault();

                var zipArchivePath = GetStringValueOrDefault(this.OutputPackageFileName, LambdaDefinedCommandOptions.ARGUMENT_OUTPUT_PACKAGE, false);

                string publishLocation;
                var    success = LambdaPackager.CreateApplicationBundle(this.DefaultConfig, this.Logger, this.WorkingDirectory, projectLocation, configuration, targetFramework, msbuildParameters, disableVersionCheck, layerPackageInfo, out publishLocation, ref zipArchivePath);
                if (!success)
                {
                    this.Logger.WriteLine("Failed to create application package");
                    return(false);
                }


                this.Logger.WriteLine("Lambda project successfully packaged: " + zipArchivePath);
                var dotnetSharedStoreValue = layerPackageInfo.GenerateDotnetSharedStoreValue();
                if (!string.IsNullOrEmpty(dotnetSharedStoreValue))
                {
                    this.NewDotnetSharedStoreValue = dotnetSharedStoreValue;

                    this.Logger.WriteLine($"\nWarning: You must set the {LambdaConstants.ENV_DOTNET_SHARED_STORE} environment variable when deploying the package. " +
                                          "If not set the layers specified will not be located by the .NET Core runtime. The trailing '/' is required.");
                    this.Logger.WriteLine($"{LambdaConstants.ENV_DOTNET_SHARED_STORE}: {dotnetSharedStoreValue}");
                }
            }

            return(true);
        }