Example #1
0
        async Task TestInstallStable(CancellationToken cancellationToken)
        {
            var newModel = new Api.Models.Byond
            {
                Version = TestVersion
            };
            var test = await byondClient.SetActiveVersion(newModel, cancellationToken).ConfigureAwait(false);

            Assert.IsNotNull(test.InstallJob);
            Assert.IsNull(test.Version);
            await WaitForJob(test.InstallJob, 60, false, null, cancellationToken).ConfigureAwait(false);

            var currentShit = await byondClient.ActiveVersion(cancellationToken).ConfigureAwait(false);

            Assert.AreEqual(newModel.Version.Semver(), currentShit.Version);

            var dreamMaker = "DreamMaker";

            if (new PlatformIdentifier().IsWindows)
            {
                dreamMaker += ".exe";
            }

            var dreamMakerDir = Path.Combine(metadata.Path, "Byond", newModel.Version.ToString(), "byond", "bin");

            Assert.IsTrue(Directory.Exists(dreamMakerDir), $"Directory {dreamMakerDir} does not exist!");
            Assert.IsTrue(File.Exists(Path.Combine(dreamMakerDir, dreamMaker)), $"Missing DreamMaker executable! Dir contents: {String.Join(", ", Directory.GetFileSystemEntries(dreamMakerDir))}");
        }
Example #2
0
        async Task TestInstallFakeVersion(CancellationToken cancellationToken)
        {
            var newModel = new Api.Models.Byond
            {
                Version = new Version(5011, 1385)
            };
            var test = await byondClient.SetActiveVersion(newModel, cancellationToken).ConfigureAwait(false);

            Assert.IsNotNull(test.InstallJob);
            await WaitForJob(test.InstallJob, 60, true, ErrorCode.ByondDownloadFail, cancellationToken).ConfigureAwait(false);
        }
        public async Task <IActionResult> Update([FromBody] Api.Models.Byond model, CancellationToken cancellationToken)
        {
            if (model == null)
            {
                throw new ArgumentNullException(nameof(model));
            }

            if (model.Version == null)
            {
                return(BadRequest(new ErrorMessage {
                    Message = "Missing version!"
                }));
            }

            var byondManager = instanceManager.GetInstance(Instance).ByondManager;

            // remove cruff fields
            var installingVersion = new Version(model.Version.Major, model.Version.Minor);

            var result = new Api.Models.Byond();

            if (byondManager.InstalledVersions.Any(x => x == model.Version))
            {
                Logger.LogInformation("User ID {0} changing instance ID {1} BYOND version to {2}", AuthenticationContext.User.Id, Instance.Id, installingVersion);
                await byondManager.ChangeVersion(model.Version, cancellationToken).ConfigureAwait(false);
            }
            else
            {
                Logger.LogInformation("User ID {0} installing BYOND version to {2} on instance ID {1}", AuthenticationContext.User.Id, Instance.Id, installingVersion);

                // run the install through the job manager
                var job = new Models.Job
                {
                    Description      = $"Install BYOND version {installingVersion}",
                    StartedBy        = AuthenticationContext.User,
                    CancelRightsType = RightsType.Byond,
                    CancelRight      = (ulong)ByondRights.CancelInstall,
                    Instance         = Instance
                };
                await jobManager.RegisterOperation(job, (paramJob, databaseContext, progressHandler, ct) => byondManager.ChangeVersion(installingVersion, ct), cancellationToken).ConfigureAwait(false);

                result.InstallJob = job.ToApi();
            }

            if ((AuthenticationContext.GetRight(RightsType.Byond) & (ulong)ByondRights.ReadActive) != 0)
            {
                result.Version = byondManager.ActiveVersion;
            }
            return(result.InstallJob != null ? (IActionResult)Accepted(result) : Json(result));
        }
        async Task TestInstall511(CancellationToken cancellationToken)
        {
            var newModel = new Api.Models.Byond
            {
                Version = new Version(511, 1385)
            };
            var test = await byondClient.SetActiveVersion(newModel, cancellationToken).ConfigureAwait(false);

            Assert.IsNotNull(test.InstallJob);
            Assert.IsNull(test.Version);
            var job     = test.InstallJob;
            var maxWait = 60;               //it's 10MB max give me a break

            do
            {
                await Task.Delay(TimeSpan.FromSeconds(1), cancellationToken).ConfigureAwait(false);

                job = await jobsClient.GetId(job, cancellationToken).ConfigureAwait(false);

                --maxWait;
            }while (!job.StoppedAt.HasValue && maxWait > 0);
            if (!job.StoppedAt.HasValue)
            {
                await jobsClient.Cancel(job, cancellationToken).ConfigureAwait(false);

                Assert.Fail("Byond installation job timed out!");
            }

            if (job.ExceptionDetails != null)
            {
                Assert.Fail(job.ExceptionDetails);
            }

            var currentShit = await byondClient.ActiveVersion(cancellationToken).ConfigureAwait(false);

            Assert.AreEqual(newModel.Version, currentShit.Version);
        }
