Assign username, package and environment in Jython script for XLDeploy Tasks

I am creating a generalized template that will be used by multiple applications within my company, and am creating the XLDeploy tasks dynamically via a Jython script, since values will differ for each application.

The scripts does create the XLDeploy tasks, but how can I update the environment, package ID and user name via scripting? There doesn’t seem to be a function or method that exposes those fields to a Jython script.

Are there specific calls that need to be made to populate these fields, or does this method simply not work via a script? Is there a better way to handle this situation than via a Jython script?

Thanks in advance for any suggestions.

Good morning,

depending on the version you are using, there is a better way since 9.x .
Using blueprints, you can actualy doing this by using yaml templates instead of Jython scripts.
https://docs.xebialabs.com/v.9.6/xl-release/concept/get-started-with-blueprints/

There are some public blueprints available as a starting point here:
https://dist.xebialabs.com/public/blueprints/

If you have older versions, it should also work with python, please see the following code examples (you have to reformat it, sorry copy and paste here destroys all formatting :slight_smile: )

from java.util import Properties
from java.util import HashSet

PROJECT_NAME = “Project_”
PREFIX = “Templates”
dictName = ‘SSH_Host’
propFileName = ‘C:/tmp/initial.properties’
PROD=1 # PROD =0 means script runs on a Dev/Test/QA System, PROD !=0 (e.g. 1) mean, the script runs on a PROD System

def defineRoles():
try:
security.assignRole(“Security”, [“Security”])
security.assignRole(“Business”, [“Business”])
security.assignRole(“DBA”, [“not assigned yet”])
security.assignRole(“DeployAdmin”, [“DeployAdmin_DEV, GL_XL_PR_DeployAdmin_PROD”])
security.assignRole(“Developer”, [“Developer”])
security.assignRole(“QA”, [“QA”])
security.assignRole(“ReleaseManager”, [“ReleaseManager”])
security.assignRole(“Tool Chain Admin”, [“ToolChainAdmin”])
if (PROD==0):
security.assignRole(“ServiceDelivery_DEV”, [“DEV”])
else:
security.assignRole(“ServiceDelivery_PROD”, [“PROD”])
except Exception as e:
print “something happened during security role creation:”
print str(e)

def createCommonDictionary(dictName, propFileName):
props = Properties()
props_encrypted = Properties()
file = open(propFileName, ‘r’)
file_encrypted= open(propFileName + ‘.encrypted’, ‘r’)
props.load(file)
props_encrypted.load(file_encrypted)
print “create common dictionary”
try:
myDict = repository.read(‘Environments/CommonDictionary/’ + dictName)
except:
print “New Dictionary ‘%s’. Creating a new item with ID ‘Environments/%s’ in the repository NOW” % (dictName, dictName)
myDict = factory.configurationItem(“Environments/CommonDictionary/” + dictName, “udm.Dictionary”)
repository.create(myDict)
myDict = repository.read(‘Environments/CommonDictionary/’ + dictName)

for prop in props.entrySet():
	print prop.key
	print prop.value
	oneKey = prop.key
	oneValue = prop.value
	myDict.entries[oneKey] = oneValue

for prop in props_encrypted.entrySet():
	print prop.key
	print prop.value
	oneKey = prop.key
	oneValue = prop.value
	myDict.encryptedEntries[oneKey] = oneValue
repository.update(myDict)
return myDict

def createSshHost(id):
try:
host = repository.read(id)
print “host with " + id + " already exists - skip creation”
return host
except:
print “host with " + id + " does not exist - execute creation”
values = {‘address’:‘FQDN’,‘os’:‘UNIX’,‘connectionType’:‘SUDO’,‘username’:’{{CONNECTION_USER_NAME}}’,‘privateKeyFile’:’{{PRIVATE_KEY_FILE}}’,‘port’:‘22’, ‘temporaryDirectoryPath’:’/tmp’,‘passphrase’:’{{KEY_PASS_PHRASE}}’,‘sudoUsername’:‘root’}
host = factory.configurationItem(id,‘overthere.SshHost’, values)
repository.create(host)
return host

def createEnvironment(id):
try:
env = repository.read(id)
print “env with " + id + " already exists - skip creation”
return env
except:
print “env with " + id + " does not exist - execute creation”
env = factory.configurationItem(id,‘udm.Environment’, {})
repository.create(env)
return env

def createDirectory(id):
try:
dir = repository.read(id)
print “directory with " + id + " already exists - skip creation”

except:
	print "directory with " + id + " does not exist - execute creation"
	repository.create(factory.configurationItem(id,'core.Directory', {}))

def createApplication(id):
try:
app = repository.read(id)
print “app with " + id + " already exists - skip creation”
return app
except:
print “app with " + id + " not existing - execute creation”
app = factory.configurationItem(id,‘udm.Application’,{})
repository.create(app)
return app

def createPipeline(id, orderedList):
try:
pipeline = repository.read(id)
print “pipeline with id " + id + " already exists - skip creation”
return pipeline
except:
print “pipeline with id " + id + " does not exist - execute creation”
pipeline = factory.configurationItem(id,‘release.DeploymentPipeline’, orderedList)
repository.create(pipeline)
return pipeline

print "start building project templates

