Subversion Repositories havirt

Rev

Rev 45 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 45 Rev 46
Line 149... Line 149...
149
 
149
 
150
                  
150
                  
151
print "Command = $command\nAction = $action\n" if $config->{'flags'}->{'debug'};
151
print "Command = $command\nAction = $action\n" if $config->{'flags'}->{'debug'};
152
 
152
 
153
 
153
 
154
# we allow a three part command for some actions on a domain, ie start, shutdown, migrate and destroy
154
# Allow shortcut commands for domains (start, shutdown, migrate, destroy)
155
# for simplicity, if the command is one of the above, allow the user to enter like that, but we will
-
 
156
# restructure the command as if they had used the full, three part (ie, with domain as the command)
-
 
157
# so, the following are equivilent
-
 
158
# havirt domain start nameofdomain
-
 
159
# havirt start nameofdomain
-
 
160
 
-
 
161
if ( $command eq 'start' || $command eq 'shutdown' || $command eq 'migrate' || $command eq 'destroy' ) { # shortcut for working with domains
155
if ( $command =~ /^(start|shutdown|migrate|destroy)$/ ) {
162
   push @ARGV, $action; # this is the domain we are working with
156
    push @ARGV, $action;      # domain name becomes argument
163
   $action = $command; # this is what we want to do (start, shutdown, etc...)
157
    $action  = $command;      # action is the command
164
   $command = 'domain'; # keywork domain, for correct module
158
    $command = 'domain';      # use the domain module
165
}
159
}
166
 
160
 
167
# ok, this is some serious weirdness. $command is actually the name of a module, and $action is a method
161
# ok, this is some serious weirdness. $command is actually the name of a module, and $action is a method
168
# defined in the module.
162
# defined in the module.
169
 
163
 
Line 176... Line 170...
176
 
170
 
177
 
171
 
178
# we have to concat here since the double colon causes some interpretation problems
172
# we have to concat here since the double colon causes some interpretation problems
179
my $execute = $command . '::' . $action;
173
my $execute = $command . '::' . $action;
180
 
174
 
181
# load the module, die if it doesn't exist.
175
# Load the module dynamically, die if it doesn't exist
182
if (-f $config->{'script dir'} . "/$command.pm" ) {
176
my $module_path = $config->{'script dir'} . "/$command.pm";
-
 
177
if ( -f $module_path ) {
183
   require "$command.pm";
178
    require "$command.pm";
184
   Module->import( $command ); # for require, you must manually import
179
    Module->import($command); # For require, must manually import
185
} else {
180
} else {
186
   die "Error, I don't know the command [$command]\n";
181
   die "Error, I don't know the command [$command]\n";
187
}
182
}
188
 
183
 
189
if ( defined &{\&{$execute}} ) { # check if module::sub exists (ie, $command::action)
184
if ( defined &{\&{$execute}} ) { # check if module::sub exists (ie, $command::action)
190
  my $message =  &{\&{$execute}}(@ARGV); # yes, it exists, so call it with any remaining arguments
185
  my $message =  &{\&{$execute}}(@ARGV); # yes, it exists, so call it with any remaining arguments
191
  if ( ! $main::config->{'flags'}->{'quiet'} ) {
186
  print $message unless !$message || $main::config->{'flags'}->{'quiet'} && !$main::config->{'flags'}->{'dryrun'};
192
     print $message if $message;
-
 
193
  }
-
 
194
} else { # method $action does not exist in module $command, so just a brief error message
187
} else { # method $action does not exist in module $command, so just a brief error message
195
  die "Error, could not find action [$action] for module [$command]\n";
188
  die "Error, could not find action [$action] for module [$command]\n";
196
} 
189
} 
197
 
190
 
198
1;
191
1;