Пример #1
0
        /// <summary>
        /// Init instance options and starts the connections
        /// </summary>
        private void InitInstances()
        {
            if (Options.Instances == null || Options.Instances.Length < 1)
            {
                return;
            }

            _connectors = new TmqStickyConnector[Options.Instances.Length];

            for (int i = 0; i < _connectors.Length; i++)
            {
                InstanceOptions options   = Options.Instances[i];
                TimeSpan        reconnect = TimeSpan.FromMilliseconds(options.ReconnectWait);

                TmqStickyConnector connector = options.KeepMessages
                                                   ? new TmqAbsoluteConnector(reconnect, () => CreateInstanceClient(options))
                                                   : new TmqStickyConnector(reconnect, () => CreateInstanceClient(options));

                _connectors[i] = connector;
                connector.Tag  = options;

                connector.AddHost(options.Host);
                connector.Run();
            }
        }
        internal RunningInstance(IFileSystem fileSystem, InstanceOptions options, IRunnableInstance instanceOf, Functions outboundFunctions)
        {
            FileSystem         = fileSystem;
            InstanceOf         = instanceOf;
            _outboundFunctions = outboundFunctions;
            _asyncOperations   = new AsyncManager(_outboundFunctions);
            var callbacks = new PRJ_CALLBACKS
            {
                CancelCommandCallback             = ProcessCancelCommand,
                QueryFileNameCallback             = fileSystem is ISeekableFileSystem ? ProcessQueryFileName : (PRJ_QUERY_FILE_NAME_CB)null,
                StartDirectoryEnumerationCallback = ProcessStartEnumeration,
                EndDirectoryEnumerationCallback   = ProcessEndEnumeration,
                GetDirectoryEnumerationCallback   = ProcessEnumerationStep,
                GetPlaceholderInfoCallback        = ProcessPlaceholderRequest,
                GetFileDataCallback = ProcessDataRequest
            };
            PRJ_STARTVIRTUALIZING_OPTIONS optionsNative = CreateOptions(fileSystem, options, callbacks);

            _callbacks = new NativeBuffer <PRJ_CALLBACKS>(callbacks);

            if (_outboundFunctions.PrjStartVirtualizing(
                    instanceOf.RootPath,
                    _callbacks.Buffer,
                    IntPtr.Zero,
                    optionsNative,
                    out _namespaceVirtualizationContext) != HRESULT.S_OK)
            {
                throw new Exception("Failed to start up");
            }
            if (_outboundFunctions.PrjGetVirtualizationInstanceInfo(_namespaceVirtualizationContext, ref _virtualizationInfo) != HRESULT.S_OK)
            {
                throw new Exception("Failed to start up");
            }
        }
 public RunnableInstance(string name, string rootPath, Guid instanceGuid, InstanceOptions options, Functions outboundFunctions)
 {
     Name               = name;
     RootPath           = rootPath;
     InstanceGuid       = instanceGuid;
     DefaultOptions     = options;
     _outboundFunctions = outboundFunctions;
 }
Пример #4
0
        /// <summary>
        /// Client creation action for server instances
        /// </summary>
        private static TmqClient CreateInstanceClient(InstanceOptions options)
        {
            TmqClient client = new TmqClient();

            client.SetClientName(options.Name);
            client.SetClientToken(options.Token);
            client.SetClientType("server");
            return(client);
        }
