| 1 | package Test2::Harness::Renderer::UI; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | use Carp qw/croak/; | ||
| 6 | |||
| 7 | use Test2::Harness::UI; | ||
| 8 | use Test2::Harness::UI::Config; | ||
| 9 | use Test2::Harness::UI::RunProcessor; | ||
| 10 | |||
| 11 | use Test2::Util qw/pkg_to_file/; | ||
| 12 | use Test2::Harness::Util qw/mod2file/; | ||
| 13 | use Test2::Harness::UI::Util qw/share_dir share_file dbd_driver qdb_driver/; | ||
| 14 | use Test2::Harness::UI::UUID qw/gen_uuid/; | ||
| 15 | |||
| 16 | use DBIx::QuickDB; | ||
| 17 | use Plack::Builder; | ||
| 18 | use Plack::App::Directory; | ||
| 19 | use Plack::App::File; | ||
| 20 | use Plack::Runner; | ||
| 21 | |||
| 22 | use Net::Domain qw/hostfqdn/; | ||
| 23 | |||
| 24 | our $VERSION = '0.000150'; | ||
| 25 | |||
| 26 | use parent 'Test2::Harness::Renderer::UIDB'; | ||
| 27 | use Test2::Harness::Util::HashBase qw{ | ||
| 28 | qdb | ||
| 29 | app | ||
| 30 | port | ||
| 31 | }; | ||
| 32 | |||
| 33 | sub init { | ||
| 34 | my $self = shift; | ||
| 35 | |||
| 36 | my $settings = $self->{+SETTINGS}; | ||
| 37 | |||
| 38 | my $schema = $settings->yathui->schema // 'PostgreSQL'; | ||
| 39 | require(pkg_to_file("Test2::Harness::UI::Schema::$schema")); | ||
| 40 | |||
| 41 | my $tmp = $settings->check_prefix('workspace') ? $settings->workspace->workdir : undef; | ||
| 42 | local $ENV{TMPDIR} = $tmp if $tmp; | ||
| 43 | |||
| 44 | my $db = DBIx::QuickDB->build_db(harness_ui => {driver => qdb_driver($schema), dbd_driver => dbd_driver($schema)}); | ||
| 45 | $self->{+QDB} = $db; | ||
| 46 | |||
| 47 | my $dbh = $db->connect('quickdb', AutoCommit => 1, RaiseError => 1); | ||
| 48 | $dbh->do('CREATE DATABASE harness_ui') or die "Could not create db " . $dbh->errstr; | ||
| 49 | |||
| 50 | $db->load_sql(harness_ui => share_file('schema/' . $schema . '.sql')); | ||
| 51 | |||
| 52 | my $dsn = $db->connect_string('harness_ui'); | ||
| 53 | |||
| 54 | $ENV{HARNESS_UI_DSN} = $dsn; | ||
| 55 | |||
| 56 | my $config = Test2::Harness::UI::Config->new( | ||
| 57 | dbi_dsn => $dsn, | ||
| 58 | dbi_user => '', | ||
| 59 | dbi_pass => '', | ||
| 60 | single_user => 1, | ||
| 61 | single_run => 1, | ||
| 62 | ); | ||
| 63 | |||
| 64 | $self->{+USER} = 'root'; | ||
| 65 | my $user = $config->schema->resultset('User')->create({username => 'root', password => 'root', realname => 'root', user_id => gen_uuid()}); | ||
| 66 | |||
| 67 | $self->{+PROJECT} = 'default'; | ||
| 68 | my $proj = $config->schema->resultset('Project')->create({name => 'default', project_id => gen_uuid()}); | ||
| 69 | |||
| 70 | $self->{+CONFIG} = $config; | ||
| 71 | |||
| 72 | my $port = $settings->yathui->port; | ||
| 73 | if (my $cmd = $settings->yathui->port_command) { | ||
| 74 | $cmd =~ s/\$\$/$$/; | ||
| 75 | chomp($port = `$cmd`); | ||
| 76 | } | ||
| 77 | $port //= 8080; | ||
| 78 | $self->{+PORT} = $port; | ||
| 79 | |||
| 80 | $self->{+APP} = $self->start_app(); | ||
| 81 | |||
| 82 | $self->SUPER::init(); | ||
| 83 | } | ||
| 84 | |||
| 85 | sub links { | ||
| 86 | my $self = shift; | ||
| 87 | |||
| 88 | return $self->{+LINKS} if defined $self->{+LINKS}; | ||
| 89 | |||
| 90 | my $port = $self->{+PORT}; | ||
| 91 | my $fqdn = hostfqdn(); | ||
| 92 | |||
| 93 | $self->{+LINKS} = "\nYathUI:\n local: http://127.0.0.1:$port\n"; | ||
| 94 | if ($fqdn) { | ||
| 95 | $self->{+LINKS} .= " host: http://$fqdn:$port\n"; | ||
| 96 | } | ||
| 97 | |||
| 98 | my $dsn = $self->{+QDB}->connect_string('harness_ui'); | ||
| 99 | $self->{+LINKS} .= " DSN: $dsn\n"; | ||
| 100 | |||
| 101 | if ($self->settings->yathui->resources) { | ||
| 102 | $self->{+LINKS} .= "\nResource Links:\n"; | ||
| 103 | my $run_id = $self->settings->run->run_id; | ||
| 104 | |||
| 105 | $self->{+LINKS} .= " local: http://127.0.0.1:$port/resources/$run_id\n"; | ||
| 106 | if (my $fqdn = hostfqdn()) { | ||
| 107 | $self->{+LINKS} .= " host: http://$fqdn:$port/resources/$run_id\n"; | ||
| 108 | } | ||
| 109 | } | ||
| 110 | |||
| 111 | return $self->{+LINKS} .= "\n"; | ||
| 112 | } | ||
| 113 | |||
| 114 | sub start_app { | ||
| 115 | my $self = shift; | ||
| 116 | |||
| 117 | my $config = $self->{+CONFIG}; | ||
| 118 | my $settings = $self->{+SETTINGS}; | ||
| 119 | |||
| 120 | my $pid = fork // die "Could not fork: $!"; | ||
| 121 | if ($pid) { | ||
| 122 | return $pid; | ||
| 123 | } | ||
| 124 | |||
| 125 | setpgrp(0, 0); | ||
| 126 | |||
| 127 | my $app = builder { | ||
| 128 | mount '/js' => Plack::App::Directory->new({root => share_dir('js')})->to_app; | ||
| 129 | mount '/css' => Plack::App::Directory->new({root => share_dir('css')})->to_app; | ||
| 130 | mount '/favicon.ico' => Plack::App::File->new({file => share_dir('img') . '/favicon.ico'})->to_app; | ||
| 131 | mount '/img' => Plack::App::Directory->new({root => share_dir('img')})->to_app; | ||
| 132 | |||
| 133 | mount '/' => sub { | ||
| 134 | Test2::Harness::UI->new(config => $config)->to_app->(@_); | ||
| 135 | }; | ||
| 136 | }; | ||
| 137 | |||
| 138 | $ENV{PLACK_ENV} = 'test'; | ||
| 139 | my $r = Plack::Runner->new(access_log => undef, default_middleware => 0); | ||
| 140 | my @options = ("--server", "Starman", '--workers' => 10); | ||
| 141 | |||
| 142 | push @options => ('--listen' => ":" . $self->{+PORT}); | ||
| 143 | |||
| 144 | $r->parse_options(@options); | ||
| 145 | open(STDERR, '>', '/dev/null'); | ||
| 146 | $r->run($app); | ||
| 147 | |||
| 148 | exit(0); | ||
| 149 | } | ||
| 150 | |||
| 151 | sub finish { | ||
| 152 | my $self = shift; | ||
| 153 | my $out = $self->SUPER::finish(); | ||
| 154 | |||
| 155 | return $out unless kill(0, $self->{+APP}); | ||
| 156 | |||
| 157 | print "Leaving yathui server open, press enter to stop it...\n"; | ||
| 158 | my $in = <STDIN>; | ||
| 159 | |||
| 160 | kill('TERM', $self->{+APP}); | ||
| 161 | waitpid($self->{+APP}, 0); | ||
| 162 | return $out; | ||
| 163 | } | ||
| 164 | |||
| 165 | sub DESTROY { | ||
| 166 | my $self = shift; | ||
| 167 | |||
| 168 | if (my $pid = $self->{+APP}) { | ||
| 169 | kill('TERM', $pid); | ||
| 170 | waitpid($pid, 0); | ||
| 171 | } | ||
| 172 | |||
| 173 | $self->SUPER::DESTROY(); | ||
| 174 | } | ||
| 175 | |||
| 176 | 1; |