WLST script to create user and assign to group
In this blog post we are going to write a WLST script, which will connect to the running admin server and then create a user and assign the user to a group. The following code can be downloaded from the following link:
1. The first step is to create a properties file and name it as domain.properties. We are going to add the values to this properties file and the WLST script is going to read the values from properties file and will take action.
#***************************************************** # Security Info #***************************************************** ADMIN_USERNAME=weblogic ADMIN_PASSWORD=welcome1 ADMIN_URL=t3://192.168.1.9:7001 DOMAIN_NAME=wl_server #***************************************************** # Server Info #***************************************************** SERVER_NAME=AdminServer #***************************************************** # User Info #***************************************************** NEW_USER=togotutordemouser USER_PASSWD=togotutordemouser123 USER_DESCRIPTION=Weblogic Administrator USER_GROUP=Administrators
2. In the next step we are going to write a utility to read the properties file in a given format
#! /usr/bin/python
#****************************************************************************
# File: utils.py
#****************************************************************************
from java.lang import System
#****************************************************************************
# def notNullOrEmpty()
#****************************************************************************
def notNullOrEmpty(val):
return val != None and len(val) != 0
#****************************************************************************
# def substitute()
#****************************************************************************
def substitute(orig, subs):
startIndex = orig.find('${')
#print 'startIndex =', startIndex
if startIndex == -1:
return orig
endIndex = orig.find('}', startIndex)
#print 'endIndex =', endIndex
if endIndex == -1:
return orig
substitutionKey = orig[startIndex + 2:endIndex]
value = System.getProperty(substitutionKey)
if subs.has_key(substitutionKey):
value = subs[substitutionKey]
if value != None:
return orig[0:startIndex] + value + substitute(orig[endIndex + 1:], subs)
if len(orig) == endIndex + 1:
print 'WARNING: No substitution found for key:', orig
return orig
else:
return orig[0:endIndex + 1] + substitute(orig[endIndex + 1], subs)
#****************************************************************************
# def loadOrderedProps
#****************************************************************************
def loadOrderedProps(filename):
def split(line):
index = line.find('=')
if index == -1:
return None
else:
return (line[:index], line[index + 1:])
propLines = open(filename, 'r').readlines()
propLines = [line.strip() for line in propLines]
propLines = [line for line in propLines if len(line) > 0 and not line.startswith('#')]
propLines = [split(line) for line in propLines]
propLines = [line for line in propLines if line]
return propLines
#****************************************************************************
# def loadProperties()
#****************************************************************************
def loadProperties(propFile):
props = {}
originalProps = loadOrderedProps(propFile)
for key, value in originalProps:
#print ' KEY: [' + key + ']'
value = substitute(value, props)
#print ' VALUE: [' + value + ']'
props[key] = value
return props
#****************************************************************************
# def getValue
#****************************************************************************
def getValue(propKey, defaultKey, properties):
if properties.get(propKey) != None:
val = properties[propKey]
else:
if properties.get(defaultKey) != None:
val = properties[defaultKey]
else:
val = ''
return val
#****************************************************************************
# def getKeyValue
#****************************************************************************
def getKeyValue(propkey, properties):
if properties.get(propkey) != None:
val = properties[propkey]
else:
val = ''
return val
3. This is the step where we are going to write the WLST online script to create a user and assign that user to a group.
#################################################
#
# Name : Create User WLST Script
# Author : Togotutor
# Copyright ©2012 www.togotutor.com
#
################################################
"""
Connection To Admin Server
"""
def __connectToAdmin(properties):
adminusername = getKeyValue('ADMIN_USERNAME', properties)
adminpassword = getKeyValue('ADMIN_PASSWORD', properties)
adminurl = getKeyValue('ADMIN_URL', properties)
domainname = getKeyValue('DOMAIN_NAME', properties)
print '[INFO]: Connecting to the domain'+' '+domainname
try:
connect(adminusername, adminpassword, adminurl)
except WLSTException:
print '[ERROR]: Trying to connect to the domain:' + domainname
exit()
"""
DisConnect To Admin Server
"""
def __disconnectToAdmin():
disconnect()
"""
Create a User in Weblogic
"""
def __createuser(properties):
serverConfig()
newuser = getKeyValue('NEW_USER', properties)
userpassword = getKeyValue('USER_PASSWD', properties)
userdescription = getKeyValue('USER_DESCRIPTION', properties)
usergroup = getKeyValue('USER_GROUP', properties)
getSecurityConfiguration=cmo.getSecurityConfiguration().getDefaultRealm().lookupAuthenticationProvider("DefaultAuthenticator")
print '[INFO]: Creating a weblogic user:'+' '+newuser
print '[INFO]: Assinging user'+' '+newuser+' '+'to group'+' '+usergroup
try:
getSecurityConfiguration.createUser(newuser,userpassword,userdescription)
getSecurityConfiguration.addMemberToGroup(usergroup,newuser)
except WLSTException:
print '[ERROR]: Creating the user:'+newuser
exit()
#****************************************************************************
# MAIN
#****************************************************************************
#
# Calling all the Methods here
print(' -------------------------- ')
print('[INFO] Printing the output')
print('')
execfile('utils.py')
propFilename = 'domain.properties'
properties = {}
properties = loadProperties(propFilename)
__connectToAdmin(properties)
__createuser(properties)
__disconnectToAdmin()
print ''
print '[INFO]: Quitting the WLST script'
print(' -------------------------- ')
print '[INFO] Completed.'


