Class: SrvanyManager

Inherits:
Object
  • Object
show all
Defined in:
lib/srvany_manager.rb

Constant Summary collapse

Version =
VERSION = '0.2.1'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name_key = 'SRVANY_', srv_any_path = "#{ENV['ProgramFiles']}\\Windows Resource Kits\\Tools\\srvany.exe") ⇒ SrvanyManager

Construct a new service manager, just sets up a name_key, which is used as a leading string to the name of all services managed by the instance. srv_any_path must be a full path to srvany.exe.



22
23
24
25
26
# File 'lib/srvany_manager.rb', line 22

def initialize(name_key = 'SRVANY_', srv_any_path = "#{ENV['ProgramFiles']}\\Windows Resource Kits\\Tools\\srvany.exe")
  self.class.load_dependencies
  @name_key = name_key
  @srv_any_path = srv_any_path
end

Class Method Details

.load_dependenciesObject



8
9
10
11
12
13
14
15
16
17
# File 'lib/srvany_manager.rb', line 8

def self.load_dependencies
  once = false
  require 'win32/service'
  require 'win32/registry'
rescue LoadError => e
  raise e if once
  require 'rubygems'
  once = true
  retry
end

.versionObject



4
5
6
# File 'lib/srvany_manager.rb', line 4

def self.version
  Version
end

Instance Method Details

#create(name, command, args = '', description = nil, options = {}) ⇒ Object

Create a new service. The service name will be appended to the name_key and inserted into the registry using Win32::Service. The arguments are then adjusted with win32-registry. One recommended pattern is to store persisence details about the service as yaml in the optional description field.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/srvany_manager.rb', line 33

def create(name, command, args = '', description = nil, options = {})
defaults = {
  :service_type       => Win32::Service::WIN32_OWN_PROCESS,
  :start_type         => Win32::Service::DEMAND_START,
  :error_control      => Win32::Service::ERROR_NORMAL
  }.merge(options)
  name = @name_key + name.to_s
  options = defaults.merge(
  :display_name       => name,
  :service_name       => name,
  :description        => description || name,
  :binary_path_name   => @srv_any_path
  )
  Win32::Service.create(options)
  # `#{@ins_srv_path} #{name} "#{@srv_any_path}"`
  registry(name) do |reg|
    reg.create('Parameters') do |params|
      params.write_i("Start", 3)
      params.write_s("Application", command)
      params.write_s("AppParameters", args)
    end
  end
end

#delete(name) ⇒ Object

Mark a service for deletion (note, does not terminate the service)



58
59
60
# File 'lib/srvany_manager.rb', line 58

def delete(name)
  Win32::Service.delete(@name_key + name.to_s)
end

#listObject

Returns an array of tuples of name and description



71
72
73
74
75
76
77
78
79
80
# File 'lib/srvany_manager.rb', line 71

def list
  services = Win32::Service.services.select do |svc|
    # TODO in future, 1.8.7, can use start_with?
    svc.display_name[0,@name_key.size] == @name_key
  end
  services.map do |svc_info|
    name = svc_info.display_name
    [name.slice(@name_key.size, name.size), svc_info.description]
  end
end

#start(name) ⇒ Object



62
63
64
# File 'lib/srvany_manager.rb', line 62

def start(name)
  Win32::Service.start(@name_key + name.to_s)
end

#stop(name) ⇒ Object



66
67
68
# File 'lib/srvany_manager.rb', line 66

def stop(name)
  Win32::Service.stop(@name_key + name.to_s)
end