public void InsertPavlovServer()
        {
            // arrange
            var sshServer = SshServerServiceTests.SshServerInsert(_sshServerSerivce);

            // act
            _pavlovServerService.Upsert(PavlovServer(sshServer)).GetAwaiter().GetResult();
            // assert
            var pavlovServers = _pavlovServerService.FindAll().GetAwaiter().GetResult();

            pavlovServers.Should().HaveCount(1);
        }
        public static PavlovServer[] PavlovServers(SshServer sshServer, PavlovServerService pavlovServerService)
        {
            pavlovServerService.Upsert(PavlovServer(sshServer), false);
            var pavlovServers = pavlovServerService.FindAll().GetAwaiter().GetResult();

            return(pavlovServers);
        }
        public void DefaultDB(bool teamMatch)
        {
            // arrange
            var pavlovServers =
                PavlovServerServiceTests.InitializePavlovServer(_sshServerSerivce, _pavlovServerService);

            pavlovServers.First().ServerType = ServerType.Event;
            _pavlovServerService.Upsert(pavlovServers.First());
            var tmpMatch = CreateMatch(_matchService, pavlovServers.First(), teamMatch, _teamService,
                                       _steamIdentityService, _userManager, _matchSelectedSteamIdentitiesService,
                                       _matchSelectedTeamSteamIdentitiesService);

            // act
            var result  = _matchService.FindOne(tmpMatch.Id).GetAwaiter().GetResult();
            var result2 = _matchService.FindAll().GetAwaiter().GetResult();

            result.Status = Status.Finshed;
            _matchService.Upsert(result).GetAwaiter().GetResult();

            if (teamMatch)
            {
                var matchTeamSelectedSteamIdentities =
                    _matchSelectedTeamSteamIdentitiesService.FindAll().GetAwaiter().GetResult();
                //cause i added 2
                matchTeamSelectedSteamIdentities.Should().HaveCount(2);

                var selected = _matchSelectedTeamSteamIdentitiesService.FindAllSelectedForMatchAndTeam(tmpMatch.Id,
                                                                                                       matchTeamSelectedSteamIdentities.First().TeamId).GetAwaiter().GetResult();
                //cause i selected one per team
                selected.Should().HaveCount(1);
                _matchSelectedTeamSteamIdentitiesService.RemoveFromMatch(matchTeamSelectedSteamIdentities.First().Id)
                .GetAwaiter().GetResult();

                matchTeamSelectedSteamIdentities =
                    _matchSelectedTeamSteamIdentitiesService.FindAll().GetAwaiter().GetResult();
                matchTeamSelectedSteamIdentities.Should().BeNullOrEmpty();
            }
            else
            {
                var matchSelectedSteamIdentities =
                    _matchSelectedSteamIdentitiesService.FindAll().GetAwaiter().GetResult();
                matchSelectedSteamIdentities.Should().HaveCount(2);


                matchSelectedSteamIdentities = _matchSelectedSteamIdentitiesService.FindAllSelectedForMatch(tmpMatch.Id)
                                               .GetAwaiter().GetResult();

                matchSelectedSteamIdentities.Should().HaveCount(2);


                _matchSelectedSteamIdentitiesService.RemoveFromMatch(tmpMatch.Id).GetAwaiter().GetResult();

                matchSelectedSteamIdentities = _matchSelectedSteamIdentitiesService.FindAll().GetAwaiter().GetResult();
                //cause i removed 1
                matchSelectedSteamIdentities.Should().BeNullOrEmpty();
            }


            var result3 = _matchService.CanBeDeleted(tmpMatch.Id).GetAwaiter().GetResult();
            var result4 = _matchService.Delete(tmpMatch.Id).GetAwaiter().GetResult();

            var result5 = _matchService.FindOne(tmpMatch.Id).GetAwaiter().GetResult();

            // assert
            result.Should().NotBeNull();
            result2.Should().HaveCount(1);
            result3.Should().BeTrue();
            result4.Should().BeTrue();
            result5.Should().BeNull();
        }
        public async Task <IActionResult> SaveServer(PavlovServerViewModel server)
        {
            if (!ModelState.IsValid)
            {
                return(View("Server", server));
            }

            if (!await RightsHandler.HasRightsToThisPavlovServer(HttpContext.User,
                                                                 await _userservice.getUserFromCp(HttpContext.User), server.Id, _service, _pavlovServerService))
            {
                return(Forbid());
            }

            server.LiteDbUsers = (await _userservice.FindAll()).ToList();

            server.Owner = (await _userservice.FindAll())
                           .FirstOrDefault(x => x.Id == new ObjectId(server.LiteDbUserId));
            var resultServer = new PavlovServer();

            server.SshServer = await _service.FindOne(server.sshServerId);

            if (server.create)
            {
                // Duplicate Database entries check
                try
                {
                    await _pavlovServerService.IsValidOnly(server, false);

                    if (string.IsNullOrEmpty(server.SshServer.SshPassword))
                    {
                        throw new ValidateException("Id",
                                                    "Please add a sshPassword to the ssh user (Not root user). Sometimes the systems asks for the password even if the keyfile and passphrase is used.");
                    }
                    if (string.IsNullOrEmpty(server.ServerFolderPath))
                    {
                        throw new ValidateException("ServerFolderPath",
                                                    "The server ServerFolderPath is needed!");
                    }
                    if (!server.ServerFolderPath.EndsWith("/"))
                    {
                        throw new ValidateException("ServerFolderPath",
                                                    "The server ServerFolderPath needs a / at the end!");
                    }
                    if (!server.ServerFolderPath.StartsWith("/"))
                    {
                        throw new ValidateException("ServerFolderPath",
                                                    "The server ServerFolderPath needs a / at the start!");
                    }
                    if (server.ServerPort <= 0)
                    {
                        throw new ValidateException("ServerPort", "The server port is needed!");
                    }
                    if (server.TelnetPort <= 0)
                    {
                        throw new ValidateException("TelnetPort", "The rcon port is needed!");
                    }
                    if (string.IsNullOrEmpty(server.ServerSystemdServiceName))
                    {
                        throw new ValidateException("ServerSystemdServiceName",
                                                    "The server service name is needed!");
                    }
                    if (string.IsNullOrEmpty(server.Name))
                    {
                        throw new ValidateException("Name", "The Gui name is needed!");
                    }
                }
                catch (ValidateException e)
                {
                    ModelState.AddModelError(e.FieldName, e.Message);
                    return(await GoBackEditServer(server,
                                                  "Field is not set: " + e.Message));
                }

                if (server.SshKeyFileNameForm != null)
                {
                    await using var ms = new MemoryStream();
                    await server.SshKeyFileNameForm.CopyToAsync(ms);

                    var fileBytes = ms.ToArray();
                    server.SshKeyFileNameRoot = fileBytes;
                    // act on the Base64 data
                }

                try
                {
                    await _pavlovServerService.CreatePavlovServer(server);
                }
                catch (Exception e)
                {
                    return(await GoBackEditServer(server, e.Message, true));
                }
            }

            try
            {
                //save and validate server a last time
                resultServer = await _pavlovServerService.Upsert(server.toPavlovServer(server));
            }
            catch (ValidateException e)
            {
                ModelState.AddModelError(e.FieldName, e.Message);
                return(await GoBackEditServer(server,
                                              "Could not validate server -> " + e.Message, server.create));
            }

            if (ModelState.ErrorCount > 0)
            {
                return(await EditServer(server));
            }
            if (server.create)
            {
                return(Redirect("/PavlovServer/EditServerSelectedMaps/" + resultServer.Id));
            }

            return(RedirectToAction("Index", "SshServer"));
        }
