1
+ # !/usr/bin/perl -w
2
+ package main ;
3
+ use strict;
4
+ use warnings;
5
+ use feature qw( switch) ;
6
+ use Data::Dumper;
7
+ use Carp;
8
+ $SIG { __DIE__ } = sub { Carp::confess( @_ ) };
9
+ $SIG { __WARN__ } = sub { Carp::confess( @_ ) };
10
+ # use File::IO;
11
+ use Getopt::Long;
12
+ use File::Copy qw( cp) ;
13
+ use File::Temp qw( tempfile tempdir) ;
14
+ use POSIX qw( strftime ceil floor abs) ;
15
+ # use MIME::Base64 qw();
16
+ use Cwd;
17
+ $| = 1;
18
+
19
+ =pod
20
+ ################################################################################
21
+ # #
22
+ # Use this script to reconfigure apache Listen directives so that it only #
23
+ # listens on ports 127.0.0.1:80 and mgt_dev:80. This is to allow lanforge #
24
+ # versions of nginx to listen on managed ports without conflict. Run this #
25
+ # script every time you run /home/lanforge/lfconfig. #
26
+ # #
27
+ ################################################################################
28
+ =cut
29
+
30
+ our $is_fedora =0;
31
+ our $is_centos =0;
32
+ our $is_ubuntu =0;
33
+
34
+ sub error {
35
+ for (@_ ) {
36
+ print STDERR " $_ \n " ;
37
+ }
38
+ }
39
+
40
+ if (!-f " /etc/os-release" ) {
41
+ die (" no /etc/os-release, bye." );
42
+ }
43
+ my @os_release_lines = ` cat /etc/os-release` ;
44
+ my @os_name_lines = grep {/ ^NAME=/ } @os_release_lines ;
45
+ die (" Unknown OS" ) if (@os_name_lines < 1);
46
+
47
+ if ($os_name_lines [0] =~ / CentOS( Linux)?/ ) {
48
+ $is_centos = 1;
49
+ }
50
+ elsif ($os_name_lines [0] =~ / \b Fedora\b / ) {
51
+ $is_fedora = 1;
52
+ }
53
+ elsif ( $os_name_lines [0] =~ / \b Ubuntu\b / ) {
54
+ $is_ubuntu = 1;
55
+ }
56
+ else {
57
+ die (" Unknown OS: $os_name_lines [0]" );
58
+ }
59
+
60
+ my @new_httpconf_lines = ();
61
+ if ((! -d " /home/lanforge" ) || (! -f " /home/lanforge/config.values" )) {
62
+ error(" * Unable to find /home/lanforge/config.values. Not configuring apache." );
63
+ return ;
64
+ }
65
+ my @config_values = ` cat /home/lanforge/config.values` ;
66
+ my @mgt_dev_lines = grep {/ ^mgt_dev/ } @config_values ;
67
+ if (@mgt_dev_lines != 1) {
68
+ error(" * mgt_dev misconfigured in config.values. Not configuring apache." );
69
+ return ;
70
+ }
71
+ my $mgt_dev = (split (/ \s +/ , $mgt_dev_lines [0]))[0];
72
+ if (!(defined $mgt_dev ) || $mgt_dev eq " " ) {
73
+ err(" * Unable to find mgt_dev value. Not configuring apache." );
74
+ return ;
75
+ }
76
+
77
+ our $new_include ;
78
+ our $httpconf ;
79
+
80
+ # if we have a Listen directive in apache, comment it out and create an include
81
+ sub collectListenLines { # original httpd.conf, listen.conf
82
+ my $conf = shift ;
83
+ my $new_include = shift ;
84
+ my @httpconf_lines = ` cat $conf ` ;
85
+ chomp (@httpconf_lines );
86
+ my @new_httpconf_lines = ();
87
+ my @new_listen_lines = ();
88
+ my @listen_lines = grep {/ ^Listen / } @httpconf_lines ;
89
+ chomp (@listen_lines );
90
+ my @include_lines = grep {m |^Include conf/lf_listen.conf$| } @httpconf_lines ;
91
+ chomp (@include_lines );
92
+
93
+ if (@listen_lines < 1 ) {
94
+ error(" * Apache misconfigured? No Listen directive and not include lf_listen.conf. Not modifying." );
95
+ return ;
96
+ }
97
+
98
+ for my $line (@httpconf_lines ) {
99
+ if ($line =~ / ^Listen / ) {
100
+ $line = " # Moved to lf_listen.conf: $line " ;
101
+ push (@listen_lines , " # $line " );
102
+ }
103
+ push (@new_httpconf_lines , $line );
104
+ }
105
+ } # ~collectListenLines
106
+
107
+ # # ## ## ## ## ## ## ## ## ##
108
+ # # M A I N ##
109
+ # # ## ## ## ## ## ## ## ## ##
110
+
111
+ if ($is_fedora || $is_centos ) {
112
+ die (" * Unable to find /etc/httpd/conf/httpd.conf. Not configuring apache." )
113
+ unless ( -f " /etc/httpd/conf/httpd.conf" );
114
+
115
+ $: :new_include = " /etc/httpd/conf/lf_listen.conf" ;
116
+ $: :httpconf = " /etc/httpd/conf/httpd.conf" ;
117
+ if (-f $new_include ) {
118
+ print (" # lf_listen.conf already created, not creating\n " );
119
+ exit 0;
120
+ }
121
+ collectListenLines($: :httpconf, $: :new_include);
122
+ exit 0;
123
+ } # ~fedora
124
+ elsif ($is_ubuntu ) {
125
+ $: :new_include = " /etc/apache2/lf_listen.conf" ;
126
+ $: :httpconf = " /etc/apache2/ports.conf" ;
127
+ die (" * Unable to find $: :httpconf. Not configuring apache." )
128
+ unless ( -f $: :httpconf );
129
+
130
+ collectListenLines($: :httpconf, $: :new_include);
131
+ }
132
+ else {
133
+ error(" Configuring apache mgt port not configured on other systems." );
134
+ exit 1;
135
+ }
136
+
137
+ # eof
0 commit comments