Пример #5
0
        private bool ReadRecord()
        {
            byte[] smallBuffer = new byte[36];
            var    bytesRead   = _dataFile.Read(smallBuffer, 0, smallBuffer.Length);

            if (bytesRead == 0)
            {
                return(false);
            }
            if (bytesRead != smallBuffer.Length)
            {
                throw new NotSupportedException(UnexpectedFF);
            }
            if (smallBuffer[0] != 0xFF)
            {
                throw new NotSupportedException(UnexpectedFF);
            }
            var options = new InstanceOptions();

            if ((smallBuffer[1] & 1) == 1)
            {
                options.NegativePathCache = true;
            }
            options.PoolThreadCount       = BinaryPrimitives.ReadInt32BigEndian(smallBuffer.AsSpan(4, 4));
            options.ConcurrentThreadCount = BinaryPrimitives.ReadInt32BigEndian(smallBuffer.AsSpan(8, 4));
            var instanceGuid = new Guid(smallBuffer.AsSpan(12, 16).ToArray());
            var nameLength   = BinaryPrimitives.ReadInt32BigEndian(smallBuffer.AsSpan(28, 4));
            var pathLength   = BinaryPrimitives.ReadInt32BigEndian(smallBuffer.AsSpan(32, 4));
            var nameBuffer   = new byte[nameLength];
            var pathBuffer   = new byte[pathLength];

            if (_dataFile.Read(nameBuffer, 0, nameLength) != nameLength)
            {
                throw new NotSupportedException(UnexpectedFF);
            }
            if (_dataFile.Read(pathBuffer, 0, pathLength) != pathLength)
            {
                throw new NotSupportedException(UnexpectedFF);
            }
            var name = Encoding.UTF8.GetString(nameBuffer);
            var path = Encoding.UTF8.GetString(pathBuffer);

            if (_names.ContainsKey(name) || _paths.ContainsKey(path) || _guids.ContainsKey(instanceGuid))
            {
                throw new NotSupportedException(UnexpectedFF);
            }
            var runnable = new RunnableInstance(name, path, instanceGuid, options, _outboundFunctions);

            _names.Add(name, runnable);
            _paths.Add(path, runnable);
            _guids.Add(instanceGuid, runnable);
            return(true);
        }
Пример #6
0
        private readonly ReaderWriterLockSlim configLock; // synchronizes access to the instance configuration


        /// <summary>
        /// Initializes a new instance of the class.
        /// </summary>
        public ScadaInstance(AppDirs appDirs, ILog log, InstanceOptions instanceOptions, InstanceConfig instanceConfig)
        {
            if (appDirs == null)
            {
                throw new ArgumentNullException(nameof(appDirs));
            }

            this.log             = log ?? throw new ArgumentNullException(nameof(log));
            this.instanceOptions = instanceOptions ?? throw new ArgumentNullException(nameof(instanceOptions));
            this.instanceConfig  = instanceConfig ?? throw new ArgumentNullException(nameof(instanceConfig));
            configLock           = new ReaderWriterLockSlim();
            PathBuilder          = new PathBuilder(instanceOptions.Directory, instanceConfig.LogDir, appDirs.ExeDir);
        }
        private PRJ_STARTVIRTUALIZING_OPTIONS CreateOptions(IFileSystem fileSystem, InstanceOptions options, PRJ_CALLBACKS callbacks)
        {
            var initialNotifications = Enumerable.Empty <PRJ_NOTIFICATION_MAPPING>();

            if (fileSystem is INotifiableFileSystem notifiable)
            {
                callbacks.NotificationCallback = ProcessNotification;
                initialNotifications           = notifiable.StartingNotifications().Select(n => new PRJ_NOTIFICATION_MAPPING {
                    NotificationRoot = n.Path, NotificationBitMask = (PRJ_NOTIFY_TYPES)(int)n.Notifications
                });
            }

            var optionsNative = new PRJ_STARTVIRTUALIZING_OPTIONS
            {
                PoolThreadCount       = options.PoolThreadCount,
                ConcurrentThreadCount = options.ConcurrentThreadCount,
                Flags = options.NegativePathCache ? PRJ_STARTVIRTUALIZING_FLAGS.PRJ_FLAG_USE_NEGATIVE_PATH_CACHE : PRJ_STARTVIRTUALIZING_FLAGS.PRJ_FLAG_NONE
            };

            optionsNative.NotificationMappings      = initialNotifications.ToArray();
            optionsNative.NotificationMappingsCount = optionsNative.NotificationMappings.Length;
            return(optionsNative);
        }
