1
#!/inktest/dist/bin/perl
3
# Licensed to the Apache Software Foundation (ASF) under one
4
# or more contributor license agreements. See the NOTICE file
5
# distributed with this work for additional information
6
# regarding copyright ownership. The ASF licenses this file
7
# to you under the Apache License, Version 2.0 (the
8
# "License"); you may not use this file except in compliance
9
# with the License. You may obtain a copy of the License at
11
# http://www.apache.org/licenses/LICENSE-2.0
13
# Unless required by applicable law or agreed to in writing, software
14
# distributed under the License is distributed on an "AS IS" BASIS,
15
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
# See the License for the specific language governing permissions and
17
# limitations under the License.
24
# script to manage traffic_server under the DEFT testing
39
our @additional_config_entries = ();
45
my @user_info = getpwuid $UID;
46
my $default_user = $user_info[0];
48
#####################################
50
# Finding the ethernet interface #
52
#####################################
54
# Defaults in case we can't access ifconfig
55
our %default_interface =
67
my $dif = $default_interface{$OSNAME};
70
die "Unknown OS type, could determine default internface\n";
76
sub process_if_record {
77
my ($if_name, $if_info) = @_;
83
# Do not return loop back addrs
84
if ($if_name =~ /^lo/) {
88
# Check to see if the address has an address
89
if ($if_info =~ /(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/) {
90
# If it's not localhost, it's good enough
91
if ($1 != 127 || $2 == 0 || $3 == 0 || $4 == 1) {
99
sub find_ethernet_if {
101
if (!open(IFCONFIG, "ifconfig -a |") &&
102
!open(IFCONFIG, "/usr/sbin/ifconfig -a |")) {
103
warn "Warning: Could not open ifconfig : $! : guessing interface\n";
104
return get_default_if();
107
# Basic strategy is loop over the interfaces and use the first
108
# one that is up that is not localhost
110
# Between the various OS's there are several different formats
111
# for ifconfig output. They have in common that interfaces
112
# start the line with "<if_name>:" Some put new line between
113
# interfaces and others do not
118
while ($tmp = <IFCONFIG>) {
120
if ($tmp =~ /^(\w+):(.*)/) {
122
# We've got a new record, process old info
126
if (process_if_record($if_name, $if_info)) {
130
$if_name = $new_name;
131
$if_info = $new_info;
133
} elsif ($tmp =~ /^\s*$/) {
135
# Blank lines tell us the old record is done
136
if (process_if_record($if_name, $if_info)) {
144
$if_info = $if_info . " " . $tmp;
148
if (process_if_record($if_name, $if_info)) {
152
return get_default_if;
155
my $eth_if = find_ethernet_if();
157
###############*######################
159
# End: finding ethernet interface #
161
######################################
165
"proxy.config.socks.socks_server_port" => 1080,
166
"proxy.config.proxy_name" => "ink-proxy.inktomi.com",
167
"proxy.config.bin_path" => "bin",
168
"proxy.config.alarm_email" => $default_user,
169
"proxy.config.cluster.mc_group_addr" => "225.0.1.89",
170
"proxy.config.admin.html_doc_root" => "ui",
171
"proxy.config.admin.admin_user" => "admin",
172
"proxy.config.admin.admin_password" => "21232F297A57A5A743894A0",
173
"proxy.config.admin.user_id" => $default_user,
174
"proxy.config.net.connections_throttle" => 8000,
175
"proxy.config.cluster.ethernet_interface" => $eth_if,
176
"proxy.config.icp.icp_interface" => $eth_if,
177
"proxy.config.hostdb.size" => 200000,
178
"proxy.config.reverse_proxy.enabled" => 0,
179
"proxy.config.loadshedding.max_connections" => 0,
184
"proxy.config.http.server_port" => "tsHttpPort",
187
our %base_port_hash =
189
"proxy.config.http.server_port" => 0,
190
"proxy.config.admin.web_interface_port" => 2,
191
"proxy.config.admin.overseer_port" => 3,
192
"proxy.config.admin.autoconf_port" => 4,
193
"proxy.config.process_manager.mgmt_port" => 5,
194
"proxy.config.log.collation_port" => 7,
195
"proxy.config.cluster.cluster_port" => 8,
196
"proxy.config.cluster.rsport" => 10,
197
"proxy.config.cluster.mcport" => 11,
200
our %config_meta = ();
202
sub process_records_blob {
204
while ($line = <CONFIG_BLOB>) {
205
if ($line =~ /^\[([^\]]*)\]\s*$/) {
209
my ($cmd, $arg1, $arg2, $arg3, $arg4) = split(/\s+/, $line, 5);
211
if ($cmd eq "base_port") {
213
} elsif ($cmd eq "add") {
214
my $config_entry = $arg1 . " " . $arg2 . " " . $arg3 . " " . $arg4;
215
push @additional_config_entries, $config_entry;
217
$override{$cmd} = $arg1;
224
sub process_remap_config {
226
while ($line = <CONFIG_BLOB>) {
227
if ($line =~ /^\[([^\]]*)\]\s*$/) {
230
push(@remap_lines, $line);
236
sub process_filter_config {
238
while ($line = <CONFIG_BLOB>) {
239
if ($line =~ /^\[([^\]]*)\]\s*$/) {
242
push(@filter_lines, $line);
248
sub process_ipnat_conf {
250
while ($line = <CONFIG_BLOB>) {
251
if ($line =~ /^\[([^\]]*)\]\s*$/) {
254
push(@ipnat_lines, $line);
260
sub process_meta_config {
262
while ($line = <CONFIG_BLOB>) {
263
if ($line =~ /^\[([^\]]*)\]\s*$/) {
266
if ($line =~ /(\S+):\s+(\S+)/) {
267
print "meta config '$1' = '$2'\n";
268
$config_meta{$1} = $2;
269
} elsif ($line !~ /^\s*$/) {
270
warn("Malformed meta config: $line\n");
277
sub process_other_file_blob {
278
my ($file_name) = @_;
279
my $file_path = $run_dir . "/etc/trafficserver/" . $file_name;
281
open(OTHER_FILE, "> $file_path") || die "Failed to write file $file_name: $!\n";
284
while ($line = <CONFIG_BLOB>) {
285
if ($line =~ /^\[([^\]]*)\]\s*$/) {
289
print OTHER_FILE $line;
296
sub read_test_config_blob {
297
my ($test_config_blob) = @_;
299
open (CONFIG_BLOB, "< $test_config_blob") || die "Failed to open config_blob: $!\n";
303
while ($line = <CONFIG_BLOB>) {
305
if ($line =~ /^\[([^\]]*)\]\s*$/) {
311
while ($file_name ne "") {
312
if ($file_name eq "records.config") {
313
$file_name = process_records_blob();
314
} elsif ($file_name eq "remap.config") {
315
$file_name = process_remap_config();
316
} elsif ($file_name eq "filter.config") {
317
$file_name = process_filter_config();
318
} elsif ($file_name eq "ipnat.conf") {
319
$file_name = process_ipnat_conf();
320
} elsif ($file_name eq "meta") {
321
$file_name = process_meta_config();
323
# any other file is writting to config
324
$file_name = process_other_file_blob($file_name);
331
my %records_seen = {};
333
sub output_records_config {
334
my ($records_config_in, $records_config_out, $port_start) = @_;
336
open (RECORDS_IN, "< $records_config_in") ||
337
die "Couldn't open $records_config_in: $!\n";
339
open (RECORDS_OUT, "> $records_config_out") ||
340
die "Couldn't open $records_config_out: $!\n";
343
my $num_ports_alloc = 0;
345
while($rec_line = <RECORDS_IN>) {
347
if ($rec_line =~ /^\s*#/) {
348
$new_line = $rec_line;
352
my ($r_class, $r_name, $r_type, $r_val) = split (/\s+/, $rec_line, 4);
355
if (defined($tmp = $defaults{$r_name})) {
360
# Look at the port stuff
361
if (defined($tmp = $base_port_hash{$r_name})) {
362
$r_val = $port_start + $num_ports_alloc;
367
# Look at rc overrides
368
if (defined($tmp = $override{$r_name})) {
373
$records_seen{$r_name} = $r_val;
376
$new_line = $r_class . " " . $r_name . " " . "$r_type" . " " . "$r_val" . "\n";
378
$new_line = $rec_line;
382
print RECORDS_OUT $new_line;
388
foreach $config_entry (@additional_config_entries) {
389
my ($r_class, $r_name, $r_type, $r_val) = split (/\s+/, $config_entry, 4);
391
if ($records_seen{$r_name}) {
392
# We already has an entry for this record so ignore the addition
396
if ($r_val eq "<alloc_port>") {
398
if (defined($tmp = $base_port_hash{$r_name})) {
399
$r_val = $num_ports_alloc + $port_start;
400
$config_entry = $r_class . " " . $r_name . " " . "$r_type" . " " . "$r_val" . "\n";
405
$records_seen{$r_name} = $r_val;
406
print RECORDS_OUT $config_entry . "\n";
411
return $num_ports_alloc;
414
sub output_remap_config {
415
my $remap_file = $run_dir . "/etc/trafficserver/remap.config";
417
open(REMAP_FILE, "> $remap_file") || die "Failed to write file $remap_file: $!\n";
420
while ($line = shift(@remap_lines)) {
422
my $output_line = "";
424
if ($line =~ /([^\(]*)&&\(([^\)]+)\)(.*)/) {
425
$output_line = $output_line . $1;
428
if ($records_seen{$rec_var}) {
429
$output_line = $output_line . $records_seen{$rec_var};
431
$output_line = $output_line . "&&(" . $2 . ")";
435
$output_line = $output_line . $line;
440
print REMAP_FILE "$output_line\n";
446
sub output_ipnat_conf {
447
my $ipnat_file = $run_dir . "/etc/trafficserver/ipnat.conf";
449
open(IPNAT_FILE, "> $ipnat_file") || die "Failed to write file $ipnat_file: $!\n";
452
while ($line = shift(@ipnat_lines)) {
454
my $output_line = "";
456
if ($line =~ /([^\(]*)&&\(([^\)]+)\)(.*)/) {
457
$output_line = $output_line . $1;
460
if ($records_seen{$rec_var}) {
461
$output_line = $output_line . $records_seen{$rec_var};
463
$output_line = $output_line . "&&(" . $2 . ")";
467
$output_line = $output_line . $line;
472
print IPNAT_FILE "$output_line\n";
479
sub output_filter_config {
480
my $filter_file = $run_dir . "/etc/trafficserver/filter.config";
482
open(FILTER_FILE, "> $filter_file") || die "Failed to write file $filter_file: $!\n";
485
while ($line = shift(@filter_lines)) {
487
my $output_line = "";
489
if ($line =~ /([^\(]*)&&\(eval(.)(.*)\2\)(.*)/) {
490
$output_line = $output_line . $1;
491
# print "eval|" . $3 . "|\n";
493
$output_line = $output_line . $tmp;
495
} elsif ($line =~ /([^\(]*)&&\(([^\)]+)\)(.*)/) {
496
$output_line = $output_line . $1;
499
if ($records_seen{$rec_var}) {
500
$output_line = $output_line . $records_seen{$rec_var};
502
$output_line = $output_line . "&&(" . $2 . ")";
506
$output_line = $output_line . $line;
511
print FILTER_FILE "$output_line\n";
517
sub build_port_bindings {
518
my ($records_config) = @_;
519
my $output_line = "port_binding:";
521
open (RECORDS, "< $records_config") ||
522
die "Couldn't open $records_config: $!\n";
526
while($rec_line = <RECORDS>) {
527
if ($rec_line !~ /^\s*#/) {
528
my ($r_class, $r_name, $r_type, $r_val) = split (/\s+/, $rec_line);
530
if ($export_ports{$r_name}) {
531
$output_line = $output_line . " " . $export_ports{$r_name} . " " . $r_val;
539
our @populate_dirs = ( "bin", "etc/trafficserver", "etc/trafficserver/internal", "logs");
541
our %populate_symlinks =
542
( "bin/traffic_server" => "bin/traffic_server",
543
"bin/traffic_manager" => "bin/traffic_manager",
544
"bin/traffic_cop" => "bin/traffic_cop",
545
"bin/traffic_line" => "bin/traffic_line",
546
"bin/start_traffic_server" => "bin/start_traffic_server",
547
"bin/stop_traffic_server" => "bin/stop_traffic_server",
548
"bin/filter_to_policy" => "bin/filter_to_policy",
549
"etc/trafficserver/body_factory" => "etc/trafficserver/body_factory",
550
"etc/trafficserver/plugins" => "etc/trafficserver/plugins",
555
our %populate_exclude_config =
557
"records.config" => 1,
558
"records.config.shadow" => 1,
559
"storage.config.shadow" => 1
562
sub populate_run_dir() {
563
my ($dir, $from_file_key);
565
foreach $dir (@populate_dirs) {
566
my $rdir = $run_dir . "/" . $dir;
572
foreach $from_file_key (keys (%populate_symlinks)) {
573
my $from_file = $bin_dir . "/" . $from_file_key;
574
my $to_file = $run_dir . "/" . $populate_symlinks{$from_file_key};
576
if (! -e $to_file && ! -l $to_file) {
577
symlink($from_file, $to_file);
581
my $config_dir_source = $bin_dir . "/etc/trafficserver";
582
my $config_dir_target = $run_dir . "/etc/trafficserver";
584
opendir(CONFIG_SOURCE_DIR, $config_dir_source) ||
585
die "Couldn't open config src dir: $!\n";
588
while ($dir_ent = readdir(CONFIG_SOURCE_DIR)) {
589
if (! $populate_exclude_config{$dir_ent}) {
590
my $source_file = $config_dir_source . "/" . $dir_ent;
591
if (-f $source_file) {
592
my $target_file = $config_dir_target . "/" . $dir_ent;
593
copy($source_file, $target_file) || die "Copy of $dir_ent failed: $!\n";
599
open(OUTPUT,">&=$ARGV[1]") || die "Couldn't open output: $!";
604
while ($tmp = <STDIN>) {
606
if ($tmp =~ /^([^:]+):\s(.*)\n/) {
607
$input_args{$1} = $2;
611
my $records_config_in = "";
612
$bin_dir = $input_args{"bin_dir"};
615
$records_config_in = $bin_dir . "/etc/trafficserver/records.config";
617
warn("bin_dir not sent\n");
621
$run_dir = $input_args{"run_dir"};
622
my $no_run_dir = $input_args{"no_run_dir"};
624
my $records_config_out;
626
$records_config_out = $bin_dir . "/etc/trafficserver/records.config.shadow";
629
$records_config_out = $run_dir . "/etc/trafficserver/records.config";
633
warn("run_dir not sent\n");
641
my $ports = $input_args{"ports_avail"};
643
my $config_blob = $input_args{"config_file"};
645
die("config_blob not sent\n");
648
read_test_config_blob($config_blob);
650
my ($start_port, $end_port);
651
if ($ports =~ /^(\d+)-(\d+)/) {
655
warn("ports_avail invalid\n");
658
my $use_start_port = $base_port;
659
if ($use_start_port == 0) {
660
$use_start_port = $start_port;
665
$ports_used = output_records_config($records_config_in,
666
$records_config_out, $use_start_port);
668
if (scalar(@remap_lines) > 0) {
669
output_remap_config();
672
if (scalar(@filter_lines) > 0) {
673
output_filter_config();
676
if (scalar(@ipnat_lines) > 0) {
680
my $cmd_line = "cmd_line: $bin_dir/bin/";
681
if ($config_meta{"run_manager"}) {
682
$cmd_line = $cmd_line . "traffic_manager";
684
$cmd_line = $cmd_line . "traffic_server";
687
if ($use_start_port == $start_port) {
688
print OUTPUT "ports_used: $ports_used\n";
691
my $bindings = build_port_bindings($records_config_out);
692
print OUTPUT $bindings . "\n";
694
print OUTPUT $cmd_line . "\n";
697
my $ld_lib_path = $ENV{"LD_LIBRARY_PATH"};
699
if ($ld_lib_path eq "") {
700
$ld_lib_path = $run_dir . "/bin";
701
$ld_lib_path .= $run_dir . "/lib";
703
$ld_lib_path = $ld_lib_path . ":" . $run_dir . "/bin";
704
$ld_lib_path .= $ld_lib_path . ":" . $run_dir . "/lib";
707
print OUTPUT "env_vars: LD_LIBRARY_PATH=" . $ld_lib_path . "\n";