Exemple #1
0
        /// <summary>
        /// This method tries to update an entity in the database through setting EntityFramework Core's Entry property to EntityState.Modified. If the update fails an exception is thrown. If the update succeeds then the plan parameter object passed in is saved to the database.
        /// </summary>
        /// <param name="plan"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task <bool> ModifyStateAsync(Domain.Models.Plan plan, int id)
        {
            var mappedPlan = Mapper.MapPlan(plan);

            /*_context.Entry(plan).State = EntityState.Modified;*/
            _context.Entry(mappedPlan).State = EntityState.Modified;

            try
            {
                await SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!PlanExists(id))
                {
                    return(false);
                    // plan not found
                }
                else
                {
                    throw;
                }
            }
            return(true);
            // it worked, so return true
        }
Exemple #2
0
 public static Models.Plan MapPlan(Domain.Models.Plan plan)
 {
     return(new Models.Plan
     {
         PlanId = plan.PlanId,
         Description = plan.Description,
         EstCost = plan.EstCost,
         PositivesMax = plan.PositivesMax,
         PlanProLabels = plan.PlanProLabels.Select(MapPlanProLabels).ToList()
     });
 }
        private async Task <Domain.Models.Plan> CreatePlan(Domain.Models.Run run)
        {
            var plan = new Domain.Models.Plan
            {
                RunId  = run.Id,
                Status = Domain.Models.PlanStatus.Planning
            };

            run.Plan   = plan;
            run.Status = Domain.Models.RunStatus.Planning;

            await _db.AddAsync(plan);

            await _db.SaveChangesAsync();

            _output = _outputService.GetOrAddOutput(plan.Id);
            await _mediator.Publish(new RunUpdated(run.Id));

            return(plan);
        }
        private async Task <bool> DoWork(Domain.Models.Run run)
        {
            var projectId   = run.Workspace.Directory.ProjectId;
            var dynamicHost = run.Workspace.DynamicHost;

            Host host = null;

            _plan = await CreatePlan(run);

            var workspace = run.Workspace;
            var files     = await _db.GetWorkspaceFiles(workspace, workspace.Directory);

            var workingDir = workspace.GetPath(_options.RootWorkingDirectory);

            if (dynamicHost)
            {
                // check if host already selected for this workspace
                if (workspace.HostId.HasValue)
                {
                    host = workspace.Host;
                }
                else
                {
                    // select a host. multiply by 1.0 to cast as double
                    host = await _db.Hosts
                           .Where(h => h.ProjectId == projectId && h.Enabled && !h.Development)
                           .OrderBy(h => (h.Machines.Count * 1.0 / h.MaximumMachines * 1.0) * 100.0).FirstOrDefaultAsync();
                }

                if (host == null)
                {
                    _output.AddLine("No Host could be found to use for this Plan");
                    return(true);
                }
                else
                {
                    _output.AddLine($"Attempting to use Host {host.Name} for this Plan");
                }

                files.Add(host.GetHostFile());
            }

            await workspace.PrepareFileSystem(workingDir, files);

            _timer          = new System.Timers.Timer(_options.OutputSaveInterval);
            _timer.Elapsed += OnTimedEvent;
            _timer.Start();

            bool isError = false;

            // Init
            var initResult = _terraformService.InitializeWorkspace(workspace, OutputHandler);

            isError = initResult.IsError;

            if (!isError)
            {
                // Plan
                var planResult = _terraformService.Plan(workspace, run.IsDestroy, run.Targets, OutputHandler);
                isError = planResult.IsError;
            }

            lock (_plan)
            {
                _timerComplete = true;
                _timer.Stop();
            }

            if (dynamicHost)
            {
                var result = _terraformService.Show(workspace);
                isError = result.IsError;

                if (!isError)
                {
                    var planOutput    = JsonSerializer.Deserialize <PlanOutput>(result.Output, DefaultJsonSettings.Settings);
                    var addedMachines = planOutput.GetAddedMachines();

                    var hostLock = _lockService.GetHostLock(host.Id);

                    lock (hostLock)
                    {
                        var existingMachines = _db.HostMachines.Where(x => x.HostId == host.Id).ToList();
                        var addedCount       = addedMachines.Count();

                        if (addedCount + existingMachines.Count < host.MaximumMachines)
                        {
                            List <HostMachine> newMachines = new List <HostMachine>();

                            foreach (var addedMachine in addedMachines)
                            {
                                var name = addedMachine.Address;

                                if (existingMachines.Any(m => m.WorkspaceId == workspace.Id && m.Name == name))
                                {
                                    continue;
                                }

                                newMachines.Add(new HostMachine
                                {
                                    HostId      = host.Id,
                                    WorkspaceId = workspace.Id,
                                    Name        = name
                                });
                            }

                            _db.HostMachines.AddRange(newMachines);
                            workspace.HostId = host.Id;
                            _db.SaveChanges();

                            _output.AddLine($"Allocated {addedCount} new machines to Host {host.Name}");
                        }
                        else
                        {
                            _output.AddLine($"{addedCount} new machines exceeds the maximum assigned to Host {host.Name}");
                            isError = true;
                        }
                    }
                }
            }

            return(isError);
        }
Exemple #5
0
        /// <summary>
        /// Wraps a call to EntityFramework Core Add. The call is made with a mapped entity (DataAccess.Models.Plan) instead of the domain model passed as a parameter. The DataAccess.Model is used to communicate with EF Core.
        ///
        /// EF Core Add:
        /// Finds an entity with the given primary key values. If an entity with the given primary key values is being tracked by the context, then it is returned immediately without making a request to the database. Otherwise, a query is made to the database for an entity with the given primary key values and this entity, if found, is attached to the context and returned. If no entity is found, then null is returned.
        /// </summary>
        /// <param name="entity"></param>
        /// <returns>Domain.Models.Plan</returns>
        public void Add(Domain.Models.Plan entity)
        {
            var mappedEntity = Mapper.MapPlan(entity);

            _context.Set <Plan>().Add(mappedEntity);
        }