summaryrefslogtreecommitdiffstats
path: root/config/src/apps/vespa-config/vespa-config.pl
blob: a17a106a80f3642b5ab0ae13b5fdeb3ccea7d9e3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
#!/usr/bin/env perl
# Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#
# Various small functions used when bootstrapping the config system

# BEGIN perl environment bootstrap section
# Do not edit between here and END as this section should stay identical in all scripts

use warnings;
use File::Basename;
use File::Path;

sub findpath {
    my $myfullname = ${0};
    my($myname, $mypath) = fileparse($myfullname);

    return $mypath if ( $mypath && -d $mypath );
    $mypath=`pwd`;

    my $pwdfullname = $mypath . "/" . $myname;
    return $mypath if ( -f $pwdfullname );
    return 0;
}

# Returns the argument path if it seems to point to VESPA_HOME, 0 otherwise
sub is_vespa_home {
    my($VESPA_HOME) = shift;
    my $COMMON_ENV="libexec/vespa/common-env.sh";
    if ( $VESPA_HOME && -d $VESPA_HOME ) {
        my $common_env = $VESPA_HOME . "/" . $COMMON_ENV;
        return $VESPA_HOME if -f $common_env;
    }
    return 0;
}

# Returns the home of Vespa, or dies if it cannot
sub findhome {
    # Try the VESPA_HOME env variable
    return $ENV{'VESPA_HOME'} if is_vespa_home($ENV{'VESPA_HOME'});
    if ( $ENV{'VESPA_HOME'} ) { # was set, but not correctly
        die "FATAL: bad VESPA_HOME value '" . $ENV{'VESPA_HOME'} . "'\n";
    }

    # Try the ROOT env variable
    $ROOT = $ENV{'ROOT'};
    return $ROOT if is_vespa_home($ROOT);

    # Try the script location or current dir
    my $mypath = findpath();
    if ($mypath) {
        while ( $mypath =~ s|/[^/]*$|| ) {
            return $mypath if is_vespa_home($mypath);
        }
    }
    die "FATAL: Missing VESPA_HOME environment variable\n";
}

BEGIN {
    my $tmp = findhome();
    if ( $tmp !~ m{[/]$} ) { $tmp .= "/"; }
    $ENV{'VESPA_HOME'} = $tmp;
}
my $VESPA_HOME = $ENV{'VESPA_HOME'};

# END perl environment bootstrap section

use lib $ENV{'VESPA_HOME'} . '/lib/perl5/site_perl';
use lib $ENV{'VESPA_HOME'} . '/lib64/perl5/site_perl';
use Yahoo::Vespa::Defaults;
readConfFile();

use strict;
use warnings;
use File::Copy;
use File::Temp;

my $default_configproxy_port = "19090";
my $default_configserver_port = "19070";
my $zk_client_port;

my $base_cfg_dir = $VESPA_HOME . "conf/vespa";

# Set this to 1 to look up values (see getValue) in config files instead
# of in environment variables
my $lookupInConfig = 0;

sub vespa_base_env {
    $zk_client_port = getCCSVar('zookeeper_clientPort', 2181);
}

sub getValue {
    my ($varname, $prefix) = @_;
    if ($lookupInConfig) {
	return getConfigValue($varname, $prefix);
    } 
    else {
        return getEnvironmentValue($varname, $prefix);
    }
}

sub getConfigValue {
    my ($varname, $config) = @_;
    my $path = "$base_cfg_dir/$config.conf";
    if (open(CFG, "<$path")) {
        while (<CFG>) {
            chomp;
            if ( m{^(\w+)\s(.+)} ) {
		return $2 if $1 eq $varname;
            }
        }
        close(CFG);
    }
    return;
}

sub getEnvironmentValue {
    my ($varname, $prefix) = @_;
    my $value = $ENV{$prefix . "__" . $varname};
    if (defined $value && $value =~ m{^\s*(\S.*)\s*}) {
        return $1;
    }
    return $value;
}

sub getCCSVar {
    my ($varname, $default) = @_;
    my $value = getValue($varname, "cloudconfig_server");
    if (defined($value)) {
        return $value;
    }
    return $default;
}

