简体   繁体   中英

Chef: Installing a Windows Service

I'm new to this forum (so apologies if I've posted in the wrong place) and also VERY NEW to Chef. I've been all around the houses trying to get a clear example of how to install a Windows service.

Basically I want the Chef equivalent of "SC create"

The recipe I'm trying to use is this:

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

When I run this I get error saying theres a problem with msi.

I've tried multiple variants of this script and am finding getting clear information on how to install a simple service incredibly sparse.

So does anyone know where I've gone wrong? As I say when this works it should appear as a Windows service in the services list.

The files are already on the service in the path specified, and I've running Windows 2008 R2, with PowerShell v4.0 and the latest Chef client install.

Any and all help would be appreciate.

Thanks for your feedback

Regards

Scott

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM