dak1n1

bkr-edit

Sep 9th, 2012
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 4.26 KB | None | 0 0
  1. #!/usr/bin/perl
  2. #
  3. # A script to automate clicking through the Beaker webui for a given machine
  4. # Stefanie Edgar
  5. # Sept. 8 2012
  6.  
  7. use strict;
  8. use warnings;
  9. use WWW::Mechanize;
  10. use LWP::ConnCache;
  11. use LWP::Authen::Negotiate; # kerb support
  12. use HTTP::Cookies;
  13. use Getopt::Long;
  14.  
  15. my $host;                   # Beaker host name. Required.
  16. my $loan_machine = 1;       # default: loans the host to you
  17. my $return_machine;         # default: don't return loaned host to Beaker
  18.  
  19. GetOptions (
  20.         'host=s' => \$host,        
  21.         'loan'   => \$loan_machine,  
  22.         'return' => \$return_machine) or die print_usage();
  23.  
  24. die print_usage() unless defined $host;
  25.  
  26. # -- Configuration: Beaker login -- #
  27. # it should actually just use the kerberos ticket on your localhost
  28. # be sure to run 'kinit' to get a valid ticket before running this
  29. my $userid = 'sedgar';
  30. my $password = 'moo';
  31. my $beaker = "https://beaker.engineering.redhat.com";
  32. my $webpage = "$beaker/view/$host";
  33.  
  34. # set up automated web user agent
  35. my $agent = WWW::Mechanize->new(keep_alive => 1);
  36. $agent->conn_cache(LWP::ConnCache->new);
  37. $agent->cookie_jar(HTTP::Cookies->new());
  38. $agent->credentials("https://beaker.engineering.redhat.com:80", "REDHAT.COM", "$userid", "$password");
  39.  
  40. # trick Beaker into letting us authenticate by pulling up an admin-only page
  41. # this works around the referrer redirect problem encountered when logging in through the Login page
  42. my $response = $agent->get("https://beaker.engineering.redhat.com/csv/csv_import");
  43. if ($response->is_success) {
  44.    print "Successful login \n";
  45. }
  46. else {
  47.     die $response->status_line;
  48. }
  49.  
  50. # fetch webpage for host
  51. $agent->get("$webpage");
  52. $agent->success() and print "Successfully fetched host page \n" or die "Failed to fetch host page \n";
  53.  
  54. # do whatever we've been instructed to
  55. if ($return_machine) {
  56.    return_machine();
  57.    exit(0);
  58. }
  59. elsif ($loan_machine) {
  60.    loan_machine();
  61.    set_automated();
  62. };
  63.  
  64. # remove credentials cache
  65. $agent->clear_credentials();
  66.  
  67. exit(0);
  68.  
  69. # -- Functions -- #
  70.  
  71. # Function loan_machine
  72. # loans the machine out to the specified user
  73. sub loan_machine {
  74.  
  75.     # if the machine is available to loan, grab the 'loan' link
  76.     my $loan = $agent->find_link( text => '(Loan)', url_regex => qr/loan_change/ );
  77.     if ($loan) {
  78.         $agent->follow_link( url => $loan->url);
  79.         print "Host \'$host\' is not currently loaned out. Loaning...\n"
  80.     }
  81.     else {
  82.        # if the machine is already loaned out, grab the 'change' link
  83.        my $change = $agent->find_link( text => '(Change)', url_regex => qr/loan_change/ );
  84.        $agent->follow_link( url => $change->url );
  85.        print "Host \'$host\' is currently loaned out. Changing loan to \'$userid\'...\n"
  86.     };
  87.  
  88.     # insert userid into 'loan' text field
  89.     $agent->form_id('Loan');
  90.     $agent->field("user", $userid);
  91.  
  92.     my $submit = $agent->click();
  93.     $submit->is_success and print "Successfully loaned \'$host\' to \'$userid\'" or die "Couldn't loan \'$host\' to \'$userid\'\n";
  94.  
  95. };
  96.  
  97. # Function set_automated
  98. # enters the host's 'edit' page and sets the status to 'automated'.
  99. sub set_automated {
  100.  
  101.     $agent->get("$beaker/edit/$host");
  102.     $agent->success() and print "Successfully fetched 'Edit System' page \n" or die "Failed to fetch 'Edit System' page \n";
  103.  
  104.     $agent->form_name('Form');
  105.     $agent->field("status", "Automated");
  106.  
  107.     my $submit = $agent->click();
  108.     $submit->is_success and print "System set to 'automated'\n" or die "Couldn't submit edit form";
  109. };
  110.  
  111. sub return_machine {
  112.  
  113.     my $loan = $agent->find_link( text => '(Return)', url_regex => qr/loan_change/ );
  114.     if ($loan) {
  115.         $agent->follow_link( url => $loan->url);
  116.         print "Host \'$host\' is loaned out. Returning...\n";
  117.         $agent->success() and print "Loan returned for \'$host\'.\n" or die "Loan NOT returned for host \'$host\'!\n";
  118.     }
  119.     else {
  120.        print "Host \'$host\' is not currently loaned out! Nothing to do. \n"
  121.     };
  122. };
  123.  
  124. sub print_usage {
  125.     print "bkr-edit, a script to automate Beaker webui tasks.\nSupports 'loan' (to your user), or 'return' loaned machine to Beaker. Machines loaned to you will automatically be set to 'automated' status.\n    Usage: $0 --host=<beaker-host> --loan --return \n";
  126. };
Advertisement
Add Comment
Please, Sign In to add comment