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);
            }
        }
Beispiel #2
0
        internal static bool StopDataCollector()
        {
            string arguments = String.Format(
                CultureInfo.InvariantCulture,
                "stop {0}",
                DataCollectorName);
            var retryCount = 3;

            for (;;)
            {
                int errorCode = Utility.ExecuteCommand("logman", arguments, DefaultLogmanWaitTimeout);
                if ((errorCode == LogmanSuccessExitCode) ||
                    (errorCode == LogmanDataCollectorNotFoundExitCode) ||
                    (errorCode == LogmanDataCollectorNotRunningExitCode))
                {
                    return(true);
                }

                if (retryCount-- == 0)
                {
                    DeployerTrace.WriteError(
                        "Unable to stop data collector for performance counters. The command \"logman {0}\" failed with error code {1}.",
                        arguments,
                        errorCode);
                    return(false);
                }

                DeployerTrace.WriteInfo("Removing scheduled task and then retrying.");
                DeletePlaScheduledTask(DataCollectorName);
                Thread.Sleep(TimeSpan.FromSeconds(5));
            }
        }
        public DockerDnsHelper(DeploymentParameters parameters, string ipAddressOrFQDN)
        {
            DeployerTrace.WriteInfo("DockerDnsHelper current node IPAddressOrFQDN <{0}>", ipAddressOrFQDN);

            this.deploymentParameters = parameters ?? new DeploymentParameters();
            this.nodeIPAddressOrFQDN  = string.IsNullOrEmpty(ipAddressOrFQDN) ? string.Empty : ipAddressOrFQDN;
        }
        internal static int GetPrefixLength(IPAddress subnetMask, string networkAdapterName)
        {
            subnetMask.Validate("subnetMask");

            DeployerTrace.WriteInfo("Getting prefix length of subnet mask {0} and network adapter {1}", subnetMask, networkAdapterName);

            byte[] bytes = subnetMask.GetAddressBytes();

            int count = 0;

            foreach (var b in bytes)
            {
                // preferring over right-shift till b is 0, since right-shift may pad with 1 in some cases
                byte mask = 1;
                for (byte i = 0; i < sizeof(byte) * 8; i++, mask <<= 1)
                {
                    if ((b & mask) != 0)
                    {
                        count++;
                    }
                }
            }

            DeployerTrace.WriteInfo("Successfully obtained prefix length of subnet mask {0} and network adapter {1}. Prefix length: {2}", subnetMask, networkAdapterName, count);
            return(count);
        }
        internal static void RemoveFirewallRule()
        {
            DeployerTrace.WriteInfo("Removing firewall rule {0} if it exists...", FirewallRuleName);

            try
            {
#if !DotNetCoreClrLinux
                INetFwPolicy2 fwPolicy2 = GetFirewallPolicy();
                if (fwPolicy2 == null)
                {
                    DeployerTrace.WriteWarning(StringResources.Warning_FabricDeployer_DockerDnsSetup_ErrorGettingFirewallPolicy2);
                    return;
                }

                bool exists = DoesFirewallRuleExist(fwPolicy2);
                if (!exists)
                {
                    DeployerTrace.WriteInfo("Firewall rule {0} doesn't exist. Nothing to remove", FirewallRuleName);
                    return;
                }

                fwPolicy2.Rules.Remove(FirewallRuleName);
#else
                INetFwRules rules = NetFwRules.GetAllRules();
                rules.Remove(FirewallRuleName);
#endif
                DeployerTrace.WriteInfo("Firewall rule {0} removed", FirewallRuleName);
            }
            catch (Exception ex)
            {
                DeployerTrace.WriteWarning(StringResources.Warning_FabricDeployer_DockerDnsSetup_ErrorRemovingFirewallRule, FirewallRuleName, ex);
            }
        }
        internal static IPAddress GetFirstUnicastAddress(NetworkInterface nic, string networkAdapterName)
        {
            DeployerTrace.WriteInfo("Getting first unicast address for network adapter {0}", networkAdapterName);

            IPInterfaceProperties ipProperties = nic.GetIPProperties();

            if (ipProperties == null)
            {
                var message = string.Format(
                    CultureInfo.CurrentUICulture,
                    StringResources.Warning_FabricDeployer_DockerDnsSetup_ErrorGettingSubnetMask1, networkAdapterName);

                DeployerTrace.WriteWarning(message);
                throw new InvalidOperationException(message);
            }

            UnicastIPAddressInformationCollection ipInfos = ipProperties.UnicastAddresses;

            foreach (UnicastIPAddressInformation ipInfo in ipInfos)
            {
                return(ipInfo.Address);
            }

            var message2 = string.Format(
                CultureInfo.CurrentUICulture,
                StringResources.Warning_FabricDeployer_DockerDnsSetup_ErrorGettingSubnetMask2, networkAdapterName);

            DeployerTrace.WriteWarning(message2);
            throw new InvalidOperationException(message2);
        }
        internal static IPAddress GetSubnetMask(NetworkInterface nic, IPAddress ipAddress, string networkAdapterName)
        {
            DeployerTrace.WriteInfo("Getting subnet mask for IP address {0} and network adapter {1}", ipAddress, networkAdapterName);

            IPInterfaceProperties ipProperties = nic.GetIPProperties();

            if (ipProperties == null)
            {
                var message = string.Format(
                    CultureInfo.CurrentUICulture,
                    StringResources.Warning_FabricDeployer_DockerDnsSetup_ErrorGettingSubnetMask1, networkAdapterName);

                DeployerTrace.WriteWarning(message);
                throw new InvalidOperationException(message);
            }

            UnicastIPAddressInformationCollection ipInfos = ipProperties.UnicastAddresses;

            foreach (UnicastIPAddressInformation ipInfo in ipInfos)
            {
                // ToString() is the only valid way to get the IP address. There is no other explicit property
                if (string.Equals(ipInfo.Address.ToString(), ipAddress.ToString(), StringComparison.OrdinalIgnoreCase))
                {
                    DeployerTrace.WriteInfo("Successfully obtained subnet mask {0} for IP address {1} and network adapter {2}", ipInfo.IPv4Mask, ipAddress, networkAdapterName);
                    return(ipInfo.IPv4Mask);
                }
            }

            var message2 = string.Format(
                CultureInfo.CurrentUICulture,
                StringResources.Warning_FabricDeployer_DockerDnsSetup_ErrorGettingSubnetMask2, ipAddress, networkAdapterName);

            DeployerTrace.WriteWarning(message2);
            throw new InvalidOperationException(message2);
        }
        internal static void RemoveNetworkAdapter(string networkAdapterName)
        {
            DeployerTrace.WriteInfo("Removing network adapter {0}...", networkAdapterName);

            string script = "Remove-VMNetworkAdapter -Name {0} -ManagementOS -SwitchName {1}"
                            .ToFormat(networkAdapterName, SwitchName);

            var psObjects = Utility.ExecutePowerShellScript(script, false);

            if (psObjects != null)
            {
                DeployerTrace.WriteInfo("PowerShell Invoke completed and returned {0} objects", psObjects.Count);

                foreach (var psObject in psObjects)
                {
                    DeployerTrace.WriteInfo("{0}", psObject);
                }
            }
            else
            {
                DeployerTrace.WriteInfo("PowerShell Invoke completed and returned null");
            }

            DeployerTrace.WriteInfo("Network adapter {0} removed successfully", networkAdapterName);
        }
        private static async Task ExecuteAsync(Action func, string funcName)
        {
            for (int i = 0; i < MaxRetries; i++)
            {
                DeployerTrace.WriteInfo("Executing {0}, attempt {1} of {2}", funcName, i + 1, MaxRetries);

                try
                {
                    // all operations are idempotent, so retry from beginning is okay
                    func();
                    DeployerTrace.WriteInfo("Successfully completed {0}", funcName);
                    break;
                }
                catch (Exception ex)
                {
                    string message = string.Format(
                        CultureInfo.CurrentUICulture,
                        StringResources.Warning_FabricDeployer_DockerDnsSetup_RetryMessage1, funcName, i + 1, MaxRetries, ex);

                    if (i == MaxRetries - 1)
                    {
                        message += string.Format(CultureInfo.CurrentUICulture, StringResources.Warning_FabricDeployer_DockerDnsSetup_RetryExhausted, Environment.NewLine);
                        DeployerTrace.WriteWarning(message);
                        throw;
                    }

                    message += string.Format(CultureInfo.CurrentUICulture, StringResources.Warning_FabricDeployer_DockerDnsSetup_RetryContinue, Environment.NewLine);
                    DeployerTrace.WriteWarning(message);
                }

                await Task.Delay(TimeSpan.FromSeconds(RetryDelayInSeconds));
            }
        }
Beispiel #10
0
        private int DeploySeedInfoFile()
        {
            Dictionary <string, string> seedMapping = infrastructure.VoteMappings;

            if (seedMapping == null)
            {
                return(Constants.ErrorCode_Success);
            }
            string dataRoot         = nodeSettings.DeploymentFoldersInfo.DataRoot;
            string seedInfoFileName = Path.Combine(dataRoot, Constants.FileNames.SeedInfo);

            try
            {
                using (FileStream fileStream = new FileStream(seedInfoFileName, FileMode.Create, FileAccess.Write, FileShare.Read))
                {
                    using (TextWriter writer = new StreamWriter(fileStream))
                    {
                        writer.WriteLine(Constants.SectionNames.SeedMapping);
                        seedMapping.ForEach(pair => writer.WriteLine("  {0} = {1}", pair.Key, pair.Value));
                    }
                }
                DeployerTrace.WriteInfo("Generated SeedInfoFile successfully");
            }
            catch (Exception e)
            {
                DeployerTrace.WriteError("Unable to generated SeedInfoFile because {0}", e);
                throw;
            }
            return(Constants.ErrorCode_Success);
        }
        /// <summary>
        /// Gets a new IP address to be reassigned to <see cref="SFNetworkAdapterName"/>.
        /// The new IP address is 1 more than Docker's DNS IP address.
        /// </summary>
        internal static IPAddress GetNewIPAddressForSFNic(IPAddress dockerDnsIPAddress)
        {
            var bytes = dockerDnsIPAddress.GetAddressBytes();

            try
            {
                checked
                {
                    bytes[3] += 1;
                }

                var newIP = new IPAddress(bytes);

                var message =
                    "New IP address for network adapter {0} is: {1}".ToFormat(SFNetworkAdapterName, newIP);
                DeployerTrace.WriteInfo(message);

                return(newIP);
            }
            catch (OverflowException ex)
            {
                var message = string.Format(
                    CultureInfo.CurrentUICulture,
                    StringResources.Warning_FabricDeployer_DockerDnsSetup_ErrorGettingNewIPAddress, SFNetworkAdapterName, dockerDnsIPAddress, ex);
                DeployerTrace.WriteWarning(message);
                throw;
            }
        }