Пример #8
0
        /// <summary>
        /// Install/uninstall MSI
        /// </summary>
        /// <returns></returns>
        public Result InstallUninstall(InstanceOptions options)
        {
            ConsoleOutput.WriteLine("Running 'Remote:{0}', '{1}'", _installerConfig.Name, _installerConfig.DestinationPath);

            // remote logfiles
            string install_logfile = string.Empty;
            string uninstall_logfile = string.Empty;

            // the remote install result
            Result result = new Result(
                _installerConfig.Name,
                _installerConfig.SvnRevision);

            if (!Directory.Exists(LogPath))
            {
                ConsoleOutput.WriteLine("Creating directory '{0}'", LogPath);
                Directory.CreateDirectory(LogPath);
            }

            ConsoleOutput.WriteLine("Saving logs in '{0}'", LogPath);

            SequenceDrivers additionalSequences = new SequenceDrivers();

            ExecuteDriver executeDriver = new ExecuteDriver(this);
            executeDriver.Add(_config.Tasks);
            executeDriver.Add(_config.VirtualMachines.Tasks);
            executeDriver.Add(_vmConfig.Tasks);
            executeDriver.Add(_vmConfig.Snapshots.Tasks);
            executeDriver.Add(_snapshotConfig.Tasks);
            executeDriver.Add(_config.Installers.Tasks);
            executeDriver.Add(_installerConfig.Tasks);
            additionalSequences.Add(executeDriver);

            CopyFilesDriver copyFilesDriver = new CopyFilesDriver(this);
            copyFilesDriver.Add(_config.CopyFiles);
            copyFilesDriver.Add(_config.VirtualMachines.CopyFiles);
            copyFilesDriver.Add(_vmConfig.CopyFiles);
            copyFilesDriver.Add(_vmConfig.Snapshots.CopyFiles);
            copyFilesDriver.Add(_snapshotConfig.CopyFiles);
            copyFilesDriver.Add(_config.Installers.CopyFiles);
            copyFilesDriver.Add(_installerConfig.CopyFiles);
            additionalSequences.Add(copyFilesDriver);

            // execute and copy files before all
            result.AddRange(additionalSequences.ExecuteSequence(
                SequenceWhen.beforeall));

            DateTime start = DateTime.UtcNow;
            try
            {
                // execute the installers
                try
                {
                    VirtualMachineDeployment deploy = null;

                    if (!_simulationOnly)
                    {
                        deploy = _installerConfig.CreateDeployment(_vm);
                    }

                    // install
                    if (options.Install)
                    {
                        // execute and copy files before install
                        result.AddRange(additionalSequences.ExecuteSequence(
                            SequenceWhen.beforeinstall));

                        ConsoleOutput.WriteLine("Installing 'Remote:{0}', '{1}'",
                            _installerConfig.Name, _installerConfig.DestinationPath);

                        result.SuccessfulInstall = InstallResult.False;

                        if (!_simulationOnly)
                        {
                            deploy.Install(out install_logfile);
                        }

                        result.SuccessfulInstall = InstallResult.True;
                        result.AddRange(additionalSequences.ExecuteSequence(
                            SequenceWhen.aftersuccessfulinstall));
                    }

                    // uninstall
                    if (options.Uninstall)
                    {
                        // execute and copy files before uninstall
                        result.AddRange(additionalSequences.ExecuteSequence(
                            SequenceWhen.beforeuninstall));

                        DateTime startUnInstall = DateTime.UtcNow;
                        ConsoleOutput.WriteLine("Uninstalling 'Remote:{0}', '{1}'", _installerConfig.Name, _installerConfig.DestinationPath);

                        result.SuccessfulUnInstall = InstallResult.False;

                        if (!_simulationOnly)
                        {
                            deploy.UnInstall(out uninstall_logfile);
                        }

                        result.SuccessfulUnInstall = InstallResult.True;
                        result.AddRange(additionalSequences.ExecuteSequence(
                            SequenceWhen.aftersuccessfuluninstall));
                    }

                    if (! _simulationOnly && deploy != null && deploy.IsRebootRequired())
                    {
                        ConsoleOutput.WriteLine("Reboot required after 'Remote:{0}'", _installerConfig.Name);
                        result.RebootRequired = true;
                    }
                }
                catch (Exception ex)
                {
                    result.LastError = ex.Message;
                    result.Success = false;
                    ConsoleOutput.WriteLine(ex);
                    result.AddRange(additionalSequences.ExecuteSequence(
                        SequenceWhen.afterfailedinstalluninstall));
                }
            }
            finally
            {
                result.Duration = DateTime.UtcNow.Subtract(start);
                ConsoleOutput.WriteLine("Done after '{0}'", result.Duration);
            }

            result.AddRange(additionalSequences.ExecuteSequence(
                SequenceWhen.afterall));

            // copy the remote logfiles back
            if (!string.IsNullOrEmpty(install_logfile))
            {
                string target_install_logfile = Path.Combine(LogPath, Path.GetFileName(install_logfile));
                ConsoleOutput.WriteLine("Collecting 'Remote:{0}' => '{1}'", install_logfile, target_install_logfile);
                _vm.CopyFileFromGuestToHost(install_logfile, target_install_logfile);
                result.InstallLogfile = Path.Combine(ShortLogPath, Path.GetFileName(install_logfile));
            }

            if (!string.IsNullOrEmpty(uninstall_logfile))
            {
                string target_uninstall_logfile = Path.Combine(LogPath, Path.GetFileName(uninstall_logfile));
                ConsoleOutput.WriteLine("Collecting 'Remote:{0}' => '{1}'", uninstall_logfile, target_uninstall_logfile);
                _vm.CopyFileFromGuestToHost(uninstall_logfile, target_uninstall_logfile);
                result.UnInstallLogfile = Path.Combine(ShortLogPath, Path.GetFileName(uninstall_logfile));
            }

            return result;
        }
 public IRunningInstance Start(IFileSystem fileSystem, InstanceOptions overrideOptions)
 {
     return(new RunningInstance(fileSystem, overrideOptions, this, _outboundFunctions));
 }
