Guest User

Untitled

a guest
May 25th, 2018
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. #
  2. # role-manager - Installs and uninstalls software by role.
  3. #
  4.  
  5.  
  6. LOG_FILE = "c:\\temp\\logfile.txt"
  7.  
  8. DEBUG_LEVEL = 0
  9.  
  10. unless ARGV.empty?
  11. require 'rubygems'
  12. require 'win32/daemon'
  13. require "win32/service"
  14. include Win32
  15. end
  16.  
  17. ### logging
  18. SEVERITY = {
  19. :message => "MSG",
  20. :fail => "FAIL",
  21. :warn => "WARN"
  22. }
  23.  
  24. def log (text, severity = :message)
  25. File.open(LOG_FILE, "a+") do |f|
  26. f.puts("#{SEVERITY[severity]} - #{Time.now} - #{text}")
  27. end
  28. end
  29. ### end logging
  30.  
  31.  
  32.  
  33. begin
  34. require 'rubygems'
  35. require 'win32/daemon'
  36.  
  37. include Win32
  38.  
  39. class FooService < Daemon
  40.  
  41. def service_init
  42. log("Doing initializaton tasks")
  43. end
  44.  
  45. def service_main (*args)
  46. run_at = Time.now + INTERVAL
  47. log("Next running at #{run_at}") if DEBUG_LEVEL > 0
  48. while running?
  49. sleep 5
  50. if Time.now > run_at
  51. log("Running!") if DEBUG_LEVEL > 0
  52. do_something()
  53. run_at = Time.now + INTERVAL
  54. log("Next running at #{run_at}") if DEBUG_LEVEL > 0
  55. end
  56. end
  57.  
  58. log("Exiting main. Shutting down.")
  59. end
  60.  
  61. def service_shutdown
  62. # the awesome thing about not keeping ay state when running is that you don't need to clean up
  63. # before exit...
  64. log("Shutdown")
  65. exit(0)
  66. end
  67.  
  68. end
  69.  
  70.  
  71. # main
  72. case ARGV.first
  73. when 'install'
  74. puts "Domain\Username to run as:"
  75. user = gets
  76. puts "Password for that user:"
  77. pass = gets
  78. Service.create('FooService', nil,
  79. :display_name => 'FooService',
  80. :service_type => Service::WIN32_OWN_PROCESS,
  81. :description => 'ECN Role Manager Service',
  82. :start_type => Service::AUTO_START,
  83. :error_control => Service::ERROR_NORMAL,
  84. :binary_path_name => "c:\\ruby\\bin\\rubyw.exe #{File.expand_path($0)}",
  85. :load_order_group => 'Network',
  86. :dependencies => ['W32Time', 'Schedule'],
  87. :service_start_name => user,
  88. :password => pass
  89. )
  90. exit!
  91. when 'uninstall'
  92. Service.delete('FooService')
  93. exit!
  94. end
  95.  
  96.  
  97. RoleManager.mainloop
  98. rescue Exception => err
  99. File.open(LOG_FILE, "a+") do |f|
  100. f.puts("FAIL - #{Time.now} - Daemon threw an exception - #{err}")
  101. f.puts(err.backtrace)
  102. raise
  103. end
  104. end
Add Comment
Please, Sign In to add comment