Beispiel #12
0
        /// <summary>
        /// Checks that the network interface has the expected ip address.
        /// This is usually needed after the network has been set up, to allow time to plumb ip address.
        /// </summary>
        /// <param name="networkInterface"></param>
        /// <param name="expectedAddress"></param>
        /// <returns></returns>
        public static bool IsIPAddressMatching(NetworkInterface networkInterface, IPAddress expectedAddress)
        {
            bool matching = false;

            for (int i = 0; i < 5; i++)
            {
                DeployerTrace.WriteInfo("Waiting to match ip address {0} on network interface {1}.", expectedAddress.ToString(), networkInterface.Name);

                Thread.Sleep(3000);

                IPAddress ipv4Address = null;
                IPAddress ipv4Mask    = null;
                Utility.GetDefaultIPV4AddressAndMask(networkInterface, out ipv4Address, out ipv4Mask);

                if (ipv4Address != null && IPAddress.Equals(ipv4Address, expectedAddress))
                {
                    matching = true;
                    break;
                }
            }

            DeployerTrace.WriteInfo("Matched ip address {0} on network interface {1}:{2}.", expectedAddress.ToString(), networkInterface.Name, matching);

            return(matching);
        }
Beispiel #13
0
        public static int Main(string[] args)
        {
            string argsString = string.Join(" ", args);

            try
            {
                DeployerTrace.WriteInfo("Deployer called with {0}", argsString);
                DeploymentParameters parameters = CommandLineInfo.Parse(args);
                DeploymentOperation.ExecuteOperation(parameters, false);
                return(Constants.ErrorCode_Success);
            }
            catch (FabricHostRestartRequiredException e)
            {
                DeployerTrace.WriteInfo(e.ToString());
                return(Constants.ErrorCode_RestartRequired);
            }
            catch (FabricHostRestartNotRequiredException e)
            {
                DeployerTrace.WriteInfo(e.ToString());
                return(Constants.ErrorCode_RestartNotRequired);
            }
            catch (Exception e)
            {
                DeployerTrace.WriteError("Deployer failed with args: {0}", argsString);
                DeployerTrace.WriteError(e.ToString());
                return(Constants.ErrorCode_Failure);
            }
        }
        private void EnsureImageStoreForSingleMachineDeployment(string defaultImageStoreRoot)
        {
            SettingsOverridesTypeSection managementSection = this.manifest.FabricSettings.FirstOrDefault(
                section => section.Name.Equals(Constants.SectionNames.Management, StringComparison.OrdinalIgnoreCase));

            SettingsOverridesTypeSectionParameter imageStoreParameter = null;

            if (managementSection != null)
            {
                imageStoreParameter = managementSection.Parameter.FirstOrDefault(
                    parameter => parameter.Name.Equals(Constants.ParameterNames.ImageStoreConnectionString, StringComparison.OrdinalIgnoreCase));
            }

            if (imageStoreParameter == null)
            {
                return;
            }

            SecureString imageStoreRoot = fabricValidator.ImageStoreConnectionString;

            char[] secureImageStoreChars = Utility.SecureStringToCharArray(imageStoreRoot);

            if ((secureImageStoreChars == null) || (new string(secureImageStoreChars)).Equals(Constants.DefaultTag, StringComparison.OrdinalIgnoreCase))
            {
                var imageStoreIncomingFolder = Path.Combine(defaultImageStoreRoot, Constants.DefaultImageStoreIncomingFolderName);
                if (!Directory.Exists(imageStoreIncomingFolder))
                {
                    Directory.CreateDirectory(imageStoreIncomingFolder);
                }
                DeployerTrace.WriteInfo("Default ImageStore incoming folder: {0}", imageStoreIncomingFolder);

                imageStoreParameter.Value       = string.Format(CultureInfo.InvariantCulture, "{0}{1}", System.Fabric.FabricValidatorConstants.FileImageStoreConnectionStringPrefix, defaultImageStoreRoot);
                imageStoreParameter.IsEncrypted = false;
            }
        }
Beispiel #15
0
        internal static T GetRegistryKeyValue <T>(
            RegistryKey baseRegistryKey,
            string keyName,
            string valueName,
            T defaultValue)
        {
            baseRegistryKey.Validate("baseRegistryKey");
            keyName.Validate("keyName");
            valueName.Validate("valueName");

            DeployerTrace.WriteInfo("Getting key-value data for key name: {0}, value name: {1}", keyName, valueName);

            using (RegistryKey registryKey = baseRegistryKey.OpenSubKey(keyName))
            {
                if (registryKey == null)
                {
                    string message = "Error opening registry sub key: {0}, using default value: {1}".ToFormat(keyName, defaultValue);
                    DeployerTrace.WriteWarning(message);
                    return(defaultValue);
                }

                T value = (T)Registry.GetValue(registryKey.Name, valueName, defaultValue);

                DeployerTrace.WriteInfo(
                    "Value successfully obtained. Key name: {0}, reg value name: {1}, reg value: {2}", keyName,
                    valueName, value);
                return(value);
            }
        }
