Sample scripts

This section has code listings for simple bash scripts that invoke a variety of SA CLI methods. These scripts demonstrate how to pass method parameters on the command-line, including complex objects and the self parameter. If you decide to copy and paste these example scripts, you will need to change some of the hard-coded object names, such as the d04.example.com server. For tutorial instructions on creating and running scripts within the OGFS, see SA CLI method tutorial.

The script remediate_policy.sh creates a software policy, adds a package to the policy, and in the last line, installs the package on a managed server by invoking the startFullRemediateNow method.

create_custom_field.sh

This script creates a custom field (virtual column), named TestFieldA attaches the field to all servers, and then sets the value of the field on a single server. Until it is attached, the custom field does not appear in the SA Client. You can create custom fields for servers, device groups, or software policies. To create a custom field, your SA user must belong to a user group with the Manage Virtual Columns permission.

Unlike a custom attribute, a custom field applies to all instances of a type. For an example that creates a custom attribute in the OGFS, see Managing custom attributes in the Server Automation Using Guide on the HPE SSO portal.

The create_custom_field.sh script has the following code:

#!/bin/bash
# create_custom_field.sh
 
cd /opsw/api/com/opsware/custattr/VirtualColumnService/method
 
# Create a virtual column.
# Remember the name because you cannot search for the
# displayName.
./create vo=’{ name=TestFieldA type=SHORT_STRING \
displayName="Test Field A" }’
 
column_id=‘./.findVirtualColumn:i name=TestFieldA‘
 
echo --- column_id = $column_id
 
cd /opsw/api/com/opsware/server/ServerService/method
 
# Attach the column to all servers.
# All servers will have this custom field.
./attachVirtualColumn virtualColumn:i=$column_id
 
# Get the ID of the server named d04.example.com
devices_id=‘./.findServerRefs:i \
filter=\
’device:{ ServerVO.hostname CONTAINS "d04.example.com" }’‘
 
echo --- devices_id = $devices_id
 
# Set the value of the custom field (virtual column) for
# a specific server.
./setCustomField self:i=$devices_id fieldName=TestFieldA \
strValue="This is something."

create_device_group.sh

This script creates a static device group and adds a server to the group. Next, the script creates a dynamic group, sets a rule on the group, and refreshes the membership of the group. The last statement of the script lists the devices that belong to the dynamic group.

Here is the script’s code:

#!/bin/bash
# create_device_group.sh
 
cd /opsw/api/com/opsware/device/DeviceGroupService/method
 
# Get the ID of the public root group (top of hierarchy).
public_root=‘./.getPublicRoot:i‘
 
# Create a public static group.
./create "vo={ parent:i=$public_root shortName=’Test Group A’ }"
 
# Get the ID of the group just created.
group_id=‘./.findDeviceGroupRefs:i \
filter=’{ DeviceGroupVO.shortName = "Test Group A" }’ ‘
 
echo --- group_id = $group_id
 
cd /opsw/api/com/opsware/server/ServerService/method
 
# Get the ID of the server named d04.example.com
devices_id=‘./.findServerRefs:i \
filter=\
’device:{ ServerVO.hostname CONTAINS "d04.example.com" }’‘
 
echo --- devices_id = $devices_id
 
cd /opsw/api/com/opsware/device/DeviceGroupService/method
 
# Add a server to the device group.
./addDevices \
self:i=$group_id devices:i=$devices_id
 
# Create a dynamic device group.
./create \
"vo={ parent:i=$public_root \
shortName=’Test Dyn B’ dynamic=true }"
 
# Get the ID of the device group.
dynamic_group_id=‘./.findDeviceGroupRefs:i \
filter=’{ DeviceGroupVO.shortName = "Test Dyn B" }’ ‘
 
echo --- dynamic_group_id = $dynamic_group_id
 
# Set the rule so that this group contains servers with 
# hostnames containing the string example.com.
# The rule parameter has the same syntax as the filter
# parameter of the find methods.
./setDynamicRule self:i=$dynamic_group_id \
rule=’device:{ ServerVO.hostname CONTAINS example.com }’
 
# By default, membership in dynamic device groups is refreshed
# once
# an hour, so force the refresh now.
./refreshMembership selves:i=$dynamic_group_id now=true
 
# Display the names of the devices that belong to the group.
echo --- Devices in group:
./getDevices selves:i=$dynamic_group_id

create_folder.sh

This script creates a folder named /Test 1, lists the folders under the root (/) folder, and then creates the subfolder /Test 1/Test 2. After creating these folders, you can view them under the Library in the navigation pane of the SA Client.

Here is the code for this script:

#!/bin/bash
# create_folder.sh
 
cd /opsw/api/com/opsware/folder/FolderService/method
 
# Get the ID of the root (top) folder.
root_id=`./.getRoot:i`
 
# Create a new folder under the root folder.
./create vo="{ name='Test 1' folder:i=$root_id }"
 
# Display the names of the folders under the root folder.
./getChildren self:i=$root_id
 
# Get the ID of the folder "/Test 1"
folder_id=`./.getFolderRef:i path="Test 1"`
 
# Create a subfolder.
./create vo="{ name='Test 2' folder:i=$folder_id }"
 
# Get the ID of the folder "/Test 1/Test 2"
folder_id=`./.getFolderRef:i path="Test 1" path="Test 2"`
echo folder_id = $folder_id