sub getVar {
    my ($varname, $default, $warn) = @_;
    # print "GET var '$varname'\n";
    my $cloud = getValue($varname, "services");
    my $vespa = getValue($varname, "vespa_base");
    if (defined($cloud) && defined($vespa)) {
        print STDERR "Found settings for both services.$varname and vespa_base.$varname, using settings from services\n";
    }
    if (defined($cloud)) {
        return $cloud;
    } elsif (defined($vespa)) {
        return $vespa;
    } elsif ($warn > 0) {
        print STDERR "No value found for 'services.$varname' or 'vespa_base.$varname'; using '$default'\n";
    }
    return $default;
}

sub printConfigServerPort {
    my $port = getVar('port_configserver_rpc', $default_configserver_port, 0);
    print "$port\n";
}

sub getConfigServers {
    my @ret;

    my $hostname = `hostname`;
    chomp $hostname;

    my $addr = getVar('addr_configserver', $hostname, 1);
    my $port = getVar('port_configserver_rpc', $default_configserver_port, 0);

    my $h;
    foreach $h (split(/,|\s+/, $addr)) {
        if ($h =~ m{(\S+:\d+)}) {
            push @ret, $1;
        } else {
            push @ret, "${h}:${port}";
        }
    }
    return @ret;
}

sub getZKString {
    my $out;
    my $addr;
    foreach $addr (getConfigServers()) {
        $addr =~ s{:\d+}{:$zk_client_port,};
        $out .= $addr;
    }
    chop($out);                 # last comma
    return $out;
}

sub printZKString {
    my $out = getZKString();
    print $out . "\n";
}

sub printAllConfigSourcesWithPort {
    my $cfport = getVar('port_configserver_rpc', $default_configserver_port, 0);
    my $cpport = getVar('port_configproxy_rpc',  $default_configproxy_port,  0);
    my $addr = "localhost";
    my $out = "tcp/${addr}:${cpport}";
    foreach $addr (getConfigServers()) {
        if ($addr =~ m{\/}) {
            if ($addr =~ m{\:}) {
                $out .= ",${addr}";
            } else {
                $out .= ",${addr}:${cfport}";
            }
        } else {
            if ($addr =~ m{\:}) {
                $out .= ",tcp/${addr}";
            } else {
                $out .= ",tcp/${addr}:${cfport}";
            }
        }
    }
    print $out . "\n";
}

sub printConfigSources {
    my $out;
    my $addr;
    foreach $addr (getConfigServers()) {
        $out .= "tcp/${addr},";
    }
    chop($out);                 # last comma
    print $out . "\n";
}

sub printConfigHttpSources {
    my $out;
    my $addr;
    foreach $addr (getConfigServers()) {
        my $host = "";
        my $port = 0;
        if ($addr =~ /(.*):(\d+)$/) {
            $host = $1;
            $port = $2;
        }
        $port++;                        # HTTP is rpc + 1
        $out .= "http://$host:$port ";
    }
    chop($out);                 # last space
    print $out . "\n";
}

sub makeFiledistributorZKConfig {
    my $cfgFile = $VESPA_HOME . "conf/filedistributor/zookeepers.cfg";
    open(CFG, "> ${cfgFile}.new") or die "Cannot write to '${cfgFile}.new'";
    my $zkservers = getZKString();
    print CFG "zookeeperserverlist \"$zkservers\"\n";
    close(CFG);
    rename("${cfgFile}.new", ${cfgFile})
        or die "Cannot rename '${cfgFile}.new' -> '${cfgFile}': $!\n";
    print STDERR "wrote '${cfgFile}' \n";
}

sub makeFiledistributorDistributorConfig {
    my $cfgFile = $VESPA_HOME . "conf/filedistributor/filedistributor.cfg";
    open(CFG, "> ${cfgFile}.new") or die "Cannot write to '${cfgFile}.new'";
    print CFG "torrentport 19093\n";
    my $hostname = `hostname`;
    chomp $hostname;
    print CFG "hostname \"$hostname\"\n";
    print CFG "filedbpath \"$VESPA_HOME" . "var/db/vespa/filedistribution\"\n";
    print CFG "maxdownloadspeed 0\n";
    print CFG "maxuploadspeed 0\n";
    close(CFG);
    rename("${cfgFile}.new", ${cfgFile})
        or die "Cannot rename '${cfgFile}.new' -> '${cfgFile}': $!\n";
    print STDERR "wrote '${cfgFile}' \n";
}

