-
Notifications
You must be signed in to change notification settings - Fork 6
/
websphere.py
221 lines (193 loc) · 8.41 KB
/
websphere.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
import sys
import getopt
import time
import traceback
host = r"{{host}}"
cell = r"{{cell}}"
cluster = r"{{cluster}}"
server = r"{{server}}"
webservers = r"{{webservers}}"
node = r"{{node}}"
applicationName = r"{{applicationName}}"
contextRoot = r"{{contextRoot}}"
virtualHost = r"{{virtualHost}}"
sharedLibs = r"{{sharedLibs}}"
parentLast = r"{{parentLast}}"
webModuleParentLast = r"{{webModuleParentLast}}"
packageFile = r"{{packageFile}}"
restartAfterDeploy = r"{{restartAfterDeploy}}"
deployOptions = r"{{deployOptions}}"
class WebSphere:
def listApplications(self):
print "[LIST APPLICATIONS]", host
print time.strftime("%Y-%b-%d %H:%M:%S %Z")
print AdminApp.list()
def restartServer(self):
print '-' * 60
print "[RESTARTING SERVER]", host
print time.strftime("%Y-%b-%d %H:%M:%S %Z")
print '-' * 60
try:
if "" != cluster:
appManager = AdminControl.queryNames('name=' + cluster + ',type=Cluster,process=dmgr,*')
print AdminControl.invoke(appManager, 'rippleStart')
print ""
print '=' * 60
print "[NOTICE]: It takes a few minutes for the cluster to fully back running after the build finished!"
print '=' * 60
print ""
elif "" != node:
appManager = AdminControl.queryNames('node=' + node + ',type=Server,process=' + server + ',*')
print AdminControl.invoke(appManager, 'restart')
else:
appManager = AdminControl.queryNames('type=Server,process=' + server + ',*')
print AdminControl.invoke(appManager, 'restart')
except:
print "FAILED to restart cluster/server:"
print '-' * 10
traceback.print_exc(file=sys.stdout)
print '-' * 10
print "try to startApplication directly..."
self.startApplication()
def startApplication(self):
print '-' * 60
print "[STARTING APPLICATION]", host, applicationName
print time.strftime("%Y-%b-%d %H:%M:%S %Z")
print '-' * 60
try:
if "" != cluster:
print AdminApplication.startApplicationOnCluster(applicationName, cluster)
elif "" != node:
appManager = AdminControl.queryNames('node=' + node + ',type=ApplicationManager,process=' + server + ',*')
print AdminControl.invoke(appManager, 'startApplication', applicationName)
else:
appManager = AdminControl.queryNames('type=ApplicationManager,process=' + server + ',*')
print AdminControl.invoke(appManager, 'startApplication', applicationName)
except:
print "FAILED to start application:"
print '-' * 10
traceback.print_exc(file=sys.stdout)
print '-' * 10
raise
def stopApplication(self):
print '-' * 60
print "[STOPPING APPLICATION]", host, applicationName
print time.strftime("%Y-%b-%d %H:%M:%S %Z")
print '-' * 60
try:
if "" != cluster:
print AdminApplication.stopApplicationOnCluster(applicationName, cluster)
elif "" != node:
appManager = AdminControl.queryNames('node=' + node + ',type=ApplicationManager,process=' + server + ',*')
print AdminControl.invoke(appManager, 'stopApplication', applicationName)
else:
appManager = AdminControl.queryNames('type=ApplicationManager,process=' + server + ',*')
print AdminControl.invoke(appManager, 'stopApplication', applicationName)
except:
print "FAILED to stop application:"
print '-' * 10
traceback.print_exc(file=sys.stdout)
print '-' * 10
raise
def installApplication(self):
print '-' * 60
print "[INSTALLING APPLICATION]", host, applicationName
print time.strftime("%Y-%b-%d %H:%M:%S %Z")
print '-' * 60
options = ['-distributeApp', '-appname', applicationName]
if "" != deployOptions:
options[:0] = deployOptions.split()
try:
if "" != cluster:
serverMapping = 'WebSphere:cluster=' + cluster
if "" != webservers:
for webserver in webservers.split(','):
serverMapping += '+WebSphere:server=' + webserver.strip()
options += ['-cluster', cluster, '-MapModulesToServers', [['.*', '.*', serverMapping]]]
else:
serverMapping = 'WebSphere:server=' + server
if "" != webservers:
for webserver in webservers.split(','):
serverMapping += '+WebSphere:server=' + webserver.strip()
options += ['-MapModulesToServers', [['.*', '.*', serverMapping]]]
if "" != contextRoot:
options += ['-contextroot', contextRoot]
if "" != virtualHost:
options += ['-MapWebModToVH', [['.*', '.*', virtualHost]]]
if "" != sharedLibs:
options += ['-MapSharedLibForMod', [['.*', '.*', sharedLibs.replace(',', '+')]]]
print ""
print "options: ", options
print ""
print "INSTALLING"
print AdminApp.install(packageFile, options)
self.__modifyClassloader()
print "SAVING CONFIG"
AdminConfig.save()
if "" != cluster:
print "SYNCING"
AdminNodeManagement.syncActiveNodes()
result = AdminApp.isAppReady(applicationName)
while result == "false":
print "STATUS:", AdminApp.getDeployStatus(applicationName)
time.sleep(5)
result = AdminApp.isAppReady(applicationName)
print "INSTALLED", applicationName
except:
print "FAILED to install application: ", applicationName
print '-' * 10
traceback.print_exc(file=sys.stdout)
print '-' * 10
raise
def __modifyClassloader(self):
deployments = AdminConfig.getid("/Deployment:" + applicationName + "/")
deploymentObject = AdminConfig.showAttribute(deployments, "deployedObject")
if "true" == parentLast:
# AdminApplication.configureClassLoaderLoadingModeForAnApplication(applicationName, "PARENT_LAST")
classloader = AdminConfig.showAttribute(deploymentObject, "classloader")
AdminConfig.modify(classloader, [['mode', 'PARENT_LAST']])
if "true" == webModuleParentLast:
modules = AdminConfig.showAttribute(deploymentObject, "modules")
arrayModules = modules[1:len(modules) - 1].split(" ")
for module in arrayModules:
if module.find('WebModuleDeployment') != -1:
AdminConfig.modify(module, [['classloaderMode', 'PARENT_LAST']])
def uninstallApplication(self):
print '-' * 60
print "[UNINSTALLING APPLICATION]", host, applicationName
print time.strftime("%Y-%b-%d %H:%M:%S %Z")
print '-' * 60
try:
print AdminApp.uninstall(applicationName)
AdminConfig.save()
if "" != cluster:
AdminNodeManagement.syncActiveNodes()
except:
print "FAILED to uninstall application: ", applicationName
print '-' * 10
traceback.print_exc(file=sys.stdout)
print '-' * 10
raise
def isApplicationInstalled(self):
return AdminApplication.checkIfAppExists(applicationName)
def deploy(self):
if "true" == self.isApplicationInstalled():
self.uninstallApplication()
self.installApplication()
if "true" == restartAfterDeploy:
self.restartServer()
else:
print "Starting application instead of restarting server..."
self.startApplication()
print '-' * 60
print "[FINISHED]", host, applicationName
print time.strftime("%Y-%b-%d %H:%M:%S %Z")
print '-' * 60
# -----------------------------------------------------------------
# Main
# -----------------------------------------------------------------
if __name__ == "__main__":
methods, args = getopt.getopt(sys.argv, 'o:')
for name, method in methods:
if name == "-o":
getattr(WebSphere(), method)()