Thursday, December 17, 2009

python to start, stop, deploy and undeploy application

import sys
import os
from java.lang import System

import getopt

def usage():
print "Usage:"
print "java weblogic.WLST app_ctl.py [-n] -u user -p password -a url -m moduleName -c command -d sourcedir -t targets -s stagemode"
try:
opts, args = getopt.getopt(sys.argv[1:], "nu:p:a:m:c:d:t:s:",
["user=", "password=", "url=",
"moduleName=", "command=", "soucedir", "targets", "stagemode"])
except getopt.GetoptError, err:
print str(err)
usage()
#sys.exit(2)

reallyDoIt = true
user=None
password=None
url=None
moduleName=None
command=None
sourcedir=None
targets=None
stagemode=None

for opt, arg in opts:
if opt == "-n":
reallyDoIt = false
elif opt == "-u":
user = arg
elif opt == "-p":
password = arg
elif opt == "-a":
url = arg
elif opt == "-m":
moduleName = arg
elif opt == "-c":
command = arg
elif opt == "-d":
sourcedir = arg
elif opt == "-t":
targets = arg
elif opt == "-s":
stagemode = arg

if not user:
print "Missing \"-u user\" parameter."
usage()
#sys.exit(2)
elif not password:
print "Missing \"-p password\" parameter."
usage()
#sys.exit(2)
elif not url:
print "Missing \"-a url\" parameter."
usage()
#sys.exit(2)
elif not moduleName:
print "Missing \"-m moduleName\" parameter."
usage()
#sys.exit(2)
elif not command:
print "Missing \"-c command\" parameter."
usage()
#sys.exit(2)

print "Got all the required parameters."

#######################
if command == 'stop':
connect(user, password, url)
print 'Starting stop of '+moduleName+'...'
progress='null'
try:
cd("/AppDeployments/"+moduleName)
progress=stopApplication(moduleName)
print 'Checking stop status...'
state = progress.getState()
if state == 'completed':
print 'OK: Stop of '+moduleName+' was successful...'
else:
print 'ERROR: Stop of '+moduleName+' was not successful...'
except :
print 'Application '+moduleName+' does not exist.'
elif command == 'start':
connect(user, password, url)
print 'Starting start of '+moduleName+'...'
progress='null'
try:
cd("/AppDeployments/"+moduleName)
progress=startApplication(moduleName)
print 'Checking start status...'
state = progress.getState()
if state == 'completed':
print 'OK: Start of '+moduleName+' was successful...'
else:
print 'ERROR: Start of '+moduleName+' was not successful...'
except :
print 'Application '+moduleName+' does not exist.'
elif command == 'deploy':
connect(user, password, url)
print 'Starting deploy of '+moduleName+'...'
progress='null'
try:
#cd("/AppDeployments/"+moduleName)
progress=deploy(appName=moduleName, path=sourcedir, targets=targets, stageMode=stagemode)
print 'Checking deploy status...'
state = progress.getState()
if state == 'completed':
print 'OK: Deploy of '+moduleName+' was successful...'
else:
print 'ERROR: Deploy of '+moduleName+' was not successful...'
except :
print 'Application '+moduleName+' does not exist.'
elif command == 'undeploy':
connect(user, password, url)
print 'Starting undeploy of '+moduleName+'...'
progress='null'
try:
#cd("/AppDeployments/"+moduleName)
progress=undeploy(moduleName)
print 'Checking undeploy status...'
state = progress.getState()
if state == 'completed':
print 'OK: Undeploy of '+moduleName+' was successful...'
else:
print 'ERROR: Undeploy of '+moduleName+' was not successful...'
except :
print 'Application '+moduleName+' does not exist.'

################################
#
# stopProgress='null'
# stopProgress=stopApplication("spd")
# print 'Checking stop status...'
# stopProgress.getState()
#
#if stopProgress.getState == 'completed':
# print "Stop successful..."
#else:
# print "ERROR: stop spd not successful..."
#
#elif action == "start":
#
# connect(uname, pwd, url)
# startProgress='null'
# startProgress=startApplication('spd')
# print 'Checking start status...'
# startProgress.getState()

1 comment: