Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- #
- # A script to automate clicking through the Beaker webui for a given machine
- # Stefanie Edgar
- # Sept. 8 2012
- use strict;
- use warnings;
- use WWW::Mechanize;
- use LWP::ConnCache;
- use LWP::Authen::Negotiate; # kerb support
- use HTTP::Cookies;
- use Getopt::Long;
- my $host; # Beaker host name. Required.
- my $loan_machine = 1; # default: loans the host to you
- my $return_machine; # default: don't return loaned host to Beaker
- GetOptions (
- 'host=s' => \$host,
- 'loan' => \$loan_machine,
- 'return' => \$return_machine) or die print_usage();
- die print_usage() unless defined $host;
- # -- Configuration: Beaker login -- #
- # it should actually just use the kerberos ticket on your localhost
- # be sure to run 'kinit' to get a valid ticket before running this
- my $userid = 'sedgar';
- my $password = 'moo';
- my $beaker = "https://beaker.engineering.redhat.com";
- my $webpage = "$beaker/view/$host";
- # set up automated web user agent
- my $agent = WWW::Mechanize->new(keep_alive => 1);
- $agent->conn_cache(LWP::ConnCache->new);
- $agent->cookie_jar(HTTP::Cookies->new());
- $agent->credentials("https://beaker.engineering.redhat.com:80", "REDHAT.COM", "$userid", "$password");
- # trick Beaker into letting us authenticate by pulling up an admin-only page
- # this works around the referrer redirect problem encountered when logging in through the Login page
- my $response = $agent->get("https://beaker.engineering.redhat.com/csv/csv_import");
- if ($response->is_success) {
- print "Successful login \n";
- }
- else {
- die $response->status_line;
- }
- # fetch webpage for host
- $agent->get("$webpage");
- $agent->success() and print "Successfully fetched host page \n" or die "Failed to fetch host page \n";
- # do whatever we've been instructed to
- if ($return_machine) {
- return_machine();
- exit(0);
- }
- elsif ($loan_machine) {
- loan_machine();
- set_automated();
- };
- # remove credentials cache
- $agent->clear_credentials();
- exit(0);
- # -- Functions -- #
- # Function loan_machine
- # loans the machine out to the specified user
- sub loan_machine {
- # if the machine is available to loan, grab the 'loan' link
- my $loan = $agent->find_link( text => '(Loan)', url_regex => qr/loan_change/ );
- if ($loan) {
- $agent->follow_link( url => $loan->url);
- print "Host \'$host\' is not currently loaned out. Loaning...\n"
- }
- else {
- # if the machine is already loaned out, grab the 'change' link
- my $change = $agent->find_link( text => '(Change)', url_regex => qr/loan_change/ );
- $agent->follow_link( url => $change->url );
- print "Host \'$host\' is currently loaned out. Changing loan to \'$userid\'...\n"
- };
- # insert userid into 'loan' text field
- $agent->form_id('Loan');
- $agent->field("user", $userid);
- my $submit = $agent->click();
- $submit->is_success and print "Successfully loaned \'$host\' to \'$userid\'" or die "Couldn't loan \'$host\' to \'$userid\'\n";
- };
- # Function set_automated
- # enters the host's 'edit' page and sets the status to 'automated'.
- sub set_automated {
- $agent->get("$beaker/edit/$host");
- $agent->success() and print "Successfully fetched 'Edit System' page \n" or die "Failed to fetch 'Edit System' page \n";
- $agent->form_name('Form');
- $agent->field("status", "Automated");
- my $submit = $agent->click();
- $submit->is_success and print "System set to 'automated'\n" or die "Couldn't submit edit form";
- };
- sub return_machine {
- my $loan = $agent->find_link( text => '(Return)', url_regex => qr/loan_change/ );
- if ($loan) {
- $agent->follow_link( url => $loan->url);
- print "Host \'$host\' is loaned out. Returning...\n";
- $agent->success() and print "Loan returned for \'$host\'.\n" or die "Loan NOT returned for host \'$host\'!\n";
- }
- else {
- print "Host \'$host\' is not currently loaned out! Nothing to do. \n"
- };
- };
- sub print_usage {
- 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";
- };
Advertisement
Add Comment
Please, Sign In to add comment