sub makeFiledistributorRpcConfig {
    my $cfgFile = $VESPA_HOME . "conf/filedistributor/filedistributorrpc.cfg";
    open(CFG, "> ${cfgFile}.new") or die "Cannot write to '${cfgFile}.new'";
    my $hostname = `hostname`;
    chomp $hostname;
    print CFG "connectionspec \"tcp/$hostname:19092\"\n";
    close(CFG);
    rename("${cfgFile}.new", ${cfgFile})
        or die "Cannot rename '${cfgFile}.new' -> '${cfgFile}': $!\n";
    print STDERR "wrote '${cfgFile}' \n";
}

sub makeFiledistributorReferencesConfig {
    my $cfgFile = $VESPA_HOME . "conf/filedistributor/filereferences.cfg";
    open(CFG, "> ${cfgFile}.new") or die "Cannot write to '${cfgFile}.new'";
    close(CFG);
    rename("${cfgFile}.new", ${cfgFile})
        or die "Cannot rename '${cfgFile}.new' -> '${cfgFile}': $!\n";
    print STDERR "wrote '${cfgFile}' \n";
}

sub makeFiledistributorConfig {
    makeFiledistributorZKConfig();
    makeFiledistributorDistributorConfig();
    makeFiledistributorRpcConfig();
    makeFiledistributorReferencesConfig();
}

sub isThisAConfigServer {
    my $hostnameForThisHost = `hostname`;
    chomp $hostnameForThisHost;
    my $addr;
    foreach $addr (getConfigServers()) {
        my $host = "";
        my $port = 0;
        if ($addr =~ /(.*):(\d+)$/) {
            $host = $1;
        }
        if ($hostnameForThisHost eq $host or $host eq "localhost") {
          print "yes\n";
          exit 0;
        }
    }

    print "no\n";
    exit 1;
}

# Perl trim function to remove whitespace from the start and end of the string
sub trim($) {
    my $string = shift;
    $string =~ s/^\s+//;
    $string =~ s/\s+$//;
    return $string;
}

sub getLastLine {
    my ($file) = @_;
    `grep -v \"\^\$\" $file | tail -n 1` # skip blank lines
}

sub usage {
    print "usage: ";
    print "vespa-config [-configsources | -confighttpsources | -zkstring | -mkfiledistributorconfig | -configserverport | -zkclientport | -isthisaconfigserver]\n";
}

if ( @ARGV == 0 ) {
    usage();
    exit 1;
}

if ( $ARGV[0] eq "-allconfigsources" ) {
    vespa_base_env();
    printAllConfigSourcesWithPort();
    exit 0;
}
if ( $ARGV[0] eq "-configsources" ) {
    vespa_base_env();
    printConfigSources();
    exit 0;
}
if ( $ARGV[0] eq "-confighttpsources" ) {
    $lookupInConfig = 1;
    vespa_base_env();
    printConfigHttpSources();
    exit 0;
}
if ( $ARGV[0] eq "-zkstring" ) {
    vespa_base_env();
    printZKString();
    exit 0;
}
if ( $ARGV[0] eq "-configserverport" ) {
    $lookupInConfig = 1;
    vespa_base_env();
    printConfigServerPort();
    exit 0;
}
if ( $ARGV[0] eq "-mkfiledistributorconfig" ) {
    vespa_base_env();
    makeFiledistributorConfig();
    exit 0;
}
if ( $ARGV[0] eq "-zkclientport" ) {
    vespa_base_env();
    print "$zk_client_port\n";
    exit 0;
}
if ( $ARGV[0] eq "-isthisaconfigserver" ) {
    vespa_base_env();
    isThisAConfigServer();
    exit 0;
}

usage();
exit 1;