d73f3fb2076cddb50c7b50ba0b195a655e57d5ce
[packages/precise/mcollective.git] / ext / action_helpers / python / kwilczynski / README.markdown
1 A simple helper to assist with writing MCollective actions in Python.
2
3 Given an action as below:
4
5 <pre>
6 action "echo" do
7    validate :message, String
8
9    implemented_by "/tmp/echo.py"
10 end
11 </pre>
12
13 The following Python script will implement the echo action externally
14 replying with _message_ and current _time_.
15
16 <pre>
17 #!/usr/bin/env python
18
19 import sys
20 import time
21 import mcollective_action as mc
22
23 if __name__ == '__main__':
24     mc = mc.MCollectiveAction()
25     request = mc.request()
26     mc.message = request['data']['message']
27     mc.time = time.strftime('%c')
28     mc.info("Some text to info log in the server")
29
30     sys.exit(0)
31 </pre>
32
33 Calling it with _mco rpc_ results in:
34
35 <pre>
36 $ mco rpc test echo message="Hello World"
37 Determining the amount of hosts matching filter for 2 seconds .... 1
38
39  * [ ============================================================> ] 1 / 1
40
41
42 host.example.com              : OK
43     {:message=>"Hello World", :time=>"Tue Mar 15 19:20:53 +0000 2011"}
44 </pre>
45
46 This implementation was successfully tested with Python 2.4 and 2.6.