Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #
- # role-manager - Installs and uninstalls software by role.
- #
- LOG_FILE = "c:\\temp\\logfile.txt"
- DEBUG_LEVEL = 0
- unless ARGV.empty?
- require 'rubygems'
- require 'win32/daemon'
- require "win32/service"
- include Win32
- end
- ### logging
- SEVERITY = {
- :message => "MSG",
- :fail => "FAIL",
- :warn => "WARN"
- }
- def log (text, severity = :message)
- File.open(LOG_FILE, "a+") do |f|
- f.puts("#{SEVERITY[severity]} - #{Time.now} - #{text}")
- end
- end
- ### end logging
- begin
- require 'rubygems'
- require 'win32/daemon'
- include Win32
- class FooService < Daemon
- def service_init
- log("Doing initializaton tasks")
- end
- def service_main (*args)
- run_at = Time.now + INTERVAL
- log("Next running at #{run_at}") if DEBUG_LEVEL > 0
- while running?
- sleep 5
- if Time.now > run_at
- log("Running!") if DEBUG_LEVEL > 0
- do_something()
- run_at = Time.now + INTERVAL
- log("Next running at #{run_at}") if DEBUG_LEVEL > 0
- end
- end
- log("Exiting main. Shutting down.")
- end
- def service_shutdown
- # the awesome thing about not keeping ay state when running is that you don't need to clean up
- # before exit...
- log("Shutdown")
- exit(0)
- end
- end
- # main
- case ARGV.first
- when 'install'
- puts "Domain\Username to run as:"
- user = gets
- puts "Password for that user:"
- pass = gets
- Service.create('FooService', nil,
- :display_name => 'FooService',
- :service_type => Service::WIN32_OWN_PROCESS,
- :description => 'ECN Role Manager Service',
- :start_type => Service::AUTO_START,
- :error_control => Service::ERROR_NORMAL,
- :binary_path_name => "c:\\ruby\\bin\\rubyw.exe #{File.expand_path($0)}",
- :load_order_group => 'Network',
- :dependencies => ['W32Time', 'Schedule'],
- :service_start_name => user,
- :password => pass
- )
- exit!
- when 'uninstall'
- Service.delete('FooService')
- exit!
- end
- RoleManager.mainloop
- rescue Exception => err
- File.open(LOG_FILE, "a+") do |f|
- f.puts("FAIL - #{Time.now} - Daemon threw an exception - #{err}")
- f.puts(err.backtrace)
- raise
- end
- end
Add Comment
Please, Sign In to add comment