Module: Capsaicin::Service::Windows

Included in:
Capsaicin::Service
Defined in:
lib/capsaicin/service/windows.rb

Constant Summary collapse

DEFAULT_ACTIONS =
[:start, :stop]

Instance Method Summary collapse

Instance Method Details

#windows(id, *args) ⇒ Object

Defines a recipe to control a generic Windows NT service.


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/capsaicin/service/windows.rb', line 17

def windows(id,*args)
  options = Hash===args.last ? args.pop : {}

  svc_name = id.to_s
  svc_desc = next_description(:reset) || (svc_name.capitalize unless options.delete(:hide))
  svc_actions = DEFAULT_ACTIONS 
  svc_actions += args.pop if Array === args.last

  namespace id do
    case args.first
    when String; id = args.shift.intern
    when Symbol; id = args.shift
    end

    [:default, :status].each do |k|
      desc "#{svc_desc}: #{SVC_ACTION_CAPTIONS[:status]}" if svc_desc
      task k, options do
        service.windows? id, true or
          abort "Failed to get service status for #{svc_name}"
      end
    end

    DEFAULT_ACTIONS.each do |svc_action|
      svc_action = svc_action.intern if String === svc_action
      desc "#{svc_desc}: #{SVC_ACTION_CAPTIONS[svc_action]}" if svc_desc
      task svc_action, options do
        local_run "net #{svc_action} \"#{id}\""
      end
    end

    desc "#{svc_desc}: #{SVC_ACTION_CAPTIONS[:restart]}" if svc_desc
    task :restart, options do
      case service.windows?(id)
      when 4, 2; stop
      when NilClass; abort "Failed to get service status for #{svc_name}"
      end
      start
    end
  
    instance_eval { yield } if block_given?
  end
end

#windows?(id, verbose = false) ⇒ Boolean

Check for the existance of a generic Windows NT service.

Returns:

  • (Boolean)

8
9
10
11
12
13
# File 'lib/capsaicin/service/windows.rb', line 8

def windows?(id, verbose=false)
  logger.trace "executing locally: sc query \"#{id}\"" if logger and verbose
  $1.to_i if `sc query "#{id}"` =~ /STATE +: +([0-9])+ +([^ ]+)/
ensure
  logger.trace "   service status => #{$2} (#{$1})" if logger and verbose
end