source: ror-farm/ror-build-haproxy @ 370

Last change on this file since 370 was 370, checked in by lucas, 11 years ago

ror-build-haproxy: default backend is hostname, since haproxy runs on the root server hosting the admin rorette

  • Property svn:executable set to *
File size: 2.2 KB
Line 
1#!/usr/bin/perl
2
3# ror-build-haproxy (c) 2010 Bearstech - http://bearstech.com
4#
5# Depends: util-vserver vserver-debiantools haproxy
6
7# This program is free software: you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation, either version 3 of the License, or
10# (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
20# Automatically build the lighttpd config file to dispatch queries to
21# the appropriate vserver according to their URLs
22
23use strict;
24use warnings;
25
26use Text::Template;
27use POSIX qw(strftime);
28
29use Data::Dumper;
30
31# Variables
32my $haproxy_cfg = "haproxy/haproxy.cfg";
33my $haproxy_template = "haproxy/haproxy.cfg.tmpl";
34
35# RoR hosts
36my @hosts = ('twix9','kitkat4');
37my $my_hostname = `hostname`;
38chomp $my_hostname;
39
40# vserver config
41my $vserver_dir   = "/etc/vservers";
42
43my %rorettes;
44
45# Functions
46sub list_rorettes;
47
48### Start
49
50%rorettes = list_rorettes;
51
52#print Dumper(\%rorettes);
53
54my $now = strftime "%Y-%m-%d %H:%M", localtime;
55
56# init template
57my $template = Text::Template->new(TYPE => 'FILE',  SOURCE => $haproxy_template);
58
59# backup the current lighttpd.conf before overwriting it, because You Never Know
60`cp $haproxy_cfg $haproxy_cfg.old`;
61
62my $params = {rorettes => \%rorettes, now => $now, default_backend => $my_hostname};
63
64my $config = $template->fill_in(HASH => $params);
65
66my $FH;
67open($FH,'>',$haproxy_cfg) or die("Unable to open $haproxy_cfg for writing.");
68print $FH $config;
69close($FH);
70
71### End
72
73
74sub list_rorettes() {
75  my %list = ();
76
77  foreach my $host (@hosts) {
78    if ($my_hostname eq $host) {
79      # avoid SSH if working locally
80      my @list = split(/\n/,`ls -d $vserver_dir/*/ | xargs -l basename`);
81      $list{$host} = \@list;
82    } else {
83      my @list = split(/\n/,`ssh rormanager\@$host "ls -d $vserver_dir/*/ | xargs -l basename"`);
84      $list{$host} = \@list;
85    }
86  }
87
88  return %list;
89}
90
Note: See TracBrowser for help on using the repository browser.