このコンテンツは選択した言語では利用できません。

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

詳細情報

試用、購入および販売

コミュニティー

Red Hat ドキュメントについて

Red Hat をお使いのお客様が、信頼できるコンテンツが含まれている製品やサービスを活用することで、イノベーションを行い、目標を達成できるようにします。 最新の更新を見る.

多様性を受け入れるオープンソースの強化

Red Hat では、コード、ドキュメント、Web プロパティーにおける配慮に欠ける用語の置き換えに取り組んでいます。このような変更は、段階的に実施される予定です。詳細情報: Red Hat ブログ.

会社概要

Red Hat は、企業がコアとなるデータセンターからネットワークエッジに至るまで、各種プラットフォームや環境全体で作業を簡素化できるように、強化されたソリューションを提供しています。

Theme

© 2025 Red Hat