Sha256: f36701bfa961b60c0954349aa09621213de4ba99c7feb6de3f858ae7a7440ab9

Contents?: true

Size: 1.96 KB

Versions: 2

Compression:

Stored size: 1.96 KB

Contents

module CapistranoExtensions
  module Service
    module Windows

      DEFAULT_ACTIONS = [:start, :stop]

      # Check for the existance of a generic Windows NT service.
      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

      # Defines a recipe to control a generic Windows NT service.
      #
      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
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
joekhoobyar-capistrano-extensions-0.0.7 lib/capistrano_extensions/service/windows.rb
joekhoobyar-capistrano-extensions-0.0.8 lib/capistrano_extensions/service/windows.rb