Beispiel #16
0
 private static void RemoveNodeConfigurationXCopy(string machineName, bool deleteLog, string nodeName)
 {
     try
     {
         string fabricDataRoot = Utility.GetFabricDataRoot(machineName);
         if (string.IsNullOrEmpty(fabricDataRoot))
         {
             DeployerTrace.WriteWarning("FabricDataRoot on machine {0} is empty or not present; no current installation likely exists", machineName);
         }
         else
         {
             DeployerTrace.WriteInfo("FabricDataRoot on machine {0} is {1}", machineName, fabricDataRoot);
             FabricDeployerServiceController.GetServiceInStoppedState(machineName, Constants.FabricInstallerServiceName);
             WriteTargetInformationFile(fabricDataRoot, machineName, deleteLog, nodeName);
             RunInstallerService(machineName);
         }
         FabricDeployerServiceController.DeleteFabricInstallerService(machineName);
         Utility.DeleteRegistryKeyTree(machineName);
     }
     catch (TimeoutException)
     {
         DeployerTrace.WriteError("FabricInstallerSvc timeout on machine {0}. Cleaning up to avoid environment corruption.", machineName);
         SuppressExceptions(() => { FabricDeployerServiceController.StopHostSvc(machineName); });
         SuppressExceptions(() => { FabricDeployerServiceController.DeleteFabricHostService(machineName); });
         SuppressExceptions(() => { FabricDeployerServiceController.StopInstallerSvc(machineName); });
         SuppressExceptions(() => { FabricDeployerServiceController.DeleteFabricInstallerService(machineName); });
         SuppressExceptions(() => { Utility.DeleteRegistryKeyTree(machineName); });
         throw;
     }
 }
Beispiel #17
0
        internal void ExecuteOperation(string containerNetworkName, string containerServiceArguments, string fabricDataRoot, DeploymentOperations operation = DeploymentOperations.None)
        {
            if (operation == DeploymentOperations.UpdateInstanceId)
            {
                DeployerTrace.WriteInfo("Skipping Container network clean up invoked in context: {0}", operation);
                return;
            }

            if (!Utility.IsContainersFeaturePresent())
            {
                DeployerTrace.WriteInfo("Skipping container network clean up since containers feature has been disabled.");
                return;
            }

#if !DotNetCoreClrLinux
            if (!Utility.IsContainerNTServicePresent())
            {
                DeployerTrace.WriteInfo("Skipping container network clean up since containers NT service is not installed.");
                return;
            }
#endif

            // Wire server check is being made for the edge cluster scenario,
            // so that clean up is a noop. Wire server is not explicitly needed
            // for the clean up process.
            if (!IsWireServerAvailable())
            {
                DeployerTrace.WriteInfo("Skipping container network clean up since wire server is not available.");
                return;
            }

            containerNetworkName      = (!string.IsNullOrEmpty(containerNetworkName)) ? (containerNetworkName) : (FlatNetworkConstants.NetworkName);
            containerServiceArguments = (!string.IsNullOrEmpty(containerServiceArguments)) ? (containerServiceArguments) : (FlatNetworkConstants.ContainerServiceArguments);

            if (string.IsNullOrEmpty(fabricDataRoot))
            {
                fabricDataRoot = Utility.GetFabricDataRoot();
                DeployerTrace.WriteInfo("Fabric data root passed in was empty, using environment fabric data root instead: {0}.", fabricDataRoot);
            }

            DeployerTrace.WriteInfo("Container network clean up invoked in context: {0}", operation);

            bool containerServiceRunning = false;
            if (operation == DeploymentOperations.Update || operation == DeploymentOperations.UpdateInstanceId)
            {
                containerServiceRunning = IsContainerServiceRunning();
                DeployerTrace.WriteInfo("Container service running: {0}", containerServiceRunning);
            }

            if (CleanupContainerNetwork(containerNetworkName, containerServiceArguments, fabricDataRoot, containerServiceRunning))
            {
                DeployerTrace.WriteInfo("Successfully cleaned up container network.");
            }
            else
            {
                string message = "Failed to clean up container network.";
                DeployerTrace.WriteError(message);
                throw new InvalidDeploymentException(message);
            }
        }
        // Checks if current node should be removed for Windows Server deployments only.
        private bool ShouldRemoveCurrentNode(string nodeName, ClusterManifestType clusterManifest)
        {
            var infrastructureType = clusterManifest.Infrastructure.Item as ClusterManifestTypeInfrastructureWindowsServer;

            // For deployments that are not standalone, this section will not be present. Continue update operation for this node as is.
            if (infrastructureType == null)
            {
                DeployerTrace.WriteInfo("Node section not found in cluster manifest infrastructure section.");
                return(false);
            }

            if (nodeName == null)
            {
                return(false);
            }
            else
            {
                var nodesToBeRemovedSection = clusterManifest.FabricSettings.FirstOrDefault(section => section.Name.Equals(Constants.SectionNames.Setup, StringComparison.OrdinalIgnoreCase) &&
                                                                                            section.Parameter != null &&
                                                                                            section.Parameter.Any(parameter => parameter.Name.Equals(Constants.ParameterNames.NodesToBeRemoved)));
                List <string> nodesToRemove = new List <string>();
                if (nodesToBeRemovedSection != null)
                {
                    nodesToRemove = nodesToBeRemovedSection.Parameter.First(parameter => parameter.Name.Equals(Constants.ParameterNames.NodesToBeRemoved, StringComparison.OrdinalIgnoreCase)).Value.Split(',').Select(p => p.Trim()).ToList();
                }

                bool isNodeToBeRemoved = !infrastructureType.NodeList.Any(n => n.NodeName == nodeName) && nodesToRemove.Contains(nodeName);
                DeployerTrace.WriteInfo("Checking if current node needs to be removed : {0}", isNodeToBeRemoved);
                return(isNodeToBeRemoved);
            }
        }
