Esempio n. 1
0
    protected override int Execute()
    {
        ConsoleWriter.Shared.WriteProgressWithoutSave("self-update");
        Helper.SaveLastUpdateTime();

        try
        {
            InstallPowerShell();
            InstallClinkScript();
            InstallBashScript();

            CreateRunners();
            AddInstallToPath();
        }
        catch (Exception exception)
        {
            Log.LogError(exception, "Fail to install cement: '{ErrorMessage}'", exception.Message);
            ConsoleWriter.Shared.WriteError("Fail to install cement: " + exception);
        }

        Log.LogInformation("Cement server: {CementServerUri}", server);

        using ICementUpdater updater = server == null
            ? new GitHubReleaseCementUpdater(Log)
            : new ServerCementUpdater(Log, ConsoleWriter.Shared, server, branch);

        Log.LogInformation("Updater: {CementUpdaterName}", updater.Name);
        return(UpdateBinary(updater));
    }
Esempio n. 2
0
        private int UpdateBinary(ICementUpdater updater)
        {
            var currentCommitHash = Helper.GetCurrentBuildCommitHash();

            ConsoleWriter.WriteProgressWithoutSave("Looking for cement updates");
            var newCommitHash = updater.GetNewCommitHash();

            if (newCommitHash == null)
            {
                return(-1);
            }

            if (IsInstallingCement)
            {
                currentCommitHash = "(NOT INSTALLED)" + currentCommitHash;
            }
            if (!HasAllCementFiles() || !currentCommitHash.Equals(newCommitHash))
            {
                if (!UpdateBinaries(updater, currentCommitHash, newCommitHash))
                {
                    return(-1);
                }
            }
            else
            {
                ConsoleWriter.WriteInfo($"No cement binary updates available ({updater.GetName()})");
                Log.DebugFormat("Already has {0} version", currentCommitHash);
            }
            return(0);
        }
Esempio n. 3
0
    private bool UpdateBinaries(ICementUpdater updater, string oldHash, string newHash)
    {
        ConsoleWriter.Shared.WriteProgressWithoutSave("Updating cement binaries");

        try
        {
            var zipContent = updater.GetNewCementZip();

            if (zipContent is null)
            {
                ConsoleWriter.Shared.WriteWarning("Failed to receive cement binary");
                return(false);
            }

            using (var tempDir = new TempDirectory())
            {
                File.WriteAllBytes(Path.Combine(tempDir.Path, "cement.zip"), zipContent);
                ZipFile.ExtractToDirectory(Path.Combine(tempDir.Path, "cement.zip"), Path.Combine(tempDir.Path, "cement"));
                CopyNewCmExe(tempDir.Path);
            }

            var okMessage = $"Update succeeded: {oldHash} -> {newHash} ({updater.Name})";
            ConsoleWriter.Shared.WriteOk(okMessage);
            Log.LogDebug(okMessage);
            return(true);
        }
        catch (WebException webException)
        {
            Log.LogError(webException, "Fail self-update, exception: '{ErrorMessage}'", webException.Message);

            if (webException.Status == WebExceptionStatus.ProtocolError && webException.Response != null)
            {
                var resp = (HttpWebResponse)webException.Response;
                if (resp.StatusCode == HttpStatusCode.NotFound) // HTTP 404
                {
                    ConsoleWriter.Shared.WriteWarning($"Failed to look for updates on branch {branch}. Server replied 404 ({updater.Name})");
                    return(false);
                }
            }

            ConsoleWriter.Shared.WriteWarning($"Failed to look for updates on branch {branch}. {webException.Message} ({updater.Name})");
            return(false);
        }
    }
Esempio n. 4
0
        private bool UpdateBinaries(ICementUpdater updater, string oldHash, string newHash)
        {
            ConsoleWriter.WriteProgressWithoutSave("Updating cement binaries");

            try
            {
                var zipContent = updater.GetNewCementZip();
                using (var tempDir = new TempDirectory())
                {
                    File.WriteAllBytes(Path.Combine(tempDir.Path, "cement.zip"), zipContent);
                    ZipFile.ExtractToDirectory(Path.Combine(tempDir.Path, "cement.zip"), Path.Combine(tempDir.Path, "cement"));
                    CopyNewCmExe(tempDir.Path);
                }

                var okMessage = $"Successfully updated cement binaries. {oldHash} -> {newHash} ({updater.GetName()})";
                ConsoleWriter.WriteOk(okMessage);
                Log.Debug(okMessage);
                return(true);
            }
            catch (WebException ex)
            {
                Log.Error("Fail self-update", ex);

                if (ex.Status == WebExceptionStatus.ProtocolError && ex.Response != null)
                {
                    var resp = (HttpWebResponse)ex.Response;
                    if (resp.StatusCode == HttpStatusCode.NotFound) // HTTP 404
                    {
                        ConsoleWriter.WriteError($"Failed to look for updates on branch {branch}. Server responsed 404 ({updater.GetName()})");
                        return(false);
                    }
                }

                ConsoleWriter.WriteError($"Failed to look for updates on branch {branch}. {ex.Message} ({updater.GetName()})");
                return(false);
            }
        }