summaryrefslogtreecommitdiffstats
path: root/config-model/src/main/perl/vespa-deploy
blob: fede8b994c151d1420795d91e8bfe2e74bec0843 (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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
#!/usr/bin/env perl
# Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
# This script is for uploading, preparing, activating and fetching
# application packages to a cloud config server

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

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";
}

sub findhost {
    my $tmp = $ENV{'VESPA_HOSTNAME'};
    my $bin = $ENV{'VESPA_HOME'} . "/bin";
    if (!defined $tmp) {
        $tmp = `${bin}/vespa-detect-hostname || hostname -f || hostname || echo "localhost"`;
        chomp $tmp;
    }
    my $validate = "${bin}/vespa-validate-hostname";
    if (-f "${validate}") {
       system("${validate} $tmp");
       ( $? == 0 ) or die "Could not validate hostname\n";
    }
    return $tmp;
}

BEGIN {
    my $tmp = findhome();
    $ENV{'VESPA_HOME'} = $tmp;
    $tmp = findhost();
    $ENV{'VESPA_HOSTNAME'} = $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 feature qw(switch say);
use vars qw/ $opt_c $opt_h $opt_n $opt_v $opt_f $opt_t $opt_a $opt_e $opt_E $opt_r $opt_i $opt_p $opt_H $opt_R $opt_F $opt_V /;
use Env qw($HOME);
use JSON;
use Getopt::Std;
use File::Path qw(make_path);
use Scalar::Util qw(looks_like_number);

my $cloudconfig_dir = "$HOME/.cloudconfig";
my $session_id_file;
my $configsource_url_used_file = "$cloudconfig_dir/deploy-configsource-url-used";



my $pathPrefix;
my $tenant = "default";
my $application = "default";
my $environment = "prod";
my $region = "default";
my $instance = "default";
my $version = "v2";
my $configserver = "";
my $port = "19071";
getopts('c:fhnt:ve:E:r:a:i:p:HR:F:V:');

if ($opt_h) {
  usage();
  exit 0;
}

if ($opt_c) {
    $configserver = $opt_c;
}

if ($opt_e) {
  $tenant = $opt_e;
}

if ($opt_r) {
  $region = $opt_r;
}

if ($opt_E) {
  $environment = $opt_E;
}

if ($opt_a) {
  $application = $opt_a;
}

if ($opt_i) {
  $instance = $opt_i;
}

if ($opt_p) {
  $port = $opt_p;
}

$pathPrefix = "/application/v2/tenant/$tenant/session";

create_cloudconfig_dir();

$session_id_file = "$cloudconfig_dir/$tenant/deploy-session-id";

my $command = shift;
$command ||= "help";

my $curl_command = $VESPA_HOME . '/libexec/vespa/vespa-curl-wrapper -A vespa-deploy --silent --show-error --connect-timeout 30 --max-time 1200';

my $CURL_PUT = $curl_command . ' --write-out \%{http_code} --request PUT';
my $CURL_GET = $curl_command . ' --request GET';
my $GZIP = "gzip";
my $CURL_POST_WITH_HEADERS = $curl_command . ' -i --request POST --header "Content-Type: application/x-gzip" --data-binary @- -D /tmp/http-headers';
my $CURL_POST = $curl_command . ' --write-out \%{http_code} --request POST --header "Content-Type: application/x-gzip" --data-binary @-';
my $CURL_POST_ZIP = $curl_command . ' --write-out \%{http_code} --request POST --header "Content-Type: application/zip" --data-binary @-';

if ($command eq "upload") {
  my $application_package = shift;
  if (!$opt_F) {
    if (!$application_package) {
      print "Command failed. No application package specified\n";
      usage("upload");
      exit 1;
    }
    if (!(-e $application_package)) {
      print "Command failed. No such directory found: '$application_package'\n";
      exit 1;
    }
    check_application_directory($application_package);
  }

  do_http_request("upload", $application_package);
} elsif ($command eq "prepare") {
    my $arg = shift;
    if ($arg && looks_like_number($arg) && !(-d $arg)) {
      do_http_request("prepare", "", $arg);
    } elsif ($arg) {
      check_application_directory($arg);
      do_http_request("upload", $arg);
      do_http_request("prepare");
    } else {
      do_http_request("prepare");
    }
} elsif ($command eq "activate") {
    my $session_id = shift;
    do_http_request("activate", "", $session_id);
} elsif ($command eq "fetch") {
    my $arg = shift;
    if ($arg) {
      fetch_active_application($arg);
    } else {
      usage("fetch", $arg);
    }
} elsif ($command eq "help") {
  my $arg = shift;
  usage($command, $arg);
} else {
    usage($command);
}


sub check_application_directory {
  my ($application_package) = shift;

  if (-d $application_package) {
    # OK
  } elsif ((-f $application_package) && ($application_package =~ /.*\.zip/ )) {
    # OK
  } else {
    print "Command failed. No directory or zip file found: '$application_package'\n";
    exit 1;
  }
}

sub usage {
  my ($command, $arg) = @_;

  if ($command && $command eq "help") {
    $command = $arg;
  }
  $command ||= "help";

  if ($command eq "upload") {
      usage_upload();
  } elsif ($command eq "prepare") {
      usage_prepare();
  } elsif ($command eq "activate") {
      usage_activate();
  } elsif ($command eq "fetch") {
      usage_fetch();
  } else {
    print "Usage: vespa-deploy [-h] [-v] [-f] [-t] [-c] [-p] [-z] [-V] [<command>] [args]\n";
    print "Supported commands: 'upload', 'prepare', 'activate', 'fetch' and 'help'\n";
    print "Supported options: '-h' (help), '-v' (verbose), '-f' (force/ignore validation errors), '-t' (timeout in seconds), '-p' (config server http port)\n";
    print "    '-h' (help)\n";
    print "    '-v' (verbose)\n";
    print "    '-n' (dry-run)\n";
    print "    '-f' (force/ignore validation errors)\n";
    print "    '-t <timeout>' (timeout in seconds)\n";
    print "    '-c <server>' (config server hostname)\n";
    print "    '-p <port>' (config server http port)\n";

    print "Try 'vespa-deploy help <command>' to get more help\n";
  }
}

sub usage_upload {
  print "Usage: vespa-deploy upload <application package>\n";
}

sub usage_prepare {
  print  "Usage: vespa-deploy prepare [<session_id> | <application package>]\n";
}

sub usage_activate {
  print "Usage: vespa-deploy activate [<session_id>]\n";
}

sub usage_fetch {
  print "Usage: vespa-deploy fetch <output directory>\n";
}

sub fetch_active_application {
  my ($outputdir) = @_;
  my ($configsource_url, @configsources) = get_configsource_url("fetch");
  my $url = "$configsource_url$pathPrefix/active/content/";
  if ($version eq "v2") {
    $url = $configsource_url
         . "/application/v2"
         . "/tenant/${tenant}"
         . "/application/${application}"
         . "/environment/${environment}"
         . "/region/${region}"
         . "/instance/${instance}"
         . "/content/";
  }
  my $output = http_content($url);
  my $exitcode = $? >> 8;
  if ($exitcode != 0) {
    print_request_failed($exitcode, $configsource_url);
    exit 1;
  } else {
    print "Writing active application to $outputdir\n";
    `mkdir -p $outputdir`;
    die "$outputdir is not writeable. Please check permissions\n" if (! -w $outputdir);
    my $json_text = get_json($output);
    if(ref($json_text) eq 'ARRAY'){
      fetch_directory($json_text, $outputdir);
    } else {
      print "Error response: $json_text->{message}\n";
      exit 1;
    }
  }
}

sub fetch_directory {
  my ($json, $outputdir) = @_;
  `mkdir -p $outputdir`;
  foreach my $entry (@{$json}) {
    my $name = "$outputdir/";
    if ($entry =~ /\/([^\/]+\/?)$/) {
      $name .= $1;
    }
    if ($name =~ /(.*)\/$/) {
      my $dir = $1;
      my $output = http_content($entry);
      my $json_text = get_json($output);
      fetch_directory($json_text, "$dir");
    } else {
      my $output = http_content($entry);
      open(FH, ">$name");
      print FH $output;
      close(FH);
    }
  }
}

sub get_configsource_url {
  my ($command) = @_;

  my @configsources;
  if ($configserver and $configserver ne "") {
      @configsources = ('http://' . $configserver . ':' . $port . '/');
  } else {
      @configsources = split(' ', `$VESPA_HOME/bin/vespa-print-default configservers_http`);
  }

  my $configsource_url = shift(@configsources);
  if (!$configsource_url) {
    die "Could not get url to config server, make sure that VESPA_HOME and VESPA_CONFIGSERVERS are set\n";
  }
  chomp($configsource_url);
  my @temp = split(':', $configsource_url, 3);
  $configsource_url = $temp[0] . ":" . $temp[1] . ":" . $port;
  if (!$configsource_url) {
    print "Could not get url to config server, make sure that VESPA_CONFIGSERVERS is set\n";
    exit 1;
  }

  # configsource_url to be used by prepare and activate
  if ($command eq "prepare" || $command eq "activate") {
    my $temp = get_configsource_url_used();
    if ($temp and $temp ne "") {
      $configsource_url = $temp;
      debug("Using config server URL " . $configsource_url . " read from file\n");
    } else {
      print "Could not read config server URL used for previous upload of an application package, trying to use $configsource_url\n";
    }
  }
  return ($configsource_url, @configsources);
}

sub do_http_request {
  my ($command, $application_package, $supplied_session_id) = @_;
  my ($configsource_url, @configsources) = get_configsource_url($command);

  my $output;
  my $exitcode = 1;
  if ($command eq "upload") {
    ($exitcode, $output) = http_upload(\@configsources, $configsource_url, $application_package);
  } elsif ($command eq "prepare")  {
    $output = http_prepare($configsource_url, $supplied_session_id);
    $exitcode = $? >> 8;
  } elsif ($command eq "activate") {
    $output = http_activate($configsource_url, $supplied_session_id);
    $exitcode = $? >> 8;
  }

  my $response;
  if ($exitcode != 0) {
    print_request_failed($exitcode, $configsource_url);
    exit 1;
  } else {
    my $status_code;
    ($status_code, $response) = parse_http_response($output);
    if ($status_code != 200) {
      print "Request failed. HTTP status code: $status_code\n";
      print_response($response);
      exit 1;
    }
    print_response($response);
  }
}

sub http_upload {
  my ($temp, $configsource_url, $application_package) = @_;
  my @configsources = @{$temp};

  my $output;
  my $exitcode = 0;
  my $retry = 0;
  my $configsource_url_used = $configsource_url;
  LOOP: {
      do {
        my $status_code;
        my $response;
        $output = http_upload_lowlevel($configsource_url, $application_package);
        ($status_code, $response) = parse_http_response($output);
        $exitcode = $? >> 8;
        last LOOP if ($exitcode == 0 && $status_code == 200);

        debug("exitcode=$exitcode\n");
        debug("output=$output\n");
        $configsource_url = shift(@configsources);
        if ($configsource_url) {
          $configsource_url_used = $configsource_url;
          $retry = 1;
          print_request_failed($exitcode, $configsource_url_used);
          print "Retrying with another config server\n";
        } else {
          if ($exitcode != 0) {
            print_request_failed($exitcode, $configsource_url_used);
            exit 1;
          } else { # Non-200 HTTP status code
            print "Request failed. HTTP status code: $status_code\n";
            print_response($response);
            exit 1;
          }
        }
      } while ($retry);
    }

  write_session_id($output);
  write_configsource_url_used($configsource_url_used);

  return ($exitcode, $output);
}

sub http_upload_lowlevel {
  my ($source, $app) = @_;

  my $url = $source . $pathPrefix;
  $url = add_url_property_from_flag($url, $opt_v, "verbose");
  if ($opt_F) {
    $url = add_url_property_from_option($url, $opt_F, "from");
    `$CURL_POST $url`;
  } else {
    my $TAR="tar -C $app --dereference --exclude='.[a-zA-Z0-9]*' --exclude=ext -cf - . --transform=\"s#^#application/#\" ";
    print "Uploading application '$app' using $url\n";
    if (-f $app) {
      `cat $app | $CURL_POST_ZIP $url`;
    } else {
      `$TAR | $GZIP | $CURL_POST $url`;
    }
  }
}

sub http_prepare {
  my $source = shift;
  my $session_id = shift || get_session_id();

  my $url = $source . $pathPrefix . "/$session_id/prepared";
  $url = add_url_property_from_flag($url, $opt_f, "ignoreValidationErrors");
  $url = add_url_property_from_flag($url, $opt_n, "dryrun");
  $url = add_url_property_from_flag($url, $opt_v, "verbose");
  $url = add_url_property_from_flag($url, $opt_H, "hostedVespa");
  $url = add_url_property_from_option($url, $opt_a, "applicationName");
  $url = add_url_property_from_option($url, $opt_i, "instance");
  $url = add_url_property_from_option($url, $opt_t, "timeout");
  $url = add_url_property_from_option($url, $opt_R, "rotations");
  $url = add_url_property_from_option($url, $opt_V, "vespaVersion");
  print "Preparing session $session_id using $url\n";
  `$CURL_PUT \"$url\"`;
}

sub http_content {
  my $url = shift;
  print "Getting content using $url\n";
  `$CURL_GET \"$url\"`;
}

sub http_activate {
  my $source = shift;
  my $session_id = shift || get_session_id();

  my $url = $source . $pathPrefix . "/$session_id/active";
  $url = add_url_property_from_flag($url, $opt_v, "verbose");
  $url = add_url_property_from_option($url, $opt_t, "timeout");
  print "Activating session $session_id using $url\n";
  `$CURL_PUT \"$url\"`;
}

sub get_session_id {
  my $session_id = `cat $session_id_file 2>/dev/null`;
  unless ($session_id) {
    print "Could not read session id from file, and no session id supplied as argument. Exiting.\n";
    exit 1
  }
  $session_id;
}

sub get_session_id_from_response {
  my ($response) = @_;

  my $new_session_id;
  if ($response =~ /.*"session-id":"(\d+)".*/) { $new_session_id = $1; }
  $new_session_id;
}

sub print_response {
  my ($response) = @_;
  chomp($response);
  debug("$response\n");
  if ($response) {
    my $json_text = get_json($response);
    my $error = $json_text->{error};
    if ($error) {
      print "$json_text->{error}\n";
    }
    my $status = $json_text->{status};
    foreach my $log_message (@{$json_text->{log}}) {
      print "$log_message->{level}: $log_message->{message}\n";
    }
    my $message = $json_text->{message};
    if ($message) {
      print "$message\n";
    }
    my $metadata_deploy = $json_text->{deploy};
    if ($metadata_deploy) {
      my $timestamp = $metadata_deploy->{timestamp};
      my $metadata_application = $json_text->{application};
      my $checksum = $metadata_application->{checksum};
      my $generation = $metadata_application->{generation};
      print "Checksum:   $checksum\n";
      print "Timestamp:  $timestamp\n";
      print "Generation: $generation\n";
    }
  } else {
    print "Empty response";
  }
}

sub get_json {
  my ($response) = @_;

  my $json_dir = JSON->new;
  return $json_dir->utf8->decode($response);
}

# extend $url with $url_property=true if $flag is set
sub add_url_property_from_flag {
   my ($url, $flag, $url_property) = @_;
   return $url unless $flag;
   return add_url_property($url, "$url_property=true");
}

# extend $url with $url_property=$opt if $opt is set
sub add_url_property_from_option {
  my ($url, $opt, $url_property) = @_;
  return $url unless $opt;
  add_url_property($url, "$url_property=$opt");
}

sub add_url_property {
  my ($url, $url_property) = @_;
  if ($url =~ /\?/) {
    $url = $url . "&" . $url_property;
  } else {
    $url = $url . "?" . $url_property;
  }
  $url;
}

sub write_session_id {
  my ($response) = @_;

  my $new_session_id = get_session_id_from_response($response);
  if ($new_session_id) {
    open(my $fh, '>', $session_id_file) or die "Could not open file '$session_id_file' $!";
    print $fh $new_session_id;
    close $fh;
  }
}

sub write_configsource_url_used {
  my ($configsource_url) = @_;

  if ($configsource_url) {
    open(my $fh2, '>', $configsource_url_used_file) or die "Could not open file '$configsource_url_used_file' $!";
    print $fh2 $configsource_url;
    close $fh2;
  }
}

sub get_configsource_url_used {
  my $configsource_url = `cat $configsource_url_used_file 2>/dev/null` || "";
  $configsource_url;
}

sub print_request_failed {
  my ($exitcode, $configsource_url) = @_;

  my $message = "HTTP request failed";
  if ($exitcode == 7) {
    $message .= ". Could not connect to $configsource_url";
  } else {
    $message .= " with curl exit code $exitcode";
  }

  print $message . "\n";
}


sub debug {
  my ($message) = @_;

  if ($opt_v) {
    print "$message";
  }
}

sub parse_http_response {
  my ($response) = @_;

  my $message = "";
  my $status_code = 500;

  if ($response =~ /(.*)(\d\d\d)/) {
      $message = $1;
      $status_code = int($2);
    }
  return ($status_code, $message);
}

sub create_cloudconfig_dir {
  my $path = "$cloudconfig_dir/$tenant";
  if (-e $path) {
    check_dir_permissions($path);
  } else {
      make_path($path);
  }
}


sub check_dir_permissions {
  my ($dir) = @_;

  if (!(-d $dir)) {
    print "$dir is not a directory, please fix\n";
  }
  if (!(-r $dir)) {
    print "$dir is not readable, please fix\n";
  }
}