Beispiel #19
0
        /// <summary>
        /// Retrieves network interface that is on the given subnet.
        /// </summary>
        public static NetworkInterface GetNetworkInterfaceOnSubnet(string subnet)
        {
            DeployerTrace.WriteInfo("Getting network interface with ip address on subnet {0}.", subnet);

            foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
            {
                IPAddress ipv4Address;
                IPAddress ipv4Mask;
                GetDefaultIPV4AddressAndMask(nic, out ipv4Address, out ipv4Mask);

                // Calculate subnet by ANDing ip address and mask
                var networkAddress       = GetNetworkAddress(ipv4Address, ipv4Mask);
                var networkAddressString = (networkAddress != null) ? (networkAddress.ToString()) : (string.Empty);

                DeployerTrace.WriteInfo("Network broadcast ip {0}", networkAddressString);

                if (string.Equals(networkAddressString, subnet))
                {
                    DeployerTrace.WriteInfo("Network interface with ip address on subnet {0} successfully obtained, name: {1}", subnet, nic.Name);
                    return(nic);
                }
            }

            DeployerTrace.WriteInfo("Unable to detect network interface with ip address on subnet {0}.", subnet);
            return(null);
        }
Beispiel #20
0
        internal static int ExecuteCommand(string command, string arguments, TimeSpan timeout)
        {
            string workingDir = Directory.GetCurrentDirectory();

            DeployerTrace.WriteInfo("Executing command: {0} {1}", command, arguments);
            using (Process p = new Process())
            {
                p.StartInfo.FileName         = command;
                p.StartInfo.Arguments        = arguments;
                p.StartInfo.UseShellExecute  = false;
                p.StartInfo.CreateNoWindow   = true;
                p.StartInfo.WorkingDirectory = workingDir;
                p.Start();

                if (timeout == TimeSpan.MaxValue)
                {
                    p.WaitForExit();
                }
                else
                {
                    if (!p.WaitForExit((int)timeout.TotalMilliseconds))
                    {
                        p.Kill();
                        DeployerTrace.WriteInfo("Execution of command {0} {1} timedout after {2} milliseconds", command, arguments, timeout.TotalMilliseconds);
                        return(Constants.ErrorCode_Failure);
                    }
                }

                return(p.ExitCode);
            }
        }
Beispiel #21
0
        private void RemoveNodeConfigurationMsi(bool deleteLog)
        {
            DeployerTrace.WriteInfo("Checking if fabric host is running");
            if (FabricDeployerServiceController.IsRunning(Helpers.GetMachine()))
            {
                DeployerTrace.WriteInfo("Stopping fabric host");
                FabricDeployerServiceController.StopHostSvc(Helpers.GetMachine());
            }

            string fabricCodePath = String.Empty;

            try
            {
                fabricCodePath = FabricEnvironment.GetCodePath();
            }
            catch (FabricException)
            {
                DeployerTrace.WriteError(StringResources.Error_FabricCodePathNotFound);
            }

            string fabricSetupFilepath = Path.Combine(fabricCodePath, "FabricSetup.exe");

            ProcessStartInfo FabricSetupExeStartInfo = new ProcessStartInfo();

            FabricSetupExeStartInfo.FileName         = fabricSetupFilepath;
            FabricSetupExeStartInfo.Arguments        = string.Format("/operation:removenodestate");
            FabricSetupExeStartInfo.WorkingDirectory = fabricCodePath == String.Empty ? Directory.GetCurrentDirectory() : fabricCodePath;

            try
            {
                using (Process exeProcess = Process.Start(FabricSetupExeStartInfo))
                {
                    DeployerTrace.WriteInfo("Starting FabricSetup.exe");
                    exeProcess.WaitForExit();
                }
            }
            catch (Exception e)
            {
                DeployerTrace.WriteError("Starting FabricSetup.exe failed with exception {0}.", e);
            }

            string value = deleteLog ? DeleteLogTrue : DeleteLogFalse;

            using (RegistryKey regKey = Registry.LocalMachine.OpenSubKey(FabricConstants.FabricRegistryKeyPath, true))
            {
                regKey.SetValue(Constants.Registry.RemoveNodeConfigurationValue, value);
            }

            DeployerTrace.WriteInfo("Starting fabric host");
            FabricDeployerServiceController.StartHostSvc();

            DeployerTrace.WriteInfo("Stopping fabric host");
            FabricDeployerServiceController.StopHostSvc();

            DeployerTrace.WriteInfo("Cleaning registry value");
            DeleteRemoveNodeConfigurationRegistryValue(string.Empty);

            DeployerTrace.WriteInfo("Done cleaning registry value");
        }
        private static void AclCert(CertId certId, string accountName)
        {
            X509Store store = null;

            try
            {
                store = new X509Store(certId.StoreName, StoreLocation.LocalMachine);
                store.Open(OpenFlags.ReadWrite | OpenFlags.OpenExistingOnly);

                X509Certificate2Collection certs = store.Certificates.Find(ConvertToX509FindType(certId.FindType), certId.FindValue, validOnly: false);
                if (certs == null || certs.Count == 0)
                {
                    DeployerTrace.WriteInfo("AclCert is skipped for {0} because it's not installed", certId);
                    return;
                }

                foreach (X509Certificate2 cert in certs)
                {
                    if (cert.PrivateKey == null)
                    {
                        DeployerTrace.WriteWarning("AclCert is skipped for {0} because its private key is null", certId);
                        continue;
                    }

                    RSACryptoServiceProvider privateKey = cert.PrivateKey as RSACryptoServiceProvider;
                    if (privateKey == null)
                    {
                        DeployerTrace.WriteWarning("AclCert is skipped for {0} because its private key type {1} is not supported ", certId, cert.PrivateKey.GetType());
                        continue;
                    }

                    CspParameters csp = new CspParameters(privateKey.CspKeyContainerInfo.ProviderType, privateKey.CspKeyContainerInfo.ProviderName, privateKey.CspKeyContainerInfo.KeyContainerName);
                    csp.Flags     = CspProviderFlags.UseExistingKey | CspProviderFlags.UseMachineKeyStore;
                    csp.KeyNumber = (int)privateKey.CspKeyContainerInfo.KeyNumber;
#if !DotNetCoreClr
                    csp.CryptoKeySecurity = privateKey.CspKeyContainerInfo.CryptoKeySecurity;

                    CryptoKeyAccessRule accessRule = new CryptoKeyAccessRule(accountName, CryptoKeyRights.FullControl, AccessControlType.Allow);
                    csp.CryptoKeySecurity.AddAccessRule(accessRule);
#endif

                    using (RSACryptoServiceProvider newCsp = new RSACryptoServiceProvider(csp))
                    {
                        DeployerTrace.WriteInfo("AclCert success: {0}", certId);
                    }
                }
            }
            catch (Exception ex)
            {
                DeployerTrace.WriteError("AclCert error with {0}: {1}", certId, ex);
            }
            finally
            {
                if (store != null)
                {
                    store.Close();
                }
            }
        }