print “building the initial configuratiom item tree”
#create directories for Infrasctructure
print “creating directories for Environments”
createDirectory(‘Environments/CommonDictionary’)
createDirectory(‘Environments/’ + PREFIX )
createDirectory(‘Environments/’ + PREFIX + ‘/’ + PROJECT_NAME + ‘Environment’)
if (PROD==0):
createDirectory(‘Environments/’ + PREFIX + ‘/’ + PROJECT_NAME + ‘Environment’ + ‘/01-Dev’)
createDirectory(‘Environments/’ + PREFIX + ‘/’ + PROJECT_NAME + ‘Environment’ + ‘/02-Test’)
createDirectory(‘Environments/’ + PREFIX + ‘/’ + PROJECT_NAME + ‘Environment’ + ‘/03-QA’)
else:
createDirectory(‘Environments/’ + PREFIX + ‘/’ + PROJECT_NAME + ‘Environment’ + ‘/04-PROD’)

print “** creatingCommonDictionary”
commonDictionary = createCommonDictionary(dictName, propFileName)

print “** creating directories for Infrastructure”
createDirectory(‘Infrastructure/’ + PREFIX )
createDirectory(‘Infrastructure/’ + PREFIX + ‘/’ + PROJECT_NAME + ‘Infrastructure’)
if (PROD==0):
createDirectory(‘Infrastructure/’ + PREFIX + ‘/’ + PROJECT_NAME + ‘Infrastructure’ + ‘/01-Dev’)
createDirectory(‘Infrastructure/’ + PREFIX + ‘/’ + PROJECT_NAME + ‘Infrastructure’ + ‘/02-Test’)
createDirectory(‘Infrastructure/’ + PREFIX + ‘/’ + PROJECT_NAME + ‘Infrastructure’ + ‘/03-QA’)
else:
createDirectory(‘Infrastructure/’ + PREFIX + ‘/’ + PROJECT_NAME + ‘Infrastructure’ + ‘/04-PROD’)

print “** creating directories for Applications and create Template (empty) Application”
createDirectory(‘Applications/’ + PREFIX )
createDirectory(‘Applications/’ + PREFIX + ‘/’ + PROJECT_NAME + ‘App’)
app = createApplication(‘Applications/’ + PREFIX + ‘/’ + PROJECT_NAME + ‘App’ + ‘/SampleDemoApp’)

print “** creating template ssh hosts”
if (PROD==0):
devHost = createSshHost(‘Infrastructure/’ + PREFIX + ‘/’ + PROJECT_NAME + ‘Infrastructure’ +’/01-Dev’ + ‘/Dev-Machine’)
testHost = createSshHost(‘Infrastructure/’ + PREFIX + ‘/’ + PROJECT_NAME + ‘Infrastructure’ +’/02-Test’ + ‘/Test-Machine’)
qaHost = createSshHost(‘Infrastructure/’ + PREFIX + ‘/’ + PROJECT_NAME + ‘Infrastructure’ +’/03-QA’ + ‘/QA-Machine’)
else:
prodHost = createSshHost(‘Infrastructure/’ + PREFIX + ‘/’ + PROJECT_NAME + ‘Infrastructure’ +’/04-PROD’ + ‘/PROD-Machine’)

print “creating Environments”

if (PROD==0):
devEnvironment = createEnvironment(‘Environments/’ + PREFIX + ‘/’ + PROJECT_NAME + ‘Environment’ +’/01-Dev’ + ‘/Dev’)
devEnvironment.members = [devHost.id]
devEnvironment.dictionaries = [commonDictionary.id]
repository.update(devEnvironment)

testEnvironment = createEnvironment('Environments/' + PREFIX  + '/' + PROJECT_NAME + 'Environment' +'/02-Test' + '/Test')
testEnvironment.members = [testHost.id]
devEnvironment.dictionaries = [commonDictionary.id]
repository.update(testEnvironment)

qaEnvironment = createEnvironment('Environments/' + PREFIX  + '/' + PROJECT_NAME + 'Environment' +'/03-QA' + '/QA')
qaEnvironment.members = [qaHost.id]
qaEnvironment.dictionaries = [commonDictionary.id]
repository.update(qaEnvironment)

else:
prodEnvironment = createEnvironment(‘Environments/’ + PREFIX + ‘/’ + PROJECT_NAME + ‘Environment’ +’/04-PROD’ + ‘/PROD’)
prodEnvironment.members = [prodHost.id]
prodEnvironment.dictionaries = [commonDictionary.id]
repository.update(prodEnvironment)

print “** creating Pipeline”

createDirectory(‘Configuration/Pipelines’)

if (PROD==0):
pipeline = createPipeline(‘Configuration/Pipelines/99-Master-Release-Pipeline-NON-PROD’, {“pipeline”:[devEnvironment.id, testEnvironment.id,qaEnvironment.id]})
else:
pipeline = createPipeline(‘Configuration/Pipelines/99-Master-Release-Pipeline-PROD’, {“pipeline”:[prodEnvironment.id ]})

print “** Attach pipeline to Application”
app.pipeline = pipeline.id
repository.update(app)

print “**define Roles, Rights and Permissions”
defineRoles()

print “*** setup finished ***”

print “*** Create a sample project "
createDirectory(‘Infrastructure/Departments’)
createDirectory(‘Infrastructure/Departments/PR’)
print "
copy infrastructure config”
repository.copy(‘Infrastructure/Templates/Project_Infrastructure’,‘Infrastructure/Departments/PR/CSB’)

print “*** copy environment config as a starting point”
createDirectory(‘Environments/Departments’)
createDirectory(‘Environments/Departments/PR’)
repository.copy(‘Environments/Templates/Project_Environment’,‘Environments/Departments/PR/CSB’)