Пример #10
0
        /// <summary>
        /// Install/uninstall MSI
        /// </summary>
        /// <returns></returns>
        public Result InstallUninstall(InstanceOptions options)
        {
            ConsoleOutput.WriteLine("Running 'Remote:{0}', '{1}'", _installerConfig.Name, _installerConfig.DestinationPath);

            // remote logfiles
            string install_logfile   = string.Empty;
            string uninstall_logfile = string.Empty;

            // the remote install result
            Result result = new Result(
                _installerConfig.Name,
                _installerConfig.SvnRevision);

            if (!Directory.Exists(LogPath))
            {
                ConsoleOutput.WriteLine("Creating directory '{0}'", LogPath);
                Directory.CreateDirectory(LogPath);
            }

            ConsoleOutput.WriteLine("Saving logs in '{0}'", LogPath);

            SequenceDrivers additionalSequences = new SequenceDrivers();

            ExecuteDriver executeDriver = new ExecuteDriver(this);

            executeDriver.Add(_config.Tasks);
            executeDriver.Add(_config.VirtualMachines.Tasks);
            executeDriver.Add(_vmConfig.Tasks);
            executeDriver.Add(_vmConfig.Snapshots.Tasks);
            executeDriver.Add(_snapshotConfig.Tasks);
            executeDriver.Add(_config.Installers.Tasks);
            executeDriver.Add(_installerConfig.Tasks);
            additionalSequences.Add(executeDriver);

            CopyFilesDriver copyFilesDriver = new CopyFilesDriver(this);

            copyFilesDriver.Add(_config.CopyFiles);
            copyFilesDriver.Add(_config.VirtualMachines.CopyFiles);
            copyFilesDriver.Add(_vmConfig.CopyFiles);
            copyFilesDriver.Add(_vmConfig.Snapshots.CopyFiles);
            copyFilesDriver.Add(_snapshotConfig.CopyFiles);
            copyFilesDriver.Add(_config.Installers.CopyFiles);
            copyFilesDriver.Add(_installerConfig.CopyFiles);
            additionalSequences.Add(copyFilesDriver);

            // execute and copy files before all
            result.AddRange(additionalSequences.ExecuteSequence(
                                SequenceWhen.beforeall));

            DateTime start = DateTime.UtcNow;

            try
            {
                // execute the installers
                try
                {
                    VirtualMachineDeployment deploy = null;

                    if (!_simulationOnly)
                    {
                        deploy = _installerConfig.CreateDeployment(_vm);
                    }

                    // install
                    if (options.Install)
                    {
                        // execute and copy files before install
                        result.AddRange(additionalSequences.ExecuteSequence(
                                            SequenceWhen.beforeinstall));

                        ConsoleOutput.WriteLine("Installing 'Remote:{0}', '{1}'",
                                                _installerConfig.Name, _installerConfig.DestinationPath);

                        result.SuccessfulInstall = InstallResult.False;

                        if (!_simulationOnly)
                        {
                            deploy.Install(out install_logfile);
                        }

                        result.SuccessfulInstall = InstallResult.True;
                        result.AddRange(additionalSequences.ExecuteSequence(
                                            SequenceWhen.aftersuccessfulinstall));
                    }

                    // uninstall
                    if (options.Uninstall)
                    {
                        // execute and copy files before uninstall
                        result.AddRange(additionalSequences.ExecuteSequence(
                                            SequenceWhen.beforeuninstall));

                        DateTime startUnInstall = DateTime.UtcNow;
                        ConsoleOutput.WriteLine("Uninstalling 'Remote:{0}', '{1}'", _installerConfig.Name, _installerConfig.DestinationPath);

                        result.SuccessfulUnInstall = InstallResult.False;

                        if (!_simulationOnly)
                        {
                            deploy.UnInstall(out uninstall_logfile);
                        }

                        result.SuccessfulUnInstall = InstallResult.True;
                        result.AddRange(additionalSequences.ExecuteSequence(
                                            SequenceWhen.aftersuccessfuluninstall));
                    }

                    if (!_simulationOnly && deploy != null && deploy.IsRebootRequired())
                    {
                        ConsoleOutput.WriteLine("Reboot required after 'Remote:{0}'", _installerConfig.Name);
                        result.RebootRequired = true;
                    }
                }
                catch (Exception ex)
                {
                    result.LastError = ex.Message;
                    result.Success   = false;
                    ConsoleOutput.WriteLine(ex);
                    result.AddRange(additionalSequences.ExecuteSequence(
                                        SequenceWhen.afterfailedinstalluninstall));
                }
            }
            finally
            {
                result.Duration = DateTime.UtcNow.Subtract(start);
                ConsoleOutput.WriteLine("Done after '{0}'", result.Duration);
            }

            result.AddRange(additionalSequences.ExecuteSequence(
                                SequenceWhen.afterall));

            // copy the remote logfiles back
            if (!string.IsNullOrEmpty(install_logfile))
            {
                string target_install_logfile = Path.Combine(LogPath, Path.GetFileName(install_logfile));
                ConsoleOutput.WriteLine("Collecting 'Remote:{0}' => '{1}'", install_logfile, target_install_logfile);
                _vm.CopyFileFromGuestToHost(install_logfile, target_install_logfile);
                result.InstallLogfile = Path.Combine(ShortLogPath, Path.GetFileName(install_logfile));
            }

            if (!string.IsNullOrEmpty(uninstall_logfile))
            {
                string target_uninstall_logfile = Path.Combine(LogPath, Path.GetFileName(uninstall_logfile));
                ConsoleOutput.WriteLine("Collecting 'Remote:{0}' => '{1}'", uninstall_logfile, target_uninstall_logfile);
                _vm.CopyFileFromGuestToHost(uninstall_logfile, target_uninstall_logfile);
                result.UnInstallLogfile = Path.Combine(ShortLogPath, Path.GetFileName(uninstall_logfile));
            }

            return(result);
        }