Beispiel #23
0
        /// <summary>
        /// Gets the network interface matching the interface name, if provided.
        /// If network interface name is not provided then -
        /// a) On Windows - the second NIC is retrieved from NM agent. The adapter is refreshed so that the ip plumbs correctly.
        /// b) On Linux - the primary NIC is retrieved from NM agent.
        /// </summary>
        /// <param name="networkInterfaceName"></param>
        /// <returns></returns>
        private NetworkInterface GetNetworkInterfaceForIsolatedNetwork(string networkInterfaceName)
        {
            NetworkInterface networkInterface = null;

            if (!string.IsNullOrEmpty(networkInterfaceName))
            {
                DeployerTrace.WriteInfo("Get network interface for isolated network set up: {0}.", networkInterfaceName);

                foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
                {
                    if (string.Equals(nic.Name, networkInterfaceName, StringComparison.OrdinalIgnoreCase))
                    {
                        networkInterface = nic;
                    }
                }

                DeployerTrace.WriteInfo("Network interface found for {0}:{1}.", networkInterfaceName, (networkInterface != null) ? (true) : (false));
            }
            else
            {
                DeployerTrace.WriteInfo("Get network interface for isolated network set up from nm agent.");

#if !DotNetCoreClr
                string subnet     = string.Empty;
                string gateway    = string.Empty;
                string macAddress = string.Empty;
                if (Utility.RetrieveNMAgentInterfaceInfo(false, out subnet, out gateway, out macAddress))
                {
                    networkInterface = Utility.GetNetworkInterfaceByMacAddress(macAddress);

                    string script    = string.Format("Disable-NetAdapter -Name \"{0}\" -Confirm:$false", networkInterface.Name);
                    var    psObjects = Utility.ExecutePowerShellScript(script, false);
                    script    = string.Format("Enable-NetAdapter -Name \"{0}\" -Confirm:$false", networkInterface.Name);
                    psObjects = Utility.ExecutePowerShellScript(script, false);

                    // Wait for ip to be configured, after enabling adapter.
                    uint subnetIp;
                    uint subnetMask;
                    if (Utility.ParseSubnetIpAndMask(subnet, out subnetIp, out subnetMask))
                    {
                        var subnetIpAddress = Utility.ConvertToIpAddress(subnetIp, false);
                        networkInterface = Utility.GetNetworkInterfaceOnSubnetWithRetry(subnetIpAddress);
                    }
                }
#else
                string subnet     = string.Empty;
                string gateway    = string.Empty;
                string macAddress = string.Empty;
                if (Utility.RetrieveNMAgentInterfaceInfo(true, out subnet, out gateway, out macAddress))
                {
                    networkInterface = Utility.GetNetworkInterfaceByMacAddress(macAddress);
                }
#endif

                DeployerTrace.WriteInfo("Network interface from nm agent found:{0}.", (networkInterface != null) ? (true) : (false));
            }

            return(networkInterface);
        }
        private static void InitializeTracing(string testName)
        {
            string log      = Path.Combine(Environment.GetEnvironmentVariable("_NTTREE"), "FabricUnitTests", "log");
            string fileName = Path.Combine(log, string.Format(System.Globalization.CultureInfo.InvariantCulture, "{0}.trace", testName));

            DeployerTrace.UpdateFileLocation(fileName);
            DeployerTrace.WriteInfo("Test", "Starting test {0}", testName);
        }
        internal static void AclClusterLevelCerts(ClusterManifestType clusterManifest)
        {
            DeployerTrace.WriteInfo("AclClusterLevelCerts: Enter");

            ClusterManifestTypeNodeType[] nodeTypes = clusterManifest.NodeTypes;
            if (nodeTypes == null)
            {
                DeployerTrace.WriteWarning("AclClusterLevelCerts: No node type is defined on the cluster manifest.");
                return;
            }

            IEnumerable <FabricCertificateType> certs = nodeTypes.Where(nodeType => nodeType.Certificates != null).Select(nodeType => nodeType.Certificates)
                                                        .SelectMany(certificates => new FabricCertificateType[]
            {
                certificates.ClientCertificate,
                certificates.ClusterCertificate,
                certificates.ServerCertificate
            }).Where(cert => cert != null);

            if (!certs.Any())
            {
                DeployerTrace.WriteWarning("AclClusterLevelCerts: No load cert is defined.");
                return;
            }

            List <CertId> certIds = new List <CertId>();

            foreach (FabricCertificateType cert in certs)
            {
                certIds.Add(new CertId(cert.X509FindType, cert.X509FindValue, cert.X509StoreName));
                if (!string.IsNullOrWhiteSpace(cert.X509FindValueSecondary) &&
                    cert.X509FindValueSecondary != cert.X509FindValue)
                {
                    certIds.Add(new CertId(cert.X509FindType, cert.X509FindValueSecondary, cert.X509StoreName));
                }
            }

            string accountName  = "NT AUTHORITY\\NETWORK SERVICE";
            string runasAccount = TryGetRunAsAccountName(clusterManifest);

            if (runasAccount != null)
            {
                accountName = runasAccount;
            }

            List <CertId> acledCertIds = new List <CertId>();

            foreach (CertId certId in certIds)
            {
                if (!acledCertIds.Any(p => p.Equals(certId)))
                {
                    DeployerTrace.WriteInfo("AclClusterLevelCerts: processing {0}", certId);
                    AclCert(certId, accountName);
                    acledCertIds.Add(certId);
                    DeployerTrace.WriteInfo("AclClusterLevelCerts: {0} processed", certId);
                }
            }
        }
