public async Task <Tuple <T, bool> > TryGetFromStoreAsync <T>(string tag, TimeSpan timeout)
        {
            TimeoutHelper timeoutHelper = new TimeoutHelper(timeout);

            ImageBuilder.TraceSource.WriteInfo(
                TraceType,
                "Downloading {0} from store.",
                tag);

            bool exists = false;
            T    value  = default(T);

            string tempFileName = ImageBuilderUtility.GetTempFileName(this.localImageBuilderRoot);

            try
            {
                if (await this.ImageStore.DoesContentExistAsync(tag, timeoutHelper.GetRemainingTime()))
                {
                    await this.ImageStore.DownloadContentAsync(tag, tempFileName, timeoutHelper.GetRemainingTime(), CopyFlag.AtomicCopy);

                    ImageBuilderUtility.RemoveReadOnlyFlag(tempFileName);
                    value  = ImageBuilderUtility.ReadXml <T>(tempFileName);
                    exists = true;
                }
            }
            finally
            {
                ImageBuilderUtility.DeleteTempLocation(tempFileName);
            }

            return(new Tuple <T, bool>(value, exists));
        }
        public async Task <T> GetFromStoreAsync <T>(string tag, TimeSpan timeout)
        {
            TimeoutHelper timeoutHelper = new TimeoutHelper(timeout);

            ImageBuilder.TraceSource.WriteInfo(
                TraceType,
                "Downloading {0} from store async.",
                tag);

            string tempFileName = ImageBuilderUtility.GetTempFileName(this.localImageBuilderRoot);

            try
            {
                bool doesExist = await this.ImageStore.DoesContentExistAsync(tag, timeoutHelper.GetRemainingTime());

                if (doesExist)
                {
                    await this.ImageStore.DownloadContentAsync(tag, tempFileName, timeoutHelper.GetRemainingTime(), CopyFlag.AtomicCopy);

                    ImageBuilderUtility.RemoveReadOnlyFlag(tempFileName);
                    return(ImageBuilderUtility.ReadXml <T>(tempFileName));
                }
                else
                {
                    throw new FileNotFoundException(
                              string.Format(CultureInfo.CurrentCulture, StringResources.ImageStoreError_FileNotFound, tag));
                }
            }
            finally
            {
                ImageBuilderUtility.DeleteTempLocation(tempFileName);
            }
        }
        public void ProvisionFabric(
            string localCodePath,
            string localConfigPath,
#if !DotNetCoreClrLinux && !DotNetCoreClrIOT
            string configurationCsvFilePath,
#endif
            string infrastructureManifestFilePath,
            bool validateOnly,
            TimeSpan timeout)
        {
            TimeoutHelper timeoutHelper = new TimeoutHelper(timeout);

#if DotNetCoreClrLinux || DotNetCoreClrIOT
            ImageBuilder.TraceSource.WriteInfo(
                TraceType,
                "Starting ProvisionFabric. CodePath:{0}, ConfigPath:{1}, InfrastructureManifestFilePath:{2}, Timeout:{3}",
                localCodePath,
                localConfigPath,
                infrastructureManifestFilePath,
                timeoutHelper.GetRemainingTime());
#else
            ImageBuilder.TraceSource.WriteInfo(
                TraceType,
                "Starting ProvisionFabric. CodePath:{0}, ConfigPath:{1}, ConfigurationCsvFilePath:{2}, InfrastructureManifestFilePath:{3}, Timeout:{4}",
                localCodePath,
                localConfigPath,
                configurationCsvFilePath,
                infrastructureManifestFilePath,
                timeoutHelper.GetRemainingTime());
#endif

            // TODO: Once FabricTest has been modified to generate InfrastructureManifest.xml file, we should not
            // allow InfrastructureManifest file to be optional
            if (string.IsNullOrEmpty(infrastructureManifestFilePath) || !FabricFile.Exists(infrastructureManifestFilePath))
            {
                infrastructureManifestFilePath = null;
                ImageBuilder.TraceSource.WriteWarning(
                    TraceType,
                    "The InfrastrucutreManifestFile:{0} is not found",
                    infrastructureManifestFilePath);
            }

            string codeVersion = string.Empty, clusterManifestVersion = string.Empty;

            if (!string.IsNullOrEmpty(localCodePath))
            {
                codeVersion = GetCodeVersion(localCodePath);
            }

            if (!string.IsNullOrEmpty(localConfigPath))
            {
                try
                {
                    var parameters = new Dictionary <string, dynamic>
                    {
                        { DeploymentParameters.ClusterManifestString, localConfigPath },
                        { DeploymentParameters.InfrastructureManifestString, infrastructureManifestFilePath }
                    };

                    var deploymentParameters = new DeploymentParameters();
                    deploymentParameters.SetParameters(parameters, DeploymentOperations.ValidateClusterManifest);
                    DeploymentOperation.ExecuteOperation(deploymentParameters);
                }
                catch (Exception e)
                {
                    ImageBuilderUtility.TraceAndThrowValidationError(
                        FabricProvisionOperation.TraceType,
                        StringResources.ImageBuilderError_ClusterManifestValidationFailed,
                        e.ToString(),
                        localConfigPath);
                }
            }

            if (!validateOnly)
            {
                timeoutHelper.ThrowIfExpired();

                WinFabStoreLayoutSpecification winFabLayout = WinFabStoreLayoutSpecification.Create();
                if (!string.IsNullOrEmpty(localCodePath))
                {
                    // Upload MSP file
                    string destinationCodePath;
                    if (ImageBuilderUtility.IsInstaller(localCodePath))
                    {
                        destinationCodePath = winFabLayout.GetPatchFile(codeVersion);
                    }
                    else if (ImageBuilderUtility.IsCabFile(localCodePath))
                    {
                        // Upload CAB file
                        destinationCodePath = winFabLayout.GetCabPatchFile(codeVersion);
                    }
                    else
                    {
                        destinationCodePath = winFabLayout.GetCodePackageFolder(codeVersion);
                    }

                    this.imageStoreWrapper.UploadContent(destinationCodePath, localCodePath, timeoutHelper.GetRemainingTime());
                }

                ClusterManifestType clusterManifest = null;
                if (!string.IsNullOrEmpty(localConfigPath))
                {
                    clusterManifest        = ImageBuilderUtility.ReadXml <ClusterManifestType>(localConfigPath, this.validatingXmlReaderSettings);
                    clusterManifestVersion = clusterManifest.Version;
                }

                if (clusterManifest != null)
                {
                    // Upload ClusterManifest file

                    ReplaceDefaultImageStoreConnectionString(clusterManifest);

                    string destinationConfigPath = winFabLayout.GetClusterManifestFile(clusterManifestVersion);
                    imageStoreWrapper.SetToStore <ClusterManifestType>(destinationConfigPath, clusterManifest, timeoutHelper.GetRemainingTime());
                }
            }

#if DotNetCoreClrLinux || DotNetCoreClrIOT
            ImageBuilder.TraceSource.WriteInfo(
                TraceType,
                "Completed ProvisionFabric. CodePath:{0}, ConfigPath:{1}",
                localCodePath,
                localConfigPath);
#else
            ImageBuilder.TraceSource.WriteInfo(
                TraceType,
                "Completed ProvisionFabric. CodePath:{0}, ConfigPath:{1}, ConfigurationCsvFilePath:{2}",
                localCodePath,
                localConfigPath,
                configurationCsvFilePath);
#endif
        }