Пример #11
0
        /// <summary>
        /// Gets connected instance list
        /// </summary>
        private async Task GetInstanceList(MqClient client, TmqMessage message)
        {
            if (_server.AdminAuthorization == null)
            {
                if (message.ResponseRequired)
                {
                    await client.SendAsync(MessageBuilder.ResponseStatus(message, KnownContentTypes.Unauthorized));
                }

                return;
            }

            bool grant = await _server.AdminAuthorization.CanManageInstances(client, message);

            if (!grant)
            {
                if (message.ResponseRequired)
                {
                    await client.SendAsync(MessageBuilder.ResponseStatus(message, KnownContentTypes.Unauthorized));
                }

                return;
            }

            List <InstanceInformation> list = new List <InstanceInformation>();

            //slave instances
            List <SlaveInstance> slaves = _server.SlaveInstances.GetAsClone();

            foreach (SlaveInstance slave in slaves)
            {
                list.Add(new InstanceInformation
                {
                    IsSlave     = true,
                    Host        = slave.RemoteHost,
                    IsConnected = slave.Client.IsConnected,
                    Id          = slave.Client.UniqueId,
                    Name        = slave.Client.Name,
                    Lifetime    = slave.ConnectedDate.LifetimeMilliseconds()
                });
            }

            //master instances
            foreach (TmqStickyConnector connector in _server.InstanceConnectors)
            {
                InstanceOptions options = connector.Tag as InstanceOptions;
                TmqClient       c       = connector.GetClient();

                list.Add(new InstanceInformation
                {
                    IsSlave     = false,
                    Host        = options?.Host,
                    IsConnected = connector.IsConnected,
                    Id          = c.ClientId,
                    Name        = options?.Name,
                    Lifetime    = Convert.ToInt64(connector.Lifetime.TotalMilliseconds)
                });
            }

            TmqMessage response = message.CreateResponse();

            message.ContentType = KnownContentTypes.InstanceList;
            await response.SetJsonContent(list);

            await client.SendAsync(response);
        }
