Example #1
0
        public void MakeSureDirectoryInDotnetSharedStoreValueOnce()
        {
            var info = new LayerPackageInfo();

            info.Items.Add(new LayerPackageInfo.LayerPackageInfoItem
            {
                Directory = LambdaConstants.DEFAULT_LAYER_OPT_DIRECTORY
            });
            info.Items.Add(new LayerPackageInfo.LayerPackageInfoItem
            {
                Directory = "Custom/Foo"
            });
            info.Items.Add(new LayerPackageInfo.LayerPackageInfoItem
            {
                Directory = LambdaConstants.DEFAULT_LAYER_OPT_DIRECTORY
            });

            var env    = info.GenerateDotnetSharedStoreValue();
            var tokens = env.Split(':');

            Assert.Equal(2, tokens.Length);
            Assert.Equal(1, tokens.Count(x => x.Equals("/opt/" + LambdaConstants.DEFAULT_LAYER_OPT_DIRECTORY + "/")));
            Assert.Equal(1, tokens.Count(x => x.Equals("/opt/Custom/Foo/")));
        }
Example #2
0
        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 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();
            }

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

            // 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));
            }

            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);
        }