此内容没有您所选择的语言版本。

2. Example: Scripts to Manage Resources of a Specific Type


Section 1, “Example: Scripts to Manage Inventory (All Resource Types)” shows a general resource discovery and import script. In some environments, it may be more useful to discover and import only specific types of resources. For example, and administrator may be constantly deploying new applications to an app server, or maybe spinning up and destroying application servers dynamically to respond to load. In that case, the administrator only wants to find the specific resources which he knows are frequently created.
For this example, the workflow has some pretty basic steps:
  1. Search for new, uncommitted resources of a specific type.
  2. Get the resource IDs.
  3. Import those new resources.
  4. Do something with the newly-imported resources.
Steps 1 through 3 are basically the same as the autoimport.js example in Section 1.1, “Automatically Import New Resources: autoimport.js”, with one significant change. The search has an extra filter for the resource type.
Uncommitted (new) resources can be identified through a ResourceCriteria search by adding a search parameter based on the inventory status and the resource type (in this example, JBoss EAP 6 domain deployments, for new web applications). The new findUncommittedJbasApps() function runs that search.
function findUncommittedJbasApps() {
    var criteria = ResourceCriteria();
    criteria.addFilterInventoryStatus(InventoryStatus.NEW);
    criteria.addFilterResourceTypeName('DomainDeployment');
     
    return ResourceManager.findResourcesByCriteria(criteria);
}
Copy to Clipboard Toggle word wrap
As with the autoimport.js example, a getIds function retrieves an array of the resource IDs.
function getIds(resources) {
	var ids = [];

	if (resources.size() > 0) {
		println("Found resources to import: ");
		for (i = 0; i < resources.size(); i++) {
			resource = resources.get(i);
			ids[i] =  resource.id;
			println("  " + resource.name);
		}
	} else {
		println("No resources found awaiting import...");
	}

    return ids;
}
Copy to Clipboard Toggle word wrap
Those two functions accomplish the first two steps: search for uncommitted resources of a specific type and then retrieve those IDs.
Step 3 then runs the DiscoveryBoss method to import the discovered resources.
The first half of the script, then mimics the original autoimport.js, with the slight adjustment to the new function to search for JBoss EAP 6 domain deployments.
rhq.login('rhqadmin', 'rhqadmin');
println("Running autoImport.js");
 
var resources = findUncommittedJbasApps();
var resourceIds = getIds(resources);
DiscoveryBoss.importResources(resourceIds);
Copy to Clipboard Toggle word wrap
The last step simply does something with the new resource.
For a new domain deployment, then it is probably most useful to assign the new domain deployment to server group, so the new application is deployed. For example, this assigns all new applications automatically to a server group used for the staging environment, using the promote operation:
var config = new Configuration();
config.put(new PropertySimple("server-group", "Staging-Server-Group") );

OperationManager.scheduleResourceOperation(
          resources.get(0).id,
          "promote",
	  0, // 0 means that the delay was skipped
          1,
	  0, // this skips the repeat count
          10000000,
	  config, 
          "promote new app to server group"
	  );
Copy to Clipboard Toggle word wrap
Alternatively, if this script is used to add a new service or a new server, then it may be more appropriate to add the new resource to a group. This example uses an explicit group ID (15001) for an existing compatible group. The assumption is that the group has already been created, before this script is run, and there is no need to create a new group for the resources.
ResourceGroupManager.addResourcesToGroup(15001, [resourceIds]);
Copy to Clipboard Toggle word wrap
Then, have the script log out from the CLI and close.
rhq.logout();
Copy to Clipboard Toggle word wrap
返回顶部
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。 了解我们当前的更新.

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

Theme

© 2025 Red Hat