]> rtime.felk.cvut.cz Git - novaboot.git/commitdiff
AMT: cleanups, simplifications
authorMichal Sojka <sojkam1@fel.cvut.cz>
Fri, 14 Feb 2014 17:23:43 +0000 (18:23 +0100)
committerMichal Sojka <sojkam1@fel.cvut.cz>
Fri, 14 Feb 2014 17:27:34 +0000 (18:27 +0100)
novaboot

index 887702bce158b8d1e88729ecffb4adc1328f7506..98562e4fa5e68836263a100d02966e3c1e33b030 100755 (executable)
--- a/novaboot
+++ b/novaboot
@@ -547,17 +547,17 @@ elsif ($serial) {
 } elsif (defined $amt) {
     use LWP::UserAgent;
     use LWP::Authen::Digest;
-    sub genXML { #HOST, username, password, schema, className, pstate
+
+    sub genXML {
         my ($host, $username, $password, $schema, $className, $pstate) = @_;
-        #AMT numbers for PowerStateChange: 2 on, 4 standby, 7 hibernate, 8 off,
-        #10 reset, 11 MNI interupt(on windows->bluescreen;-))
-        my %pstates = ("on", 2,
-                      "standby", 4,
-                      "hibernate", 7,
-                      "off", 8,
-                      "reset", 10,
-                      "MNI", 11);
-        my $text = <<END;
+       #AMT numbers for PowerStateChange (MNI => bluescreen on windows;-)
+       my %pstates = ("on"        => 2,
+                      "standby"   => 4,
+                      "hibernate" => 7,
+                      "off"       => 8,
+                      "reset"     => 10,
+                      "MNI"       => 11);
+        return <<END;
                <s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:w="http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd">
                <s:Header><a:To>http://$host:16992/wsman</a:To>
                <w:ResourceURI s:mustUnderstand="true">$schema</w:ResourceURI>
@@ -578,34 +578,32 @@ elsif ($serial) {
                </p:RequestPowerStateChange_INPUT>
                </s:Body></s:Envelope>
 END
-        return $text;
     }
 
-    sub sendPOST{ #ip, username, password, content
-        my ($host, $username, $password, $content)=@_;+
-           my $req = HTTP::Request->new(POST => "http://$host:16992/wsman");
+    sub sendPOST {
+        my ($host, $username, $password, $content) = @_;
+
         my $ua = LWP::UserAgent->new();
+        $ua->agent("novaboot");
+
+       my $req = HTTP::Request->new(POST => "http://$host:16992/wsman");
         my $res = $ua->request($req);
-        die ($res->status_line) unless $res->code==401;
-        my $header = $res->header("WWW-Authenticate");
-        my $digRealm = "Digest realm=\"";
-        my $posRealm = index($header,$digRealm)+length($digRealm);
-        my $realm = substr($header,$posRealm,index($header,"\"",$posRealm)-$posRealm);
+        die ("Unexpected AMT response: " . $res->status_line) unless $res->code == 401;
+
+        my ($realm) = $res->header("WWW-Authenticate") =~ /Digest realm="(.*?)"/;
+        $ua->credentials("$host:16992", $realm, $username => $password);
 
-        $ua = LWP::UserAgent->new();
-        $ua->agent("novaboot");
-        $ua->credentials("$host:16992",$realm,  $username => $password);
         # Create a request
         $req = HTTP::Request->new(POST => "http://$host:16992/wsman");
         $req->content_type('application/x-www-form-urlencoded');
         $req->content($content);
         $res = $ua->request($req);
-        die ($res->status_line) unless $res->is_success;
+        die ("AMT power change request failed: " . $res->status_line) unless $res->is_success;
         $res->content() =~ /<g:ReturnValue>(\d+)<\/g:ReturnValue>/;
         return $1;
     }
 
-    sub powerChange  {#IP, username, password, pstate
+    sub powerChange  {
         my ($host, $username, $password, $pstate)=@_;
         my $schema="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_PowerManagementService";
         my $className="/RequestPowerStateChange";
@@ -613,32 +611,36 @@ END
         return sendPOST($host, $username, $password, $content);
     }
 
-    my (@amt_info, $len, $amt_password, $result, $host, $port, $user);
-    ($user,$amt_password,$host,$port) = ($amt =~ /(?:(.*?)(?::(.*))?@)?([^:]*)(?::([0-9]*))?/);;
-    $len=@amt_info;
-    die "AMT host not specified" unless $host;
+    my ($user,$amt_password,$host,$port) = ($amt =~ /(?:(.*?)(?::(.*))?@)?([^:]*)(?::([0-9]*))?/);;
     $user ||= "admin";
-    $port ||= 16994;
     $amt_password ||= $ENV{'AMT_PASSWORD'} || die "AMT password not specified";
+    $host || die "AMT host not specified";
+    $port ||= 16994;
+
     $target_power_off = sub {
-       $result = powerChange($host,$user,$amt_password, "off");
-       die "Cannot turn off the computer, somebody is connected or computer is already off" unless $result==0;
+       my $result = powerChange($host,$user,$amt_password, "off");
+       die "AMT power off failed (ReturnValue $result)" if $result != 0;
     };
 
     $target_power_on = sub {
-       $result = powerChange($host,$user,$amt_password, "on");
+       my $result = powerChange($host,$user,$amt_password, "on");
+       die "AMT power on failed (ReturnValue $result)" if $result != 0;
     };
 
     $target_reset = sub {
-       $result = powerChange($host,$user,$amt_password, "reset");
+       my $result = powerChange($host,$user,$amt_password, "reset");
        if ($result != 0) {
            print STDERR "Warning: Cannot reset $host, trying power on. ";
            $result = powerChange($host,$user,$amt_password, "on");
        }
-       die "AMT reset failed (code $result)" if $result;
-       sleep(2); # Without pause, connection to AMT machine is initiated before reset and fails after restart
+       die "AMT reset failed (ReturnValue $result)" if $result != 0;
+
+       sleep(2); # Without pause, connection to AMT machine is
+                 # initiated before reset and fails after restart
+
+       # amtterm must be run here because AMT doesn't allow to
+       # reset/on/off computer when somebody is connected
        $exp = Expect->spawn("amtterm -u $user -p $amt_password $host $port");
-       #expect must be here because AMT doesnt allow to reset/on/off computer when somebody is connected
     };
 }