Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 144 lines (112 sloc) 3.847 kB
8ffbc1f @jberger renamed create_database command to "setup"
authored
1 package Galileo::Command::setup;
06c16f2 @jberger Implemented cleaner setup interface
authored
2 use Mojo::Base 'Mojolicious::Command';
3
1a531a1 @jberger web setup is now the only setup
authored
4 use Mojolicious::Command::daemon;
e6a772a @jberger prompt for username and password for admin account
authored
5
1a531a1 @jberger web setup is now the only setup
authored
6 use Mojolicious::Routes;
7 use Mojo::JSON 'j';
8 use Mojo::Util 'spurt';
06c16f2 @jberger Implemented cleaner setup interface
authored
9
1a531a1 @jberger web setup is now the only setup
authored
10 has description => "Configure your Galileo CMS via a web interface\n";
2bdaa5f @jberger move the output
authored
11
1a531a1 @jberger web setup is now the only setup
authored
12 sub run {
13 my ($self, @args) = @_;
14
15 my $app = $self->app;
f3b3d8a @jberger Fixes for dbicdh.
authored
16 $app->plugin('Galileo::Plugin::Deploy'); # provides dh helper
1a531a1 @jberger web setup is now the only setup
authored
17
18 my $r = Mojolicious::Routes->new;
19 $app->routes($r); # remove all routes
20
21 $app->helper( 'control_group' => sub {
22 my $self = shift;
23 my $contents = pop;
24 my %args = @_;
f1e000c @jberger remove trailing whitespace
authored
25
f409164 @jberger fix reply->not_found
authored
26 $self->render_to_string(
dd92cdb @jberger Move setup templates from the __DATA__ section to proper files.
authored
27 template => 'setup/control_group',
1a531a1 @jberger web setup is now the only setup
authored
28 'control_group.contents' => ref $contents ? $contents->() : $contents,
29 'control_group.label' => $args{label} || '',
30 'control_group.for' => $args{for} || '',
31 );
32 });
33
dd92cdb @jberger Move setup templates from the __DATA__ section to proper files.
authored
34 $r->any( '/' => 'setup/welcome' );
35 $r->any( '/configure' => 'setup/configure' );
1a531a1 @jberger web setup is now the only setup
authored
36 $r->any( '/store_config' => sub {
37 my $self = shift;
38 my @params = sort $self->param;
39
40 # map JSON keys to Perl data
41 my %params = map { $_ => scalar $self->param($_) } @params;
ad0ce99 @keedi Load pagedown-extra with default options
keedi authored
42 foreach my $key ( qw/extra_css extra_js extra_static_paths secrets db_options pagedown_extra_options/ ) {
1a531a1 @jberger web setup is now the only setup
authored
43 $params{$key} = j($params{$key});
44 }
45
5e0cd78 @jberger use dumper helper
authored
46 spurt $self->dumper(\%params), $self->app->config_file;
f1e000c @jberger remove trailing whitespace
authored
47
1a531a1 @jberger web setup is now the only setup
authored
48 $self->app->load_config;
49 $self->humane_flash( 'Configuration saved' );
50 $self->redirect_to('/');
51 });
52
53 $r->any( '/database' => sub {
54 my $self = shift;
55
56 my $dh = $self->dh;
57
58 # Nothing installed
f3b3d8a @jberger Fixes for dbicdh.
authored
59 my $installed = $dh->installed_version;
60 unless ( $installed ) {
dd92cdb @jberger Move setup templates from the __DATA__ section to proper files.
authored
61 return $self->render( 'setup/database' );
1a531a1 @jberger web setup is now the only setup
authored
62 }
63
f3b3d8a @jberger Fixes for dbicdh.
authored
64 # Something is installed, check for upgrades
65 my $available = $dh->schema->schema_version;
1a531a1 @jberger web setup is now the only setup
authored
66
67 # Do nothing if version is current
68 if ( $installed == $available ) {
f3b3d8a @jberger Fixes for dbicdh.
authored
69 $self->flash( 'galileo.message' => 'Database schema is current.' );
1a531a1 @jberger web setup is now the only setup
authored
70 } else {
71 $dh->do_upgrade;
f3b3d8a @jberger Fixes for dbicdh.
authored
72 $self->flash( 'galileo.message' => "Upgrade database $installed -> $available." );
1a531a1 @jberger web setup is now the only setup
authored
73 }
74
75 $self->redirect_to('finish');
76 });
77
78 $r->any( '/database_install' => sub {
79 my $self = shift;
80 my $pw1 = $self->param('pw1');
81 my $pw2 = $self->param('pw2');
82 unless ( $pw1 eq $pw2 ) {
83 $self->humane_flash( q{Passwords don't match!} );
84 return $self->redirect_to('database');
85 }
86
87 my $user = $self->param('user');
88 my $full = $self->param('full');
89
f3b3d8a @jberger Fixes for dbicdh.
authored
90 my $dh = $self->dh;
91
92 eval {
93 $dh->do_install;
94 $dh->inject_sample_data($user, $pw1, $full);
95 };
96
c5db1b8 @jberger handle bad input to database installation (closes #14)
authored
97 if ($@) {
98 my $error = "$@";
99 chomp $error;
100 $self->humane_flash( $error );
101 return $self->redirect_to('database');
102 }
1a531a1 @jberger web setup is now the only setup
authored
103
f3b3d8a @jberger Fixes for dbicdh.
authored
104 $self->flash( 'galileo.message' => 'Database has been setup.' );
1a531a1 @jberger web setup is now the only setup
authored
105 $self->redirect_to('finish');
106 });
107
108 $r->any('/finish' => sub {
109 my $self = shift;
110 my $message = $self->flash( 'galileo.message' );
111
f3b3d8a @jberger Fixes for dbicdh.
authored
112 my $dh = $self->dh;
113 my $installed = $dh->installed_version;
114 my $available = $dh->schema->schema_version;
115 my $has_admin = $dh->has_admin_user;
116
117 if ($installed) {
118 unless ($has_admin) {
119 $message .= ' No administration user was created.';
120 }
121 unless ($installed == $available) {
122 $message .= " Installed database version ($installed) is older than the newest available ($available).";
123 }
1a531a1 @jberger web setup is now the only setup
authored
124 $self->stash( 'galileo.success' => 1 );
125 $self->stash( 'galileo.message' => $message );
126 } else {
127 $self->stash( 'galileo.success' => 0 );
f1e000c @jberger remove trailing whitespace
authored
128 $self->stash(
1a531a1 @jberger web setup is now the only setup
authored
129 'galileo.message' =>
130 'It does not appear that your database is setup, please rerun the setup utility'
131 );
132 }
133
134 $self->humane_stash( 'Goodbye' );
dd92cdb @jberger Move setup templates from the __DATA__ section to proper files.
authored
135 $self->render('setup/finish');
dfb3190 @jberger improve the mechanism for stopping the setup server
authored
136 $self->tx->on( finish => sub { exit } );
1a531a1 @jberger web setup is now the only setup
authored
137 });
138
139 $self->Mojolicious::Command::daemon::run(@args);
cf46d76 @jberger prepare for release 0.012
authored
140 }
141
1a531a1 @jberger web setup is now the only setup
authored
142 1;
e6a772a @jberger prompt for username and password for admin account
authored
143
Something went wrong with that request. Please try again.