Beispiel #26
0
        protected override void OnExecuteOperation(DeploymentParameters parameters, ClusterManifestType targetClusterManifest, Infrastructure infrastructure)
        {
            DeployerTrace.UpdateConsoleLevel(EventLevel.Verbose);
#if !DotNetCoreClrIOT
            new DockerDnsHelper(parameters, string.Empty).CleanupAsync().GetAwaiter().GetResult();
#else
            DeployerTrace.WriteInfo("CoreClrIOT: Dns cleanup skipped on CoreClrIOT.");
#endif
        }
Beispiel #27
0
        private static bool ReduceDynamicPortRange(int dynamicStartPort, int dynamicEndPort)
        {
            if (dynamicStartPort == 0 && dynamicEndPort == 0)
            {
                DeployerTrace.WriteInfo("Dynamic port range not specified in cluster manifest. Start port : {0}. Using default dynamic port range for server.", dynamicStartPort);
                return(true);
            }

#if DotNetCoreClrLinux
            DeployerTrace.WriteInfo("Reducing dynamic port range. Start port : {0}. End port  : {1}.", dynamicStartPort, dynamicEndPort);
            if (!ReduceDynamicPortRangeForLinux(dynamicStartPort, dynamicEndPort))
            {
                DeployerTrace.WriteError("Failed to reduce dynamic port range for  Start port : {0}. End port {1}.", dynamicStartPort, dynamicEndPort);
                return(false);
            }
#else
            int dynamicPortCount = dynamicEndPort - dynamicStartPort + 1;

            DeployerTrace.WriteInfo("Reducing dynamic port range. Start port : {0}. Dynamic port count : {1}.", dynamicStartPort, dynamicPortCount);

            if (Socket.OSSupportsIPv4)
            {
                if (!ReduceDynamicPortRange(dynamicStartPort, dynamicPortCount, "ipv4", "tcp"))
                {
                    DeployerTrace.WriteError("Failed to reduce dynamic port range for ipv4:tcp. Start port : {0}. Dynamic port count : {1}.", dynamicStartPort, dynamicPortCount);

                    return(false);
                }

                if (!ReduceDynamicPortRange(dynamicStartPort, dynamicPortCount, "ipv4", "udp"))
                {
                    DeployerTrace.WriteError("Failed to reduce dynamic port range for ipv4:udp. Start port : {0}. Dynamic port count : {1}.", dynamicStartPort, dynamicPortCount);

                    return(false);
                }
            }

            if (Socket.OSSupportsIPv6)
            {
                if (!ReduceDynamicPortRange(dynamicStartPort, dynamicPortCount, "ipv6", "tcp"))
                {
                    DeployerTrace.WriteError("Failed to reduce dynamic port range for ipv6:tcp. Start port : {0}. Dynamic port count : {1}.", dynamicStartPort, dynamicPortCount);

                    return(false);
                }

                if (!ReduceDynamicPortRange(dynamicStartPort, dynamicPortCount, "ipv6", "udp"))
                {
                    DeployerTrace.WriteError("Failed to reduce dynamic port range for ipv6:udp. Start port : {0}. Dynamic port count : {1}.", dynamicStartPort, dynamicPortCount);

                    return(false);
                }
            }
#endif

            return(true);
        }
        internal static void InstallFabricInstallerService(string fabricPackageRoot, string machineName)
        {
            if (string.IsNullOrEmpty(machineName))
            {
                machineName = Helpers.GetMachine();
            }
            DeployerTrace.WriteInfo(StringResources.Info_CheckIfFabricInstallerServiceAlreadyExists, machineName);
            var fabricInstallerService = GetService(Constants.FabricInstallerServiceName, machineName);

            if (fabricInstallerService == null)
            {
                DeployerTrace.WriteInfo(StringResources.Info_InstallFabricInstallerServiceOnMachine, machineName);
                string servicePath = Path.Combine(fabricPackageRoot, Constants.FabricInstallerServiceRelativePathFromRoot);
                var    remotePath  = Helpers.GetRemotePath(servicePath, machineName);
                if (!File.Exists(remotePath))
                {
                    string errorMessage = string.Format(CultureInfo.InvariantCulture, StringResources.Error_UnableToInstallFabricInstallerSvcFNF, remotePath);
                    DeployerTrace.WriteError(errorMessage);
                    throw new FileNotFoundException(errorMessage);
                }

                var serviceCreationinfo = new ServiceCreationInfo()
                {
                    ServicePath = string.Format("\"{0}\"", servicePath),
                    DisplayName = Constants.FabricInstallerDisplayName
                };

                IntPtr svcHandle = GetServiceHandle(machineName, Constants.FabricInstallerServiceName, serviceCreationinfo);
                if (svcHandle == IntPtr.Zero)
                {
                    string errorMessage = string.Format(CultureInfo.InvariantCulture,
                                                        StringResources.Error_OpenSCManagerReturnsErrorWhileCreating, machineName, Utility.GetWin32ErrorMessage(Marshal.GetLastWin32Error()));
                    DeployerTrace.WriteError(errorMessage);
                    throw new InvalidOperationException(errorMessage);
                }
                else
                {
                    try
                    {
                        SetServiceDescription(svcHandle, Constants.FabricInstallerServiceDescription);
                        SetStartupTypeDelayedAuto(svcHandle);
                        SetServiceFailureActionsForRestart(svcHandle,
                                                           Constants.FabricInstallerServiceDelayInSecondsBeforeRestart,
                                                           Constants.FabricUpgradeFailureCountResetPeriodInDays);
                    }
                    finally
                    {
                        NativeMethods.CloseServiceHandle(svcHandle);
                    }
                }
            }
            else
            {
                DeployerTrace.WriteInfo(StringResources.Info_FabricInstallerSvcAlreadyInstalled, machineName);
            }
        }
