示例#1
0
        async Task TestInstallStable(CancellationToken cancellationToken)
        {
            var newModel = new ByondVersionRequest
            {
                Version = TestVersion
            };
            var test = await byondClient.SetActiveVersion(newModel, null, cancellationToken).ConfigureAwait(false);

            Assert.IsNotNull(test.InstallJob);
            await WaitForJob(test.InstallJob, 120, 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))}");
        }
示例#2
0
        async Task TestInstallFakeVersion(CancellationToken cancellationToken)
        {
            var newModel = new ByondVersionRequest
            {
                Version = new Version(5011, 1385)
            };
            var test = await byondClient.SetActiveVersion(newModel, null, cancellationToken).ConfigureAwait(false);

            Assert.IsNotNull(test.InstallJob);
            await WaitForJob(test.InstallJob, 60, true, ErrorCode.ByondDownloadFail, cancellationToken).ConfigureAwait(false);
        }
#pragma warning disable CA1506 // TODO: Decomplexify
        public async Task <IActionResult> Update([FromBody] ByondVersionRequest 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 ErrorMessageResponse(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 ByondInstallResponse();

            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 ErrorMessageResponse(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 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;
                    }
                }

                return result.InstallJob != null ? (IActionResult)Accepted(result) : Json(result);
            })
                   .ConfigureAwait(false));
        }