private webModel.ModulePushNotification ScheduleJob(webModel.ModuleBackgroundJobOptions options)
        {
            var notification = new webModel.ModulePushNotification(_userNameResolver.GetCurrentUserName());

            switch (options.Action)
            {
            case webModel.ModuleAction.Install:
                notification.Title = "Install Module";
                notification.ProgressLog.Add(new webModel.ProgressMessage {
                    Level = ProgressMessageLevel.Info.ToString(), Message = "Starting installation..."
                });
                break;

            case webModel.ModuleAction.Uninstall:
                notification.Title = "Uninstall Module";
                notification.ProgressLog.Add(new webModel.ProgressMessage {
                    Level = ProgressMessageLevel.Info.ToString(), Message = "Starting uninstall..."
                });
                break;
            }

            _pushNotifier.Upsert(notification);

            BackgroundJob.Enqueue(() => ModuleBackgroundJob(options, notification));

            return(notification);
        }
        public IHttpActionResult UninstallModule(webModel.ModuleDescriptor[] modules)
        {
            EnsureModulesCatalogInitialized();

            var options = new webModel.ModuleBackgroundJobOptions
            {
                Action  = webModel.ModuleAction.Uninstall,
                Modules = modules
            };
            var result = ScheduleJob(options);

            return(Ok(result));
        }
        public void ModuleBackgroundJob(webModel.ModuleBackgroundJobOptions options, webModel.ModulePushNotification notification)
        {
            try
            {
                notification.Started = DateTime.UtcNow;
                var moduleInfos = _moduleCatalog.Modules.OfType <ManifestModuleInfo>()
                                  .Where(x => options.Modules.Any(y => y.Identity.Equals(x.Identity)))
                                  .ToArray();
                var reportProgress = new Progress <ProgressMessage>(m =>
                {
                    lock (_lockObject)
                    {
                        notification.ProgressLog.Add(m.ToWebModel());
                        _pushNotifier.Upsert(notification);
                    }
                });

                switch (options.Action)
                {
                case webModel.ModuleAction.Install:
                    _moduleInstaller.Install(moduleInfos, reportProgress);
                    break;

                case webModel.ModuleAction.Uninstall:
                    _moduleInstaller.Uninstall(moduleInfos, reportProgress);
                    break;
                }
            }
            catch (Exception ex)
            {
                notification.ProgressLog.Add(new webModel.ProgressMessage
                {
                    Level   = ProgressMessageLevel.Error.ToString(),
                    Message = ex.ExpandExceptionMessage(),
                });
            }
            finally
            {
                _settingsManager.SetValue(_autoInstallStateSetting, webModel.AutoInstallState.Completed);

                notification.Finished = DateTime.UtcNow;
                notification.ProgressLog.Add(new webModel.ProgressMessage
                {
                    Level   = ProgressMessageLevel.Info.ToString(),
                    Message = "Installation finished.",
                });
                _pushNotifier.Upsert(notification);
            }
        }
        public IHttpActionResult TryToAutoInstallModules()
        {
            var notification = new webModel.ModuleAutoInstallPushNotification(User.Identity.Name)
            {
                Title = "Modules installation",
                //set completed by default
                Finished = DateTime.UtcNow
            };


            if (!_settingsManager.GetValue("VirtoCommerce.ModulesAutoInstalled", false))
            {
                lock (_lockObject)
                {
                    if (!_settingsManager.GetValue("VirtoCommerce.ModulesAutoInstalled", false))
                    {
                        var moduleBundles = ConfigurationHelper.SplitAppSettingsStringValue("VirtoCommerce:AutoInstallModuleBundles");
                        if (!moduleBundles.IsNullOrEmpty())
                        {
                            _settingsManager.SetValue("VirtoCommerce.ModulesAutoInstalled", true);
                            _settingsManager.SetValue(_autoInstallStateSetting, webModel.AutoInstallState.Processing);

                            EnsureModulesCatalogInitialized();

                            var modules             = new List <ManifestModuleInfo>();
                            var moduleVersionGroups = _moduleCatalog.Modules
                                                      .OfType <ManifestModuleInfo>()
                                                      .Where(x => x.Groups.Intersect(moduleBundles, StringComparer.OrdinalIgnoreCase).Any())
                                                      .GroupBy(x => x.Id);

                            //Need install only latest versions
                            foreach (var moduleVersionGroup in moduleVersionGroups)
                            {
                                var alreadyInstalledModule = _moduleCatalog.Modules.OfType <ManifestModuleInfo>().FirstOrDefault(x => x.IsInstalled && x.Id.EqualsInvariant(moduleVersionGroup.Key));
                                //skip already installed modules
                                if (alreadyInstalledModule == null)
                                {
                                    var latestVersion = moduleVersionGroup.OrderBy(x => x.Version).LastOrDefault();
                                    if (latestVersion != null)
                                    {
                                        modules.Add(latestVersion);
                                    }
                                }
                            }

                            var modulesWithDependencies = _moduleCatalog.CompleteListWithDependencies(modules)
                                                          .OfType <ManifestModuleInfo>()
                                                          .Where(x => !x.IsInstalled)
                                                          .Select(x => x.ToWebModel())
                                                          .ToArray();

                            if (modulesWithDependencies.Any())
                            {
                                var options = new webModel.ModuleBackgroundJobOptions
                                {
                                    Action  = webModel.ModuleAction.Install,
                                    Modules = modulesWithDependencies
                                };
                                //reset finished date
                                notification.Finished = null;
                                BackgroundJob.Enqueue(() => ModuleBackgroundJob(options, notification));
                            }
                        }
                    }
                }
            }
            return(Ok(notification));
        }
        public IHttpActionResult TryToAutoInstallModules()
        {
            var notification = new webModel.ModuleAutoInstallPushNotification("System")
            {
                Title = "Modules installation",
                //set completed by default
                Finished = DateTime.UtcNow
            };

            EnsureModulesCatalogInitialized();

            if (!_settingsManager.GetValue("VirtoCommerce.ModulesAutoInstalled", false))
            {
                lock (_lockObject)
                {
                    if (!_settingsManager.GetValue("VirtoCommerce.ModulesAutoInstalled", false))
                    {
                        var moduleBundles = ConfigurationManager.AppSettings.GetValues("VirtoCommerce:AutoInstallModuleBundles");
                        if (!moduleBundles.IsNullOrEmpty())
                        {
                            _settingsManager.SetValue("VirtoCommerce.ModulesAutoInstalled", true);
                            var modules = new List<ManifestModuleInfo>();
                            var moduleVersionGroups = _moduleCatalog.Modules
                                .OfType<ManifestModuleInfo>()
                                .Where(x => x.Groups.Intersect(moduleBundles, StringComparer.OrdinalIgnoreCase).Any())
                                .GroupBy(x => x.Id);

                            //Need install only latest versions
                            foreach (var moduleVersionGroup in moduleVersionGroups)
                            {
                                var alreadyInstalledModule = _moduleCatalog.Modules.OfType<ManifestModuleInfo>().FirstOrDefault(x => x.IsInstalled && x.Id.EqualsInvariant(moduleVersionGroup.Key));
                                //skip already installed modules
                                if (alreadyInstalledModule == null)
                                {
                                    var latestVersion = moduleVersionGroup.OrderBy(x => x.Version).LastOrDefault();
                                    if (latestVersion != null)
                                    {
                                        modules.Add(latestVersion);
                                    }
                                }
                            }

                            var modulesWithDependencies = _moduleCatalog.CompleteListWithDependencies(modules)
                                .OfType<ManifestModuleInfo>()
                                .Where(x => !x.IsInstalled)
                                .Select(x => x.ToWebModel())
                                .ToArray();

                            if (modulesWithDependencies.Any())
                            {
                                var options = new webModel.ModuleBackgroundJobOptions
                                {
                                    Action = webModel.ModuleAction.Install,
                                    Modules = modulesWithDependencies
                                };
                                //reset finished date
                                notification.Finished = null;
                                BackgroundJob.Enqueue(() => ModuleBackgroundJob(options, notification));
                            }
                        }
                    }
                }
            }
            return Ok(notification);
        }
        public IHttpActionResult UninstallModule(webModel.ModuleDescriptor[] modules)
        {
            EnsureModulesCatalogInitialized();

            var options = new webModel.ModuleBackgroundJobOptions
            {
                Action = webModel.ModuleAction.Uninstall,
                Modules = modules
            };
            var result = ScheduleJob(options);
            return Ok(result);
        }