remediate_policy.sh

This script creates a software policy named TestPolicyA in an existing folder named Test 2, adds a package containing ismtool to the policy, attaches the policy to a single server (not a group), and then remediates the server. The remediation action launches a job that installs the package onto the server. You can check the progress and results of the job in the SA Client. For examples that search for jobs with SA CLI methods, see Finding jobs.

In this script, in the create method of the SoftwarePolicyService, the value of the platforms parameter is hard-coded. In most of these example scripts, hard-coding is avoided by searching for an object by name. In the case of platforms, searching by the name attribute is difficult because if differs from the displayName attribute, which is exposed in the SA Client but is not searchable. The easiest way to find a platform ID is by going to the twister and running the PlatformService.findPlatformRefs method with no parameters.

The update method in this script hard-codes the ID of softwarePolicyItems, an object that can be difficult to search for by name if the Software Repository contains many packages with similar names. One way to get the ID is to run the SA Client, search for Software by fields such as File Name and Operating System, open the package located by the search, and note the SA ID in the properties view of the package.

In the following listing, the update method has a bad line break. If you copy this code, edit the script so that the vo parameter is on a single line.

Here is the source code for the remediate_policy.sh script:

#!/bin/bash
# remediate_policy.sh
 
# Get the ID of the folder where the policy will reside.
cd /opsw/api/com/opsware/folder/FolderService/method
folder_id=`./.findFolders:i filter='{ FolderVO.name = "Test 2" }'`
 
cd /opsw/api/com/opsware/swmgmt/SoftwarePolicyService/method
 
# Create a software policy named TestPolicyA.
# This policy resides in the folder located in the preceding findFolders
# call.
# The platform for this policy is Windows 2008 (ID 160076)
./create vo="{ platforms:i=160076 name="TestPolicyA" \
folder:i=$folder_id lifecycle=AVAILABLE }"
 
policy_id=`./.findSoftwarePolicyRefs:i \
filter='{ SoftwarePolicyVO.name = "TestPolicyA" }'`
 
echo --- policy_id = $policy_id
 
# Call the update method to add a package to the software policy.
# The package ID for the "ismtool" msi installer is 4010001.
 
# Note that "force = true" is required.
 
./update self:i=$policy_id force=true \
vo='{ softwarePolicyItems:i=com.opsware.pkg.windows.MSIRef:4010001 }'
 
cd /opsw/api/com/opsware/server/ServerService/method
 
# Get the ID of the server named d04.opsware.com
devices_id=`./.findServerRefs:i \
filter='device:{ ServerVO.hostname CONTAINS "d04.opsware.com" }'`
 
echo --- devices_id = $devices_id
 
# Attach the policy to a single server (not a group).
./attachPolicies self:i=$devices_id \
policies:i=$policy_id
 
# Remediate the server to install the package in the policy.
job_id=`./.startFullRemediateNow:i self:i=$devices_id`
 
echo --- job_id = $job_id

remove_custom_field.sh

Although not common in an operational environment, removing custom fields is sometimes necessary in a testing environment. Note that a custom field must be unattached before it can be removed.

Here is the code for remove_custom_field.sh:

#!/bin/bash
# remove_custom_field.sh
 
if [ ! -n "$1" ]
   then
   echo "Usage: ‘basename $0‘ <name>"
   echo "Example: ‘basename $0‘ hmp"
   exit
fi
 
cd /opsw/api/com/opsware/custattr/VirtualColumnService/method
 
column_id=‘./.findVirtualColumn:i name=$1‘
 
echo --- column_id = $column_id
 
cd /opsw/api/com/opsware/server/ServerService/method
 
# Column must be detached before it can be removed.
./detachVirtualColumn virtualColumn:i=$column_id
 
cd /opsw/api/com/opsware/custattr/VirtualColumnService/method
 
# Remove the virtual column.
./remove self:i=$column_id

schedule_audit_task.sh

This script starts an audit task, scheduling it for a future date. With SA CLI methods, date parameters are specified with the following syntax:

YYYY/MM/DD HH:MM:SS.sss

The method that launches the task, startAudit, returns the ID of the job that performs the audit. For examples that search for jobs with SA CLI methods, see Finding jobs.

Here is the code for schedule_audit_task.sh:

#!/bin/bash
# schedule_audit_task.sh
 
cd /opsw/api/com/opsware/compliance/sco/AuditTaskService/method
 
# Get the ID of the audit task to schedule.
 
audit_task_id=`./.findAuditTask:i \
filter='audit_task:{ (( AuditTaskVO.name BEGINS_WITH "HW check" ) \
& ( AuditTaskVO.createdBy = "gsmith" )) }'`
 
echo --- audit_task_id = $audit_task_id
 
# Schedule the audit task for Oct. 16, 2013.
# In the startDate parameter, note that the last delimiter for the time
# is a period, not a colon.
 
job_id=`./.startAudit:i self:i=$audit_task_id
schedule:s='{ startDate="2013/10/16 00:00:00.000" }' \
notification:s='{ onFailureOwner="sjones@opsware.com" \
onFailureRecipients="jdoe@opsware.com" \
onSuccessOwner="sjones@opsware.com" \
onSuccessRecipients="jdoe@opsware.com" }'`
 
echo --- job_id = $job_id