64876688cfafc92c2417ecb64e7a74d459fa1bd7
[packages/precise/mcollective.git] / ext / action_helpers / php / mcollective_action.php
1 <?php
2 class MCollectiveAction {
3     public $infile = "";
4     public $outfile = "";
5     public $request = array();
6
7     function __construct() {
8         if (!isSet($_ENV["MCOLLECTIVE_REQUEST_FILE"])) {
9             throw new Exception("no MCOLLECTIVE_REQUEST_FILE environment variable");
10         }
11
12         if (!isSet($_ENV["MCOLLECTIVE_REPLY_FILE"])) {
13             throw new Exception("no MCOLLECTIVE_REPLY_FILE environment variable");
14         }
15
16         $this->infile = $_ENV["MCOLLECTIVE_REQUEST_FILE"];
17         $this->outfile = $_ENV["MCOLLECTIVE_REPLY_FILE"];
18
19         $this->readJSON();
20     }
21
22     function __destruct() {
23         $this->save();
24     }
25
26     function readJSON() {
27         $this->request = json_decode(file_get_contents($this->infile), true);
28         unset($this->request["data"]["process_results"]);
29     }
30
31     function save() {
32         file_put_contents($this->outfile, json_encode($this->request["data"]));
33     }
34
35     // prints a line to STDERR that will log at error level in the
36     // mcollectived log file
37     function error($msg) {
38         fwrite(STDERR, "$msg\n");
39     }
40
41     // prints a line to STDOUT that will log at info level in the
42     // mcollectived log file
43     function info($msg) {
44         fwrite(STDOUT, "$msg\n");
45     }
46
47     // logs an error message and exits with RPCAborted
48     function fail($msg) {
49         $this->error($msg);
50         exit(1);
51     }
52
53     function __get($property) {
54         if (isSet($this->request[$property])) {
55             return $this->request[$property];
56         } else {
57             throw new Exception("No $property in request");
58         }
59     }
60
61     function __set($property, $value) {
62         $this->request["data"][$property] = $value;
63     }
64 }
65 ?>