コード例 #1
0
 private async Task <IActionResult> GoBackEditServer(PavlovServerViewModel server, string error,
                                                     bool remove = false)
 {
     if (!await RightsHandler.HasRightsToThisPavlovServer(HttpContext.User,
                                                          await _userservice.getUserFromCp(HttpContext.User), server.Id, _service, _pavlovServerService))
     {
         return(Forbid());
     }
     if (remove)
     {
         var isDevelopment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") == "Development";
         if (!isDevelopment)
         {
             await _service.RemovePavlovServerFromDisk(server, true);
         }
     }
     ModelState.AddModelError("Id", error);
     return(await EditServer(server));
 }
コード例 #2
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));
        }