Beispiel #29
0
        protected static void WriteFabricHostSettingsFile(string hostSettingsFolder, SettingsType hostSettings, string machineName)
        {
            Dictionary <string, HostedServiceState> hostedServiceStates = GetHostedServicesState(hostSettingsFolder);

            List <SettingsTypeSection> sections = new List <SettingsTypeSection>();

            if (hostSettings.Section != null)
            {
                foreach (var hostSettingsSection in hostSettings.Section)
                {
                    if (hostedServiceStates.ContainsKey(hostSettingsSection.Name))
                    {
                        switch (hostedServiceStates[hostSettingsSection.Name])
                        {
                        case HostedServiceState.Up:
                            sections.Add(hostSettingsSection);
                            break;

                        case HostedServiceState.Killed:
                            List <SettingsTypeSectionParameter> parameters = new List <SettingsTypeSectionParameter>(hostSettingsSection.Parameter);
                            parameters.Add(new SettingsTypeSectionParameter()
                            {
                                Name = Constants.ParameterNames.Disabled, Value = true.ToString()
                            });
                            sections.Add(new SettingsTypeSection()
                            {
                                Name = hostSettingsSection.Name, Parameter = parameters.ToArray()
                            });
                            break;

                        default:
                            continue;
                        }
                    }
                    else
                    {
                        sections.Add(hostSettingsSection);
                    }
                }
            }

            SettingsType outputSettings = new SettingsType()
            {
                Section = sections.ToArray()
            };
            string fabricHostSettingPath = Path.Combine(hostSettingsFolder, Constants.FileNames.FabricHostSettings);

            if (!string.IsNullOrEmpty(machineName) && !machineName.Equals(Helpers.GetMachine()))
            {
                fabricHostSettingPath = Helpers.GetRemotePath(fabricHostSettingPath, machineName);
            }

            XmlHelper.WriteXmlExclusive <SettingsType>(fabricHostSettingPath, outputSettings);
            DeployerTrace.WriteInfo("Host Settings file generated at {0}", fabricHostSettingPath);
        }
Beispiel #30
0
        internal static bool ExecuteCommand(string command, string arguments, string workingDir, bool waitForExit, TimeSpan?timeout)
        {
            bool success = false;

            DeployerTrace.WriteInfo("Executing command: {0} {1} from working dir {2} wait for exit {3}", command, arguments, workingDir, waitForExit);

            try
            {
                var p = new Process();
                p.StartInfo.FileName         = command;
                p.StartInfo.Arguments        = arguments;
                p.StartInfo.UseShellExecute  = false;
                p.StartInfo.CreateNoWindow   = false;
                p.StartInfo.WorkingDirectory = workingDir;
                p.Start();

                success = true;

                if (waitForExit)
                {
                    if (timeout.HasValue)
                    {
                        if (timeout.Value == TimeSpan.MaxValue)
                        {
                            p.WaitForExit();
                        }
                        else
                        {
                            if (!p.WaitForExit((int)timeout.Value.TotalMilliseconds))
                            {
                                p.Kill();
                                DeployerTrace.WriteInfo("Execution of command {0} {1} from working dir {2} timed out after {3} milliseconds",
                                                        command,
                                                        arguments,
                                                        workingDir,
                                                        timeout.Value.TotalMilliseconds);
                                success = false;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                DeployerTrace.WriteError("Failed to execute command {0} {1} from working dir {2} wait for exit {3} exception {4}",
                                         command,
                                         arguments,
                                         workingDir,
                                         waitForExit,
                                         ex);
            }

            return(success);
        }