#pragma warning disable CA1506 // TODO: Decomplexify
        public async Task <IActionResult> Update([FromBody] Api.Models.Byond model, CancellationToken cancellationToken)
#pragma warning restore CA1506
        {
            if (model == null)
            {
                throw new ArgumentNullException(nameof(model));
            }

            var uploadingZip = model.UploadCustomZip == true;

            if (model.Version == null ||
                model.Version.Revision != -1 ||
                (uploadingZip && model.Version.Build > 0))
            {
                return(BadRequest(new ErrorMessage(ErrorCode.ModelValidationFailure)));
            }

            var userByondRights = AuthenticationContext.InstancePermissionSet.ByondRights.Value;

            if ((!userByondRights.HasFlag(ByondRights.InstallOfficialOrChangeActiveVersion) && !uploadingZip) ||
                (!userByondRights.HasFlag(ByondRights.InstallCustomVersion) && uploadingZip))
            {
                return(Forbid());
            }

            // remove cruff fields
            var result = new Api.Models.Byond();

            return(await WithComponentInstance(
                       async instance =>
            {
                var byondManager = instance.ByondManager;
                if (!uploadingZip && byondManager.InstalledVersions.Any(x => x == model.Version))
                {
                    Logger.LogInformation(
                        "User ID {0} changing instance ID {1} BYOND version to {2}",
                        AuthenticationContext.User.Id,
                        Instance.Id,
                        model.Version);
                    await byondManager.ChangeVersion(model.Version, null, cancellationToken).ConfigureAwait(false);
                }
                else if (model.Version.Build > 0)
                {
                    return BadRequest(new ErrorMessage(ErrorCode.ByondNonExistentCustomVersion));
                }
                else
                {
                    var installingVersion = model.Version.Build <= 0
                                                        ? new Version(model.Version.Major, model.Version.Minor)
                                                        : model.Version;

                    Logger.LogInformation(
                        "User ID {0} installing BYOND version to {1} on instance ID {2}",
                        AuthenticationContext.User.Id,
                        installingVersion,
                        Instance.Id);

                    // run the install through the job manager
                    var job = new Models.Job
                    {
                        Description = $"Install {(!uploadingZip ? String.Empty : "custom ")}BYOND version {model.Version.Major}.{model.Version.Minor}",
                        StartedBy = AuthenticationContext.User,
                        CancelRightsType = RightsType.Byond,
                        CancelRight = (ulong)ByondRights.CancelInstall,
                        Instance = Instance
                    };

                    IFileUploadTicket fileUploadTicket = null;
                    if (uploadingZip)
                    {
                        fileUploadTicket = fileTransferService.CreateUpload(false);
                    }

                    try
                    {
                        await jobManager.RegisterOperation(
                            job,
                            async(core, databaseContextFactory, paramJob, progressHandler, jobCancellationToken) =>
                        {
                            Stream zipFileStream = null;
                            if (fileUploadTicket != null)
                            {
                                using (fileUploadTicket)
                                {
                                    var uploadStream = await fileUploadTicket.GetResult(jobCancellationToken).ConfigureAwait(false);
                                    if (uploadStream == null)
                                    {
                                        throw new JobException(ErrorCode.FileUploadExpired);
                                    }

                                    zipFileStream = new MemoryStream();
                                    try
                                    {
                                        await uploadStream.CopyToAsync(zipFileStream, jobCancellationToken).ConfigureAwait(false);
                                    }
                                    catch
                                    {
                                        await zipFileStream.DisposeAsync().ConfigureAwait(false);
                                        throw;
                                    }
                                }
                            }

                            using (zipFileStream)
                                await core.ByondManager.ChangeVersion(
                                    model.Version,
                                    zipFileStream,
                                    jobCancellationToken)
                                .ConfigureAwait(false);
                        },
                            cancellationToken)
                        .ConfigureAwait(false);

                        result.InstallJob = job.ToApi();
                        result.FileTicket = fileUploadTicket?.Ticket.FileTicket;
                    }
                    catch
                    {
                        fileUploadTicket?.Dispose();
                        throw;
                    }
                }

                if ((AuthenticationContext.GetRight(RightsType.Byond) & (ulong)ByondRights.ReadActive) != 0)
                {
                    result.Version = byondManager.ActiveVersion;
                }
                return result.InstallJob != null ? (IActionResult)Accepted(result) : Json(result);
            })
                   .ConfigureAwait(false));
        }
