【问题标题】:Chef: Installing a Windows ServiceChef:安装 Windows 服务
【发布时间】:2014-12-23 12:27:01
【问题描述】:

我是这个论坛的新手(如果我在错误的地方发帖,请大家见谅),对 Chef 来说也很陌生。我一直在四处寻找有关如何安装 Windows 服务的清晰示例。

基本上我想要厨师相当于“SC create”

我尝试使用的配方是这样的:

windows_package "RMS_EU" do
  installer_type :msi
  action :install
  source "c:\Servies\V5.5\EUNTRouteManager\Routing.WindowsService.exe"
end

当我运行此程序时,我收到错误消息,提示 msi 存在问题。

我已经尝试了这个脚本的多种变体,并且发现关于如何安装简单服务的清晰信息非常稀少。

那么有人知道我哪里出错了吗?正如我所说,当它起作用时,它应该在服务列表中显示为 Windows 服务。

文件已经在指定路径中的服务上,并且我运行的是 Windows 2008 R2,带有 PowerShell v4.0 和最新的 Chef 客户端安装。

我们将不胜感激。

感谢您的反馈

问候

斯科特

【问题讨论】:

    标签: windows service chef-infra


    【解决方案1】:

    有几件事要看,首先将所有的斜线换成"c:/Servies/V5.5/EUNTRouteManager/Routing.WindowsService.exe"。 Ruby 和大多数编程语言都使用反斜杠作为转义序列来编码您通常看不到的字符,例如 \n 用于换行或 \t 用于制表符。

    接下来是软件包安装,您告诉它该文件是一个 MSI,但它以 .exe 结尾,所以这不太可能。从您的文字中,我猜您实际上并没有尝试安装软件包文件,但将来您必须将安装类型与已知类型之一(MSI、NSIS 等)相匹配。

    最后,要控制服务,您需要使用service or windows_service 资源,但您仍然需要创建它。幸运的是,有一个隐藏的助手:

    ruby_block 'create service' do
      block do
         Chef::Application::WindowsServiceManager.new(
           service_name: "EUNTRouteManager",
           service_display_name: "Something",
           service_description: "Longer something.",
           service_file_path: "c:/Servies/V5.5/EUNTRouteManager/Routing.WindowsService.exe",
         ).run(%w{-a install})
      end
    end
    
    service 'EUNTRouteManager' do
      action [:enable, :start]
    end
    

    我没有 Windows 机器来测试它,但我认为它应该可以工作。

    【讨论】:

    • 感谢这个有用的提示。不幸的是,我在目标节点上收到“NameError uninitialized constant Chef::Application::WindowsServiceManager”。
    • 代码结构在过去一年发生了变化,您可能需要获取相关文件。
    【解决方案2】:

    在您的资源中,您正在使用 windows_package 资源。此资源不用于安装服务,而是用于安装包(如使用安装向导的 MSI)。 windows_package 资源将调用机器上的 msiexec.exe,或自定义安装程序(如果您指定)。

    从可执行文件安装服务不是本机命令,但有一种方法可以执行 sc.exe 以进行安装:

        execute "Installing Service <SERVICE_NAME>" do
            command "sc.exe create <SERVICE_NAME> binPath=<PHYSICAL_PATH_TO_EXE>
            action :run
            notifies :run, "execute[Setting Log On User For <SERVICE_NAME>]", :immediately
        end
    

    您可能还想将服务设置为以特定用户身份登录:

        execute "Setting Log On User For <SERVICE_NAME>" do
            command "sc.exe config \"<SERVICE_NAME>\" obj=<USER_NAME> password=<USER_PASSWORD>"
            action :nothing
        end
    

    然后确保服务启动。

        service "<SERVICE_NAME>" do
            supports :status => true, :restart => true
            action [ :enable, :start ]
        end
    

    注意事项! 第一个资源块具有操作 :run,然后每次 chef-client 在机器上运行时都会运行。如果服务已经安装,这将失败,因此代码最好围绕 IF 检查进行包装,或者在执行第一个代码块之前使用 not_ifs 来查看服务是否已经存在。

    【讨论】:

    【解决方案3】:

    Obliviams 的补充回答:

    这里是一个例子,如何使用 nssm 启动一个普通的 .exe 作为服务并检查服务是否存在(服务是 nginx):

    # Start nginx as service
    # make sure nssm.exe is in C:\Windows
    
    execute "Installing Service nginx" do
        command "nssm install nginx \"path\\to\\exe\\nginx.exe\""
        not_if { ::Win32::Service.exists?( 'nginx' ) }
    end
    
    service 'nginx' do
        supports :status => true, :restart => true
        action [ :enable, :start ]
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-15
      • 2011-09-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多