Create Pack

icon

(icon.png)

https://github.com/StackStorm/st2/tree/master/contrib/hello_st2

https://docs.stackstorm.com/reference/packs.html

Create the pack folder structure and related files. Let’s keep the metadata files such as pack.yaml, config.schema.yaml, and requirements.txt empty for now:

# Use the name of the pack for the folder name.
mkdir hello_st2
cd hello_st2
mkdir actions
mkdir rules
mkdir sensors
mkdir aliases
mkdir policies
touch pack.yaml
touch requirements.txt

Note: All folders are optional. It is safe to skip a folder or keep it empty. Only create the config.schema.yaml file if it is required. An empty schema file is not valid.

Create the pack definition file, pack.yaml:

sudo chmod 777 pack.yaml


 


ref: hello_st2
name: Hello StackStorm
description: Simple pack containing examples of sensor, rule, and action.
keywords:
– example
– test
version: 0.1.0
author: StackStorm, Inc.
email: info@stackstorm.com

Create the action. An action consists of meta data, and entrypoint. The following example simply echoes a greeting.

Copy the following content to actions/greet.yaml:


name: greet
pack: hello_st2
runner_type: “local-shell-cmd”
description: Greet StackStorm!
enabled: true
entry_point: greet.sh
parameters:
greeting:
type: string
description: Greeting you want to say to StackStorm (i.e. Hello, Hi, Yo, etc.)
required: true
position: 1

Copy the following content to actions/greet.sh:

#!/bin/bash
echo $1, StackStorm!”

Create a sensor. The sample sensor below publishes an event to StackStorm every 60 seconds.

Copy the following content to sensors/sensor1.yaml:


class_name: “HelloSensor”
entry_point: “sensor1.py”
description: “Test sensor that emits triggers.”
trigger_types:

name: “event1”
description: “An example trigger.”
payload_schema:
type: “object”

Copy the following content to sensors/sensor1.py:

import eventlet

from st2reactor.sensor.base import Sensor

class HelloSensor(Sensor):
def __init__(self, sensor_service, config):
super(HelloSensor, self).__init__(sensor_service=sensor_service, config=config)
self._logger = self.sensor_service.get_logger(name=self.__class__.__name__)
self._stop = False

def setup(self):
pass

def run(self):
while not self._stop:
self._logger.debug(‘HelloSensor dispatching trigger…’)
count = self.sensor_service.get_value(‘hello_st2.count’) or 0
payload = {‘greeting’: ‘Yo, StackStorm!’, ‘count’: int(count) + 1}
self.sensor_service.dispatch(trigger=‘hello_st2.event1’, payload=payload)
self.sensor_service.set_value(‘hello_st2.count’, payload[‘count’])
eventlet.sleep(60)

def cleanup(self):
self._stop = True

# Methods required for programmable sensors.
def add_trigger(self, trigger):
pass

def update_trigger(self, trigger):
pass

def remove_trigger(self, trigger):
pass

Create a rule. The sample rule below is triggered by an event from the sensor and invokes the action from the samples above.

Copy the following content to rules/rule1.yaml:


name: on_hello_event1
pack: hello_st2
description: Sample rule firing on hello_st2.event1.
enabled: true
trigger:
type: hello_st2.event1
action:
ref: hello_st2.greet
parameters:
greeting: Yo

Create an action alias. The sample action alias below aliases the greet action and makes it accessible from ChatOps.

Copy the following content to aliases/alias1.yaml:


name: greet
pack: hello_st2
description: “Greet StackStorm”
action_ref: “hello_st2.greet”
formats:
“greet {{greeting}}”

Create a policy. The sample policy below limits concurrent operation of the greet action.

Copy the following content to policies/policy1.yaml:


name: greet.concurrency
pack: hello_st2
description: Limits the concurrent executions of the greet action.
enabled: true
resource_ref: hello_st2.greet
policy_type: action.concurrency
parameters:
threshold: 10

Install the pack. We encourage using git. If you do so, st2 pack will greatly simplify your pack management. Of course, you can define your own tools and workflow for editing and versioning packs. You’ll need to place the files in /opt/stackstorm/packs and [re-]load the content.

Use git and pack install (recommended):

# Get the code under git
cd hello_st2
git init && git add ./* && git commit -m “Initial commit”
# Install from local git repo
st2 pack install file:///$PWD

Initialised empty Git repository in /opt/stackstorm/packs/hello_st2/.git/

*** Please tell me who you are.

Run

git config –global user.email “you@example.com”
git config –global user.name “Your Name”

to set your account’s default identity.
Omit –global to set the identity only in this repository.

st2 execution list

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s