summaryrefslogtreecommitdiffstats
path: root/scripts/entrypoint.pl
blob: 4a3cb2183ef8c03e19f4cc2251edbcfebb803367 (plain)
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
#!/usr/bin/perl
use 5.10.1;
use strict;
use warnings;
use lib qw(/app /app/local/lib/perl5);
use autodie qw(:all);

use Bugzilla::Install::Localconfig ();
use Bugzilla::Install::Util qw(install_string);
use Bugzilla::Test::Util qw(create_user);
use Bugzilla::DaemonControl qw(
  run_cereal_and_httpd
  run_cereal_and_jobqueue
  assert_httpd assert_database assert_selenium
  on_finish on_exception
);

use DBI;
use Data::Dumper;
use English qw(-no_match_vars $EUID);
use File::Copy::Recursive qw(dircopy);
use Getopt::Long qw(:config gnu_getopt);
use IO::Async::Loop;
use IO::Async::Process;
use IO::Async::Signal;
use IO::Async::Timer::Periodic;
use LWP::Simple qw(get);
use POSIX qw(WEXITSTATUS setsid);
use Sys::Hostname;
use User::pwent;

BEGIN {
  STDOUT->autoflush(1);
  STDERR->autoflush(1);
}

use constant CI => $ENV{CI};

my $cmd  = shift @ARGV;
my $opts = __PACKAGE__->can("opt_$cmd") // sub {@ARGV};
my $func = __PACKAGE__->can("cmd_$cmd") // sub {
  check_data_dir();
  wait_for_db();
  run($cmd, @_);
};

fix_path();
check_user();
check_env(
  qw(
    LOCALCONFIG_ENV
    BMO_db_host
    BMO_db_name
    BMO_db_user
    BMO_db_pass
    BMO_memcached_namespace
    BMO_memcached_servers
    BMO_urlbase
    )
);

$func->($opts->());

sub cmd_demo {
  unless (-f '/app/data/params') {
    cmd_load_test_data();
    check_env(
      qw(
        PHABRICATOR_BOT_LOGIN
        PHABRICATOR_BOT_PASSWORD
        PHABRICATOR_BOT_API_KEY
        CONDUIT_USER_LOGIN
        CONDUIT_USER_PASSWORD
        CONDUIT_USER_API_KEY
        )
    );
    run('perl', 'scripts/generate_conduit_data.pl');
  }
  cmd_httpd();
}

sub cmd_httpd {
  check_data_dir();
  wait_for_db();

  my $httpd_exit_f = run_cereal_and_httpd();
  assert_httpd()->get();
  exit $httpd_exit_f->get();
}

sub cmd_jobqueue {
  my (@args) = @_;
  check_data_dir();
  wait_for_db();
  exit run_cereal_and_jobqueue(@args)->get;
}

sub cmd_dev_httpd {
  my $have_params = -f "/app/data/params";
  assert_database->get();

  run('perl', 'checksetup.pl', '--no-template', $ENV{BZ_ANSWERS_FILE});
  if (not $have_params) {
    run(
      'perl', 'scripts/generate_bmo_data.pl',
      '--param' => 'use_mailer_queue=0',
      'vagrant@bmo-web.vm'
    );
  }

  my $httpd_exit_f = run_cereal_and_httpd('-DACCESS_LOGS');
  assert_httpd()->get;
  exit $httpd_exit_f->get;
}

sub cmd_checksetup_gen_files {
  my (@args) = @_;
  run('perl', 'checksetup.pl', '--no-database', @args);
}

sub cmd_checksetup {
  check_data_dir();
  wait_for_db();
  run('perl', 'checksetup.pl', '--no-template', '--no-permissions');
}

sub cmd_load_test_data {
  wait_for_db();

  die 'BZ_QA_ANSWERS_FILE is not set' unless $ENV{BZ_QA_ANSWERS_FILE};
  run('perl', 'checksetup.pl', '--no-template', $ENV{BZ_QA_ANSWERS_FILE});

  if ($ENV{BZ_QA_LEGACY_MODE}) {
    run('perl', 'scripts/generate_bmo_data.pl', '--user-pref',
      'ui_experiments=off');
    chdir '/app/qa/config';
    say 'chdir(/app/qa/config)';
    run('perl', 'generate_test_data.pl');
  }
  else {
    run('perl', 'scripts/generate_bmo_data.pl', '--param' => 'use_mailer_queue=0');
  }
}