Пример #12
0
 IRunnableInstance IInstanceManager.Register(string name, string rootPath, string targetPath, InstanceOptions defaultOptions, PlaceholderVersion rootVersionInfo, Guid instanceGuid)
 {
     lock (_instanceLock)
     {
         if (_names.ContainsKey(name))
         {
             throw new ArgumentException("Name already registered", nameof(name));
         }
         if (_paths.ContainsKey(rootPath))
         {
             throw new ArgumentException("Path already registered", nameof(rootPath));
         }
         if (_guids.ContainsKey(instanceGuid))
         {
             throw new ArgumentException("Instance already registered", nameof(instanceGuid));
         }
         if (_outboundFunctions.PrjMarkDirectoryAsPlaceholder(rootPath, targetPath, LevelShifter.PRJ_PLACEHOLDER_VERSION_INFOFromPlaceholderVersion(rootVersionInfo), instanceGuid) != HRESULT.S_OK)
         {
             throw new NotSupportedException("Could not register path with System");
         }
         var instance = new RunnableInstance(name, rootPath, instanceGuid, defaultOptions, _outboundFunctions);
         _names.Add(name, instance);
         _paths.Add(rootPath, instance);
         _guids.Add(instanceGuid, instance);
         WriteRecord(instance);
         _dataFile.Flush();
         return(instance);
     }
 }