Project

General

Profile

Download (3.9 KB) Statistics
| Branch: | Tag: | Revision:
1 7b996ea3 Scott Ullrich
<?php
2 5be2fc35 Scott Ullrich
/* $Id$ */
3 7b996ea3 Scott Ullrich
/*
4
	cmd_chain.inc
5 77cdcbad Scott Ullrich
	Part of pfSense
6 7b996ea3 Scott Ullrich
	Copyright (C) 2008 Scott Ullrich
7
	All rights reserved.
8
9
	Redistribution and use in source and binary forms, with or without
10
	modification, are permitted provided that the following conditions are met:
11
12
	1. Redistributions of source code must retain the above copyright notice,
13
	   this list of conditions and the following disclaimer.
14
15
	2. Redistributions in binary form must reproduce the above copyright
16
	   notice, this list of conditions and the following disclaimer in the
17
	   documentation and/or other materials provided with the distribution.
18
19
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
20
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
21
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
23
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28
	POSSIBILITY OF SUCH DAMAGE.
29
*/
30
31
/* 
32
 * HANDY FOR STAND ALONE DEBUGGING OF CLASS. 
33
 *
34 77cdcbad Scott Ullrich
35 7b996ea3 Scott Ullrich
	function log_error($text) {
36
		echo $text . "\n";
37
	}
38 77cdcbad Scott Ullrich
39 7b996ea3 Scott Ullrich
*/
40
41 77cdcbad Scott Ullrich
/*
42
 *   CmdCHAIN allows a chaining of commands into one call.
43
 *   If debugging is enabled verbose logging is applied.
44
 *   During the command(s) execution if it fails the result
45
 *   will be reported to syslog so that the problem can be reported.
46
 */
47 7b996ea3 Scott Ullrich
Class CmdCHAIN {
48
49
		var $cmd_chain_array = array();
50
		var $is_debugging;
51 5fec455a Scott Ullrich
		var $halt_on_errors = true;
52 7b996ea3 Scott Ullrich
		
53
		/* clear() erases the current cmdchain */
54
		function clear() {
55
			unset($cmd_chain_array);
56
			$this->cmd_chain_array = array();
57
		}
58
		
59
		/* enables log_error() of each command we run */
60
		function setdebug() {
61
			$this->is_debugging = true;
62
		}
63 5fec455a Scott Ullrich
		
64
		/* no halt execution of CmdCHAIN if there is a failure */
65
		function nohaltonerror() {
66
			$this->halt_on_errors = false;			
67
		}
68
69
		/* halts execution of CmdCHAIN if there is a failure */
70
		function sethaltonerror() {
71
			$this->halt_on_errors = true;			
72
		}
73 7b996ea3 Scott Ullrich
74 77cdcbad Scott Ullrich
		/* adds a command to the CmdCHAIN */
75
		function add($cmd_title = "", $command = "", $ignore_return_text = false) {
76 7b996ea3 Scott Ullrich
			if(!$cmd_title) 
77 77cdcbad Scott Ullrich
				return;
78
			if(!$command) 
79
				return;
80 7b996ea3 Scott Ullrich
			$temp = array();
81
			$temp['cmd_title'] = $cmd_title;
82
			$temp['command'] = $command;
83 77cdcbad Scott Ullrich
			if($ignore_return_text)
84
				$temp['ignore_return_text'] = true;
85
			else 
86
				$temp['ignore_return_text'] = false;
87
			$this->cmd_chain_array[] = $temp; // add array to class
88
			return array();
89 7b996ea3 Scott Ullrich
		}
90
		
91 77cdcbad Scott Ullrich
		/* executes the CmdCHAIN one command at a time */
92 7b996ea3 Scott Ullrich
		function execute() {
93
			foreach($this->cmd_chain_array as $cmd) {
94
				$cmd_title = $cmd['cmd_title'];
95
				$command = $cmd['command'];
96 77cdcbad Scott Ullrich
				$ignore_return_text = $cmd['ignore_return_text'];
97
				// Should we perform verbose debugging?
98 7b996ea3 Scott Ullrich
				if($this->is_debugging == true) {
99
					log_error("CmdCHAIN is executing -> {$cmd_title} - {$command}");
100
					usleep(100);	// give network stack time to deliver network syslog message
101
				}
102 77cdcbad Scott Ullrich
				// Execute command
103 7b996ea3 Scott Ullrich
				$status = exec($command);
104 5fec455a Scott Ullrich
				if($this->ignore_return_text == true) 
105
					continue;
106
				if(intval($status) <> 0) {
107 77cdcbad Scott Ullrich
					log_error("{$cmd_title} failed with return code -> {$status}.  The command was {$command}");
108 5fec455a Scott Ullrich
					if($this->halt_on_errors == true) 
109
						return("{$cmd_title} failed with return code -> {$status}.  The command was {$command}");
110 7b996ea3 Scott Ullrich
				}
111
			}
112
			return;
113
		}
114
}
115
116
/* 
117
 * example usage:
118
 *
119
120
$cmdchain = new CmdCHAIN();
121 77cdcbad Scott Ullrich
$cmdchain->add("grab freebsd version", "uname -a", false);
122
$cmdchain->setdebug(); // optional for verbose logging
123 5025a56c Scott Ullrich
$cmdchain->nohaltonerror(); // tells cmdchain to keep processing commands if any of them fail
124 7b996ea3 Scott Ullrich
$cmdchain->execute();
125
126 5025a56c Scott Ullrich
$cmdchain->clear(); // clears the previous added entries
127
128 7b996ea3 Scott Ullrich
*/
129
130
?>