Chef: Installing a Windows Service

為{幸葍}努か 提交于 2019-12-06 16:21:04

A few things to look at, first switch all your slashes around so it is "c:/Servies/V5.5/EUNTRouteManager/Routing.WindowsService.exe". Ruby, and most programming languages, use backslashes as escape sequences to encode characters that you can't normally see, like \n for newlines or \t for tabs.

Next is the package install, you are telling it the file is an MSI but it ends with .exe so this is unlikely. From your text I'm guessing you aren't actually trying to install package file but for the future you'll have to match the install type to one of the known types (MSI, NSIS, etc).

Finally, to control a service you'll want to use the service or windows_service resources, but you still need to create it. Fortunately there is a hidden helper for this:

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

I don't have a Windows machine to test that on, but I think it should work.

In your resource, you are using the windows_package resource. This resource is not used to install services, but install to install packages (like MSI's that use an install wizard). The windows_package resource will call msiexec.exe on the machine, or a custom installer if you so specify.

The is no native command to install a service from an executable, but there is a way to execute sc.exe in order to do the install:

    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

You may also want to set the service to logon as a specific user:

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

And then make sure the service starts.

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

A Note of Caution! The first resource block has the action :run and will then run every time chef-client runs on the machine. This will fail if the service is already installed, so the code is best wrapped around an IF check, or to use not_ifs to see if the service already exists before executing the first code block.

An addition to Obliviams answer:

Here is an example how to start a normal .exe as service with nssm and checking if service exists (Service is 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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!