private static void RemoveNodeConfigurationInner(bool deleteLog, FabricPackageType fabricPackageType, string machineName)
        {
            var parameters = new Dictionary <string, dynamic>
            {
                { DeploymentParameters.MachineNameString, machineName }
            };

            // Create DeploymentParameters object with conditional retrieval of FabricBinRoot (to work around infinite recursion due to invalid reflection)
            bool setBinRoot           = fabricPackageType != FabricPackageType.XCopyPackage;
            var  deploymentParameters = new DeploymentParameters(setBinRoot);

            deploymentParameters.DeleteLog             = deleteLog;
            deploymentParameters.DeploymentPackageType = fabricPackageType;
            deploymentParameters.SetParameters(parameters, DeploymentOperations.RemoveNodeConfig);

            try
            {
                DeploymentOperation.ExecuteOperation(deploymentParameters);
            }
            catch (Exception e)
            {
                DeployerTrace.WriteError("{0}", e.ToString());
                throw;
            }
        }
 internal static void RemoveNodeConfiguration(bool deleteLog, FabricPackageType fabricPackageType, string machineName)
 {
     try
     {
         RemoveNodeConfigurationInner(deleteLog, fabricPackageType, machineName);
     }
     catch (Exception exception)
     {
         DeployerTrace.WriteError(exception.ToString());
         throw;
     }
 }
 internal static void NewNodeConfiguration(
     string clusterManifestPath,
     string infrastructureManifestPath,
     string jsonClusterConfigPath, /* Value is null if it's not Standalone deployment */
     string fabricDataRoot,
     string fabricLogRoot,
     string fabricHostCredentialUser,
     SecureString fabricHostCredentialPassword,
     bool runFabricHostServiceAsManual,
     bool removeExistingConfiguration,
     FabricPackageType fabricPackageType,
     string fabricPackageRoot,
     string machineName,
     string bootstrapPackagePath)
 {
     try
     {
         NewNodeConfigurationInner(
             clusterManifestPath,
             infrastructureManifestPath,
             jsonClusterConfigPath,
             fabricDataRoot,
             fabricLogRoot,
             fabricHostCredentialUser,
             fabricHostCredentialPassword,
             runFabricHostServiceAsManual,
             removeExistingConfiguration,
             fabricPackageType,
             fabricPackageRoot,
             machineName,
             bootstrapPackagePath);
     }
     catch (Exception exception)
     {
         DeployerTrace.WriteError(exception.ToString());
         throw;
     }
 }
        private void CopyBaselinePackageIfPathExists(string bootstrapPackagePath, string fabricDataRoot, FabricPackageType fabricPackageType, string machineName)
        {
            string targetPath = Helpers.GetRemotePath(fabricDataRoot, machineName);

            DeployerTrace.WriteInfo("Copying {0} from {1} to {2}", fabricPackageType == FabricPackageType.MSI ? "BootstrapMSI" : "Standalone CAB", bootstrapPackagePath, fabricDataRoot);
            if (fabricPackageType == FabricPackageType.XCopyPackage)
            {
                targetPath = Path.Combine(targetPath, Constants.FileNames.BaselineCab);
            }
            else
            {
                targetPath = Path.Combine(targetPath, Path.GetFileName(bootstrapPackagePath));
            }

            using (FileStream sourceStream = File.Open(bootstrapPackagePath, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                using (FileStream destStream = File.Create(targetPath))
                {
                    sourceStream.CopyTo(destStream);
                }
            }
            if (!File.Exists(targetPath))
            {
                string message = string.Format(StringResources.Error_SFErrorCreatingCab, targetPath, machineName);
                DeployerTrace.WriteError(message);
                throw new FileNotFoundException(message);
            }
            string adminConfigPath = Environment.GetEnvironmentVariable(Constants.FabricTestAdminConfigPath);

            if (!string.IsNullOrEmpty(adminConfigPath))
            {
                CopyNewAdminConfigToFabricDataRoot(fabricDataRoot, adminConfigPath, machineName);
            }
        }
        internal void WriteTargetInformationFile(string clusterManifestLocation, string infrastructureManifestLocation, string fabricDataRoot, string machineName, string packageLocation, FabricPackageType fabricPackageType, string sourcePackageLocation)
        {
            ReleaseAssert.AssertIfNull(fabricDataRoot, "DataRoot Should not be null after configuration");
            TargetInformationType targetInformation = new TargetInformationType
            {
                TargetInstallation = new WindowsFabricDeploymentInformation
                {
                    TargetVersion                  = Utility.GetCurrentCodeVersion(sourcePackageLocation),
                    ClusterManifestLocation        = clusterManifestLocation,
                    UpgradeEntryPointExe           = "FabricSetup.exe",
                    UpgradeEntryPointExeParameters = "/operation:Install"
                }
            };

            if (fabricPackageType == FabricPackageType.XCopyPackage)
            {
                targetInformation.TargetInstallation.MSILocation = packageLocation;
            }

            if (!string.IsNullOrEmpty(infrastructureManifestLocation))
            {
                targetInformation.TargetInstallation.InfrastructureManifestLocation = infrastructureManifestLocation;
            }

            string targetInformationFileName = Path.Combine(fabricDataRoot, Constants.FileNames.TargetInformation);

            DeployerTrace.WriteInfo("TargetInformationFileName is {0}", targetInformationFileName);
            if (!string.IsNullOrEmpty(machineName) && !machineName.Equals(Helpers.GetMachine()))
            {
                targetInformation.TargetInstallation.ClusterManifestLocation = Path.Combine(fabricDataRoot, "clusterManifest.xml");
                targetInformationFileName = Helpers.GetRemotePath(targetInformationFileName, machineName);
            }

            XmlHelper.WriteXmlExclusive <TargetInformationType>(targetInformationFileName, targetInformation);
            DeployerTrace.WriteInfo("Target information file {0} written on machine: {1}", targetInformationFileName, machineName);
        }
        internal static AnalysisSummary AnalyzeClusterSetup(
            string configPath,
            string oldConfigPath,
            string cabPath,
            bool usingClusterManifest           = false,
            FabricPackageType fabricPackageType = FabricPackageType.XCopyPackage,
            bool isForcedRun          = false,
            int maxPercentFailedNodes = 0)
        {
            SFDeployerTrace.WriteInfo(StringResources.Info_BPAStart);
            var summary = new AnalysisSummary();

            // Check user has local admin privilege
            summary.LocalAdminPrivilege = CheckLocalAdminPrivilege();

            // Validate JSON config
            StandAloneInstallerJsonModelBase standAloneModel    = null;
            StandAloneInstallerJsonModelBase oldStandAloneModel = null;

            if (!usingClusterManifest)
            {
                standAloneModel = StandAloneInstallerJsonModelBase.GetJsonConfigFromFile(configPath);

                if (!string.IsNullOrEmpty(oldConfigPath))
                {
                    oldStandAloneModel = StandAloneInstallerJsonModelBase.GetJsonConfigFromFile(oldConfigPath);
                }

                summary.IsJsonValid = IsJsonConfigModelValid(standAloneModel, oldStandAloneModel, validateDownNodes: true, throwIfError: false) && ValidateCodeVersionExists(standAloneModel);
            }

            // Deliberately not checking empty. Some invocations aren't intended to test cabPath.
            if (cabPath != null)
            {
                summary.IsCabValid = CheckIsCabFile(cabPath);
            }

            // Below depends on JSON being valid
            if (!summary.Passed)
            {
                SFDeployerTrace.WriteError(StringResources.Error_BPABailing);
                return(summary);
            }

            // Get machine names from JSON config
            IEnumerable <string> healthyMachineNames = usingClusterManifest
                                        ? StandaloneUtility.GetMachineNamesFromClusterManifest(configPath)
                                        : standAloneModel.GetClusterTopology().Machines;

            MachineHealthContainer machineHealthContainer = new MachineHealthContainer(healthyMachineNames, maxPercentFailedNodes);

            // Validate machine FQDNs, Check SMB ports opened
            summary.RequiredPortsOpen = StandaloneUtility.CheckRequiredPorts(machineHealthContainer);

            // Below depends on machines being reachable
            if (!summary.Passed)
            {
                SFDeployerTrace.WriteError(StringResources.Error_BPABailing);
                return(summary);
            }

            // Validate Remote Registry Service is not disabled on all machines
            summary.RemoteRegistryAvailable = CheckRemoteRegistryEnabled(machineHealthContainer);

            // Below depends on remote registry service
            if (!summary.Passed)
            {
                SFDeployerTrace.WriteError(StringResources.Error_BPABailing);
                return(summary);
            }

            summary.FirewallAvailable = CheckFirewallEnabled(machineHealthContainer);

            // Below depends on firewall service
            if (!summary.Passed)
            {
                SFDeployerTrace.WriteError(StringResources.Error_BPABailing);
                return(summary);
            }

            // Run RPC check to validate end-to-end registry access to all machines
            summary.RpcCheckPassed = CheckRPCAccess(machineHealthContainer);

            // Below depend on remote registry access
            if (!summary.Passed)
            {
                SFDeployerTrace.WriteError(StringResources.Error_BPABailing);
                return(summary);
            }

            summary.NoDomainController = CheckNoMachineIsDomainController(machineHealthContainer);

            // Below depends on machines not being domain controller
            if (!summary.Passed)
            {
                SFDeployerTrace.WriteError(StringResources.Error_BPABailing);
                return(summary);
            }

            if (fabricPackageType == FabricPackageType.XCopyPackage)
            {
                // Validate that Fabric runtime MSI is not installed since this will be conflicting with Standalone
                summary.NoConflictingInstallations = !StandaloneUtility.IsMsiInstalled(machineHealthContainer);
            }

            string fabricDataRoot = null;
            string fabricLogRoot  = null;

            if (!usingClusterManifest)
            {
                if (string.IsNullOrEmpty(oldConfigPath))
                {
                    summary.FabricInstallable = CheckForCleanInstall(standAloneModel, machineHealthContainer, isForcedRun); // Fabric is not installed on target machines
                }

                var importantSettings = standAloneModel.GetFabricSystemSettings();
                fabricDataRoot = importantSettings.ContainsKey(DMConstants.FabricDataRootString) ?
                                 importantSettings[DMConstants.FabricDataRootString] :
                                 null;
                fabricLogRoot = importantSettings.ContainsKey(DMConstants.FabricLogRootString) ?
                                importantSettings[DMConstants.FabricLogRootString] :
                                null;

                summary.DataDrivesAvailable = CheckDataSystemDrives(machineHealthContainer, fabricDataRoot, fabricLogRoot); // System drives for path-based settings, exist on target machines

                // Below depend on data system drives
                if (!summary.Passed)
                {
                    SFDeployerTrace.WriteError(StringResources.Error_BPABailing);
                    return(summary);
                }
            }

            summary.DrivesEnoughAvailableSpace = CheckDrivesAvailableSpace(machineHealthContainer, fabricDataRoot, fabricLogRoot);

            // Below depend on root drives having enough space available
            if (!summary.Passed)
            {
                SFDeployerTrace.WriteError(StringResources.Error_BPABailing);
                return(summary);
            }

            summary.IsAllOrNoneIOTDevice = CheckAllIOTDevices(machineHealthContainer);

            // Below depend on all or none machines being IOT Devices
            if (!summary.Passed)
            {
                SFDeployerTrace.WriteError(StringResources.Error_BPABailing);
                return(summary);
            }

            // Check dotnet.exe exists in %Path%
            // Currently we only need to check for IOTCore environment.
            summary.DotNetExeInPath = IsDotNetExeInPath(machineHealthContainer);

            // Below depend on IOT Devices having dotnet.exe in path
            if (!summary.Passed)
            {
                SFDeployerTrace.WriteError(StringResources.Error_BPABailing);
                return(summary);
            }

            summary.MachineHealthContainer = machineHealthContainer;
            LogResult(summary);

            return(summary);
        }
        internal static bool ValidateClusterSetupXmlManifest(string xmlManifestPath, FabricPackageType fabricPackageType)
        {
            AnalysisSummary result = AnalyzeClusterSetup(xmlManifestPath, oldConfigPath: null, cabPath: null, usingClusterManifest: true, fabricPackageType: fabricPackageType);

            return(result.Passed);
        }
        private static void NewNodeConfigurationInner(
            string clusterManifestPath,
            string infrastructureManifestPath,
            string jsonClusterConfigPath,
            string fabricDataRoot,
            string fabricLogRoot,
            string fabricHostCredentialUser,
            SecureString fabricHostCredentialPassword,
            bool runFabricHostServiceAsManual,
            bool removeExistingConfiguration,
            FabricPackageType fabricPackageType,
            string fabricPackageRoot,
            string machineName,
            string bootstrapPackagePath)
        {
            if (!string.IsNullOrEmpty(machineName))
            {
                if (!string.IsNullOrEmpty(fabricHostCredentialUser) || fabricHostCredentialPassword != null)
                {
                    throw new InvalidOperationException(StringResources.Error_RemoteNodeConfigNotSupportedWithCredential);
                }
            }

            // Delete the registry value used for RemoveNodeConfiguration if it is present
            if (fabricPackageType == FabricPackageType.MSI)
            {
                RemoveNodeConfigOperation.DeleteRemoveNodeConfigurationRegistryValue(machineName);
            }

            if (removeExistingConfiguration)
            {
                RemoveNodeConfigurationInner(true, fabricPackageType, machineName);
            }

            var parameters = new Dictionary <string, dynamic>
            {
                { DeploymentParameters.ClusterManifestString, clusterManifestPath },
                { DeploymentParameters.InfrastructureManifestString, infrastructureManifestPath },
                { DeploymentParameters.FabricDataRootString, fabricDataRoot },
                { DeploymentParameters.FabricLogRootString, fabricLogRoot }
            };

            if (fabricHostCredentialUser != null)
            {
                parameters.Add(DeploymentParameters.RunAsUserNameString, fabricHostCredentialUser);
            }

            if (jsonClusterConfigPath != null)
            {
                parameters.Add(DeploymentParameters.JsonClusterConfigLocationString, jsonClusterConfigPath);
            }

            if (fabricHostCredentialPassword != null)
            {
                parameters.Add(DeploymentParameters.RunAsPaswordString, fabricHostCredentialPassword);
            }

            if (machineName != null)
            {
                parameters.Add(DeploymentParameters.MachineNameString, machineName);
            }

            if (fabricPackageRoot != null)
            {
                parameters.Add(DeploymentParameters.FabricPackageRootString, fabricPackageRoot);
            }

            if (runFabricHostServiceAsManual)
            {
                parameters.Add(DeploymentParameters.ServiceStartupTypeString, FabricDeployerServiceController.ServiceStartupType.Manual.ToString());
            }

            if (bootstrapPackagePath != null)
            {
                parameters.Add(DeploymentParameters.BootstrapMSIPathString, bootstrapPackagePath);
            }

            // Create DeploymentParameters object with conditional retrieval of FabricBinRoot (to work around infinite recursion due to invalid reflection)
            bool setBinRoot           = fabricPackageType != FabricPackageType.XCopyPackage;
            var  deploymentParameters = new DeploymentParameters(setBinRoot);

            deploymentParameters.DeploymentPackageType = fabricPackageType;

            try
            {
                deploymentParameters.SetParameters(parameters, DeploymentOperations.Configure);

                DeploymentOperation.ExecuteOperation(deploymentParameters);
            }
            catch (Exception e)
            {
                DeployerTrace.WriteError("{0}", e.ToString());
                throw;
            }
        }