forked from kbaseattic/trees
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglassfish_administer_service.py
executable file
·308 lines (281 loc) · 12.6 KB
/
glassfish_administer_service.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#!/usr/bin/env python
'''
Created on Dec 6, 2013
@author: [email protected]
'''
from __future__ import print_function
from argparse import ArgumentParser
import subprocess
import os
import xml.etree.ElementTree as ET
import urllib2
from subprocess import CalledProcessError
import sys
_PARALLEL_GC = "-XX:-UseParallelGC"
_PARALLEL_GC_ESC = "-XX\:-UseParallelGC"
def _parseArgs():
parser = ArgumentParser(description='script to administer a Glassfish ' +
' application.')
parser.add_argument('-w', '--war',
help='path to the application WAR file. If ' +
'omitted, the service at the port and domain is ' +
'stopped.')
parser.add_argument('-a', '--admin', required=True,
help='location of the Glassfish asadmin program.')
parser.add_argument('-d', '--domain', required=True,
help='name of the Glassfish domain where the ' +
'application is or will be installed.')
parser.add_argument('-l', '--domain-dir',
help='directory where the glassfish domain ' +
'information and logs will be stored. Defaults to ' +
'glassfish/domains.')
parser.add_argument('-p', '--port', required=True, type=int,
help='the port where the application runs.')
parser.add_argument('-t', '--threads', type=int, default=20,
help='the number of threads for the application.')
parser.add_argument('-s', '--Xms', type=int,
help='minimum memory for the domain in MB. ' +
'This will cause a domain restart if changed.')
parser.add_argument('-x', '--Xmx', type=int,
help='maximum memory for the domain in MB. ' +
'This will cause a domain restart if changed.')
parser.add_argument('-r', '--properties', nargs='*',
help='JVM system properties to add to the server.')
parser.add_argument('--set', nargs='*',
help='Set glassfish configuration attribute (ie asadmin set ...).')
parser.add_argument('-g', '--noparallelgc', action='store_true',
help='permanently turn off the parallel garbage ' +
' collector and use the standard gc.')
return parser.parse_args()
class CommandGlassfishDomain(object):
def __init__(self, asadminpath, domain, domainpath):
self.asadminpath = asadminpath
self.domain = domain
self.path = None
if (domainpath):
domaindir = os.path.abspath(os.path.expanduser(domainpath))
if not os.path.isdir(domaindir):
if not os.path.exists(domaindir):
os.mkdir(domaindir)
else:
print('Domain path ' + domainpath + ' must be a directory')
sys.exit(1)
self.path = domaindir
p = (' at ' + self.path) if(self.path) else ''
if self.exists():
print('Domain ' + self.domain + ' exists' + p +
', skipping creation')
else:
print('Creating domain ' + self.domain + p)
print(self._run_local_command('create-domain', '--nopassword=true',
self.domain).rstrip())
self.adminport = self.get_admin_port()
self.start_domain()
def get_admin_port(self):
#the fact I have to do this is moronic
if (self.path):
domains = self.path
else:
bindir = os.path.dirname(self.asadminpath)
glassfish = os.path.join(bindir, "..")
domains = os.path.join(glassfish, "domains")
domain = os.path.join(domains, self.domain)
configfile = os.path.join(domain, "config/domain.xml")
xml = ET.parse(configfile)
root = xml.getroot()
config = root.findall("./configs/config[@name='server-config']")[0]
adminlist = config.findall(
"./network-config/network-listeners/network-listener[@protocol=" +
"'admin-listener']")[0]
return adminlist.attrib['port']
def start_domain(self):
if self.is_running():
print ("Domain " + self.domain + " is already running on port " +
self.adminport)
else:
print("Starting domain " + self.domain)
print(self._run_local_command('start-domain', self.domain)
.rstrip())
self.adminport = self.get_admin_port()
def restart_domain(self):
if self.is_running():
print("Restarting " + self.domain + ", please wait")
print(self._run_local_command('restart-domain', self.domain)
.rstrip())
else:
self.start_domain()
def exists(self):
return self.domain in self._list_domains()
def is_running(self):
return self.domain + " running" in self._list_domains()
def start_service(self, war, port, threads):
portstr = str(port)
threadstr = str(threads)
if 'server-' + portstr in self._run_remote_command(
'list-virtual-servers'):
print("Virtual server already exists")
else:
print(self._run_remote_command(
'create-virtual-server', '--hosts',
'${com.sun.aas.hostName}', 'server-' + portstr).rstrip())
if 'thread-pool-' + portstr in self._run_remote_command(
'list-threadpools', 'server'):
print("Threadpool already exists")
else:
print(self._run_remote_command(
'create-threadpool', '--maxthreadpoolsize=' + threadstr,
'--minthreadpoolsize=' + threadstr, 'thread-pool-' + portstr)
.rstrip())
if 'http-listener-' + portstr in self._run_remote_command(
'list-http-listeners'):
print('Http listener already exists')
else:
print(self._run_remote_command(
'create-http-listener', '--listeneraddress', '0.0.0.0',
'--listenerport', portstr,
'--default-virtual-server', 'server-' + portstr,
'--securityEnabled=false', '--acceptorthreads=' + threadstr,
'http-listener-' + portstr).rstrip())
print(self._run_remote_command(
'set', 'server.network-config.network-listeners.' +
'network-listener.http-listener-' + portstr +
'.thread-pool=thread-pool-' + portstr).rstrip())
print(self._run_remote_command(
'set', 'server.network-config.protocols.protocol.' +
'http-listener-' + portstr + '.http.timeout-seconds=1800')
.rstrip())
if 'app-' + portstr in self._run_remote_command('list-applications'):
print(self._run_remote_command('undeploy', 'app-' + portstr)
.rstrip())
print(self._run_remote_command(
'deploy', '--virtualservers', 'server-' + portstr,
'--contextroot', '/', '--name', 'app-' + portstr, war).rstrip())
try:
urllib2.urlopen('http://localhost:' + portstr)
except urllib2.HTTPError as h:
resp = h.read()
else:
print('Unexpected response from server - the server did not ' +
'start up successfully. Please check the glassfish logs.')
return False
if '32603' in resp:
print('The server failed to start up successfully and is ' +
'running in protected mode. Please check the system and ' +
'glassfish logs.')
return False
elif '32300' in resp:
print('The server started successfully.')
return True
else:
print('The server failed to start up successfully and is not '
+ 'running. Please check the system and glassfish logs.')
return False
def stop_service(self, port):
portstr = str(port)
if 'app-' + portstr in self._run_remote_command('list-applications'):
print(self._run_remote_command('undeploy', 'app-' + portstr)
.rstrip())
if 'http-listener-' + portstr in self._run_remote_command(
'list-http-listeners'):
print(self._run_remote_command(
'delete-http-listener', 'http-listener-' + portstr).rstrip())
if 'http-listener-' + portstr in self._run_remote_command(
'list-protocols'):
print(self._run_remote_command(
'delete-protocol', 'http-listener-' + portstr).rstrip())
if 'thread-pool-' + portstr in self._run_remote_command(
'list-threadpools', 'server'):
print(self._run_remote_command(
'delete-threadpool', 'thread-pool-' + portstr).rstrip())
if 'server-' + portstr in self._run_remote_command(
'list-virtual-servers'):
print(self._run_remote_command(
'delete-virtual-server', 'server-' + portstr).rstrip())
def set_min_max_memory(self, minm, maxm):
# will restart the domain if changes are necessary
xmx = []
xms = []
for o in self._run_remote_command('list-jvm-options').split('\n'):
if o.startswith('-Xmx'):
xmx.append(o)
if o.startswith('-Xms'):
xms.append(o)
if (len(xms) > 1 and minm is None):
print('WARNING: multiple Xms parameters set on service: ' +
str(xms))
if (len(xmx) > 1 and maxm is None):
print('WARNING: multiple Xmx parameters set on service: ' +
str(xmx))
changed = self._set_memory(None if minm is None else '-Xms' +
str(minm) + 'm', xms)
changed2 = self._set_memory(None if maxm is None else '-Xmx'
+ str(maxm) + 'm', xmx)
if changed or changed2:
self.restart_domain()
def stop_parallel_gc(self):
for o in self._run_remote_command('list-jvm-options').split('\n'):
if o == _PARALLEL_GC:
return
self.create_jvm_option(_PARALLEL_GC_ESC)
self.restart_domain()
def create_property(self, prop):
print('Creating property ' + prop)
print(self._run_remote_command('create-system-properties', prop)
.rstrip())
def create_jvm_option(self, prop):
print('Creating jvm property ' + prop)
print(self._run_remote_command('create-jvm-options', prop)
.rstrip())
def set_glassfish_config_option(self, prop):
print('Setting glassfish configuration option ' + prop)
print(self._run_remote_command('set', prop)
.rstrip())
def _set_memory(self, memstr, memlist):
if (memstr is not None and [memstr] != memlist):
print("Removing options " + str(memlist))
for o in memlist:
self._remove_option(o)
print("Setting option " + memstr)
self._set_option(memstr)
return True
else:
return False
def _set_option(self, opt):
self._run_remote_command('create-jvm-options', opt)
def _remove_option(self, opt):
self._run_remote_command('delete-jvm-options', opt)
def _list_domains(self):
return self._run_local_command('list-domains')
def _run_local_command(self, subcmd, *args):
cmd = [self.asadminpath, subcmd]
if (self.path):
cmd.extend(['--domaindir', self.path])
try:
return subprocess.check_output(cmd + list(args))
except CalledProcessError as cpe:
print(cpe.output.rstrip())
sys.exit(1)
def _run_remote_command(self, *cmd):
try:
return subprocess.check_output([self.asadminpath, '-p',
self.adminport] + list(cmd))
except CalledProcessError as cpe:
print(cpe.output.rstrip())
sys.exit(1)
if __name__ == '__main__':
args = _parseArgs()
gf = CommandGlassfishDomain(args.admin, args.domain, args.domain_dir)
if (args.war == None):
gf.stop_service(args.port)
else:
if (args.noparallelgc):
gf.stop_parallel_gc()
gf.set_min_max_memory(args.Xms, args.Xmx)
for p in args.properties:
gf.create_property(p)
if args.set is not None:
for s in args.set:
gf.set_glassfish_config_option(s)
success = gf.start_service(args.war, args.port, args.threads)
if not success:
sys.exit(1)