Project

General

Profile

Download (3.92 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/* $Id$ */
3
/*
4
	cmd_chain.inc
5
	Part of pfSense
6
	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
	pfSense_MODULE:	utils
31
*/
32

    
33
/* 
34
 * HANDY FOR STAND ALONE DEBUGGING OF CLASS. 
35
 *
36

    
37
	function log_error($text) {
38
		echo $text . "\n";
39
	}
40

    
41
*/
42

    
43
/*
44
 *   CmdCHAIN allows a chaining of commands into one call.
45
 *   If debugging is enabled verbose logging is applied.
46
 *   During the command(s) execution if it fails the result
47
 *   will be reported to syslog so that the problem can be reported.
48
 */
49
Class CmdCHAIN {
50

    
51
		var $cmd_chain_array = array();
52
		var $is_debugging;
53
		var $halt_on_errors = true;
54
		
55
		/* clear() erases the current cmdchain */
56
		function clear() {
57
			unset($cmd_chain_array);
58
			$this->cmd_chain_array = array();
59
		}
60
		
61
		/* enables log_error() of each command we run */
62
		function setdebug() {
63
			$this->is_debugging = true;
64
		}
65
		
66
		/* no halt execution of CmdCHAIN if there is a failure */
67
		function nohaltonerror() {
68
			$this->halt_on_errors = false;			
69
		}
70

    
71
		/* halts execution of CmdCHAIN if there is a failure */
72
		function sethaltonerror() {
73
			$this->halt_on_errors = true;			
74
		}
75

    
76
		/* adds a command to the CmdCHAIN */
77
		function add($cmd_title = "", $command = "", $ignore_return_text = false) {
78
			if(!$cmd_title) 
79
				return;
80
			if(!$command) 
81
				return;
82
			$temp = array();
83
			$temp['cmd_title'] = $cmd_title;
84
			$temp['command'] = $command;
85
			if($ignore_return_text)
86
				$temp['ignore_return_text'] = true;
87
			else 
88
				$temp['ignore_return_text'] = false;
89
			$this->cmd_chain_array[] = $temp; // add array to class
90
			return array();
91
		}
92
		
93
		/* executes the CmdCHAIN one command at a time */
94
		function execute() {
95
			foreach($this->cmd_chain_array as $cmd) {
96
				$cmd_title = $cmd['cmd_title'];
97
				$command = $cmd['command'];
98
				$ignore_return_text = $cmd['ignore_return_text'];
99
				// Should we perform verbose debugging?
100
				if($this->is_debugging == true) {
101
					log_error("CmdCHAIN is executing -> {$cmd_title} - {$command}");
102
					usleep(100);	// give network stack time to deliver network syslog message
103
				}
104
				// Execute command
105
				$status = exec($command);
106
				if($this->ignore_return_text == true) 
107
					continue;
108
				if(intval($status) <> 0) {
109
					log_error("{$cmd_title} failed with return code -> {$status}.  The command was {$command}");
110
					if($this->halt_on_errors == true) 
111
						return("{$cmd_title} failed with return code -> {$status}.  The command was {$command}");
112
				}
113
			}
114
			return;
115
		}
116
}
117

    
118
/* 
119
 * example usage:
120
 *
121

    
122
$cmdchain = new CmdCHAIN();
123
$cmdchain->add("grab freebsd version", "uname -a", false);
124
$cmdchain->setdebug(); // optional for verbose logging
125
$cmdchain->nohaltonerror(); // tells cmdchain to keep processing commands if any of them fail
126
$cmdchain->execute();
127

    
128
$cmdchain->clear(); // clears the previous added entries
129

    
130
*/
131

    
132
?>
(9-9/61)