sub cmd_test_webservices {
  my $conf = require $ENV{BZ_QA_CONF_FILE};

  check_data_dir();
  copy_qa_extension();
  assert_database()->get;
  my $httpd_exit_f = run_cereal_and_httpd('-DHTTPD_IN_SUBDIR');
  my $prove_exit_f = run_prove(
    httpd_url => $conf->{browser_url},
    prove_cmd => [
      'prove', '-qf', '-I/app', '-I/app/local/lib/perl5',
      sub { glob 'webservice_*.t' },
    ],
    prove_dir => '/app/qa/t',
  );
  exit Future->wait_any($prove_exit_f, $httpd_exit_f)->get;
}

sub cmd_test_selenium {
  my $conf = require $ENV{BZ_QA_CONF_FILE};
  $ENV{HTTP_BACKEND} = 'simple';

  check_data_dir();
  copy_qa_extension();

  assert_database()->get;
  assert_selenium()->get;
  my $httpd_exit_f = run_cereal_and_httpd('-DHTTPD_IN_SUBDIR');
  my $prove_exit_f = run_prove(
    httpd_url => $conf->{browser_url},
    prove_dir => '/app/qa/t',
    prove_cmd => [
      'prove', '-qf', '-Ilib', '-I/app', '-I/app/local/lib/perl5',
      sub { glob 'test_*.t' }
    ],
  );
  exit Future->wait_any($prove_exit_f, $httpd_exit_f)->get;
}

sub cmd_shell { run('bash', '-l'); }

sub cmd_prove {
  my (@args) = @_;
  run('prove', '-I/app', '-I/app/local/lib/perl5', @args);
}
sub cmd_version { run('cat', '/app/version.json'); }

sub cmd_test_bmo {
  my (@prove_args) = @_;
  check_data_dir();

  assert_database()->get;
  assert_selenium()->get;
  $ENV{BZ_TEST_NEWBIE}      = 'newbie@mozilla.example';
  $ENV{BZ_TEST_NEWBIE_PASS} = 'captain.space.bagel.ROBOT!';
  create_user(
    $ENV{BZ_TEST_NEWBIE},
    $ENV{BZ_TEST_NEWBIE_PASS},
    realname => 'Newbie User'
  );

  $ENV{BZ_TEST_NEWBIE2}      = 'newbie2@mozilla.example';
  $ENV{BZ_TEST_NEWBIE2_PASS} = 'captain.space.pants.time.lord';

  my $httpd_exit_f = run_cereal_and_httpd();
  my $prove_exit_f = run_prove(
    httpd_url => $ENV{BZ_BASE_URL},
    prove_cmd => ['prove', '-I/app', '-I/app/local/lib/perl5', @prove_args],
  );

  exit Future->wait_any($prove_exit_f, $httpd_exit_f)->get;
}

sub run_prove {
  my (%param) = @_;

  my $prove_cmd = $param{prove_cmd};
  my $prove_dir = $param{prove_dir};
  assert_httpd()->then(sub {
    my $loop = IO::Async::Loop->new;
    $loop->connect(socktype => 'stream', host => 'localhost', service => 5880,)
      ->then(sub {
      my $socket       = shift;
      my $prove_exit_f = $loop->new_future;
      my $prove        = IO::Async::Process->new(
        code => sub {
          chdir $prove_dir if $prove_dir;
          my @cmd = (map { ref $_ eq 'CODE' ? $_->() : $_ } @$prove_cmd);
          warn "run @cmd\n";
          exec @cmd;
        },
        setup        => [stdin => ['close'], stdout => ['dup', $socket],],
        on_finish    => on_finish($prove_exit_f),
        on_exception => on_exception('prove', $prove_exit_f),
      );
      $prove_exit_f->on_cancel(sub { $prove->kill('TERM') });
      $loop->add($prove);
      return $prove_exit_f;
      });
  });
}

sub copy_qa_extension {
  say 'copying the QA extension...';
  dircopy('/app/qa/extensions/QA', '/app/extensions/QA');
}

sub cmd_wait_for_db {
  wait_for_db();
}

sub wait_for_db {
  assert_database()->get;
}

sub check_user {
  die 'Effective UID must be 10001!' unless $EUID == 10_001;
  my $user = getpwuid($EUID)->name;
  die "Name of EUID must be app, not $user" unless $user eq 'app';
}

sub check_data_dir {
  die "/app/data must be writable by user 'app' (id: $EUID)"
    unless -w '/app/data';
  die '/app/data/params must exist' unless -f '/app/data/params';
}

sub check_env {
  my (@require_env) = @_;
  my @missing_env = grep { not exists $ENV{$_} } @require_env;
  if (@missing_env) {
    die 'Missing required environmental variables: ', join(', ', @missing_env),
      "\n";
  }
}

sub fix_path {
  $ENV{PATH} = "/app/local/bin:$ENV{PATH}";
}

sub run {
  my (@cmd) = @_;
  say "+ @cmd";
  my $rv = system @cmd;
  if ($rv != 0) {
    exit 1;
  }
}