Chapter 10. Automating tasks based on events

You can use CloudForms to automatically perform specific tasks when certain events occur on servers that are managed by XClarity Administrator through a Lenovo physical-infrastructure provider. To create an automation task, you must create and configure a custom domain. To automatically invoke the task when a certain event occurs, you must create, configure and assign a custom policy.

For more information about the CloudForms automation function, see Scripting Actions in CloudForms website.

10.1. Creating an automated task

To create an automated task, you must first create and configure a custom domain.

A domain is a collection of tasks that can be automated. The tasks are run in a sequence that is defined by the domain priority. A task in a domain with a higher priority overrides the same task in a lower-priority domain. In this way, CloudForms provides core domains and allows you to override automated tasks using your own custom domains.

Each domain contains a set of namespaces. A namespace is a container that organizes and categorizes tasks. A namespace can contain child namespaces as well as classes.

A class is a template for a specific task. A class uses a schema to populate a class instance with default values. A class instance can contain attributes, calls to methods, and relationships.

The methods define the task that you want to perform. It uses Ruby code to run the various operations.

As an example, the following steps describe how to create an automated task for performing a power action (such as powering on, power off, or restarting the server) on the physical server which is first detected by the Lenovo physical-infrastructure provider.

Note: You must be logged in to CloudForms as a user with permission to create domains. The default user is admin, password smartvm.

10.1.1. Step 1. Create a custom domain.

  1. Navigate to Automation > Automate > Explorer.
  2. Click Configuration > Add a New Domain from the top menu.
  3. Enter a domain name (for example, Lenovo).
  4. Select Enable to enable the domain.
  5. Click Add.

10.1.2. Step 2. Add the LenovoXclarity and Event Handler class to the custom domain.

  1. Copy the LenovoXclarity class to the custom domain.

    1. Select the ManageIQ core domain.
    2. Navigate to ManageIQ > System > Event > EmsEvent > LenovoXclarity.
    3. Click Configuration > Copy this Class from the top menu.
    4. Select the custom domain from the To Domain drop-down menu.
    5. Click Copy.
  2. Copy the Event Handler class to the custom domain.

    1. Select the ManageIQ core domain.
    2. Navigate to ManageIQ > System > Event Handlers.
    3. Select the instance that you wish (for example, event_action_policy).
    4. Click Configuration > Copy this Instance from the top menu.
    5. Select the custom domain from the To Domain drop-down menu.
    6. Click Copy.
    7. Select the event_action_policy method.
    8. Click the Configuration icon, and then click the Copy this method icon.
    9. Click Copy.
  3. Create a new instance of the LenovoXclarity class.

    1. Select the ManageIQ core domain.
    2. Navigation to name of the custom domain (for example, Lenovo), and click System > Event > EmsEvent >LenovoXclarity.
    3. Click Configuration > Add a new Instance from the top menu.
    4. Enter a name for the new class instance (for example, FQXHMTS0003G, which is the event that is generated when inventory data cannot be retrieved from a device).

      Important: The class-instance name must be the same as the name of the event that triggers the task to be invoked. CloudForms matches the class instance with the event by this name. To find the event name, see Messages in the XClarity Administrator online documentation.

    5. Add the path to the event handlers copied in the field’s rel3.

      There are three available policy operations:

      • Physical Server Reset
      • Physical Server Start
      • Physical Server Shutdown

        There are three available policy events:

      • physical_server_reset
      • physical_server_start
      • physical_server_shutdown

        The policy actions and the policy events must match. For example, use the following path to the policy operation Physical Server Shutdown:

        /System/event_handlers/event_action_policy?target=physical_server&policy_event=physical_server_shutdown&param=

      The policy_event value is matched with the policy operation applied.

    6. Click Add.

10.1.3. Step 3. Create a namespace.

  1. Select the custom domain (Lenovo) .
  2. Click Configuration > Add a New Namespace from the top menu.
  3. Enter a unique name for the namespace (for example, Functions).
  4. Click Add.

10.1.4. Step 4. Create a class.

  1. Select the namespace that you created in the previous step (for example, Functions).
  2. Click Configuration > Add a New Class from the top menu.
  3. Enter a unique name for the class (for example, Power_actions).
  4. Click Add.

10.1.5. Step 5. Create a method for the class.

  1. Create a method.

    1. Select the class that you created (for example, Power_actions).
    2. Click the Methods tab.
    3. Click Configuration > Add a new method from the top menu.
    4. Select “inline” for the type.
    5. Enter a name for the method (for example, power_off).
    6. Enter the following script in the Data field:

      server = $evm.vmdb('PhysicalServer').first
      $evm.log(:info, "Powering Server #{server.name} OFF")
      server.power_off
      exit MIQ_OK
    7. Click Validate to verify the syntax.
    8. Click Add.
  2. Add a schema to the class.

    1. Select the class that you created (for example, Power_actions).
    2. Select the Schema tab.
    3. Click Configuration > Edit selected Schema from the top menu.
    4. Click the + icon to add a field to the schema.
    5. Enter “execute” for the name.
    6. Select “Method” for the type.
    7. Select “String” for the data type.
    8. Enter “Power_actions” for the default value.
    9. Click the check mark icon.
    10. Click Save.
  3. Add the method to the class.

    1. Select the Instances tab.
    2. Enter the name of the method that you created earlier (for example, power_off).
    3. Click Add.

10.2. Invoking an automated task when an event occurs

To automatically invoke the task when a certain event occurs, you must create, configure and assign a custom policy.

As an example, the following steps describe how to perform a specific automated task when CloudForms receives certain events (that you define).

Note: You must be logged in to CloudForms as a user with permission to create policies. The default user is admin, password smartvm.

10.2.1. Step 1. Create and configure a policy.

  1. Navigate to Control > Explorer.
  2. Click Policies > All Policies > Control Policies > Physical Infrastructure Control Policies.
  3. Click Configuration > Add a new Physical Server Control Policy from the top menu.
  4. Enter a description (for example, Lenovo_Policy).
  5. Click Add.

10.2.2. Step 2. Create a custom action.

  1. Create a custom action.

    1. Navigation to Action > All Actions.
    2. Click Configuration > Add a new action from the top menu.
    3. Enter a description (for example, Power_Off_Server).
    4. For Action Type select Invoke a custom Automation.
    5. Enter a message (for example, create).
    6. Fill the Request field with Call_Instance.
    7. Specify the following attributes in the order give:

      • Specify the Namespace attribute, and set the value to the new domain and namespace (<domain_name>/<namespace>) (for example, Lenovo/Functions).
      • Specify the Class attribute, and set the value to the class (for example, Power_actions).
      • Specify the Instance attribute, and set the value to the instance (for example, Physical_Server_PowerOff).
    8. Click Add.
  2. Configure the policy that you created.

    1. Select the new policy (for example, Lenovo_Policy).
    2. Click Configuration > Edit this policy’s Event assignments from the top menu.
    3. Find Physical Server operation, and select the Physical Server Shutdown option.
    4. Click Save.
    5. Select the new policy event.
    6. Click Configuration > Edit Actions for this policy Event from the top menu.
    7. From the Order of Actions if All Conditions are True field, select the custom action that you created in the previous step (for example, Power_Off_Server).
    8. Click Save.

10.2.3. Step 3. Create and assign a policy profile.

  1. Create a policy profile.

    1. Click Policy Profiles > All Policy Profiles.
    2. Click Configuration > Add a new Policy Profile from the top menu.
    3. Enter a description for the policy (for example, Lenovo_Policy_Profile).
    4. Select your policy and drag it to the right.
    5. Click Add.
  2. Assign the policy profile to the Lenovo physical-infrastructure providers:

    1. Navigate to Compute > Physical Infrastructure > Providers.
    2. Select the physical-infrastructure providers to which you want to assign the policy profile.
    3. Click Policy > Manage Policies form the top menu.
    4. Select the policy profile that you just created (for example, Lenovo_Policy_Profile).
    5. Click Save.

provider assign profile