Project

General

Profile

Download (4.01 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 523855b0 Scott Ullrich
30
	pfSense_MODULE:	utils
31 7b996ea3 Scott Ullrich
*/
32
33
/* 
34
 * HANDY FOR STAND ALONE DEBUGGING OF CLASS. 
35
 *
36 77cdcbad Scott Ullrich
37 7b996ea3 Scott Ullrich
	function log_error($text) {
38
		echo $text . "\n";
39
	}
40 77cdcbad Scott Ullrich
41 7b996ea3 Scott Ullrich
*/
42
43 77cdcbad Scott Ullrich
/*
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 7b996ea3 Scott Ullrich
Class CmdCHAIN {
50
51
		var $cmd_chain_array = array();
52
		var $is_debugging;
53 5fec455a Scott Ullrich
		var $halt_on_errors = true;
54 7b996ea3 Scott Ullrich
		
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 5fec455a Scott Ullrich
		
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 7b996ea3 Scott Ullrich
76 77cdcbad Scott Ullrich
		/* adds a command to the CmdCHAIN */
77
		function add($cmd_title = "", $command = "", $ignore_return_text = false) {
78 7b996ea3 Scott Ullrich
			if(!$cmd_title) 
79 77cdcbad Scott Ullrich
				return;
80
			if(!$command) 
81
				return;
82 7b996ea3 Scott Ullrich
			$temp = array();
83
			$temp['cmd_title'] = $cmd_title;
84
			$temp['command'] = $command;
85 77cdcbad Scott Ullrich
			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 7b996ea3 Scott Ullrich
		}
92
		
93 77cdcbad Scott Ullrich
		/* executes the CmdCHAIN one command at a time */
94 7b996ea3 Scott Ullrich
		function execute() {
95
			foreach($this->cmd_chain_array as $cmd) {
96
				$cmd_title = $cmd['cmd_title'];
97
				$command = $cmd['command'];
98 77cdcbad Scott Ullrich
				$ignore_return_text = $cmd['ignore_return_text'];
99
				// Should we perform verbose debugging?
100 7b996ea3 Scott Ullrich
				if($this->is_debugging == true) {
101 addc0439 Renato Botelho
					log_error(sprintf(gettext('CmdCHAIN is executing -> %1$s - %2$s'), $cmd_title, $command));
102 7b996ea3 Scott Ullrich
					usleep(100);	// give network stack time to deliver network syslog message
103
				}
104 77cdcbad Scott Ullrich
				// Execute command
105 7b996ea3 Scott Ullrich
				$status = exec($command);
106 5fec455a Scott Ullrich
				if($this->ignore_return_text == true) 
107
					continue;
108
				if(intval($status) <> 0) {
109 addc0439 Renato Botelho
					log_error(sprintf(gettext('%1$s failed with return code -> %2$s.  The command was %3$s'), $cmd_title, $status, $command));
110 5fec455a Scott Ullrich
					if($this->halt_on_errors == true) 
111 addc0439 Renato Botelho
						return(sprintf(gettext('%1$s failed with return code -> %2$s.  The command was %3$s'), $cmd_title, $status, $command));
112 7b996ea3 Scott Ullrich
				}
113
			}
114
			return;
115
		}
116
}
117
118
/* 
119
 * example usage:
120
 *
121
122
$cmdchain = new CmdCHAIN();
123 77cdcbad Scott Ullrich
$cmdchain->add("grab freebsd version", "uname -a", false);
124
$cmdchain->setdebug(); // optional for verbose logging
125 5025a56c Scott Ullrich
$cmdchain->nohaltonerror(); // tells cmdchain to keep processing commands if any of them fail
126 7b996ea3 Scott Ullrich
$cmdchain->execute();
127
128 5025a56c Scott Ullrich
$cmdchain->clear(); // clears the previous added entries
129
130 7b996ea3 Scott Ullrich
*/
131
132 e96d2182 Carlos Eduardo Ramos
?>