Project

General

Profile

Download (3.9 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

    
31
/* 
32
 * HANDY FOR STAND ALONE DEBUGGING OF CLASS. 
33
 *
34

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

    
39
*/
40

    
41
/*
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
Class CmdCHAIN {
48

    
49
		var $cmd_chain_array = array();
50
		var $is_debugging;
51
		var $halt_on_errors = true;
52
		
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
		
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

    
74
		/* adds a command to the CmdCHAIN */
75
		function add($cmd_title = "", $command = "", $ignore_return_text = false) {
76
			if(!$cmd_title) 
77
				return;
78
			if(!$command) 
79
				return;
80
			$temp = array();
81
			$temp['cmd_title'] = $cmd_title;
82
			$temp['command'] = $command;
83
			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
		}
90
		
91
		/* executes the CmdCHAIN one command at a time */
92
		function execute() {
93
			foreach($this->cmd_chain_array as $cmd) {
94
				$cmd_title = $cmd['cmd_title'];
95
				$command = $cmd['command'];
96
				$ignore_return_text = $cmd['ignore_return_text'];
97
				// Should we perform verbose debugging?
98
				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
				// Execute command
103
				$status = exec($command);
104
				if($this->ignore_return_text == true) 
105
					continue;
106
				if(intval($status) <> 0) {
107
					log_error("{$cmd_title} failed with return code -> {$status}.  The command was {$command}");
108
					if($this->halt_on_errors == true) 
109
						return("{$cmd_title} failed with return code -> {$status}.  The command was {$command}");
110
				}
111
			}
112
			return;
113
		}
114
}
115

    
116
/* 
117
 * example usage:
118
 *
119

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

    
126
$cmdchain->clear(); // clears the previous added entries
127

    
128
*/
129

    
130
?>
(8-8/43)