/
service_wrapper.py
250 lines (199 loc) · 7.6 KB
/
service_wrapper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
'''
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
'''
import os
import optparse
import sys
import win32serviceutil
import win32api
import win32event
import win32service
from ambari_commons.ambari_service import AmbariService, ENV_PYTHON_PATH
from ambari_commons.exceptions import *
from ambari_commons.logging_utils import *
from ambari_commons.os_windows import WinServiceController
from ambari_commons.os_utils import find_in_path
from ambari_agent.AmbariConfig import AmbariConfig
from ambari_agent.HeartbeatHandlers import HeartbeatStopHandlers
AMBARI_VERSION_VAR = "AMBARI_VERSION_VAR"
SETUP_ACTION = "setup"
START_ACTION = "start"
STOP_ACTION = "stop"
RESET_ACTION = "reset"
STATUS_ACTION = "status"
DEBUG_ACTION = "debug"
def parse_options():
# parse env cmd
with open(os.path.join(os.getcwd(), "ambari-env.cmd"), "r") as env_cmd:
content = env_cmd.readlines()
for line in content:
if line.startswith("set"):
name, value = line[4:].split("=")
os.environ[name] = value.rstrip()
# checking env variables, and fallback to working dir if no env var was founded
if not os.environ.has_key("AMBARI_AGENT_CONF_DIR"):
os.environ["AMBARI_AGENT_CONF_DIR"] = os.getcwd()
if not os.environ.has_key("AMBARI_AGENT_LOG_DIR"):
os.environ["AMBARI_AGENT_LOG_DIR"] = os.path.join("\\", "var", "log", "ambari-agent")
if not os.path.exists(os.environ["AMBARI_AGENT_LOG_DIR"]):
os.makedirs(os.environ["AMBARI_AGENT_LOG_DIR"])
if not os.environ.has_key("PYTHON_EXE"):
os.environ["PYTHON_EXE"] = find_in_path("python.exe")
class AmbariAgentService(AmbariService):
AmbariService._svc_name_ = "Ambari Agent"
AmbariService._svc_display_name_ = "Ambari Agent"
AmbariService._svc_description_ = "Ambari Agent"
AmbariService._AdjustServiceVersion()
heartbeat_stop_handler = None
# Adds the necessary script dir to the Python's modules path
def _adjustPythonPath(self, current_dir):
iPos = 0
python_path = os.path.join(current_dir, "sbin")
sys.path.insert(iPos, python_path)
# Add the alerts and apscheduler subdirs to the path, for the imports to work correctly without
# having to modify the files in these 2 subdirectories
agent_path = os.path.join(current_dir, "sbin", "ambari_agent")
iPos += 1
sys.path.insert(iPos, agent_path)
for subdir in os.listdir(agent_path):
full_subdir = os.path.join(agent_path, subdir)
iPos += 1
sys.path.insert(iPos, full_subdir)
def SvcDoRun(self):
parse_options()
self.redirect_output_streams()
# Soft dependency on the Windows Time service
ensure_time_service_is_started()
self.heartbeat_stop_handler = HeartbeatStopHandlers(AmbariAgentService._heventSvcStop)
self.ReportServiceStatus(win32service.SERVICE_RUNNING)
from ambari_agent import main
main.main(self.heartbeat_stop_handler)
def _InitOptionsParser(self):
return init_options_parser()
def redirect_output_streams(self):
self._RedirectOutputStreamsToFile(AmbariConfig.getOutFile())
pass
def ensure_time_service_is_started():
ret = WinServiceController.EnsureServiceIsStarted("W32Time")
if 0 != ret:
raise FatalException(-1, "Error starting Windows Time service: " + str(ret))
pass
def ctrlHandler(ctrlType):
AmbariAgentService.DefCtrlCHandler()
return True
def svcsetup():
AmbariAgentService.set_ctrl_c_handler(ctrlHandler)
AmbariAgentService.Install()
pass
#
# Starts the Ambari Agent as a service.
# Start the Agent in normal mode, as a Windows service. If the Ambari Agent is
# not registered as a service, the function fails. By default, only one instance of the service can
# possibly run.
#
def svcstart(options):
if 0 != AmbariAgentService.Start(15):
options.exit_message = None
pass
#
# Stops the Ambari Agent.
#
def svcstop(options):
if 0 != AmbariAgentService.Stop():
options.exit_message = None
#
# The Ambari Agent status.
#
def svcstatus(options):
options.exit_message = None
statusStr = AmbariAgentService.QueryStatus()
print "Ambari Agent is " + statusStr
def svcdebug(options):
sys.frozen = 'windows_exe' # Fake py2exe so we can debug
AmbariAgentService.set_ctrl_c_handler(ctrlHandler)
win32serviceutil.HandleCommandLine(AmbariAgentService, options)
def init_options_parser():
parser = optparse.OptionParser(usage="usage: %prog action [options]", )
parser.add_option('-r', '--hostname', dest="host_name", default="localhost",
help="Use specified Ambari server host for registration.")
parser.add_option('-j', '--java-home', dest="java_home", default=None,
help="Use specified java_home. Must be valid on all hosts")
parser.add_option("-v", "--verbose",
action="store_true", dest="verbose", default=False,
help="Print verbose status messages")
parser.add_option("-s", "--silent",
action="store_true", dest="silent", default=False,
help="Silently accepts default prompt values")
parser.add_option('--jdbc-driver', default=None,
help="Specifies the path to the JDBC driver JAR file for the " \
"database type specified with the --jdbc-db option. Used only with --jdbc-db option.",
dest="jdbc_driver")
return parser
#
# Main.
#
def agent_main():
parser = init_options_parser()
(options, args) = parser.parse_args()
options.warnings = []
if len(args) == 0:
print parser.print_help()
parser.error("No action entered")
action = args[0]
possible_args_numbers = [1]
matches = 0
for args_number_required in possible_args_numbers:
matches += int(len(args) == args_number_required)
if matches == 0:
print parser.print_help()
possible_args = ' or '.join(str(x) for x in possible_args_numbers)
parser.error("Invalid number of arguments. Entered: " + str(len(args)) + ", required: " + possible_args)
options.exit_message = "Ambari Agent '%s' completed successfully." % action
try:
if action == SETUP_ACTION:
#TODO Insert setup(options) here upon need
svcsetup()
elif action == START_ACTION:
svcstart(options)
elif action == DEBUG_ACTION:
svcdebug(options)
elif action == STOP_ACTION:
svcstop(options)
elif action == STATUS_ACTION:
svcstatus(options)
else:
parser.error("Invalid action")
if options.warnings:
for warning in options.warnings:
print_warning_msg(warning)
pass
options.exit_message = "Ambari Agent '%s' completed with warnings." % action
pass
except FatalException as e:
if e.reason is not None:
print_error_msg("Exiting with exit code {0}. \nREASON: {1}".format(e.code, e.reason))
sys.exit(e.code)
except NonFatalException as e:
options.exit_message = "Ambari Agent '%s' completed with warnings." % action
if e.reason is not None:
print_warning_msg(e.reason)
if options.exit_message is not None:
print options.exit_message
if __name__ == '__main__':
try:
agent_main()
except (KeyboardInterrupt, EOFError):
print("\nAborting ... Keyboard Interrupt.")
sys.exit(1)