-
Language:
English
-
Language:
English
Red Hat Training
A Red Hat training course is available for Red Hat JBoss Operations Network
6. Example: Scripting Resource Deployments
- Find all JBoss EAP instances for a specified JBoss ON group.
- Shut down each EAP instance.
- Update binaries for existing deployed applications or create new deployments.
- Restart the EAP instance.
- End the loop.
Note
6.1. Scripting JBoss AS 4 Deployments
This script will use two custom functions to deploy the packages to create new resources.
function usage() { println("Usage: deployToGroup <fileName> <groupName>"); throw "Illegal arguments"; } function PackageParser(fullPathName) { var file = new java.io.File(fullPathName); var fileName = file.getName(); var packageType = fileName.substring(fileName.lastIndexOf('.')+1); var tmp = fileName.substring(0, fileName.lastIndexOf('.')); var realName = tmp.substring(0, tmp.lastIndexOf('-')); var version = tmp.substring(tmp.lastIndexOf('-') + 1); var packageName = realName + "." + packageType; this.packageType = packageType.toLowerCase(); this.packageName = packageName; this.version = version; this.realName = realName; }
The script should have two command-line parameters. The first should be the path of the new application that is installed in the group. The second is the name of the group itself. These parameters are parsed in the script (as described in more detail in Section 4.4, “Passing Script Arguments in the JBoss ON CLI”).
if( args.length < 2 ) usage(); var fileName = args[0]; var groupName = args[1];
// check that the file exists and that we can read it var file = new java.io.File(fileName); if( !file.exists() ) { println(fileName + " does not exist!"); usage(); } if( !file.canRead() ) { println(fileName + " can't be read!"); usage(); }
// find resource group var rgc = new ResourceGroupCriteria(); rgc.addFilterName(groupName); rgc.fetchExplicitResources(true); var groupList = ResourceGroupManager.findResourceGroupsByCriteria(rgc);
rgc.fetchExplicitResources(true);
if( groupList == null || groupList.size() != 1 ) { println("Can't find a resource group named " + groupName); usage(); } var group = groupList.get(0); println(" Found group: " + group.name ); println(" Group ID : " + group.id ); println(" Description: " + group.description);
if( group.explicitResources == null || group.explicitResources.size() == 0 ) { println(" Group does not contain explicit resources --> exiting!" ); usage(); } var resourcesArray = group.explicitResources.toArray();
resourceArray
now contains all resources which are part of the group. Next, check if there are JBoss AS 4 Server instances which need to be restarted before the application is deployed.
for( i in resourcesArray ) { var res = resourcesArray[i]; var resType = res.resourceType.name; println(" Found resource " + res.name + " of type " + resType + " and ID " + res.id); if( resType != "JBossAS Server") { println(" ---> Resource not of required type. Exiting!"); usage(); } // get server resource to start/stop it and to redeploy application var server = ProxyFactory.getResource(res.id); }
server
contains the JBossAS instance. This requires re-reading the server because it needs to be fully populated. Internally, the CLI is using simple JPA persistence, and it is necessary to not always fetch all dependent objects.
var children = server.children; for( c in children ) { var child = children[c]; if( child.name == packageName ) { } }
packageName
is the name of the application without version information and path as shown in the JBoss ON GUI as deployed applications.
println(" download old app to /tmp"); child.retrieveBackingContent("/tmp/" + packageName + "_" + server.name + "_old");
/tmp/
directory.
println(" stopping " + server.name + "...."); try { server.shutdown() } catch( ex ) { println(" --> Caught " + ex ); } println(" uploading new application code"); child.updateBackingContent(fileName); println(" restarting " + server.name + "....." ); try { server.start(); } catch( ex ) { println(" --> Caught " + ex ); }
At this point, existing application can be updated. The next step is to create the resource through the CLI and then deploy it to the JBoss server.
- The type of the application (e.g., WAR or EAR)
- The type of the container the app needs to be deployed on (such as Tomcat or JBoss AS 4)
Note
rhq-plugin.xml
descriptor. The attributes, configuration parameters, operations, and metrics for each default resource type are listed in the Resource Monitoring and Operations Reference.
var appType = ResourceTypeManager.getResourceTypeByNameAndPlugin( appTypeName, "JBossAS" ); if( appType == null ) { println(" Could not find application type. Exit."); usage(); }
var realPackageType = ContentManager.findPackageTypes( appTypeName, "JBossAS" ); if( realPackageType == null ) { println(" Could not find JBoss ON's packageType. Exit."); usage(); }
rhq-plugin.xml
descriptor. To be able to create a new resource, these parameters need to be filled in.
// create deployConfig var deployConfig = new Configuration(); deployConfig.put( new PropertySimple("deployDirectory", "deploy")); deployConfig.put( new PropertySimple("deployZipped", "true")); deployConfig.put( new PropertySimple("createBackup", "false"));
var deployConfigDef = ConfigurationManager.getPackageTypeConfigurationDefinition(realPackageType.getId());
var inputStream = new java.io.FileInputStream(file); var fileLength = file.length(); var fileBytes = java.lang.reflect.Array.newInstance(java.lang.Byte.TYPE, fileLength); for (numRead=0, offset=0; ((numRead >= 0) && (offset < fileBytes.length)); offset += numRead ) { numRead = inputStream.read(fileBytes, offset, fileBytes.length - offset); }
rhq-plugin.xml
descriptor. For example:
ResourceFactoryManager.createPackageBackedResource( server.id, appType.id, packageName, null, // pluginConfiguration packageName, packageVersion, null, // architectureId deployConfig, fileBytes, null // timeout );
6.2. Scripting JBoss AS 5 Deployments
This script will use two custom functions to deploy the packages to create new resources.
function usage() { println("Usage: deployToGroup <fileName> <groupName>"); throw "Illegal arguments"; } function PackageParser(fullPathName) { var file = new java.io.File(fullPathName); var fileName = file.getName(); var packageType = fileName.substring(fileName.lastIndexOf('.')+1); var tmp = fileName.substring(0, fileName.lastIndexOf('.')); var realName = tmp.substring(0, tmp.lastIndexOf('-')); var version = tmp.substring(tmp.lastIndexOf('-') + 1); var packageName = realName + "." + packageType; this.packageType = packageType.toLowerCase(); this.packageName = packageName; this.version = version; this.realName = realName; }
The script should have two command-line parameters. The first should be the path of the new application that is installed in the group. The second is the name of the group itself. These parameters are parsed in the script (as described in more detail in Section 4.4, “Passing Script Arguments in the JBoss ON CLI”).
if( args.length < 2 ) usage(); var fileName = args[0]; var groupName = args[1];
// check that the file exists and that we can read it var file = new java.io.File(fileName); if( !file.exists() ) { println(fileName + " does not exist!"); usage(); } if( !file.canRead() ) { println(fileName + " can't be read!"); usage(); }
// find resource group var rgc = new ResourceGroupCriteria(); rgc.addFilterName(groupName); rgc.fetchExplicitResources(true); var groupList = ResourceGroupManager.findResourceGroupsByCriteria(rgc);
rgc.fetchExplicitResources(true);
if( groupList == null || groupList.size() != 1 ) { println("Can't find a resource group named " + groupName); usage(); } var group = groupList.get(0); println(" Found group: " + group.name ); println(" Group ID : " + group.id ); println(" Description: " + group.description);
if( group.explicitResources == null || group.explicitResources.size() == 0 ) { println(" Group does not contain explicit resources --> exiting!" ); usage(); } var resourcesArray = group.explicitResources.toArray();
resourceArray
now contains all resources which are part of the group. Next, check if there are JBoss AS 5 Server instances which need to be restarted before the application is deployed.
for( i in resourcesArray ) { var res = resourcesArray[i]; var resType = res.resourceType.name; println(" Found resource " + res.name + " of type " + resType + " and ID " + res.id); if( resType != "JBossAS5 Server") { println(" ---> Resource not of required type. Exiting!"); usage(); } // get server resource to start/stop it and to redeploy application var server = ProxyFactory.getResource(res.id); }
server
contains the JBoss AS 5 instance. This requires re-reading the server because it needs to be fully populated. Internally, the CLI is using simple JPA persistence, and it is necessary to not always fetch all dependent objects.
var children = server.children; for( c in children ) { var child = children[c]; if( child.name == packageName ) { } }
packageName
is the name of the application without version information and path as shown in the JBoss ON GUI as deployed applications.
println(" download old app to /tmp"); child.retrieveBackingContent("/tmp/" + packageName + "_" + server.name + "_old");
/tmp/
directory.
println(" stopping " + server.name + "...."); try { server.shutDown(); } catch( ex ) { println(" --> Caught " + ex ); } println(" uploading new application code"); child.updateBackingContent(fileName); println(" restarting " + server.name + "....." ); try { server.start(); } catch( ex ) { println(" --> Caught " + ex ); }
At this point, existing application can be updated. The next step is to create the resource through the CLI and then deploy it to the JBoss server.
- The type of the application (e.g., WAR or EAR)
- The type of the container the app needs to be deployed on (such as Tomcat or JBoss AS 5)
Note
rhq-plugin.xml
descriptor. The attributes, configuration parameters, operations, and metrics for each default resource type are listed in the Resource Monitoring and Operations Reference.
var appType = ResourceTypeManager.getResourceTypeByNameAndPlugin( appTypeName, "JBossAS5" ); if( appType == null ) { println(" Could not find application type. Exit."); usage(); }
var realPackageType = ContentManager.findPackageTypes( appTypeName, "JBossAS5" ); if( realPackageType == null ) { println(" Could not find JBoss ON's packageType. Exit."); usage(); }
rhq-plugin.xml
descriptor. To be able to create a new resource, these parameters need to be filled in.
// create deployConfig var deployConfig = new Configuration(); deployConfig.put( new PropertySimple("deployExploded", "false")); deployConfig.put( new PropertySimple("deployFarmed", "false"));
var deployConfigDef = ConfigurationManager.getPackageTypeConfigurationDefinition(realPackageType.getId());
var inputStream = new java.io.FileInputStream(file); var fileLength = file.length(); var fileBytes = java.lang.reflect.Array.newInstance(java.lang.Byte.TYPE, fileLength); for (numRead=0, offset=0; ((numRead >= 0) && (offset < fileBytes.length)); offset += numRead ) { numRead = inputStream.read(fileBytes, offset, fileBytes.length - offset); }
rhq-plugin.xml
descriptor. For example:
ResourceFactoryManager.createPackageBackedResource( server.id, appType.id, packageName, null, // pluginConfiguration packageName, packageVersion, null, // architectureId deployConfig, fileBytes, null // timeout );