Ejemplo n.º 1
0
        public async Task <ModuleEntity> GetModule(IOperation operation, int moduleId)
        {
            if (!await moduleStore.IsModuleExists(operation, moduleId))
            {
                throw CommonExceptions.ModuleWasNotFound(operation, moduleId);
            }

            var module = await moduleStore.GetModule(operation, moduleId);

            var fileIds = module.Configurations.Select(item => item.FileId).ToList();
            var files   = (await fileStore.Download(operation, fileIds)).ToList();

            foreach (var configuration in module.Configurations)
            {
                configuration.File = files.FirstOrDefault(file => file.Id == configuration.FileId);

                var fileConfiguration = ModuleConfigurationUtils.Parse(operation, configuration.File);
                if (!fileConfiguration.IsValid())
                {
                    throw CommonExceptions.ModuleConfigurationIsInvalid(operation);
                }

                configuration.OperatingSystems = fileConfiguration.Instructions.Where(item => item.OperatingSystem.HasValue).Select(item => item.OperatingSystem.Value);
            }

            return(module);
        }
Ejemplo n.º 2
0
        public async Task <ModuleConfigurationFileUploadResult> ReadConfiguration(IOperation operation, UploadedFile file)
        {
            var configuration = ModuleConfigurationUtils.Parse(operation, file);

            if (!configuration.IsValid())
            {
                throw CommonExceptions.ModuleConfigurationIsInvalid(operation);
            }

            var module = await moduleStore.GetModuleByAlias(operation, configuration.Alias);

            var result = new ModuleConfigurationFileUploadResult
            {
                FileId           = await fileStore.Find(operation, file) ?? await fileStore.Upload(operation, file),
                Alias            = configuration.Alias.ToUpper(),
                NewName          = configuration.Name,
                NewVersion       = configuration.Version,
                OperatingSystems = configuration.Instructions.Where(item => item.OperatingSystem.HasValue).Select(item => item.OperatingSystem.Value)
            };

            if (module == null)
            {
                // Module is new for the system
                result.Verdict = ModuleConfigurationVerdict.NewModule;
                return(result);
            }

            // Module had already installed but update may be required
            result.ModuleId            = module.Id;
            result.OldName             = module.Name;
            result.OldVersion          = module.Version;
            result.UserId              = module.UserId;
            result.FirstName           = module.FirstName;
            result.LastName            = module.LastName;
            result.HostId              = module.HostId;
            result.HostName            = module.HostName;
            result.HostDomain          = module.HostDomain;
            result.HostOperatingSystem = module.HostOperatingSystem;

            if (!result.OperatingSystems.Contains(module.HostOperatingSystem))
            {
                // Configuration doesn't have instructions for OS which used on current host
                result.Verdict = ModuleConfigurationVerdict.BrokenChanges;
                return(result);
            }

            if (module.Status == ModuleStatus.Run || module.Status == ModuleStatus.Stopped || module.Status == ModuleStatus.FailedToRun || module.Status == ModuleStatus.FailedToStop)
            {
                // Module in stable mode: run or stopped
                result.Verdict = GetVerdict(operation, module.Version, configuration.Version);
                return(result);
            }

            result.Verdict = ModuleConfigurationVerdict.Updating;
            return(result);
        }
Ejemplo n.º 3
0
        public async Task <int> SubmitConfiguration(IOperation operation, ModuleConfigurationSubmitEntity entity)
        {
            var file = await fileStore.Download(operation, entity.FileId);

            if (file == null)
            {
                throw CommonExceptions.FileWasNotFound(operation, entity.FileId);
            }

            var configuration = ModuleConfigurationUtils.Parse(operation, file);

            if (!configuration.IsValid())
            {
                throw CommonExceptions.ModuleConfigurationIsInvalid(operation);
            }

            var module = await moduleStore.GetModuleByAlias(operation, configuration.Alias);

            if (module == null)
            {
                if (entity.ModuleId != null)
                {
                    throw CommonExceptions.FailedToDefineModuleForSubmission(operation);
                }

                if (!await userDataStore.IsUserExist(operation, entity.UserId))
                {
                    throw CommonExceptions.UserWasNotFound(operation, entity.UserId);
                }

                if (!await portalSettingsStore.IsHostExist(operation, entity.HostId))
                {
                    throw CommonExceptions.HostWasNotFound(operation, entity.HostId);
                }

                // Module is new for the system
                var moduleIdForCreate = await moduleStore.CreateModule(operation, entity.UserId, entity.HostId, entity.FileId, configuration);

                await new ProcessDefinition.StartProcessInstanceByKeyRequest(CamundaWorkerKey.ModuleInstallationProcess)
                {
                    BusinessKey = moduleIdForCreate.ToString(),
                    Variables   = new CamundaVariables
                    {
                        { CamundaWorkerKey.ModuleId, moduleIdForCreate },
                        { CamundaWorkerKey.Action, ActionType.NoChanges }
                    }
                }.SendRequest(operation, camundaClient, true);
                return(moduleIdForCreate);
            }

            var operatingSystems = configuration.Instructions.Where(item => item.OperatingSystem.HasValue).Select(item => item.OperatingSystem.Value);

            if (!operatingSystems.Contains(module.HostOperatingSystem))
            {
                // Configuration doesn't have instructions for OS which used on current host
                throw CommonExceptions.FailedToSubmitModuleConfiguration(operation, ModuleConfigurationVerdict.BrokenChanges);
            }

            if (module.Status == ModuleStatus.Run || module.Status == ModuleStatus.Stopped || module.Status == ModuleStatus.FailedToRun || module.Status == ModuleStatus.FailedToStop)
            {
                if (!await userDataStore.IsUserExist(operation, entity.UserId))
                {
                    throw CommonExceptions.UserWasNotFound(operation, entity.UserId);
                }

                // Module in stable mode: run or stopped
                var moduleIdForUpgrade = ValidateToUpgradeModule(operation, entity.ModuleId, module, configuration);

                var fullModule = await moduleStore.GetModule(operation, module.Id);

                var historicalConfiguration = fullModule.Configurations.FirstOrDefault(config => config.Version == configuration.Version);
                if (historicalConfiguration != null)
                {
                    await moduleStore.UpgradeModule(operation, module.Id, entity.UserId, historicalConfiguration.Id);
                }
                else
                {
                    await moduleStore.UpgradeModule(operation, moduleIdForUpgrade, entity.UserId, entity.FileId, configuration);
                }

                await new ProcessDefinition.StartProcessInstanceByKeyRequest(CamundaWorkerKey.ModuleUpgradeProcess)
                {
                    BusinessKey = moduleIdForUpgrade.ToString(),
                    Variables   = new CamundaVariables
                    {
                        { CamundaWorkerKey.ModuleId, moduleIdForUpgrade },
                        { CamundaWorkerKey.ComponentsStopRequired, module.Status == ModuleStatus.Run },
                        { CamundaWorkerKey.Action, ActionType.Upgrade }
                    }
                }.SendRequest(operation, camundaClient, true);
                return(moduleIdForUpgrade);
            }

            throw CommonExceptions.FailedToSubmitModuleConfiguration(operation, ModuleConfigurationVerdict.Updating);
        }