Ejemplo n.º 5
0
        public async Task <IActionResult> CreateServer(string apiKey, int sshServerId, bool shack, string email)
        {
            if (!HasAccess(apiKey))
            {
                return(BadRequest("No AuthKey set or wrong auth key!"));
            }
            var sshServer = await _sshServerSerivce.FindOne(sshServerId);

            if (sshServer == null)
            {
                return(BadRequest("The ssh server does not exist!"));
            }
            if (!sshServer.IsForHosting)
            {
                return(BadRequest("The ssh server ist not for hosting!"));
            }

            var guid  = Guid.NewGuid().ToString();
            var model = new PavlovServerViewModel
            {
                Name                     = "Autogenerated: " + guid,
                TelnetPort               = sshServer.PavlovServers.Max(x => x.TelnetPort) + 1,
                DeletAfter               = 7,
                TelnetPassword           = Guid.NewGuid().ToString(),
                ServerPort               = sshServer.PavlovServers.Max(x => x.ServerPort) + 1,
                ServerFolderPath         = GeneratedServerPath + guid + "/",
                ServerSystemdServiceName = "pavlov" + guid.Replace("-", ""),
                ServerType               = ServerType.Community,
                ServerServiceState       = ServerServiceState.disabled,
                SshServer                = sshServer,
                AutoBalance              = false,
                SaveStats                = true,
                Shack                    = shack,
                sshServerId              = sshServer.Id,
                create                   = true,
                SshUsernameRoot          = sshServer.SshUsernameRootForHosting,
                SshPasswordRoot          = sshServer.SshPasswordRootForHosting,
                SshKeyFileNameRoot       = sshServer.SshKeyFileNameRootForHosting,
                SshPassphraseRoot        = sshServer.SshPassphraseRootForHosting
            };

            var user = await _userService.GetUserByEmail(email);

            if (user != null)
            {
                model.LiteDbUserId = user.Id.ToString();
                model.Owner        = user;
                if (!await _userManager.IsInRoleAsync(user, "ServerRent"))
                {
                    await _userManager.AddToRoleAsync(user, "ServerRent");
                }
            }

            try
            {
                await _pavlovServerService.CreatePavlovServer(model);
            }
            catch (Exception e)
            {
                await _sshServerSerivce.RemovePavlovServerFromDisk(model, true);

                DataBaseLogger.LogToDatabaseAndResultPlusNotify("Could not create rented server! " + model.Name, LogEventLevel.Fatal, _notifyService);
                return(BadRequest(e.Message));
            }


            var resultServer = await _pavlovServerService.Upsert(model.toPavlovServer(model));

            if (user == null)
            {
                await _reservedServersService.Add(new ReservedServer()
                {
                    Email = email, ServerId = resultServer.Id
                });
            }
            return(Ok(resultServer.Id));
        }