示例#1
0
        /// <summary>
        /// ThreadStart method.  Executed in a worker thread to connect to the VM associated with this object,
        /// possibly start the EPR service and/or to pull information regarding the EPR's and profiles
        /// that this Host has.
        /// NOTE: Uses synchronized object of a Netowrk Drive Letter.
        /// </summary>
        private void Connect()
        {
            string _tnd       = null;
            bool   _connected = false;

            IPAddress[] _localIPs;

            //-------------------------------------------------------------------------------------------------------
            //Verify that the hostname is good.  if we can't find it via dns - then remove it from the list.
            try
            {
                _localIPs = Dns.GetHostAddresses(HostName);
                if (_localIPs.Length > 0)
                {
                    var _isOn = new System.Net.NetworkInformation.Ping();
                    if (_isOn.Send(HostName).Status == System.Net.NetworkInformation.IPStatus.Success)
                    {
                        mainIPAddress = _localIPs[0];
                    }
                    else
                    {
                        WorkStatus = HostVMStatus.Error;
                        GenUtils.HangError("Cannot connect with " + HostName + ". Make sure it is powered up.");
                        return;
                    }
                }
            }
            catch (Exception)
            {
                GenUtils.HangError(HostName + " is an unknown host?");
                WorkStatus = HostVMStatus.Error;
                return;
            }
            //-------------------------------------------------------------------------------------------------------
            // In order to connect, we will first assume that the EPR Service is actually running on the Host VM
            // and just attempt to establish communications.  If they fail for a timeout, then we will know that
            // we need to atempt to start the service on the remote host.
            try
            {
//				eprHostChannel = new DeviceSimulatorChannel(HostName);
                eprHostChannel    = RemOpWithTimedRetries(() => (new DeviceSimulatorChannel(HostName)), TimeSpan.FromSeconds(3), 3);
                InstalledProfiles = eprHostChannel.IntalledProfiles;
                _connected        = true;
            }
            catch (Exception _ex)
            {
                //-----------------------------------------------------------------------------------------------------
                // Landing here *usually* means that the EPR service is not running on the remote system.  So, we will
                // attempt to start the service. In order to start the remote service, we will check to see if the
                // remote dservice is running.  If it is, then there is prossibly a firewall blocking us.  If it is
                // not, then we check to see if the executable exists. If it does not, then the EPR is not installed
                // (or usable) on the remote system.  If the executable does exist, then start it up.
                if (((_ex.Message.Contains("timed out")) || _ex.Message.Contains("No endpoint", StringComparison.OrdinalIgnoreCase)) && (forceConn))
                {
                    if (!GenUtils.CheckForRemoteProcess(HostName, Program.EPRSERVICE))
                    {
                        try
                        {
                            _tnd = GenUtils.MapNetworkDrive(Path.Combine(@"\\", HostName, "C$"), @"ETL\jawa", "!QAZ2wsx");
                            bool _canStart = File.Exists(Path.Combine(_tnd, Program.EPRSERVICEFILEPATH));
                            GenUtils.DisconnectNetworkDrive(_tnd, true);
                            _tnd = null;
                            if (_canStart)
                            {
                                GenUtils.Exclaim("EPR Service is not running on " + HostName + ".  Attempting to start service...");
                                GenUtils.ExecuteRemoteProcess(HostName, Path.Combine(@"C:\", Program.EPRSERVICEFILEPATH), true, false);
                                eprHostChannel    = new DeviceSimulatorChannel(HostName);
                                InstalledProfiles = eprHostChannel.IntalledProfiles;
                                _connected        = true;
                                GenUtils.Exclaim("EPR Service started on " + HostName);
                            }
                            else
                            {
                                GenUtils.HangError("Does not appear that the EPR Service is installed on " + HostName);
                                WorkStatus = HostVMStatus.Error;
                            }
                        }
                        catch (Exception _inner)
                        {
                            if (!string.IsNullOrEmpty(_tnd))
                            {
                                GenUtils.DisconnectNetworkDrive(_tnd, true);
                                _tnd = null;
                            }
                            GenUtils.HangError("General network failure starting remote EPR service on  " + HostName + " :: " + _inner.Message);
                            WorkStatus = HostVMStatus.Error;
                        }
                    }
                    else
                    {
                        GenUtils.HangError("Could not connect with remote service on " + HostName +
                                           ". Verify the VM IP, and that it is powered on, or check for possible firewall blocking communications.");
                        WorkStatus = HostVMStatus.Error;
                    }
                }
                else if (forceConn)
                {
                    WorkStatus = HostVMStatus.Error;
                    GenUtils.HangError("General Network failure on " + HostName + " :: " + _ex.Message);
                }
                else
                {
                    WorkStatus = HostVMStatus.NotConnected;
                    GenUtils.HangError("Could not connect with " + HostName + " (-Skip specified). Host will be removed from Main Action list");
                }
            }
            //-----------------------------------------------------------------------------------------------------
            // If the connection is made, then pull all of the relevent information about the EPRs from the Host
            // VM.
            if (_connected)
            {
                try
                {
                    EPRs.Clear();
                    InstalledProfiles.Clear();
                    InstalledProfiles = eprHostChannel.IntalledProfiles;
                    foreach (IPAddress _ip in eprHostChannel.AvailableIPAddresses)
                    {
//						if ((!_ip.ToString().EndsWith(".0")) && (HP.ScalableTest.Network.NetworkUtil.IsNonRoutableIpAddress(_ip)))
                        if ((!_ip.ToString().EndsWith(".0")) &&
                            (((_localIPs.Length == 1) && (HP.ScalableTest.Network.NetworkUtil.IsNonRoutableIpAddress(_ip))) ||
                             ((_localIPs.Length > 1) && (!_ip.Equals(mainIPAddress)))))
                        {
                            //-----------------------------------------------------------------------------------------------
                            // If the user did not specify any IP address on the command line
                            //     (format -vm Host/IP1/IP2/...,Host,Host/IP1/IP2...)
                            // then we default to ALL of the IP addresses returned both in use and those that are available.
                            // If the user DID supply some IP's - then everything on this Host must be limited to those IP's
                            // ONLY - so skip anything that is not on that list.
                            if (specifiedIPs.Count == 0)
                            {
                                EPRs.Add(new EPRInstance(_ip));
                            }
                            else
                            {
                                if (specifiedIPs.Contains(_ip.ToString()))
                                {
                                    EPRs.Add(new EPRInstance(_ip));
                                }
                            }
                        }
                    }
                    EPRInstancesAvailable = eprHostChannel.AvailableIPAddresses.Count - 1;
                    foreach (DeviceInstance _di in eprHostChannel.Instances)
                    {
                        //-----------------------------------------------------------------------------------------------
                        // See the comment above...
                        if (specifiedIPs.Count == 0)
                        {
                            EPRs.Add(new EPRInstance(_di, true));
                        }
                        else
                        {
                            if (specifiedIPs.Contains(_di.Address.ToString()))
                            {
                                EPRs.Add(new EPRInstance(_di, true));
                            }
                        }
                    }
                    if (specifiedIPs.Count > 0)
                    {
                        //-------------------------------------------------------------------------------------------------
                        // If the user specified some IP's for this host on the command line, those are the ONLY IPs that
                        // can be operated on. So - check and see if they are valid.  Hang messages if not and if none
                        // are valid - then return an error so this host is removed from the Action list. This also
                        // prevents us from having to check the IP validity anywhere else!
                        int _sNdx = 0;
                        do
                        {
                            for ( ; _sNdx < specifiedIPs.Count; _sNdx++)
                            {
                                string _ip = specifiedIPs[_sNdx];
                                if (EPRs.FindIndex(_inst => string.Compare(_ip, _inst.IPAddress.ToString()) == 0) < 0)
                                {
                                    GenUtils.Exclaim(_ip + " is not a valid IP for host " + _ip + ". Removing from the IP List");
                                    break;
                                }
                            }
                            if (_sNdx < specifiedIPs.Count)
                            {
                                specifiedIPs.RemoveAt(_sNdx);
                            }
                        }while (_sNdx < specifiedIPs.Count);
                        WorkStatus = specifiedIPs.Count > 0 ? HostVMStatus.Done : HostVMStatus.Error;
                    }
                    else
                    {
                        WorkStatus = HostVMStatus.Done;
                    }
                }
                catch (Exception _ex)
                {
                    WorkStatus = HostVMStatus.Error;
                    GenUtils.HangError(HostName + " : Connection Exception caught: " + _ex.Message);
                    GenUtils.DisconnectNetworkDrive(_tnd, true);
                }
            }
        }