File Coverage

blib/lib/Rex/WebUI.pm
Criterion Covered Total %
statement 16 18 88.8
branch n/a
condition n/a
subroutine 6 6 100.0
pod n/a
total 22 24 91.6


line stmt bran cond sub pod time code
1              
2             package Rex::WebUI;
3              
4 1     1   934 use strict;
  1         2  
  1         41  
5              
6 1     1   5 use Mojo::Base "Mojolicious";
  1         1  
  1         7  
7              
8 1     1   203118 use Mojo::Log;
  1         3  
  1         13  
9 1     1   1062 use Mojolicious::Plugin::Database;
  1         43354  
  1         22  
10              
11 1     1   877 use Rex::WebUI::Model::LogBook;
  1         3  
  1         36  
12 1     1   739 use Rex::WebUI::Model::RexInterface;
  0            
  0            
13              
14             use Cwd qw(abs_path);
15              
16             use DBIx::Foo qw(:all);
17             use Data::Dumper;
18              
19             use File::Basename 'dirname';
20             use File::Copy;
21             use File::Spec::Functions 'catdir';
22              
23             our $VERSION = '0.01';
24              
25             # This method will run once at server start
26             sub startup {
27             my $self = shift;
28              
29             # Switch to installable home directory
30             $self->home->parse(catdir(dirname(__FILE__), 'WebUI'));
31             $self->static->paths->[0] = $self->home->rel_dir('public');
32             $self->renderer->paths->[0] = $self->home->rel_dir('templates');
33              
34             if (my $cfg = $self->_locate_config_file) {
35             $self->plugin('Config', file => "$cfg");
36             } else {
37             # config should always be found because we ship a default config file, but best to check
38             die "Config file not found" unless $cfg;
39             }
40              
41             if (my $secret = $self->config->{secret_passphrase}) {
42             $self->secret($secret);
43             }
44              
45             my $db_config = $self->config->{db_config} || [ dsn => 'dbi:SQLite:dbname=webui.db', username => '', password => '' ];
46              
47             $self->plugin('database', {
48             @$db_config,
49             helper => 'dbh',
50             });
51              
52             $self->check_db_config($db_config);
53              
54             $self->helper(rex => sub { state $rex = Rex::WebUI::Model::RexInterface->new });
55             $self->helper(logbook => sub { state $rex = Rex::WebUI::Model::LogBook->new($self->dbh) });
56              
57             # Router
58             my $r = $self->routes;
59              
60             # Normal route to controller
61             $r->get("/")->to("dashboard#index");
62             $r->get("/dashboard")->to("dashboard#index");
63             $r->get("/notification_message")->to("dashboard#notification_message");
64              
65             $r->get("/project/:id")->to("project#index");
66             $r->get("/project/:id/task/view/:name")->to("task#view");
67             $r->post("/project/:id/task/run/:name")->to("task#run");
68             $r->websocket("/project/:id/task/tail_ws/:jobid")->to("task#tail_ws")->name('tail_ws');
69             }
70              
71             sub check_db_config {
72             my ($self, $db_config) = @_;
73              
74             my $check = $self->selectrow_array("select userid from users order by userid limit 1");
75              
76             if ($check) {
77             warn "Database OK: $check";
78             return 1;
79             }
80             elsif ($db_config->[1] =~ /^dbi:SQLite/) {
81             return $self->_init_sqllite_db;
82             }
83             else {
84             die "Database is not initialised - check your setup";
85             }
86             }
87              
88             sub _init_sqllite_db {
89             my $self = shift;
90              
91             # This is a very simple SQLite database template to allow us to ship the app via cpan / github and have a ready to go data store.
92             # Optionally the app can be configured to use MySQL etc.
93             # Hopefully we can maintain compatibility by using standard sql syntax.
94             # TODO: Create setup scripts for other db types
95              
96             warn "Setting up SQLite Database";
97              
98             $self->dbh_do("create table users (userid INTEGER PRIMARY KEY AUTOINCREMENT, username varchar(20))");
99             $self->dbh_do("insert into users (userid, username) values (1, 'admin')");
100              
101             $self->dbh_do("create table status (statusid INTEGER PRIMARY KEY AUTOINCREMENT, status varchar(20))");
102             $self->dbh_do("insert into status (statusid, status) values (0, 'Starting')");
103             $self->dbh_do("insert into status (statusid, status) values (1, 'Running')");
104             $self->dbh_do("insert into status (statusid, status) values (2, 'Completed')");
105             $self->dbh_do("insert into status (statusid, status) values (3, 'Died')");
106              
107             $self->dbh_do("create table logbook (jobid INTEGER PRIMARY KEY AUTOINCREMENT, userid int not null, task_name varchar(100), server varchar(100), statusid int, pid int)");
108             return 1;
109             }
110              
111             sub _locate_config_file
112             {
113             my $self = shift;
114              
115             # check optional locations for config file, inc current directory
116             my @cfg = ("/etc/rex/webui.conf", "/usr/local/etc/rex/webui.conf", abs_path("webui.conf"));
117              
118             my $cfg;
119             for my $file (@cfg) {
120             if(-f $file) {
121             return $file;
122             last;
123             }
124             }
125              
126             # finally if no config file is found, copy the template and the SampleRexfile from the mojo home dir
127             foreach my $file (qw(webui.conf SampleRexfile)) {
128             copy(abs_path($self->home->rel_file($file)), abs_path($file)) or die "No config file found, and unable to copy $file to current directory";
129             }
130              
131             return abs_path("webui.conf");
132             }
133              
134             1;
135              
136              
137             __END__