Maintenance scripts
[buildfarm-server.git] / scripts / approve_system.pl
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5 use DBI;
6 use Mail::Send;
7 use Data::Dumper;
8
9 die "Must pass current sysname and new sysname\n" unless scalar @ARGV == 2;
10
11 use vars qw($dbhost $dbname $dbuser $dbpass $dbport
12             $user_list_format
13             $default_host $mail_from
14 );
15 require "$ENV{BFConfDir}/BuildFarmWeb.pl";
16
17 die "no dbname" unless $dbname;
18 die "no dbuser" unless $dbuser;
19
20 my $dsn="dbi:Pg:dbname=$dbname";
21 $dsn .= ";host=$dbhost" if $dbhost;
22 $dsn .= ";port=$dbport" if $dbport;
23
24 my $db = DBI->connect($dsn,$dbuser,$dbpass);
25
26 die $DBI::errstr unless $db;
27
28 $db->do('SELECT approve(?, ?)', undef, @ARGV);
29
30 my $sth = $db->prepare(q[ 
31        SELECT name, status, operating_system, os_version, sys_owner, owner_email,
32               secret, compiler, compiler_version, architecture
33        FROM buildsystems AS b
34        ORDER BY name ASC
35       ]);
36 $sth->execute();
37
38 sub send_welcome_email
39 {
40   my $row = shift() or return;
41   my $msg = new Mail::Send;
42   my $me = `id -un`; chomp($me);
43   my $host = `hostname`; chomp($host);
44   $host = $default_host unless ($host =~ m/[.]/ || !defined($default_host));
45   my $from_addr = $mail_from ?
46                   "Exim BuildFarm <$mail_from>" :
47                   "Exim BuildFarm <$me\@$host>" ;
48   $from_addr =~ tr /\r\n//d;
49   $msg->set('From',$from_addr);
50   $msg->to($row->{owner_email});
51   $msg->subject('Exim BuildFarm Application Approved');
52   my $fh = $msg->open;
53   print $fh "\n\nCongratulations $row->{sys_owner},\n",
54             "Your application for the Exim BuildFarm has been accepted.\n\n",
55             "Please set the following in your build-farm.conf:\n",
56             "Animal:  $row->{name}\n",
57             "Secret:  $row->{secret}\n\n",
58             "BuildFarm machine details:\n",
59             "Distro  : $row->{operating_system}\n",
60             "+Version: $row->{os_version}\n",
61             "Arch    : $row->{architecture}\n",
62             "Compiler: $row->{compiler}\n",
63             "+Version: $row->{compiler_version}\n\n",
64             "If you update your system, either the Distro or compiler version\n",
65             "you can use the update_personality.pl script to update the\n",
66             "version stored in the BuildFarm database.\n\n",
67             "-- The Exim BuildFarm Maintainers";
68   $fh->close;
69 }
70
71 printf $user_list_format,
72        "SysName", "Status", "Owner", "Email", "Distro", "Version";
73 while (my $row = $sth->fetchrow_hashref)
74 {
75   printf $user_list_format,
76                   $row->{name}, $row->{status}, $row->{sys_owner},
77                   $row->{owner_email}, $row->{operating_system},
78                   $row->{os_version};
79   if ($row->{name} eq $ARGV[1])
80   {
81     &send_welcome_email($row);
82   }
83 }
84 $db->disconnect();
85