2 # Copyright (c) 2008,2009 Zmanda, Inc. All Rights Reserved.
4 # This program is free software; you can redistribute it and/or modify it
5 # under the terms of the GNU General Public License version 2 as published
6 # by the Free Software Foundation.
8 # This program is distributed in the hope that it will be useful, but
9 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 # You should have received a copy of the GNU General Public License along
14 # with this program; if not, write to the Free Software Foundation, Inc.,
15 # 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 # Contact information: Zmanda Inc., 465 S. Mathilda Ave., Suite 300
18 # Sunnyvale, CA 94085, USA, or: http://www.zmanda.com
20 package Amanda::Script_App;
30 use Amanda::Constants;
31 use Amanda::Config qw( :init :getconf config_dir_relative );
32 use Amanda::Debug qw( :logging );
34 use Amanda::Util qw( :constants );
39 Amanda::Script_App - perl utility functions for Scripts.
43 This module should not be used directly. Instead, use C<Amanda::Application> or
50 my ($execute_where, $type, $config_name) = @_;
53 bless ($self, $class);
55 # extract the last component of the class name
58 $self->{'name'} = $name;
60 if(!defined $execute_where) {
61 $execute_where = "client";
63 Amanda::Util::setup_application($name, $execute_where, $CONTEXT_DAEMON);
64 debug("Arguments: " . join(' ', @ARGV));
66 #initialize config client to get values from amanda-client.conf
67 config_init($CONFIG_INIT_CLIENT, undef);
68 my ($cfgerr_level, @cfgerr_errors) = config_errors();
69 if ($cfgerr_level >= $CFGERR_WARNINGS) {
70 config_print_errors();
71 if ($cfgerr_level >= $CFGERR_ERRORS) {
72 confess("errors processing config file");
76 config_init($CONFIG_INIT_CLIENT | $CONFIG_INIT_EXPLICIT_NAME | $CONFIG_INIT_OVERLAY, $config_name);
77 ($cfgerr_level, @cfgerr_errors) = config_errors();
78 if ($cfgerr_level >= $CFGERR_WARNINGS) {
79 config_print_errors();
80 if ($cfgerr_level >= $CFGERR_ERRORS) {
81 confess("errors processing config file for $config_name");
86 Amanda::Util::finish_setup($RUNNING_AS_ANY);
88 $self->{error_status} = $Amanda::Script_App::GOOD;
89 $self->{type} = $type;
90 $self->{known_commands} = {};
92 debug("$type: $name\n");
99 #$_[1] status: GOOD or ERROR
100 sub print_to_server {
102 my($msg, $status) = @_;
104 $self->{error_status} = $status;
106 if ($self->{action} eq "check") {
107 if ($status == $Amanda::Script_App::GOOD) {
108 print STDOUT "OK $msg\n";
110 print STDOUT "ERROR $msg\n";
112 } elsif ($self->{action} eq "estimate") {
113 if ($status == $Amanda::Script_App::GOOD) {
116 print STDERR "ERROR $msg\n";
118 } elsif ($self->{action} eq "backup") {
119 if ($status == $Amanda::Script_App::GOOD) {
120 print {$self->{mesgout}} "| $msg\n";
121 } elsif ($status == $Amanda::Script_App::ERROR) {
122 print {$self->{mesgout}} "? $msg\n";
124 print {$self->{mesgout}} "sendbackup: error $msg\n";
126 } elsif ($self->{action} eq "restore") {
127 print STDERR "$msg\n";
128 } elsif ($self->{action} eq "validate") {
129 print STDERR "$msg\n";
131 print STDERR "$msg\n";
136 #$_[1] status: GOOD or ERROR
137 sub print_to_server_and_die {
140 $self->print_to_server( @_ );
141 if (!defined $self->{die} && $self->can("check_for_backup_failure")) {
143 $self->check_for_backup_failure();
153 if (!defined $command) {
154 $self->print_to_server_and_die("check", "no command",
155 $Amanda::Script_App::ERROR);
158 $command =~ tr/A-Z-/a-z_/;
159 debug("command: $command");
161 # first make sure this is a valid command.
162 if (!exists($self->{known_commands}->{$command})) {
163 print STDERR "Unknown command `$command'.\n";
167 my $action = $command;
168 $action =~ s/^pre_//;
169 $action =~ s/^post_//;
170 $action =~ s/^inter_//;
171 $action =~ s/^dle_//;
172 $action =~ s/^host_//;
173 $action =~ s/^level_//;
175 if ($action eq 'amcheck' || $action eq 'selfcheck') {
176 $self->{action} = 'check';
177 } elsif ($action eq 'estimate') {
178 $self->{action} = 'estimate';
179 } elsif ($action eq 'backup') {
180 $self->{action} = 'backup';
181 } elsif ($action eq 'recover' || $action eq 'restore') {
182 $self->{action} = 'restore';
183 } elsif ($action eq 'validate') {
184 $self->{action} = 'validate';
187 if ($action eq 'backup') {
188 $self->_set_mesgout();
191 # now convert it to a function name and see if it's
193 my $function_name = "command_$command";
194 my $default_name = "default_$command";
196 if (!$self->can($function_name)) {
197 if (!$self->can($default_name)) {
198 print STDERR "command `$command' is not supported by the '" .
199 $self->{name} . "' " . $self->{type} . ".\n";
202 $self->$default_name();
206 # it exists -- call it
207 $self->$function_name();