节点在线、应用在线、配置在线使用令牌查询
大石头 authored at 2021-12-16 19:49:30
2.41 KiB
Stardust
using System.Diagnostics;
using NewLife;
using Stardust.Models;

namespace StarAgent.Managers;

/// <summary>标准部署策略</summary>
/// <remarks>
/// 默认推荐模式。解压到工作目录,运行进程,守护进程。
/// 简单直接,适合大多数应用场景。
/// </remarks>
public class StandardDeployStrategy : DeployStrategyBase
{
    /// <summary>部署模式</summary>
    public override DeployMode Mode => DeployMode.Standard;

    /// <summary>是否需要守护进程</summary>
    public override Boolean NeedGuardian => true;

    /// <summary>解压部署包</summary>
    /// <param name="context">部署上下文</param>
    /// <returns>是否成功</returns>
    public override Boolean Extract(DeployContext context)
    {
        using var span = Tracer?.NewSpan("Deploy-Extract", new { context.Name, context.ZipFile, context.WorkingDirectory });

        var workDir = context.WorkingDirectory;
        var fi = RetrieveZip(context);
        if (fi != null)
        {
            context.WriteLog("Standard mode: extract to the working directory: {0}", workDir);

            // 直接解压到工作目录
            ExtractZip(fi.FullName, workDir, context.Deploy, context.Log);
        }
        else
        {
            // 没有 zip 包时,假设文件已经存在于工作目录中
            context.WriteLog("Standard mode: no extraction needed, using the working directory directly: {0}", workDir);
        }

        // 查找可执行文件
        return RetrieveExeFile(context, workDir);
    }

    /// <summary>执行应用</summary>
    /// <param name="context">部署上下文</param>
    /// <returns>启动的进程</returns>
    public override Process? Execute(DeployContext context)
    {
        using var span = Tracer?.NewSpan("Deploy-Execute", new { context.ExecuteFile, context.Arguments, context.WorkingDirectory });

        if (context.ExecuteFile.IsNullOrEmpty())
        {
            context.WriteLog("The executable path is empty");
            return null;
        }

        var runFile = context.ExecuteFile.AsFile();
        if (runFile == null || !runFile.Exists)
        {
            // 不是文件路径,尝试作为系统命令执行(如ping)
            return ExecuteCommand(context);
        }

        context.WriteLog("Run file: {0}", runFile.FullName);

        var si = BuildProcessStartInfo(context, runFile);
        return StartProcess(context, si);
    }
}