private List <string> GetPackagesPath(string localPath, StoreLayoutSpecification storeLayoutSpecification)
        {
            var packagesPath = new List <string>();
            var manifest     = ImageBuilderUtility.ReadXml <ServiceManifestType>(localPath);

            if (manifest.CodePackage != null)
            {
                foreach (var codePackage in manifest.CodePackage)
                {
                    string packagePath = storeLayoutSpecification.GetCodePackageFolder(this.ApplicationTypeName, manifest.Name, codePackage.Name, codePackage.Version);
                    packagesPath.Add(packagePath);
                }
            }

            if (manifest.ConfigPackage != null)
            {
                foreach (var configPackage in manifest.ConfigPackage)
                {
                    string packagePath = storeLayoutSpecification.GetConfigPackageFolder(this.ApplicationTypeName, manifest.Name, configPackage.Name, configPackage.Version);
                    packagesPath.Add(packagePath);
                }
            }

            if (manifest.DataPackage != null)
            {
                foreach (var dataPackage in manifest.DataPackage)
                {
                    string packagePath = storeLayoutSpecification.GetDataPackageFolder(this.ApplicationTypeName, manifest.Name, dataPackage.Name, dataPackage.Version);
                    packagesPath.Add(packagePath);
                }
            }

            return(packagesPath);
        }
        private static void OnFailure(Exception exception, string errorDetailsFile)
        {
            ImageBuilderExe.TraceSource.WriteWarning(
                ImageBuilderExe.TraceType,
                "ImageBuilderExe operation failed with the following exception: {0}",
                exception);

            if (!string.IsNullOrEmpty(errorDetailsFile))
            {
                try
                {
                    var errorDetails = new StringBuilder(exception.Message);
                    var inner        = exception.InnerException;
                    while (inner != null)
                    {
                        errorDetails.AppendFormat(" --> {0}: {1}", inner.GetType().Name, inner.Message);
                        inner = inner.InnerException;
                    }

                    ImageBuilderUtility.WriteStringToFile(
                        errorDetailsFile,
                        ImageBuilderExe.GetFabricErrorCodeOrEFail(exception) + "," + errorDetails.ToString(),
                        false,
                        Encoding.Unicode);
                }
                catch (Exception innerEx)
                {
                    ImageBuilderExe.TraceSource.WriteWarning(
                        ImageBuilderExe.TraceType,
                        "Failed to write error details: {0}",
                        innerEx.ToString());
                }
            }
        }
示例#3
0
        public void TestApplicationPackage(string connectionEndpoint, string applicationPackagePath, string applicationParameter, string imageStoreConnectionString, string certThumbprint)
        {
            string[] parameters = applicationParameter.Split(',');
            IDictionary <string, string> dictionary = new Dictionary <string, string>();

            for (int i = 0; i < parameters.Length / 2; i += 2)
            {
                dictionary.Add(parameters[i], parameters[i + 1]);
            }

            var testRootPath = Path.Combine(Path.GetTempPath(), string.Format(CultureInfo.InvariantCulture, "{0}_{1}", "TestApplicationPackage", DateTime.Now.Ticks));

            try
            {
                var applicationName         = new DirectoryInfo(applicationPackagePath).Name;
                var testRootApplicationPath = Path.Combine(testRootPath, applicationName);
                FabricDirectory.Copy(applicationPackagePath, testRootApplicationPath, true);
                ImageBuilderUtility.RemoveReadOnlyFlag(testRootApplicationPath);

                var fileImageStoreConnectionString = string.Format(CultureInfo.InvariantCulture, "{0}{1}", "file:", testRootPath.TrimEnd(Path.DirectorySeparatorChar));
                var sourceImageStore = ImageStoreFactoryProxy.CreateImageStore(fileImageStoreConnectionString);

                IImageStore destinationImageStore = null;
                if (string.IsNullOrEmpty(imageStoreConnectionString))
                {
                    destinationImageStore = sourceImageStore;
                }
                else
                {
                    destinationImageStore = this.CreateImageStore(connectionEndpoint, imageStoreConnectionString, testRootPath, certThumbprint);
                }

                var imagebuilder = new ImageBuilder(sourceImageStore, destinationImageStore, this.GetFabricFilePath("ServiceFabricServiceModel.xsd"), testRootPath);

                imagebuilder.ValidateApplicationPackage(
                    applicationName,
                    dictionary);
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                FabricDirectory.Delete(testRootPath, true, true);
            }
        }
示例#4
0
        static ArtifactsSpecificationDescription()
        {
            entries = new Collection <Entry>();
            try
            {
                Assembly   currentAssembly = typeof(ArtifactsSpecificationDescription).GetTypeInfo().Assembly;
                var        resourceStream  = currentAssembly.GetManifestResourceStream(ArtifactsSpecification);
                TextReader reader          = new StreamReader(resourceStream);
                string     line;
                while ((line = reader.ReadLine()) != null)
                {
                    if (line.StartsWith("#", StringComparison.Ordinal))
                    {
                        continue;
                    }

                    string[] artifactInfo = line.Split(',');

                    ReleaseAssert.AssertIf(artifactInfo == null || artifactInfo.Count() != 12, "Error when reading ArtifactsSpecification.csv file. Line: {0}", line);

                    Entry entry = new Entry(
                        artifactInfo[0].Trim(),
                        artifactInfo[1].Trim(),
                        artifactInfo[2].Trim(),
                        artifactInfo[3].Trim(),
                        artifactInfo[4].Trim(),
                        ImageBuilderUtility.Equals(artifactInfo[5].Trim(), "TRUE"),
                        ImageBuilderUtility.Equals(artifactInfo[6].Trim(), "TRUE"),
                        ImageBuilderUtility.Equals(artifactInfo[7].Trim(), "TRUE"),
                        (SigningCertificateType)Enum.Parse(typeof(SigningCertificateType), artifactInfo[8].Trim(), true),
                        !ImageBuilderUtility.Equals(artifactInfo[9].Trim(), "no"),
                        artifactInfo[10].Trim(),
                        artifactInfo[11].Trim());

                    entries.Add(entry);
                }
            }
            catch (IOException exception)
            {
                ImageBuilder.TraceSource.WriteError(
                    TraceType,
                    "There was an error when reading the ArtifactsSpecification.csv file: {0}",
                    exception);
                throw;
            }
        }
