more notes on configuration
[buildfarm-client.git] / update_personality
1 #!/usr/bin/env perl
2
3 =comment
4
5 Copyright (c) 2003-2010, Andrew Dunstan
6
7 See accompanying License file for license details
8
9 =cut
10
11 use vars qw($VERSION); $VERSION = '0.1';
12
13 use strict;
14 use warnings;
15 no warnings qw(once); # suppress spurious warning about conf structure
16
17 use LWP;
18 use HTTP::Request::Common;
19 use MIME::Base64;
20 use Digest::SHA  qw(sha1_hex);
21 use Getopt::Long;
22
23 # copy command line before processing - so we can later report it
24 # unmunged
25
26 my @invocation_args = (@ARGV);
27
28 my $buildconf = "build-farm.conf"; # default value
29 my ($os_version, $compiler_version,$help);
30
31 if ($0 =~ /(.*)\.pl$/) {
32     die "$0: Please use `$1@{[@ARGV ? qq{ @ARGV} : '']}` instead\n"
33         if -t;
34     exec $1, @ARGV;
35 }
36
37 GetOptions(
38     'config=s' => \$buildconf,
39     'help' => \$help,
40     'os-version=s' => \$os_version,
41     'compiler-version=s' => \$compiler_version,
42 )|| usage("bad command line");
43
44 usage("No extra args allowed")
45   if @_;
46
47 usage()
48   if $help;
49
50 usage("must specify at least one item to change")
51   unless ($os_version or $compiler_version);
52
53 #
54 # process config file
55 #
56 require $buildconf;
57
58 my ($target,$animal,$secret,$upgrade_target) =
59   @EximBuild::conf{qw(target animal secret upgrade_target)};
60
61 # default for old config files
62 unless ($upgrade_target)
63 {
64     $upgrade_target = $target;
65     $upgrade_target =~ s/eximstatus.pl/upgrade.pl/;
66 }
67
68 # make the base64 data escape-proof; = is probably ok but no harm done
69 # this ensures that what is seen at the other end is EXACTLY what we
70 # see when we calculate the signature
71
72 map{ $_ ||= ""; $_ = encode_base64($_,""); tr/+=/$@/; }
73   ($os_version,$compiler_version);
74
75 my $ts = time;
76
77 my $content = "animal=$animal\&ts=$ts";
78 $content .= "\&new_os=$os_version" if $os_version;
79 $content .= "\&new_compiler=$compiler_version" if $compiler_version;
80
81 my $sig= sha1_hex($content,$secret);
82
83 # set environment from config
84 while (my ($envkey,$envval) = each %{$EximBuild::conf{build_env}})
85 {
86     $ENV{$envkey}=$envval;
87 }
88
89 my $ua = new LWP::UserAgent;
90 $ua->agent("Exim Build Farm Reporter");
91 if (my $proxy = $ENV{BF_PROXY})
92 {
93     $ua->proxy('http',$proxy);
94 }
95
96 my $request=HTTP::Request->new(POST => "$upgrade_target/$sig");
97 $request->content_type("application/x-www-form-urlencoded");
98 $request->content($content);
99
100 my $response=$ua->request($request);
101
102 unless ($response->is_success)
103 {
104     print
105       "Query for: animal=$animal&ts=$ts\n",
106       "Target: $upgrade_target/$sig\n",
107       "Query Content: $content\n";
108     print "Status Line: ",$response->status_line,"\n";
109     print "Content: \n", $response->content,"\n";
110     exit 1;
111 }
112
113 exit(0);
114
115 #######################################################################
116
117 sub usage
118 {
119     my $opt_message = shift;
120     print "$opt_message\n" if $opt_message;
121     print  <<EOH;
122 update_personality [ option ... ]
123 where option is one or more of
124   --config=path                 /path/to/buildfarm.conf
125   --os-version=version          new operating system version
126   --compiler-version=version    new compiler version
127   --help                        get this message
128 EOH
129
130     exit defined($opt_message)+0;
131 }
132