Debian Codegraph @

docs/src/cdist-messaging.rst

raw
1 Messaging
2 =========
3
4 Description
5 -----------
6 cdist has a simple but powerful way of allowing communication between
7 the initial manifest and types as well as types and types.
8
9 Whenever execution is passed from cdist to one of the
10 scripts described below, cdist generate 2 new temporary files
11 and exports the environment variables **__messages_in** and
12 **__messages_out** to point to them.
13
14 Before handing over the control, the content of the global message
15 file is copied into the file referenced by **$__messages_in**.
16
17 After cdist gained control back, the content of the file referenced
18 by **$__messages_out** is appended to the global message file.
19
20 This way overwriting any of the two files by accident does not
21 interfere with other types.
22
23 The order of execution is not defined unless you create dependencies
24 between the different objects (see `cdist manifest <cdist-manifest.html>`_) and thus you
25 can only react reliably on messages by objects that you depend on.
26
27
28 Availability
29 ------------
30 Messaging is possible between all **local** scripts:
31
32 - initial manifest
33 - type/manifest
34 - type/gencode-local
35 - type/gencode-remote
36
37
38 Examples
39 --------
40 When you want to emit a message use:
41
42 .. code-block:: sh
43
44 echo "something" >> "$__messages_out"
45
46 When you want to react on a message use:
47
48 .. code-block:: sh
49
50 if grep -q "^__your_type/object/id:something" "$__messages_in"; then
51 echo "I do something else"
52 fi
53
54 Some real life examples:
55
56 .. code-block:: sh
57
58 # Reacting on changes from block for keepalive
59 if grep -q "^__block/keepalive-vrrp" "$__messages_in"; then
60 echo /etc/init.d/keepalived restart
61 fi
62
63 # Reacting on changes of configuration files
64 if grep -q "^__file/etc/one" $__messages_in; then
65 echo 'for init in /etc/init.d/opennebula*; do $init restart; done'
66 fi
67
68 Restart sshd on changes
69
70 .. code-block:: sh
71
72 os="$(cat "$__global/explorer/os")"
73
74 case "$os" in
75 centos|redhat|suse)
76 restart="/etc/init.d/sshd restart"
77 ;;
78 debian|ubuntu)
79 restart="/etc/init.d/ssh restart"
80 ;;
81 *)
82 cat << eof >&2
83 Unsupported os $os.
84 If you would like to have this type running on $os,
85 you can either develop the changes and send a pull
86 request or ask for a quote at www.ungleich.ch
87 eof
88 exit 1
89 ;;
90 esac
91
92 if grep -q "^__key_value/PermitRootLogin" "$__messages_in"; then
93 echo $restart
94 fi