Class: WindowsDaemon
Constant Summary
collapse
- CREATE_NEW_CONSOLE =
0x00000010
- LOG_FILE =
File.expand_path(File.join(ENV['ALLUSERSPROFILE'], 'PuppetLabs', 'puppet', 'var', 'log', 'windows.log'))
- LEVELS =
[:debug, :info, :notice, :warning, :err, :alert, :emerg, :crit]
Puppet::Util::Windows::Daemon::CONTINUE_PENDING, Puppet::Util::Windows::Daemon::CONTROL_CONTINUE, Puppet::Util::Windows::Daemon::CONTROL_INTERROGATE, Puppet::Util::Windows::Daemon::CONTROL_NETBINDADD, Puppet::Util::Windows::Daemon::CONTROL_NETBINDDISABLE, Puppet::Util::Windows::Daemon::CONTROL_NETBINDENABLE, Puppet::Util::Windows::Daemon::CONTROL_NETBINDREMOVE, Puppet::Util::Windows::Daemon::CONTROL_PARAMCHANGE, Puppet::Util::Windows::Daemon::CONTROL_PAUSE, Puppet::Util::Windows::Daemon::CONTROL_STOP, Puppet::Util::Windows::Daemon::ERROR_CALL_NOT_IMPLEMENTED, Puppet::Util::Windows::Daemon::IDLE, Puppet::Util::Windows::Daemon::IDLE_CONTROL_CODE, Puppet::Util::Windows::Daemon::NO_ERROR, Puppet::Util::Windows::Daemon::PAUSED, Puppet::Util::Windows::Daemon::PAUSE_PENDING, Puppet::Util::Windows::Daemon::RUNNING, Puppet::Util::Windows::Daemon::START_PENDING, Puppet::Util::Windows::Daemon::STOPPED, Puppet::Util::Windows::Daemon::STOP_PENDING, Puppet::Util::Windows::Daemon::Service_Ctrl_ex, Puppet::Util::Windows::Daemon::Service_Main, Puppet::Util::Windows::Daemon::SetTheServiceStatus, Puppet::Util::Windows::Daemon::ThreadProc, Puppet::Util::Windows::Daemon::WAIT_FAILED, Puppet::Util::Windows::Daemon::WAIT_OBJECT_0, Puppet::Util::Windows::Daemon::WAIT_TIMEOUT
Puppet::FFI::Windows::Constants::ABOVE_NORMAL_PRIORITY_CLASS, Puppet::FFI::Windows::Constants::ALL_SERVICE_TYPES, Puppet::FFI::Windows::Constants::BELOW_NORMAL_PRIORITY_CLASS, Puppet::FFI::Windows::Constants::CREATE_BREAKAWAY_FROM_JOB, Puppet::FFI::Windows::Constants::CREATE_DEFAULT_ERROR_MODE, Puppet::FFI::Windows::Constants::CREATE_NEW_PROCESS_GROUP, Puppet::FFI::Windows::Constants::CREATE_NO_WINDOW, Puppet::FFI::Windows::Constants::CREATE_PRESERVE_CODE_AUTHZ_LEVEL, Puppet::FFI::Windows::Constants::CREATE_PROTECTED_PROCESS, Puppet::FFI::Windows::Constants::CREATE_SEPARATE_WOW_VDM, Puppet::FFI::Windows::Constants::CREATE_SHARED_WOW_VDM, Puppet::FFI::Windows::Constants::CREATE_SUSPENDED, Puppet::FFI::Windows::Constants::CREATE_UNICODE_ENVIRONMENT, Puppet::FFI::Windows::Constants::DEBUG_ONLY_THIS_PROCESS, Puppet::FFI::Windows::Constants::DEBUG_PROCESS, Puppet::FFI::Windows::Constants::DELETE, Puppet::FFI::Windows::Constants::DETACHED_PROCESS, Puppet::FFI::Windows::Constants::ERROR_ALREADY_EXISTS, Puppet::FFI::Windows::Constants::ERROR_FILE_NOT_FOUND, Puppet::FFI::Windows::Constants::ERROR_PATH_NOT_FOUND, Puppet::FFI::Windows::Constants::ERROR_SERVICE_DOES_NOT_EXIST, Puppet::FFI::Windows::Constants::FILE_ALL_ACCESS, Puppet::FFI::Windows::Constants::FILE_APPEND_DATA, Puppet::FFI::Windows::Constants::FILE_ATTRIBUTE_DIRECTORY, Puppet::FFI::Windows::Constants::FILE_ATTRIBUTE_READONLY, Puppet::FFI::Windows::Constants::FILE_ATTRIBUTE_REPARSE_POINT, Puppet::FFI::Windows::Constants::FILE_DELETE_CHILD, Puppet::FFI::Windows::Constants::FILE_DEVICE_FILE_SYSTEM, Puppet::FFI::Windows::Constants::FILE_EXECUTE, Puppet::FFI::Windows::Constants::FILE_FLAG_BACKUP_SEMANTICS, Puppet::FFI::Windows::Constants::FILE_FLAG_OPEN_REPARSE_POINT, Puppet::FFI::Windows::Constants::FILE_GENERIC_EXECUTE, Puppet::FFI::Windows::Constants::FILE_GENERIC_READ, Puppet::FFI::Windows::Constants::FILE_GENERIC_WRITE, Puppet::FFI::Windows::Constants::FILE_READ_ATTRIBUTES, Puppet::FFI::Windows::Constants::FILE_READ_DATA, Puppet::FFI::Windows::Constants::FILE_READ_EA, Puppet::FFI::Windows::Constants::FILE_SHARE_READ, Puppet::FFI::Windows::Constants::FILE_SHARE_WRITE, Puppet::FFI::Windows::Constants::FILE_WRITE_ATTRIBUTES, Puppet::FFI::Windows::Constants::FILE_WRITE_DATA, Puppet::FFI::Windows::Constants::FILE_WRITE_EA, Puppet::FFI::Windows::Constants::FINAL_STATES, Puppet::FFI::Windows::Constants::FSCTL_GET_REPARSE_POINT, Puppet::FFI::Windows::Constants::GENERIC_ALL, Puppet::FFI::Windows::Constants::GENERIC_EXECUTE, Puppet::FFI::Windows::Constants::GENERIC_READ, Puppet::FFI::Windows::Constants::GENERIC_WRITE, Puppet::FFI::Windows::Constants::HANDLE_FLAG_INHERIT, Puppet::FFI::Windows::Constants::HIGH_PRIORITY_CLASS, Puppet::FFI::Windows::Constants::IDLE_PRIORITY_CLASS, Puppet::FFI::Windows::Constants::INHERIT_PARENT_AFFINITY, Puppet::FFI::Windows::Constants::INVALID_FILE_ATTRIBUTES, Puppet::FFI::Windows::Constants::INVALID_HANDLE_VALUE, Puppet::FFI::Windows::Constants::IO_REPARSE_TAG_CSV, Puppet::FFI::Windows::Constants::IO_REPARSE_TAG_DEDUP, Puppet::FFI::Windows::Constants::IO_REPARSE_TAG_DFS, Puppet::FFI::Windows::Constants::IO_REPARSE_TAG_DFSR, Puppet::FFI::Windows::Constants::IO_REPARSE_TAG_HSM, Puppet::FFI::Windows::Constants::IO_REPARSE_TAG_HSM2, Puppet::FFI::Windows::Constants::IO_REPARSE_TAG_MOUNT_POINT, Puppet::FFI::Windows::Constants::IO_REPARSE_TAG_NFS, Puppet::FFI::Windows::Constants::IO_REPARSE_TAG_SIS, Puppet::FFI::Windows::Constants::IO_REPARSE_TAG_SYMLINK, Puppet::FFI::Windows::Constants::IO_REPARSE_TAG_WIM, Puppet::FFI::Windows::Constants::LOGON_WITH_PROFILE, Puppet::FFI::Windows::Constants::MAXIMUM_REPARSE_DATA_BUFFER_SIZE, Puppet::FFI::Windows::Constants::METHOD_BUFFERED, Puppet::FFI::Windows::Constants::NORMAL_PRIORITY_CLASS, Puppet::FFI::Windows::Constants::OPEN_EXISTING, Puppet::FFI::Windows::Constants::PROCESS_ALL_ACCESS, Puppet::FFI::Windows::Constants::PROCESS_QUERY_INFORMATION, Puppet::FFI::Windows::Constants::PROCESS_SET_INFORMATION, Puppet::FFI::Windows::Constants::PROCESS_TERMINATE, Puppet::FFI::Windows::Constants::PROCESS_VM_READ, Puppet::FFI::Windows::Constants::REALTIME_PRIORITY_CLASS, Puppet::FFI::Windows::Constants::REPLACEFILE_IGNORE_ACL_ERRORS, Puppet::FFI::Windows::Constants::REPLACEFILE_IGNORE_MERGE_ERRORS, Puppet::FFI::Windows::Constants::REPLACEFILE_WRITE_THROUGH, Puppet::FFI::Windows::Constants::SC_MANAGER_ALL_ACCESS, Puppet::FFI::Windows::Constants::SC_MANAGER_CONNECT, Puppet::FFI::Windows::Constants::SC_MANAGER_CREATE_SERVICE, Puppet::FFI::Windows::Constants::SC_MANAGER_ENUMERATE_SERVICE, Puppet::FFI::Windows::Constants::SC_MANAGER_LOCK, Puppet::FFI::Windows::Constants::SC_MANAGER_MODIFY_BOOT_CONFIG, Puppet::FFI::Windows::Constants::SC_MANAGER_QUERY_LOCK_STATUS, Puppet::FFI::Windows::Constants::SEM_FAILCRITICALERRORS, Puppet::FFI::Windows::Constants::SEM_NOGPFAULTERRORBOX, Puppet::FFI::Windows::Constants::SERVICENAME_MAX, Puppet::FFI::Windows::Constants::SERVICE_ACCEPT_HARDWAREPROFILECHANGE, Puppet::FFI::Windows::Constants::SERVICE_ACCEPT_NETBINDCHANGE, Puppet::FFI::Windows::Constants::SERVICE_ACCEPT_PARAMCHANGE, Puppet::FFI::Windows::Constants::SERVICE_ACCEPT_PAUSE_CONTINUE, Puppet::FFI::Windows::Constants::SERVICE_ACCEPT_POWEREVENT, Puppet::FFI::Windows::Constants::SERVICE_ACCEPT_PRESHUTDOWN, Puppet::FFI::Windows::Constants::SERVICE_ACCEPT_SESSIONCHANGE, Puppet::FFI::Windows::Constants::SERVICE_ACCEPT_SHUTDOWN, Puppet::FFI::Windows::Constants::SERVICE_ACCEPT_STOP, Puppet::FFI::Windows::Constants::SERVICE_ACCEPT_TIMECHANGE, Puppet::FFI::Windows::Constants::SERVICE_ACCEPT_TRIGGEREVENT, Puppet::FFI::Windows::Constants::SERVICE_ACCEPT_USER_LOGOFF, Puppet::FFI::Windows::Constants::SERVICE_ACTIVE, Puppet::FFI::Windows::Constants::SERVICE_ALL_ACCESS, Puppet::FFI::Windows::Constants::SERVICE_AUTO_START, Puppet::FFI::Windows::Constants::SERVICE_BOOT_START, Puppet::FFI::Windows::Constants::SERVICE_CHANGE_CONFIG, Puppet::FFI::Windows::Constants::SERVICE_CONFIG_DELAYED_AUTO_START_INFO, Puppet::FFI::Windows::Constants::SERVICE_CONFIG_DESCRIPTION, Puppet::FFI::Windows::Constants::SERVICE_CONFIG_FAILURE_ACTIONS, Puppet::FFI::Windows::Constants::SERVICE_CONFIG_FAILURE_ACTIONS_FLAG, Puppet::FFI::Windows::Constants::SERVICE_CONFIG_LAUNCH_PROTECTED, Puppet::FFI::Windows::Constants::SERVICE_CONFIG_PREFERRED_NODE, Puppet::FFI::Windows::Constants::SERVICE_CONFIG_PRESHUTDOWN_INFO, Puppet::FFI::Windows::Constants::SERVICE_CONFIG_REQUIRED_PRIVILEGES_INFO, Puppet::FFI::Windows::Constants::SERVICE_CONFIG_SERVICE_SID_INFO, Puppet::FFI::Windows::Constants::SERVICE_CONFIG_TRIGGER_INFO, Puppet::FFI::Windows::Constants::SERVICE_CONFIG_TYPES, Puppet::FFI::Windows::Constants::SERVICE_CONTINUE_PENDING, Puppet::FFI::Windows::Constants::SERVICE_CONTROL_CONTINUE, Puppet::FFI::Windows::Constants::SERVICE_CONTROL_DEVICEEVENT, Puppet::FFI::Windows::Constants::SERVICE_CONTROL_HARDWAREPROFILECHANGE, Puppet::FFI::Windows::Constants::SERVICE_CONTROL_INTERROGATE, Puppet::FFI::Windows::Constants::SERVICE_CONTROL_NETBINDADD, Puppet::FFI::Windows::Constants::SERVICE_CONTROL_NETBINDDISABLE, Puppet::FFI::Windows::Constants::SERVICE_CONTROL_NETBINDENABLE, Puppet::FFI::Windows::Constants::SERVICE_CONTROL_NETBINDREMOVE, Puppet::FFI::Windows::Constants::SERVICE_CONTROL_PARAMCHANGE, Puppet::FFI::Windows::Constants::SERVICE_CONTROL_PAUSE, Puppet::FFI::Windows::Constants::SERVICE_CONTROL_POWEREVENT, Puppet::FFI::Windows::Constants::SERVICE_CONTROL_PRESHUTDOWN, Puppet::FFI::Windows::Constants::SERVICE_CONTROL_SESSIONCHANGE, Puppet::FFI::Windows::Constants::SERVICE_CONTROL_SHUTDOWN, Puppet::FFI::Windows::Constants::SERVICE_CONTROL_SIGNALS, Puppet::FFI::Windows::Constants::SERVICE_CONTROL_STOP, Puppet::FFI::Windows::Constants::SERVICE_CONTROL_TIMECHANGE, Puppet::FFI::Windows::Constants::SERVICE_CONTROL_TRIGGEREVENT, Puppet::FFI::Windows::Constants::SERVICE_DEMAND_START, Puppet::FFI::Windows::Constants::SERVICE_DISABLED, Puppet::FFI::Windows::Constants::SERVICE_ENUMERATE_DEPENDENTS, Puppet::FFI::Windows::Constants::SERVICE_FILE_SYSTEM_DRIVER, Puppet::FFI::Windows::Constants::SERVICE_INACTIVE, Puppet::FFI::Windows::Constants::SERVICE_INTERACTIVE_PROCESS, Puppet::FFI::Windows::Constants::SERVICE_INTERROGATE, Puppet::FFI::Windows::Constants::SERVICE_KERNEL_DRIVER, Puppet::FFI::Windows::Constants::SERVICE_NO_CHANGE, Puppet::FFI::Windows::Constants::SERVICE_PAUSED, Puppet::FFI::Windows::Constants::SERVICE_PAUSE_CONTINUE, Puppet::FFI::Windows::Constants::SERVICE_PAUSE_PENDING, Puppet::FFI::Windows::Constants::SERVICE_QUERY_CONFIG, Puppet::FFI::Windows::Constants::SERVICE_QUERY_STATUS, Puppet::FFI::Windows::Constants::SERVICE_RUNNING, Puppet::FFI::Windows::Constants::SERVICE_START, Puppet::FFI::Windows::Constants::SERVICE_START_PENDING, Puppet::FFI::Windows::Constants::SERVICE_START_TYPES, Puppet::FFI::Windows::Constants::SERVICE_STATES, Puppet::FFI::Windows::Constants::SERVICE_STATE_ALL, Puppet::FFI::Windows::Constants::SERVICE_STOP, Puppet::FFI::Windows::Constants::SERVICE_STOPPED, Puppet::FFI::Windows::Constants::SERVICE_STOP_PENDING, Puppet::FFI::Windows::Constants::SERVICE_SYSTEM_START, Puppet::FFI::Windows::Constants::SERVICE_USER_DEFINED_CONTROL, Puppet::FFI::Windows::Constants::SERVICE_USER_OWN_PROCESS, Puppet::FFI::Windows::Constants::SERVICE_USER_SHARE_PROCESS, Puppet::FFI::Windows::Constants::SERVICE_WIN32_OWN_PROCESS, Puppet::FFI::Windows::Constants::SERVICE_WIN32_SHARE_PROCESS, Puppet::FFI::Windows::Constants::SHGFI_DISPLAYNAME, Puppet::FFI::Windows::Constants::SHGFI_PIDL, Puppet::FFI::Windows::Constants::SPECIFIC_RIGHTS_ALL, Puppet::FFI::Windows::Constants::STANDARD_RIGHTS_ALL, Puppet::FFI::Windows::Constants::STANDARD_RIGHTS_EXECUTE, Puppet::FFI::Windows::Constants::STANDARD_RIGHTS_READ, Puppet::FFI::Windows::Constants::STANDARD_RIGHTS_REQUIRED, Puppet::FFI::Windows::Constants::STANDARD_RIGHTS_WRITE, Puppet::FFI::Windows::Constants::STARTF_USESTDHANDLES, Puppet::FFI::Windows::Constants::SYNCHRONIZE, Puppet::FFI::Windows::Constants::TOKEN_INFORMATION_CLASS, Puppet::FFI::Windows::Constants::UNSAFE_PENDING_STATES, Puppet::FFI::Windows::Constants::WRITE_DAC, Puppet::FFI::Windows::Constants::WRITE_OWNER
Puppet::FFI::Windows::Structs::MAXIMUM_REPARSE_DATA_BUFFER_SIZE
Puppet::FFI::Windows::Functions::SC_ENUM_TYPE, Puppet::FFI::Windows::Functions::SC_STATUS_TYPE
Instance Method Summary
collapse
mainloop, #mainloop, #running?, #state
Instance Method Details
#log(msg, level) ⇒ Object
133
134
135
136
137
138
139
140
141
142
143
|
# File 'ext/windows/service/daemon.rb', line 133
def log(msg, level)
if LEVELS.index(level) >= @loglevel
if (@LOG_TO_FILE)
File.open(LOG_FILE, 'a:UTF-8') { |f| f.puts("#{Time.now} Puppet (#{level}): #{msg}") }
end
native_type, native_id = Puppet::Util::Windows::EventLog.to_native(level)
report_windows_event(native_type, native_id, msg.to_s)
end
end
|
#log_exception(e) ⇒ Object
Interrogation handler is just for debug. Can be commented out or removed entirely. def service_interrogate
log_debug('Service is being interrogated')
end
128
129
130
131
|
# File 'ext/windows/service/daemon.rb', line 128
def log_exception(e)
log_err(e.message)
log_err(e.backtrace.join("\n"))
end
|
#parse_log_level(puppet_path, cmdline_debug) ⇒ Object
178
179
180
181
182
183
184
185
186
187
188
189
190
191
|
# File 'ext/windows/service/daemon.rb', line 178
def parse_log_level(puppet_path,cmdline_debug)
begin
loglevel = %x{ #{puppet_path} config --section agent --log_level notice print log_level }.chomp
unless loglevel && respond_to?("log_#{loglevel}")
loglevel = :notice
log_err("Failed to determine loglevel, defaulting to #{loglevel}")
end
rescue Exception => e
log_exception(e)
loglevel = :notice
end
LEVELS.index(cmdline_debug ? cmdline_debug : loglevel.to_sym)
end
|
#parse_runinterval(puppet_path) ⇒ Object
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
# File 'ext/windows/service/daemon.rb', line 163
def parse_runinterval(puppet_path)
begin
runinterval = %x{ #{puppet_path} config --section agent --log_level notice print runinterval }.to_i
if runinterval == 0
runinterval = 1800
log_err("Failed to determine runinterval, defaulting to #{runinterval} seconds")
end
rescue Exception => e
log_exception(e)
runinterval = 1800
end
runinterval
end
|
#report_windows_event(type, id, message) ⇒ Object
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
# File 'ext/windows/service/daemon.rb', line 145
def report_windows_event(type,id,message)
begin
eventlog = nil
eventlog = Puppet::Util::Windows::EventLog.open("Puppet")
eventlog.report_event(
:event_type => type, :event_id => id, :data => message )
rescue Exception
ensure
if (!eventlog.nil?)
eventlog.close
end
end
end
|
#service_init ⇒ Object
29
30
|
# File 'ext/windows/service/daemon.rb', line 29
def service_init
end
|
#service_main(*argsv) ⇒ Object
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
# File 'ext/windows/service/daemon.rb', line 32
def service_main(*argsv)
argsv = (argsv << ARGV).flatten.compact
args = argsv.join(' ')
@loglevel = LEVELS.index(argsv.index('--debug') ? :debug : :notice)
@LOG_TO_FILE = (argsv.index('--logtofile') ? true : false)
if (@LOG_TO_FILE)
FileUtils.mkdir_p(File.dirname(LOG_FILE))
args = args.gsub("--logtofile","")
end
base_dir = File.expand_path(File.join(File.dirname(__FILE__), '..'))
load_env(base_dir)
puppet = File.join(base_dir, 'puppet', 'bin', 'puppet')
ruby = File.join(base_dir, 'puppet', 'bin', 'ruby.exe')
ruby_puppet_cmd = "\"#{ruby}\" \"#{puppet}\""
unless File.exist?(puppet)
log_err("File not found: '#{puppet}'")
return
end
log_debug("Using '#{puppet}'")
cmdline_debug = argsv.index('--debug') ? :debug : nil
@loglevel = parse_log_level(ruby_puppet_cmd, cmdline_debug)
log_notice('Service started')
service = self
@run_thread = Thread.new do
begin
while service.running? do
runinterval = service.parse_runinterval(ruby_puppet_cmd)
if service.state == RUNNING or service.state == IDLE
service.log_notice("Executing agent with arguments: #{args}")
pid = Process.create(:command_line => "#{ruby_puppet_cmd} agent --onetime #{args}", :creation_flags => CREATE_NEW_CONSOLE).process_id
service.log_debug("Process created: #{pid}")
else
service.log_debug("Service is paused. Not invoking Puppet agent")
end
service.log_debug("Service worker thread waiting for #{runinterval} seconds")
sleep(runinterval)
service.log_debug('Service worker thread woken up')
end
rescue Exception => e
service.log_exception(e)
end
end
@run_thread.join
rescue Exception => e
log_exception(e)
ensure
log_notice('Service stopped')
end
|
#service_pause ⇒ Object
111
112
113
|
# File 'ext/windows/service/daemon.rb', line 111
def service_pause
log_notice('Service pausing')
end
|
#service_resume ⇒ Object
115
116
117
|
# File 'ext/windows/service/daemon.rb', line 115
def service_resume
log_notice('Service resuming')
end
|
#service_shutdown ⇒ Object
119
120
121
|
# File 'ext/windows/service/daemon.rb', line 119
def service_shutdown
log_notice('Host shutting down')
end
|
#service_stop ⇒ Object
106
107
108
109
|
# File 'ext/windows/service/daemon.rb', line 106
def service_stop
log_notice('Service stopping / killing worker thread')
@run_thread.kill if @run_thread
end
|