2015-09-14 13:01:33 +00:00
|
|
|
# encoding: utf-8
|
|
|
|
# license: All rights reserved
|
|
|
|
|
|
|
|
# Usage:
|
|
|
|
# describe service('dhcp') do
|
|
|
|
# it { should be_enabled }
|
|
|
|
# it { should be_installed }
|
|
|
|
# it { should be_running }
|
|
|
|
# end
|
|
|
|
class Service < Vulcano.resource(1)
|
|
|
|
name 'service'
|
|
|
|
|
|
|
|
def initialize(service_name)
|
|
|
|
@service_name = service_name
|
|
|
|
|
|
|
|
# select package manager
|
|
|
|
@service_mgmt = nil
|
2015-09-14 20:22:24 +00:00
|
|
|
@cache = nil
|
|
|
|
case vulcano.os[:family]
|
2015-09-14 13:01:33 +00:00
|
|
|
when 'windows'
|
|
|
|
@service_mgmt = WindowsSrv.new(vulcano)
|
|
|
|
end
|
|
|
|
|
|
|
|
fail 'The `service` resource is not supported on your OS yet. Please open an issue on Github.' if @service_mgmt.nil?
|
|
|
|
end
|
|
|
|
|
2015-09-14 13:15:57 +00:00
|
|
|
def info
|
2015-09-14 20:22:24 +00:00
|
|
|
return @cache if !@cache.nil?
|
|
|
|
@cache = @service_mgmt.info(@service_name)
|
2015-09-14 13:01:33 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# verifies the service is enabled
|
|
|
|
def enabled?(_level = nil)
|
2015-09-14 20:22:24 +00:00
|
|
|
info[:enabled] if info.nil?
|
2015-09-14 13:01:33 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# verifies the service is registered
|
|
|
|
def installed?(_name = nil, _version = nil)
|
2015-09-14 13:15:57 +00:00
|
|
|
!info.nil?
|
2015-09-14 13:01:33 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# verifies the service is currently running
|
|
|
|
def running?(_under = nil)
|
2015-09-14 13:15:57 +00:00
|
|
|
info[:running]
|
2015-09-14 13:01:33 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def to_s
|
|
|
|
"Service #{@service_name}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class ServiceManager
|
|
|
|
def initialize(vulcano)
|
|
|
|
@vulcano = vulcano
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Determine the service state from Windows
|
|
|
|
# Uses Powershell to retrieve the information
|
|
|
|
class WindowsSrv < ServiceManager
|
|
|
|
# Determine service details
|
|
|
|
# PS: Get-Service -Name 'dhcp'| Select-Object -Property Name, DisplayName, Status | ConvertTo-Json
|
|
|
|
# {
|
|
|
|
# "Name": "dhcp",
|
|
|
|
# "DisplayName": "DHCP Client",
|
|
|
|
# "Status": 4
|
|
|
|
# }
|
|
|
|
#
|
|
|
|
# Until StartMode is not added to Get-Service, we need to do a workaround
|
|
|
|
# @see: https://connect.microsoft.com/PowerShell/feedback/details/424948/i-would-like-to-see-the-property-starttype-added-to-get-services
|
|
|
|
# Use the following powershell to determine the start mode
|
|
|
|
# PS: Get-WmiObject -Class Win32_Service | Where-Object {$_.Name -eq $name -or $_.DisplayName -eq $name} | Select-Object -Prop
|
|
|
|
# erty Name, StartMode, State, Status | ConvertTo-Json
|
|
|
|
# {
|
|
|
|
# "Name": "Dhcp",
|
|
|
|
# "StartMode": "Auto",
|
|
|
|
# "State": "Running",
|
|
|
|
# "Status": "OK"
|
|
|
|
# }
|
|
|
|
#
|
|
|
|
# Windows Services have the following status code:
|
|
|
|
# @see: https://msdn.microsoft.com/en-us/library/windows/desktop/ms685996(v=vs.85).aspx
|
|
|
|
# - 1: Stopped
|
|
|
|
# - 2: Starting
|
|
|
|
# - 3: Stopping
|
|
|
|
# - 4: Running
|
|
|
|
# - 5: Continue Pending
|
|
|
|
# - 6: Pause Pending
|
|
|
|
# - 7: Paused
|
|
|
|
#
|
|
|
|
|
|
|
|
def info(service_name)
|
|
|
|
srv_cmd = "New-Object -Type PSObject | Add-Member -MemberType NoteProperty -Name Service -Value (Get-Service -Name #{service_name}| Select-Object -Property Name, DisplayName, Status) -PassThru | Add-Member -MemberType NoteProperty -Name WMI -Value (Get-WmiObject -Class Win32_Service | Where-Object {$_.Name -eq '#{service_name}' -or $_.DisplayName -eq '#{service_name}'} | Select-Object -Property StartMode) -PassThru | ConvertTo-Json"
|
|
|
|
cmd = @vulcano.run_command(srv_cmd)
|
|
|
|
|
|
|
|
# cannot rely on exit code for now, successful command returns exit code 1
|
|
|
|
# return nil if cmd.exit_status != 0
|
|
|
|
# try to parse json
|
|
|
|
begin
|
|
|
|
service = JSON.parse(cmd.stdout)
|
|
|
|
rescue JSON::ParserError => _e
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
|
|
|
service['Service']['Status'] == 4 ? (running = true) : (running = false)
|
|
|
|
service['WMI']['StartMode'] == 'Auto' ? (enabled = true) : (enabled = false)
|
|
|
|
|
|
|
|
{
|
|
|
|
name: service['Service']['Name'],
|
|
|
|
description: service['Service']['DisplayName'],
|
|
|
|
installed: true,
|
|
|
|
running: running,
|
|
|
|
enabled: enabled,
|
|
|
|
type: 'windows',
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|