How-to: Use a regular expression to target a model to multiple target resources

A model can be targeted to multiple resources within an environment using a regular expression notation in the target-resource attribute in the model.

In this example, we have a Linux shell script that needs to be executed on multiple Linux hosts using the Pineapple SSH plugin.

Modeling the resources

Three hosts where the model should be applied to, are defined in a environment named alpha. Three resources are defined in the resources.xml file, one for each host:

<?xml version="1.0" encoding="UTF-8"?>
<configuration xmlns="http://pineapple.dev.java.net/ns/environment_1_0" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://pineapple.dev.java.net/ns/environment_1_0 
                      http://pineapple.dev.java.net/ns/environment_1_0.xsd">

  <environments>
    <environment description="an example environment" id="alpha">
      <resources>
        <resource plugin-id="com.alpha.pineapple.plugin.ssh" credential-id-ref="node1" id="node1" >
          <property value="92.168.33.10" key="host"/>                           
          <property value="22" key="port"/>                     
          <property value="1000" key="timeout"/>                                                                
        </resource>                             
        <resource plugin-id="com.alpha.pineapple.plugin.ssh" credential-id-ref="node2" id="node2" >
          <property value="92.168.33.11" key="host"/>                           
          <property value="22" key="port"/>                     
          <property value="1000" key="timeout"/>                                                                
        </resource>                             
        <resource plugin-id="com.alpha.pineapple.plugin.ssh" credential-id-ref="node3" id="node3" >
          <property value="92.168.33.12" key="host"/>                           
          <property value="22" key="port"/>                     
          <property value="1000" key="timeout"/>                                                                
        </resource>                             
    </resources>
    </environment>
  </environments>
</configuration> 

The host resources are named node1, node1 and node1 and define usage with the SSH plugin. Each have different IP address and references a different Pineapple credential definition which is omitted here.

Creating the model

A module is created and a model named alpha for the target environment alpha is added to the module. The model defines the shell script which should be executed on each of the three hosts:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<mmd:models xmlns:mmd="http://pineapple.dev.java.net/ns/module_model_1_0"
        xmlns:shp="http://pineapple.dev.java.net/ns/plugin/ssh_1_0" >
    <mmd:model target-resource="regex:node.*">
        <mmd:content>
            <shp:ssh>
                <shp:execute command="sudo yum --assumeyes install unzip" />
                <shp:execute command="sudo yum --assumeyes install chkconfig" />
            </shp:ssh>
        </mmd:content>
    </mmd:model>
</mmd:models>

And now to the important part of the model which is the target-resource attribute defined in the model as: target-resource="regex:node.*"

At runtime Pineapple will interpret the value which follow the regex: prefix as regluar expression. Any resource in the environment (which is specified when the operation is invoked) which match the regular expression will be invoked with the model.

Care must be taken to ensure that only resources which can consume a particular model is matched.

A redundant alternative

The model defined in the previous section can be defined in a semantically identical but more redundant way as:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<mmd:models xmlns:mmd="http://pineapple.dev.java.net/ns/module_model_1_0"
        xmlns:shp="http://pineapple.dev.java.net/ns/plugin/ssh_1_0" >
    <mmd:model target-resource="node1">
        <mmd:content>
            <shp:ssh>
                <shp:execute command="sudo yum --assumeyes install unzip" />
                <shp:execute command="sudo yum --assumeyes install chkconfig" />
            </shp:ssh>
        </mmd:content>
    </mmd:model>
    <mmd:model target-resource="node2">
        <mmd:content>
            <shp:ssh>
                <shp:execute command="sudo yum --assumeyes install unzip" />
                <shp:execute command="sudo yum --assumeyes install chkconfig" />
            </shp:ssh>
        </mmd:content>
    </mmd:model>
    <mmd:model target-resource="node3">
        <mmd:content>
            <shp:ssh>
                <shp:execute command="sudo yum --assumeyes install unzip" />
                <shp:execute command="sudo yum --assumeyes install chkconfig" />
            </shp:ssh>
        </mmd:content>
    </mmd:model>
</mmd:models>

The differences between this model and the model in the previous section are:

  • The model content is repeated for each target resource (error prone).
  • Each model is only targeted to a single resource.