Skip to content

Commit 4f19b4b

Browse files
committed
Add a state run index to the elastic search docker example
Signed-off-by: Pedro Algarvio <[email protected]>
1 parent f0e55b5 commit 4f19b4b

File tree

10 files changed

+102
-2
lines changed

10 files changed

+102
-2
lines changed

docker/elastic/centosstream9.Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ ADD docker/elastic/conf/supervisord.loop-jobs.conf /etc/supervisor/conf.d/loop-j
3636
ADD docker/elastic/loop-jobs.sh /usr/bin/loop-jobs.sh
3737
ADD docker/elastic/conf/analytics.master.conf /etc/salt/master.d/salt-analytics.conf
3838
ADD docker/elastic/conf/master-1.conf /etc/salt/master.d/master-1.conf
39+
ADD docker/elastic/state-tree/*.sls /srv/salt/
3940

4041
CMD ["/usr/bin/supervisord","-c","/etc/supervisord.conf"]
4142

docker/elastic/conf/analytics.master.conf

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ engines:
44
analytics:
55

66
collectors:
7-
events-collector:
7+
jobs-collector:
88
plugin: event-bus
99
tags:
1010
- "salt/job/*"
@@ -21,8 +21,14 @@ analytics:
2121
processors:
2222
job-aggregate:
2323
plugin: job-aggregate
24+
state-job-aggregate:
25+
plugin: job-aggregate
26+
jobs:
27+
- "state.*"
2428
cast-to-es:
2529
plugin: jobs_to_es
30+
state-cast-to-es:
31+
plugin: state_jobs_to_es
2632

2733
forwarders:
2834
elasticsearch-forwarder:
@@ -35,8 +41,17 @@ analytics:
3541
jobs-pipeline:
3642
collect:
3743
- grains-collector
38-
- events-collector
44+
- jobs-collector
3945
process:
4046
- job-aggregate
4147
- cast-to-es
4248
forward: elasticsearch-forwarder
49+
50+
state-jobs-pipeline:
51+
collect:
52+
- grains-collector
53+
- jobs-collector
54+
process:
55+
- state-job-aggregate
56+
- state-cast-to-es
57+
forward: elasticsearch-forwarder

docker/elastic/debian11.Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ ADD docker/elastic/conf/supervisord.loop-jobs.conf /etc/supervisor/conf.d/loop-j
3636
ADD docker/elastic/loop-jobs.sh /usr/bin/loop-jobs.sh
3737
ADD docker/elastic/conf/analytics.master.conf /etc/salt/master.d/salt-analytics.conf
3838
ADD docker/elastic/conf/master-1.conf /etc/salt/master.d/master-1.conf
39+
ADD docker/elastic/state-tree/*.sls /srv/salt/
3940

4041
CMD ["/usr/bin/supervisord","-c","/etc/supervisor/supervisord.conf"]
4142

docker/elastic/loop-jobs.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,16 @@ while true; do
77
SLEEP=$(shuf -i 3-10 -n 1)
88
sleep $SLEEP
99
/usr/bin/salt \* test.ping
10+
SLEEP=$(shuf -i 3-10 -n 1)
11+
sleep $SLEEP
12+
/usr/bin/salt \* state.sls add-user
13+
SLEEP=$(shuf -i 3-10 -n 1)
14+
sleep $SLEEP
15+
/usr/bin/salt \* state.sls remove-user
16+
SLEEP=$(shuf -i 3-10 -n 1)
17+
sleep $SLEEP
18+
/usr/bin/salt \* state.sls install-nginx
19+
SLEEP=$(shuf -i 3-10 -n 1)
20+
sleep $SLEEP
21+
/usr/bin/salt \* state.sls uninstall-nginx
1022
done
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
james-bond:
2+
user.present:
3+
- createhome: true
4+
- empty_password: true
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
nginx:
2+
pkg.installed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
james-bond:
2+
user.absent:
3+
- purge: true
4+
- force: true
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
nginx:
2+
pkg.removed

examples/setup.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ saf.process =
5050
numpy_save_keys = saltext.safexamples.process.numpy_save_keys
5151
beacons_to_es = saltext.safexamples.process.beacons_to_es
5252
jobs_to_es = saltext.safexamples.process.jobs_to_es
53+
state_jobs_to_es = saltext.safexamples.process.state_jobs_to_es
5354

5455
[bdist_wheel]
5556
# Use this option if your package is pure-python
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Copyright 2021-2023 VMware, Inc.
2+
# SPDX-License-Identifier: Apache-2.0
3+
"""
4+
Convert beacon event to an elastic search index event.
5+
"""
6+
from __future__ import annotations
7+
8+
import json
9+
import logging
10+
import pprint
11+
from typing import TYPE_CHECKING
12+
from typing import AsyncIterator
13+
from typing import Type
14+
15+
from saf.forward.elasticsearch import ElasticSearchEvent
16+
from saf.models import PipelineRunContext
17+
from saf.models import ProcessConfigBase
18+
19+
if TYPE_CHECKING:
20+
from saf.process.job_aggregate import JobAggregateCollectedEvent
21+
22+
log = logging.getLogger(__name__)
23+
24+
25+
class SaltStateJobsToESConfig(ProcessConfigBase):
26+
"""
27+
Processor configuration.
28+
"""
29+
30+
31+
def get_config_schema() -> Type[SaltStateJobsToESConfig]:
32+
"""
33+
Get the test collect plugin configuration schema.
34+
"""
35+
return SaltStateJobsToESConfig
36+
37+
38+
async def process(
39+
*,
40+
ctx: PipelineRunContext[SaltStateJobsToESConfig], # noqa: ARG001
41+
event: JobAggregateCollectedEvent,
42+
) -> AsyncIterator[ElasticSearchEvent]:
43+
"""
44+
Method called to collect events, in this case, generate.
45+
"""
46+
data = event.dict()
47+
data.pop("data", None)
48+
data.update(event.data)
49+
data["state_name"] = data["fun_args"][0]
50+
# Some field must be cast to JSON strings or EL will complain about
51+
# different types
52+
for key in ("fun_args", "return"):
53+
if key in data:
54+
data[key] = json.dumps(data[key])
55+
data["@timestamp"] = event.start_time
56+
evt = ElasticSearchEvent.construct(index="salt_state_runs", data=data)
57+
log.debug("ElasticSearchEvent: %s", pprint.pformat(evt.dict()))
58+
yield evt

0 commit comments

Comments
 (0)