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