예제 #1
0
        public async Task Run(CommandContext context)
        {
            var server = context.Servers.Get(ServerName);

            ConsoleEx.Out.WriteLine("Retrieving latest agent version...", ConsoleColor.DarkCyan);

            var agentIndex = await DownloadTools.GetLatestAgentIndex();

            var latestVersion = agentIndex.Version;

            ConsoleEx.Out
            .Write("Found Latest Version ", ConsoleColor.DarkCyan)
            .WriteLine(latestVersion, ConsoleColor.Cyan)
            .WriteLine("Checking agent versions...", ConsoleColor.DarkCyan);

            HttpAgentVersionListResponse agentVersionResponse = null;

            await AuthRetryAsync(async() => {
                agentVersionResponse = await WebClient(server, async client => {
                    var json = (await client.DownloadStringTaskAsync("api/agent/versions")).Trim();
                    return(JsonConvert.DeserializeObject <HttpAgentVersionListResponse>(json));
                });
            });

            var updateAgents = new List <string>();

            foreach (var agentVersion in agentVersionResponse.VersionList.OrderBy(x => x.AgentName))
            {
                if (!string.IsNullOrEmpty(agentVersion.Exception))
                {
                    ConsoleEx.Out.Write("Failed to get version of agent ", ConsoleColor.DarkYellow)
                    .Write(agentVersion.AgentName, ConsoleColor.Yellow)
                    .WriteLine($"! {agentVersion.Exception}", ConsoleColor.DarkYellow);

                    continue;
                }

                if (!VersionTools.HasUpdates(agentVersion.AgentVersion, latestVersion))
                {
                    ConsoleEx.Out.Write("Agent ", ConsoleColor.DarkBlue)
                    .Write(agentVersion.AgentName, ConsoleColor.Blue)
                    .WriteLine(" is up-to-date.", ConsoleColor.DarkBlue);

                    continue;
                }

                ConsoleEx.Out.Write("Updating ", ConsoleColor.DarkCyan)
                .Write(agentVersion.AgentName, ConsoleColor.Cyan)
                .Write(" from version ", ConsoleColor.DarkCyan)
                .Write(agentVersion.AgentVersion, ConsoleColor.Cyan)
                .WriteLine(".", ConsoleColor.DarkCyan);

                updateAgents.Add(agentVersion.AgentId);
            }

            if (!updateAgents.Any())
            {
                ConsoleEx.Out.WriteLine("All agents are up-to-date.", ConsoleColor.DarkGreen);
                return;
            }

            // Download update msi

            ConsoleEx.Out.Write("Downloading Agent update ", ConsoleColor.DarkCyan)
            .Write(agentIndex.Version, ConsoleColor.Cyan)
            .WriteLine("...", ConsoleColor.DarkCyan);

            var url = NetPath.Combine(Configuration.DownloadUrl, "agent", agentIndex.Version, agentIndex.MsiFilename);

            var updateDirectory = Path.Combine(Configuration.Directory, "Updates");
            var updateFilename  = Path.Combine(updateDirectory, "Photon.Agent.msi");

            PathEx.CreatePath(updateDirectory);

            using (var client = new WebClient()) {
                await client.DownloadFileTaskAsync(url, updateFilename);
            }

            ConsoleEx.Out.WriteLine("Download Complete.", ConsoleColor.DarkBlue);

            // Perform updates

            var agentIdList = updateAgents.ToArray();

            var startResult = await StartSession(server, agentIdList, updateFilename);

            var sessionId = startResult?.SessionId;

            if (string.IsNullOrEmpty(sessionId))
            {
                throw new ApplicationException($"An invalid session-id was returned! [{sessionId}]");
            }

            var position = 0;

            while (true)
            {
                var data = await UpdateOutput(server, sessionId, position);

                if (data == null)
                {
                    throw new ApplicationException("An empty session-output response was returned!");
                }

                if (data.IsComplete)
                {
                    break;
                }

                if (!data.IsModified)
                {
                    await Task.Delay(PollIntervalMs);

                    continue;
                }

                position = data.NewLength;

                ConsoleEx.Out.WriteLine(data.NewText, ConsoleColor.Gray);
            }

            Result = await GetResult(server, sessionId);

            ConsoleEx.Out.WriteLine("Update completed successfully.", ConsoleColor.DarkGreen);
        }
예제 #2
0
        public override async Task <HttpHandlerResult> GetAsync(CancellationToken token)
        {
            var _names = GetQuery("names");

            try {
                // Get Agent Names
                string[] agentNames = null;

                if (!string.IsNullOrEmpty(_names))
                {
                    agentNames = ParseNames(_names).ToArray();
                }

                var agents = PhotonServer.Instance.Agents.All
                             .Where(x => IncludesAgent(agentNames, x)).ToArray();

                if (!agents.Any())
                {
                    throw new ApplicationException("No agents were found!");
                }

                // Get Agent Versions
                var versionMap = new ConcurrentDictionary <string, AgentVersionInfo>();

                var blockOptions = new ExecutionDataflowBlockOptions {
                    MaxDegreeOfParallelism = Configuration.Parallelism,
                    CancellationToken      = token,
                };

                var block = new ActionBlock <ServerAgent>(async agent => {
                    var result = new AgentVersionInfo {
                        Name = agent.Name,
                    };

                    try {
                        result.Version = await GetAgentVersion(agent, token);
                    }
                    catch (Exception error) {
                        result.Exception = error.UnfoldMessages();
                    }

                    versionMap[agent.Id] = result;
                }, blockOptions);

                foreach (var agent in agents)
                {
                    block.Post(agent);
                }

                block.Complete();
                await block.Completion;

                // Send Response
                var response = new HttpAgentVersionListResponse {
                    VersionList = versionMap.Select(x => new AgentVersionResponse {
                        AgentId      = x.Key,
                        AgentName    = x.Value.Name,
                        AgentVersion = x.Value.Version,
                        Exception    = x.Value.Exception,
                    }).ToArray(),
                };

                return(Response.Json(response));
            }
            catch (Exception error) {
                Log.Error("Failed to run Update-Task!", error);
                return(Response.Exception(error));
            }
        }