#pragma warning disable CA1506 // TODO: Decomplexify
        public async Task <IActionResult> Update([FromBody] Api.Models.Byond model, CancellationToken cancellationToken)
#pragma warning restore CA1506
        {
            if (model == null)
            {
                throw new ArgumentNullException(nameof(model));
            }

            if (model.Version == null ||
                model.Version.Revision != -1 ||
                (model.Content != null && model.Version.Build > 0))
            {
                return(BadRequest(new ErrorMessage(ErrorCode.ModelValidationFailure)));
            }

            var userByondRights = AuthenticationContext.InstanceUser.ByondRights.Value;

            if ((!userByondRights.HasFlag(ByondRights.InstallOfficialOrChangeActiveVersion) && model.Content == null) ||
                (!userByondRights.HasFlag(ByondRights.InstallCustomVersion) && model.Content != null))
            {
                return(Forbid());
            }

            // remove cruff fields
            var result = new Api.Models.Byond();

            return(await WithComponentInstance(
                       async instance =>
            {
                var byondManager = instance.ByondManager;
                if (model.Content == null && byondManager.InstalledVersions.Any(x => x == model.Version))
                {
                    Logger.LogInformation(
                        "User ID {0} changing instance ID {1} BYOND version to {2}",
                        AuthenticationContext.User.Id,
                        Instance.Id,
                        model.Version);
                    await byondManager.ChangeVersion(model.Version, null, cancellationToken).ConfigureAwait(false);
                }
                else if (model.Version.Build > 0)
                {
                    return BadRequest(new ErrorMessage(ErrorCode.ByondNonExistentCustomVersion));
                }
                else
                {
                    var installingVersion = model.Version.Build <= 0
                                                        ? new Version(model.Version.Major, model.Version.Minor)
                                                        : model.Version;

                    Logger.LogInformation(
                        "User ID {0} installing BYOND version to {1} on instance ID {2}",
                        AuthenticationContext.User.Id,
                        installingVersion,
                        Instance.Id);

                    // run the install through the job manager
                    var job = new Models.Job
                    {
                        Description = $"Install {(model.Content == null ? String.Empty : "custom ")}BYOND version {model.Version.Major}.{model.Version.Minor}",
                        StartedBy = AuthenticationContext.User,
                        CancelRightsType = RightsType.Byond,
                        CancelRight = (ulong)ByondRights.CancelInstall,
                        Instance = Instance
                    };
                    await jobManager.RegisterOperation(
                        job,
                        (core, databaseContextFactory, paramJob, progressHandler, jobCancellationToken) => core.ByondManager.ChangeVersion(
                            model.Version,
                            model.Content,
                            jobCancellationToken),
                        cancellationToken)
                    .ConfigureAwait(false);
                    result.InstallJob = job.ToApi();
                }

                if ((AuthenticationContext.GetRight(RightsType.Byond) & (ulong)ByondRights.ReadActive) != 0)
                {
                    result.Version = byondManager.ActiveVersion;
                }
                return result.InstallJob != null ? (IActionResult)Accepted(result) : Json(result);
            })
                   .ConfigureAwait(false));
        }