示例#5
0
        public void CopyApplicationPackage(string connectionEndpoint, string applicationPackagePath, string imageStoreConnectionString, string relativeDestinationPath, string certThumbprint)
        {
            IImageStore imageStore = CreateImageStore(connectionEndpoint, imageStoreConnectionString, Path.GetTempPath(), certThumbprint);

            if (string.IsNullOrEmpty(relativeDestinationPath))
            {
                relativeDestinationPath = new DirectoryInfo(applicationPackagePath).Name;
            }

            ImageBuilderUtility.ValidateRelativePath(relativeDestinationPath);

            imageStore.UploadContent(
                relativeDestinationPath,
                applicationPackagePath,
                AzureCliProxy.DefaultTimeSpan,
                CopyFlag.AtomicCopy,
                false);
        }
示例#6
0
        internal bool ValidateImageStorePath(string path)
        {
            string[] reservedDirectoryNames = new string[]
            {
                Constants.StoreFolder,
                Constants.WindowsFabricStoreFolder
            };

            try
            {
                ImageBuilderUtility.ValidateRelativePath(
                    path,
                    reservedDirectoryNames);
            }
            catch (FabricImageBuilderReservedDirectoryException ex)
            {
                return(this.ShouldContinue(ex.Message, string.Empty));
            }

            return(true);
        }
示例#7
0
        protected void TestSFApplicationPackage(string applicationPackagePath, Hashtable applicationParameters, string imageStoreConnectionString)
        {
            var testRootPath = Path.Combine(Path.GetTempPath(), string.Format(CultureInfo.InvariantCulture, "{0}_{1}", Constants.BaseTestRootFolderName, Stopwatch.GetTimestamp()));

            try
            {
                var absolutePath            = this.GetAbsolutePath(applicationPackagePath);
                var applicationName         = this.GetPackageName(absolutePath);
                var testRootApplicationPath = Path.Combine(testRootPath, applicationName);
                FabricDirectory.Copy(absolutePath, testRootApplicationPath, true);
                ImageBuilderUtility.RemoveReadOnlyFlag(testRootApplicationPath);

                var fileImageStoreConnectionString =
                    string.Format(
                        CultureInfo.InvariantCulture,
                        "{0}{1}",
                        Constants.ImageStoreConnectionFileType,
                        testRootPath.TrimEnd(Path.DirectorySeparatorChar));
                var sourceImageStore = ImageStoreFactoryProxy.CreateImageStore(fileImageStoreConnectionString);

                IImageStore destinationImageStore = null;
                if (string.IsNullOrEmpty(imageStoreConnectionString))
                {
                    destinationImageStore = sourceImageStore;
                }
                else
                {
                    destinationImageStore = this.CreateImageStore(imageStoreConnectionString, testRootPath);
                }

                var imagebuilder = new ImageBuilder(
                    sourceImageStore,
                    destinationImageStore,
                    this.GetFabricFilePath(Constants.ServiceModelSchemaFileName),
                    testRootPath);

                // Set SFVolumeDiskServiceEnabled to true so that client side validation does the right thing
                // if corresponding UseServiceFabricReplicatedStore attribute is set.
                //
                // We always have a final validation at runtime to catch any issues.
                imagebuilder.IsSFVolumeDiskServiceEnabled = true;

                imagebuilder.ValidateApplicationPackage(
                    applicationName,
                    this.GetDictionary(applicationParameters));

                this.WriteObject(true);
            }
            catch (AggregateException exception)
            {
                exception.Handle((ae) =>
                {
                    this.WriteObject(false);
                    this.ThrowTerminatingError(
                        ae,
                        Constants.TestApplicationPackageErrorId,
                        null);
                    return(true);
                });
            }
            catch (Exception exception)
            {
                this.WriteObject(false);
                this.ThrowTerminatingError(
                    exception,
                    Constants.TestApplicationPackageErrorId,
                    null);
            }
            finally
            {
                FabricDirectory.Delete(testRootPath, true, true);
            }
        }
        /// return 0 if program completes successfully, -1 otherwise.
        public static int Main(string[] args)
        {
            if (args.Length < 1)
            {
                Console.WriteLine("This is for internal use only");
                return(-1);
            }

#if DEBUG
            // To add a sleep time while debugging add a "Sleep" key (in milliseconds) to the following registry location:  HKLM\\Software\\Microsoft\\Service Fabric
            try
            {
                using (RegistryKey registryKey = Registry.LocalMachine.OpenSubKey(FabricConstants.FabricRegistryKeyPath, true))
                {
                    var sleepRegVal = registryKey.GetValue("Sleep");

                    if (sleepRegVal != null)
                    {
                        int sleepinms = int.Parse(sleepRegVal.ToString());
                        ImageBuilderExe.TraceSource.WriteNoise(ImageBuilderExe.TraceType, "ImageBuilderExe: Sleep time requested: {0}", sleepinms.ToString());

                        Thread.Sleep(sleepinms);
                    }
                }
            }
            catch (Exception e)
            {
                ImageBuilderExe.TraceSource.WriteNoise(ImageBuilderExe.TraceType, "ImageBuilderExe: Exception code:{0}, msg:{1} when attempting to read registry value \"Sleep\" in {2}.", e.HResult, e.Message, FabricConstants.FabricRegistryKeyPath);
            }
#endif

            // Parameters
            // /schemaPath:<The xsd schema file of Windows Fabric ServiceModel> - optional. Default: <Current Working Directory>\ServiceFabricServiceModel.xsd
            // /workingDir:<The root of working directory of the node> - optional. Default: <Current Working Directory>
            // /storeRoot:<The root path of ImageStore> - optional
            // /output: <The root folder|file where output will be placed> - required
            // /input: <The root folder|file where input will be read from> - required for Delete
            // /progress: <The file where operation progress will be read from> - optional. Used by BuildApplicationType and DownloadAndBuildApplicationType.
            // /operation:<BuildApplicationTypeInfo | BuildApplicationType | BuildApplication | BuildComposeDeployment | ValidateComposeDeployment | UpgradeApplication | GetFabricVersion | ProvisionFabric | UpgradeFabric | Delete | TestErrorDetails | DownloadAndBuildApplicationType> - required
            // /timeout:<ticks> - optional

            /* ImageBuilder arguments for DownloadAndBuildApplicationType */
            // /downloadPath: <path to .sfpkg file from a store that supports GET method> - required
            // /appTypeName:<Application Type Name> - required. Expected application type name in the downloaded package.
            // /appTypeVersion:<Application Type Version> - required. Expected application type version in the downloaded package.
            // /progress: <The file where operation progress will be read from> - optional.
            // /output: <The root folder|file where output will be placed> - required
            // /timeout:<ticks> - optional

            /* ImageBuilder arguments for BuildComposeDeployment */
            // /cf:<Compose File path> - required
            // /of:<Overrides File path> - optional
            // /appTypeName:<Application Type Name> - required
            // /appTypeVersion:<Application Type Version> - required
            // /output: <The root folder|file where output application package will be placed> - required
            // /ocf:<Output Merged compose file path> - required
            // /repoUserName:<Repository User Name> - optional
            // /repoPwd:<Repository Password> - optional
            // /pwdEncrypted:<Is Repository Password encrypted> - optional
            // /cleanup:<Cleanup the compose files> - optional
            // /timeout:<ticks> - optional

            /* ImageBuilder arguments for ValidateComposeDeployment */
            // /cf:<Compose File path> - required
            // /of:<Overrides File path> - optional
            // /appTypeName:<Application Type Name> - optional
            // /appTypeVersion:<Application Type Version> - optional
            // /cleanup:<Cleanup the compose files> - optional
            // /timeout:<ticks> - optional

            /* ImageBuilder arguments for Application */
            // /appTypeName:<Application Type Name> - required for BuildApplication and UpgradeApplication operation
            // /appTypeVersion:<Application Type Version> - required for BuildApplication and UpgradeApplication operation
            // /appId:<Application ID> - required for BuildApplication and UpgradeApplication operation
            // /nameUri:<Name URI> - required for BuildApplication operation
            // /appParam:<semi-colon separated key-value pair>. Multiple such parameters can be passed. Key cannot have ';'.
            // /buildPath: <The root folder of build layout> - required for ApplicationTypeInfo and BuildApplicationType operation
            // /currentAppInstanceVersion: <The current app instance of the Application that needs to be upgraded> - required for UpgradeApplication operation

            /* ImageBuilder arguments for Fabric Upgrade */
            // /codePath: <The path to MSP for FabricUpgrade>
            // /configPath: <The path to ClusterManifest for FabricUpgrade>
            // /currentFabricVersion: <The current FabricVersion>
            // /targetFabricVersion: <The target FabricVersion>
            // /im: <Path to InfrastructureManifest.xml file>
            //

            Dictionary <string, string> commandArgs = null;
            Exception exception        = null;
            string    errorDetailsFile = null;

            try
            {
                commandArgs = ParseParameters(args);

                // Ensure that required parameters are present
                EnsureRequiredCommandLineParameters(commandArgs);

                Dictionary <string, string> applicationParameters = ParseAppParameters(commandArgs);

                errorDetailsFile = commandArgs.ContainsKey(StringConstants.ErrorDetails)
                    ? commandArgs[StringConstants.ErrorDetails]
                    : null;

                string workingDirectory = commandArgs.ContainsKey(StringConstants.WorkingDir)
                    ? commandArgs[StringConstants.WorkingDir]
                    : Directory.GetCurrentDirectory();

                string imageStoreConnectionString;
                if (commandArgs.ContainsKey(StringConstants.StoreRoot))
                {
                    imageStoreConnectionString = commandArgs[StringConstants.StoreRoot];
                }
                else
                {
                    ImageBuilderExe.TraceSource.WriteInfo(ImageBuilderExe.TraceType, "Loading ImageStoreConnectionString from config.");

                    bool isEncrypted;
                    NativeConfigStore configStore = NativeConfigStore.FabricGetConfigStore();
                    var configValue = configStore.ReadString("Management", "ImageStoreConnectionString", out isEncrypted);
                    if (isEncrypted)
                    {
                        var secureString    = NativeConfigStore.DecryptText(configValue);
                        var secureCharArray = FabricValidatorUtility.SecureStringToCharArray(secureString);
                        imageStoreConnectionString = new string(secureCharArray);
                    }
                    else
                    {
                        imageStoreConnectionString = configValue;
                    }

                    if (string.IsNullOrEmpty(imageStoreConnectionString))
                    {
                        throw new ArgumentException(StringResources.Error_MissingImageStoreConnectionStringInManifest);
                    }
                }

                StringBuilder stringToTrace = new StringBuilder();
                foreach (var commandArg in commandArgs)
                {
                    // Skipping tracing StoreRoot since it could be a secret value
                    if (!ImageBuilderUtility.Equals(commandArg.Key, StringConstants.StoreRoot))
                    {
                        stringToTrace.AppendFormat("{0}:{1}", commandArg.Key, commandArg.Value);
                        stringToTrace.AppendLine();
                    }
                }

                ImageBuilderExe.TraceSource.WriteInfo(
                    ImageBuilderExe.TraceType,
                    "ImageBuilderExe called with - {0}",
                    stringToTrace.ToString());

                string currentExecutingDirectory = Path.GetDirectoryName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName);
                string schemaPath = commandArgs.ContainsKey(StringConstants.SchemaPath)
                    ? commandArgs[StringConstants.SchemaPath]
                    : Path.Combine(
                    currentExecutingDirectory,
                    StringConstants.DefaultSchemaPath);

                TimeSpan timeout = commandArgs.ContainsKey(StringConstants.Timeout)
                    ? TimeSpan.FromTicks(long.Parse(commandArgs[StringConstants.Timeout]))
                    : TimeSpan.MaxValue;

                Timer timer = null;
                if (timeout != TimeSpan.MaxValue)
                {
                    ImageBuilderExe.TraceSource.WriteInfo(
                        ImageBuilderExe.TraceType,
                        "ImageBuilderExe enabled timeout monitor: {0}",
                        timeout);

                    timer = new Timer(OnTimeout, errorDetailsFile, timeout, TimeSpan.FromMilliseconds(-1));
                }

                IImageStore imageStore = ImageStoreFactoryProxy.CreateImageStore(
                    imageStoreConnectionString,
                    null,
                    workingDirectory,
                    true /*isInternal*/);

                ImageBuilder imageBuilder = new ImageBuilder(imageStore, schemaPath, workingDirectory);

                string operationValue = commandArgs[StringConstants.Operation];

                bool sfVolumeDiskServiceEnabled = commandArgs.ContainsKey(StringConstants.SFVolumeDiskServiceEnabled)
                        ? ImageBuilderUtility.ConvertString <bool>(commandArgs[StringConstants.SFVolumeDiskServiceEnabled])
                        : false;

                imageBuilder.IsSFVolumeDiskServiceEnabled = sfVolumeDiskServiceEnabled;

                if (ImageBuilderUtility.Equals(operationValue, StringConstants.OperationDownloadAndBuildApplicationType))
                {
                    string progressFile = commandArgs.ContainsKey(StringConstants.Progress) ? commandArgs[StringConstants.Progress] : null;
                    bool   shouldSkipChecksumValidation = commandArgs.ContainsKey(StringConstants.DisableChecksumValidation) ? ImageBuilderUtility.ConvertString <bool>(commandArgs[StringConstants.DisableChecksumValidation]) : false;

                    imageBuilder.DownloadAndBuildApplicationType(
                        commandArgs[StringConstants.DownloadPath],
                        commandArgs[StringConstants.AppTypeName],
                        commandArgs[StringConstants.AppTypeVersion],
                        commandArgs[StringConstants.Output],
                        timeout,
                        progressFile,
                        shouldSkipChecksumValidation);
                }
                else if (ImageBuilderUtility.Equals(operationValue, StringConstants.OperationBuildApplicationTypeInfo))
                {
                    imageBuilder.GetApplicationTypeInfo(commandArgs[StringConstants.BuildPath], timeout, commandArgs[StringConstants.Output]);
                }
                else if (ImageBuilderUtility.Equals(operationValue, StringConstants.OperationBuildApplicationType))
                {
                    string outputFile   = commandArgs.ContainsKey(StringConstants.Output) ? commandArgs[StringConstants.Output] : null;
                    string progressFile = commandArgs.ContainsKey(StringConstants.Progress) ? commandArgs[StringConstants.Progress] : null;
                    bool   shouldSkipChecksumValidation = commandArgs.ContainsKey(StringConstants.DisableChecksumValidation) ? ImageBuilderUtility.ConvertString <bool>(commandArgs[StringConstants.DisableChecksumValidation]) : false;
                    bool   shouldSkipServerSideCopy     = commandArgs.ContainsKey(StringConstants.DisableServerSideCopy) ? ImageBuilderUtility.ConvertString <bool>(commandArgs[StringConstants.DisableServerSideCopy]) : false;

                    imageBuilder.BuildApplicationType(commandArgs[StringConstants.BuildPath], timeout, outputFile, progressFile, shouldSkipChecksumValidation, shouldSkipServerSideCopy);
                }
                else if (ImageBuilderUtility.Equals(operationValue, StringConstants.OperationBuildSingleInstanceApplication))
                {
                    bool generateDnsNames = commandArgs.ContainsKey(StringConstants.GenerateDnsNames)
                        ? ImageBuilderUtility.ConvertString <bool>(commandArgs[StringConstants.GenerateDnsNames])
                        : false;

                    bool useOpenNetworkConfig = commandArgs.ContainsKey(StringConstants.UseOpenNetworkConfig)
                        ? ImageBuilderUtility.ConvertString <bool>(commandArgs[StringConstants.UseOpenNetworkConfig])
                        : false;

                    Application application = null;

                    using (StreamReader file = File.OpenText(commandArgs[StringConstants.SingleInstanceApplicationDescription]))
                    {
                        JsonSerializer serializer = new JsonSerializer();
                        serializer.Converters.Add(new DiagnosticsSinkJsonConverter());
                        serializer.Converters.Add(new VolumeMountJsonConverter());

                        application =
                            (Application)serializer.Deserialize(file, typeof(Application));
                    }

                    GenerationConfig config = null;
                    if (commandArgs.ContainsKey(StringConstants.GenerationConfig))
                    {
                        using (StreamReader file = File.OpenText(commandArgs[StringConstants.GenerationConfig]))
                        {
                            JsonSerializer serializer = new JsonSerializer();
                            config =
                                (GenerationConfig)serializer.Deserialize(file, typeof(GenerationConfig));
                        }
                    }

                    imageBuilder.BuildSingleInstanceApplication(
                        application,
                        commandArgs[StringConstants.AppTypeName],
                        commandArgs[StringConstants.AppTypeVersion],
                        commandArgs[StringConstants.AppId],
                        new Uri(commandArgs[StringConstants.AppName]),
                        generateDnsNames,
                        timeout,
                        commandArgs[StringConstants.BuildPath],
                        commandArgs[StringConstants.Output],
                        useOpenNetworkConfig,
                        config);
                }
                else if (ImageBuilderUtility.Equals(operationValue, StringConstants.OperationBuildSingleInstanceApplicationForUpgrade))
                {
                    bool generateDnsNames = commandArgs.ContainsKey(StringConstants.GenerateDnsNames)
                        ? ImageBuilderUtility.ConvertString <bool>(commandArgs[StringConstants.GenerateDnsNames])
                        : false;

                    bool useOpenNetworkConfig = commandArgs.ContainsKey(StringConstants.UseOpenNetworkConfig)
                        ? ImageBuilderUtility.ConvertString <bool>(commandArgs[StringConstants.UseOpenNetworkConfig])
                        : false;

                    Application application = null;

                    using (StreamReader file = File.OpenText(commandArgs[StringConstants.SingleInstanceApplicationDescription]))
                    {
                        JsonSerializer serializer = new JsonSerializer();
                        serializer.Converters.Add(new VolumeMountJsonConverter());
                        application =
                            (Application)serializer.Deserialize(file, typeof(Application));
                    }

                    GenerationConfig config = null;
                    if (commandArgs.ContainsKey(StringConstants.GenerationConfig))
                    {
                        using (StreamReader file = File.OpenText(commandArgs[StringConstants.GenerationConfig]))
                        {
                            JsonSerializer serializer = new JsonSerializer();
                            config =
                                (GenerationConfig)serializer.Deserialize(file, typeof(GenerationConfig));
                        }
                    }

                    imageBuilder.BuildSingleInstanceApplicationForUpgrade(
                        application,
                        commandArgs[StringConstants.AppTypeName],
                        commandArgs[StringConstants.CurrentAppTypeVersion],
                        commandArgs[StringConstants.TargetAppTypeVersion],
                        commandArgs[StringConstants.AppId],
                        int.Parse(commandArgs[StringConstants.CurrentAppInstanceVersion], CultureInfo.InvariantCulture),
                        new Uri(commandArgs[StringConstants.AppName]),
                        generateDnsNames,
                        timeout,
                        commandArgs[StringConstants.BuildPath],
                        commandArgs[StringConstants.Output],
                        useOpenNetworkConfig,
                        config);
                }
                else if (ImageBuilderUtility.Equals(operationValue, StringConstants.OperationBuildApplication))
                {
                    imageBuilder.BuildApplication(
                        commandArgs[StringConstants.AppTypeName],
                        commandArgs[StringConstants.AppTypeVersion],
                        commandArgs[StringConstants.AppId],
                        new Uri(commandArgs[StringConstants.NameUri]),
                        applicationParameters,
                        timeout,
                        commandArgs[StringConstants.Output]);
                }
                else if (ImageBuilderUtility.Equals(operationValue, StringConstants.OperationUpgradeApplication))
                {
                    imageBuilder.UpgradeApplication(
                        commandArgs[StringConstants.AppId],
                        commandArgs[StringConstants.AppTypeName],
                        int.Parse(commandArgs[StringConstants.CurrentAppInstanceVersion], CultureInfo.InvariantCulture),
                        commandArgs[StringConstants.AppTypeVersion],
                        applicationParameters,
                        timeout,
                        commandArgs[StringConstants.Output]);
                }
                else if (ImageBuilderUtility.Equals(operationValue, StringConstants.OperationDelete))
                {
                    imageBuilder.Delete(commandArgs[StringConstants.Input], timeout);
                }
                else if (ImageBuilderUtility.Equals(operationValue, StringConstants.OperationCleanupApplicationPackage))
                {
                    imageBuilder.DeleteApplicationPackage(commandArgs[StringConstants.BuildPath], timeout);
                }
                else if (ImageBuilderUtility.Equals(operationValue, StringConstants.OperationGetFabricVersion))
                {
                    imageBuilder.GetFabricVersionInfo(
                        commandArgs[StringConstants.CodePath],
                        commandArgs[StringConstants.ConfigPath],
                        timeout,
                        commandArgs[StringConstants.Output]);
                }
                else if (ImageBuilderUtility.Equals(operationValue, StringConstants.OperationProvisionFabric))
                {
                    string configurationCsvFilePath = Path.Combine(currentExecutingDirectory, StringConstants.ConfigurationsFileName);
                    imageBuilder.ProvisionFabric(
                        commandArgs[StringConstants.CodePath],
                        commandArgs[StringConstants.ConfigPath],
                        configurationCsvFilePath,
                        commandArgs[StringConstants.InfrastructureManifestFile],
                        timeout);
                }
                else if (ImageBuilderUtility.Equals(operationValue, StringConstants.OperationGetClusterManifest))
                {
                    imageBuilder.GetClusterManifestContents(
                        commandArgs[StringConstants.ConfigVersion],
                        commandArgs[StringConstants.Output],
                        timeout);
                }
                else if (ImageBuilderUtility.Equals(operationValue, StringConstants.OperationUpgradeFabric))
                {
                    string configurationCsvFilePath = Path.Combine(currentExecutingDirectory, StringConstants.ConfigurationsFileName);

                    imageBuilder.UpgradeFabric(
                        commandArgs[StringConstants.CurrentFabricVersion],
                        commandArgs[StringConstants.TargetFabricVersion],
                        configurationCsvFilePath,
                        timeout,
                        commandArgs[StringConstants.Output]);
                }
                else if (ImageBuilderUtility.Equals(operationValue, StringConstants.OperationGetManifests))
                {
                    imageBuilder.GetManifests(commandArgs[StringConstants.Input], timeout);
                }
                else if (ImageBuilderUtility.Equals(operationValue, StringConstants.OperationValidateComposeDeployment))
                {
                    bool cleanupComposeFiles = commandArgs.ContainsKey(StringConstants.CleanupComposeFiles) && ImageBuilderUtility.ConvertString <bool>(commandArgs[StringConstants.CleanupComposeFiles]);

                    HashSet <string> ignoredKeys;
                    imageBuilder.ValidateComposeDeployment(
                        commandArgs[StringConstants.ComposeFilePath],
                        commandArgs.ContainsKey(StringConstants.OverrideFilePath) ? commandArgs[StringConstants.OverrideFilePath] : null,
                        commandArgs.ContainsKey(StringConstants.AppName) ? commandArgs[StringConstants.AppName] : null,
                        commandArgs.ContainsKey(StringConstants.AppTypeName) ? commandArgs[StringConstants.AppTypeName] : null,
                        commandArgs.ContainsKey(StringConstants.AppTypeVersion) ? commandArgs[StringConstants.AppTypeVersion] : null,
                        timeout,
                        out ignoredKeys,
                        cleanupComposeFiles);
                }
                else if (ImageBuilderUtility.Equals(operationValue, StringConstants.OperationBuildComposeDeployment))
                {
                    bool cleanupComposeFiles          = commandArgs.ContainsKey(StringConstants.CleanupComposeFiles) && ImageBuilderUtility.ConvertString <bool>(commandArgs[StringConstants.CleanupComposeFiles]);
                    bool shouldSkipChecksumValidation = commandArgs.ContainsKey(StringConstants.DisableChecksumValidation) && ImageBuilderUtility.ConvertString <bool>(commandArgs[StringConstants.DisableChecksumValidation]);
                    bool isPasswordEncrypted          = commandArgs.ContainsKey(StringConstants.IsRepositoryPasswordEncrypted) && ImageBuilderUtility.ConvertString <bool>(commandArgs[StringConstants.IsRepositoryPasswordEncrypted]);

                    bool generateDnsNames = commandArgs.ContainsKey(StringConstants.GenerateDnsNames) && ImageBuilderUtility.ConvertString <bool>(commandArgs[StringConstants.GenerateDnsNames]);

                    imageBuilder.BuildComposeDeploymentPackage(
                        commandArgs[StringConstants.ComposeFilePath],
                        commandArgs.ContainsKey(StringConstants.OverrideFilePath) ? commandArgs[StringConstants.OverrideFilePath] : null,
                        timeout,
                        commandArgs.ContainsKey(StringConstants.AppName) ? commandArgs[StringConstants.AppName] : null,
                        commandArgs[StringConstants.AppTypeName],
                        commandArgs[StringConstants.AppTypeVersion],
                        commandArgs.ContainsKey(StringConstants.RepositoryUserName) ? commandArgs[StringConstants.RepositoryUserName] : null,
                        commandArgs.ContainsKey(StringConstants.RepositoryPassword) ? commandArgs[StringConstants.RepositoryPassword] : null,
                        isPasswordEncrypted,
                        generateDnsNames,
                        commandArgs[StringConstants.OutputComposeFilePath],
                        commandArgs[StringConstants.Output],
                        cleanupComposeFiles,
                        shouldSkipChecksumValidation);
                }
                else if (ImageBuilderUtility.Equals(operationValue, StringConstants.OperationBuildComposeApplicationForUpgrade))
                {
                    bool cleanupComposeFiles          = commandArgs.ContainsKey(StringConstants.CleanupComposeFiles) && ImageBuilderUtility.ConvertString <bool>(commandArgs[StringConstants.CleanupComposeFiles]);
                    bool shouldSkipChecksumValidation = commandArgs.ContainsKey(StringConstants.DisableChecksumValidation) && ImageBuilderUtility.ConvertString <bool>(commandArgs[StringConstants.DisableChecksumValidation]);
                    bool isPasswordEncrypted          = commandArgs.ContainsKey(StringConstants.IsRepositoryPasswordEncrypted) && ImageBuilderUtility.ConvertString <bool>(commandArgs[StringConstants.IsRepositoryPasswordEncrypted]);

                    bool generateDnsNames = commandArgs.ContainsKey(StringConstants.GenerateDnsNames) && ImageBuilderUtility.ConvertString <bool>(commandArgs[StringConstants.GenerateDnsNames]);

                    imageBuilder.BuildComposeDeploymentPackageForUpgrade(
                        commandArgs[StringConstants.ComposeFilePath],
                        commandArgs.ContainsKey(StringConstants.OverrideFilePath) ? commandArgs[StringConstants.OverrideFilePath] : null,
                        timeout,
                        commandArgs.ContainsKey(StringConstants.AppName) ? commandArgs[StringConstants.AppName] : null,
                        commandArgs[StringConstants.AppTypeName],
                        commandArgs[StringConstants.CurrentAppTypeVersion],
                        commandArgs[StringConstants.TargetAppTypeVersion],
                        commandArgs.ContainsKey(StringConstants.RepositoryUserName) ? commandArgs[StringConstants.RepositoryUserName] : null,
                        commandArgs.ContainsKey(StringConstants.RepositoryPassword) ? commandArgs[StringConstants.RepositoryPassword] : null,
                        isPasswordEncrypted,
                        generateDnsNames,
                        commandArgs[StringConstants.OutputComposeFilePath],
                        commandArgs[StringConstants.Output],
                        cleanupComposeFiles,
                        shouldSkipChecksumValidation);
                }
                else if (ImageBuilderUtility.Equals(operationValue, StringConstants.TestErrorDetails))
                {
                    throw new Exception(StringConstants.TestErrorDetails);
                }
                else
                {
                    throw new ArgumentException(
                              string.Format(
                                  CultureInfo.CurrentCulture,
                                  StringResources.Error_ImageBuilderExeCommandLineInvalidOperation,
                                  StringConstants.Operation,
                                  operationValue));
                }
            }
            catch (AggregateException ae)
            {
                exception = ae.InnerException;

                StringBuilder exceptionStringBuilder = new StringBuilder("Aggregate exception has ");
                exceptionStringBuilder.Append(ae.InnerExceptions.Count);
                exceptionStringBuilder.Append(" exceptions. ");
                ae.InnerExceptions.ForEach(innerException => exceptionStringBuilder.AppendLine(innerException.Message));

                ImageBuilderExe.TraceSource.WriteWarning(
                    ImageBuilderExe.TraceType,
                    "AggregateException: {0}",
                    exceptionStringBuilder.ToString());
            }
            catch (Exception e)
            {
                exception = e;
            }

            if (exception != null)
            {
                OnFailure(exception, errorDetailsFile);

                return(unchecked ((int)NativeTypes.FABRIC_ERROR_CODE.FABRIC_E_IMAGEBUILDER_UNEXPECTED_ERROR));
            }
            else
            {
                ImageBuilderExe.TraceSource.WriteInfo(
                    ImageBuilderExe.TraceType,
                    "ImageBuilderExe operation completed successfully.");

                return(0);
            }
        }
        public static void EnsureRequiredCommandLineParameters(Dictionary <string, string> commandArgs)
        {
            HashSet <string> requiredImageBuilderKeys = new HashSet <string>(StringComparer.OrdinalIgnoreCase)
            {
                StringConstants.Operation
            };

            foreach (string requiredKey in requiredImageBuilderKeys)
            {
                if (!commandArgs.ContainsKey(requiredKey))
                {
                    throw new ArgumentException(
                              string.Format(
                                  CultureInfo.CurrentCulture,
                                  StringResources.Error_ImageBuilderExeMissingRequiredArgument,
                                  requiredKey));
                }
            }

            string operationValue = commandArgs[StringConstants.Operation];

            if (ImageBuilderUtility.Equals(operationValue, StringConstants.OperationBuildApplicationTypeInfo))
            {
                string[] requiredBuildApplicationTypeInfoParams = new string[]
                {
                    StringConstants.BuildPath,
                    StringConstants.Output
                };

                CheckRequiredCommandLineArguments(requiredBuildApplicationTypeInfoParams, operationValue, commandArgs);
            }
            else if (ImageBuilderUtility.Equals(operationValue, StringConstants.OperationBuildApplicationType))
            {
                string[] requiredBuildApplicationTypeParams = new string[]
                {
                    StringConstants.BuildPath
                };

                CheckRequiredCommandLineArguments(requiredBuildApplicationTypeParams, operationValue, commandArgs);
            }
            else if (ImageBuilderUtility.Equals(operationValue, StringConstants.OperationBuildApplication))
            {
                string[] requiredBuildApplicationParams = new string[]
                {
                    StringConstants.AppId,
                    StringConstants.AppTypeName,
                    StringConstants.AppTypeVersion,
                    StringConstants.NameUri,
                    StringConstants.Output
                };

                CheckRequiredCommandLineArguments(requiredBuildApplicationParams, operationValue, commandArgs);
            }
            else if (ImageBuilderUtility.Equals(operationValue, StringConstants.OperationValidateComposeDeployment))
            {
                string[] requiredBuildApplicationParams = new string[]
                {
                    StringConstants.ComposeFilePath
                };

                CheckRequiredCommandLineArguments(requiredBuildApplicationParams, operationValue, commandArgs);
            }
            else if (ImageBuilderUtility.Equals(operationValue, StringConstants.OperationBuildComposeDeployment))
            {
                string[] requiredBuildApplicationParams = new string[]
                {
                    StringConstants.ComposeFilePath,
                    StringConstants.AppTypeName,
                    StringConstants.AppTypeVersion,
                    StringConstants.Output,
                    StringConstants.OutputComposeFilePath
                };

                CheckRequiredCommandLineArguments(requiredBuildApplicationParams, operationValue, commandArgs);
            }
            else if (ImageBuilderUtility.Equals(operationValue, StringConstants.OperationUpgradeApplication))
            {
                string[] requiredUpgradeApplicationParams = new string[]
                {
                    StringConstants.AppId,
                    StringConstants.AppTypeName,
                    StringConstants.AppTypeVersion,
                    StringConstants.CurrentAppInstanceVersion,
                    StringConstants.Output
                };

                CheckRequiredCommandLineArguments(requiredUpgradeApplicationParams, operationValue, commandArgs);
            }
            else if (ImageBuilderUtility.Equals(operationValue, StringConstants.OperationDelete))
            {
                string[] requiredUpgradeApplicationParams = new string[]
                {
                    StringConstants.Input
                };

                CheckRequiredCommandLineArguments(requiredUpgradeApplicationParams, operationValue, commandArgs);
            }
            else if (ImageBuilderUtility.Equals(operationValue, StringConstants.OperationGetFabricVersion))
            {
                string[] requiredGetFabricVersionParams = new string[]
                {
                    StringConstants.Output
                };

                CheckRequiredCommandLineArguments(requiredGetFabricVersionParams, operationValue, commandArgs);
            }
            else if (ImageBuilderUtility.Equals(operationValue, StringConstants.OperationProvisionFabric))
            {
                if (!commandArgs.ContainsKey(StringConstants.CodePath) && !commandArgs.ContainsKey(StringConstants.ConfigPath))
                {
                    throw new ArgumentException(
                              string.Format(
                                  CultureInfo.CurrentCulture,
                                  StringResources.Error_ImageBuilderExeMissingCodeAndConfigPath,
                                  operationValue));
                }

                string[] requiredProvisionFabricParams = new string[]
                {
                    StringConstants.InfrastructureManifestFile
                };

                CheckRequiredCommandLineArguments(requiredProvisionFabricParams, operationValue, commandArgs);
            }
            else if (ImageBuilderUtility.Equals(operationValue, StringConstants.OperationUpgradeFabric))
            {
                string[] requiredUpgradeFabricParams = new string[]
                {
                    StringConstants.CurrentFabricVersion,
                    StringConstants.TargetFabricVersion
                };

                CheckRequiredCommandLineArguments(requiredUpgradeFabricParams, operationValue, commandArgs);
            }
        }
        public void TestValidateNATNetworkPoliciesForManifestGenerationOnOpenForComposeScenario()
        {
            ApplicationTypeInformation applicationTypeInfo = new ApplicationTypeInformation("AppType", "1.0");
            Application app = new Application();

            app.name = "TestApp";
            Management.ImageBuilder.SingleInstance.CodePackage cpkg = new Management.ImageBuilder.SingleInstance.CodePackage();
            cpkg.image = "image";
            cpkg.name  = "CodePackage";
            var endpoints = new List <Endpoint>();
            var endpoint  = new Endpoint();

            endpoint.name = "publicListener";
            endpoint.port = 8080;
            endpoints.Add(endpoint);
            cpkg.endpoints = endpoints;
            var resource = new Resources();
            var req      = new ResourceDescription();

            req.cpu           = 0.5;
            req.memoryInGB    = 1;
            resource.requests = req;
            cpkg.resources    = resource;
            Service svc = new Service();

            svc.properties.codePackages = new List <Management.ImageBuilder.SingleInstance.CodePackage>();
            svc.properties.codePackages.Add(cpkg);
            svc.name = "Svc1";
            NetworkRef nref = new NetworkRef();

            nref.name = StringConstants.NatNetworkName;
            EndpointRef endpointRef = new EndpointRef();

            endpointRef.name  = "publicListener";
            nref.endpointRefs = new List <EndpointRef>()
            {
                endpointRef
            };
            svc.properties.networkRefs = new List <NetworkRef>();
            svc.properties.networkRefs.Add(nref);
            app.properties.services = new List <Service>();
            app.properties.services.Add(svc);

            string applicationTestDir        = Path.Combine(TestUtility.TestDirectory, Guid.NewGuid().ToString());
            string currentExecutingDirectory = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);

            this.imageBuilder.BuildSingleInstanceApplication(app, "AppType", "1.0", "TestApp", new Uri("fabric:/TestApp"), false, TimeSpan.MaxValue, applicationTestDir, null, true, false, null, new GenerationConfig());
            string expectedAppManifest = Path.Combine(applicationTestDir, "ApplicationManifest.xml");

            var validatingXmlReaderSettings = new XmlReaderSettings();

            validatingXmlReaderSettings.ValidationType = ValidationType.Schema;
            validatingXmlReaderSettings.Schemas.Add(StringConstants.FabricNamespace, Path.Combine(currentExecutingDirectory, StringConstants.DefaultSchemaPath));
            validatingXmlReaderSettings.XmlResolver = null;
            var applicationManifestType = ImageBuilderUtility.ReadXml <ApplicationManifestType>(expectedAppManifest, validatingXmlReaderSettings);

            ApplicationManifestTypeServiceManifestImport[] servicemanifestimports = applicationManifestType.ServiceManifestImport;
            foreach (var serviceManifestImport in servicemanifestimports)
            {
                var policies = serviceManifestImport.Policies;
                foreach (var policy in policies)
                {
                    if (policy.GetType() == typeof(ContainerHostPoliciesType))
                    {
                        var ContainerHostPoliciesTypeItems = ((ContainerHostPoliciesType)policy).Items;
                        foreach (var item in ContainerHostPoliciesTypeItems)
                        {
                            if (item.GetType() == typeof(PortBindingType))
                            {
                                var portBindingType = (PortBindingType)item;
                                Verify.AreEqual("publicListener", portBindingType.EndpointRef);
                                Verify.AreEqual(8080, portBindingType.ContainerPort);
                            }
                        }
                    }
                }
            }
        }