Now with blog testing ability...

master
Manager Bot 3 years ago
parent 7406b204c9
commit fb9d0837b1
  1. 11
      Web/lib/MJB/Web.pm
  2. 22
      Web/lib/MJB/Web/Controller/Blog.pm
  3. 7
      Web/lib/MJB/Web/Plugin/Jekyll.pm
  4. 28
      Web/lib/MJB/Web/Plugin/Jekyll/Blog.pm
  5. 6
      Web/lib/MJB/Web/Test.pm
  6. 8
      Web/lib/Test/Mojo/MJB.pm

@ -7,6 +7,12 @@ sub startup ($self) {
? 'mjb.yml' ? 'mjb.yml'
: '/etc/mjb.yml' : '/etc/mjb.yml'
}); });
# Make an attribute that is true when running in test mode.
$self->helper( is_testmode => sub {
return 1 if exists $ENV{MJB_TESTMODE} && $ENV{MJB_TESTMODE} == 1;
return 0;
});
# Some quick configs.... # Some quick configs....
$self->config->{register}->{require_invite} = 1; $self->config->{register}->{require_invite} = 1;
@ -32,9 +38,10 @@ sub startup ($self) {
# Load the MJB::Web::Plugin::Jekyll plugin for $c->jekyll # Load the MJB::Web::Plugin::Jekyll plugin for $c->jekyll
$self->plugin('Jekyll'); $self->plugin('Jekyll');
# Create $self->db as an MJB::DB connection. # Create $self->db as an MJB::DB connection.
$self->helper( db => sub { $self->helper( db => sub ($c) {
return state $db = exists $ENV{MJB_TESTMODE} && $ENV{MJB_TESTMODE} == 1 return state $db = $c->is_testmode
? MJB::DB->connect( $ENV{MJB_DSN}, '', '' ) ? MJB::DB->connect( $ENV{MJB_DSN}, '', '' )
: MJB::DB->connect($self->config->{database}->{mjb}); : MJB::DB->connect($self->config->{database}->{mjb});
}); });

@ -61,8 +61,8 @@ sub domain_owned ( $c ) {
# show_blog_domain_owned # show_blog_domain_owned
#== #==
sub do_domain ( $c ) { sub do_domain ( $c ) {
my $domain = $c->stash->{form}->{domain} = lc($c->param('domain')); my $domain = $c->stash->{form}->{domain} = lc($c->param('domain') || "");
my $hosted_subdomain = $c->stash->{form}->{hosted_subdomain} = lc($c->param('hosted_subdomain')); my $hosted_subdomain = $c->stash->{form}->{hosted_subdomain} = lc($c->param('hosted_subdomain') || "");
my $hosted_domain_id = $c->stash->{form}->{hosted_domain_id} = $c->param('hosted_domain_id'); my $hosted_domain_id = $c->stash->{form}->{hosted_domain_id} = $c->param('hosted_domain_id');
my $calling_route = $c->stash->{form}->{calling_route} = $c->param('calling_route'); my $calling_route = $c->stash->{form}->{calling_route} = $c->param('calling_route');
@ -142,14 +142,18 @@ sub do_domain ( $c ) {
return $c->redirect_error( $calling_route ) return $c->redirect_error( $calling_route )
if $c->stash->{errors}; if $c->stash->{errors};
# Schedule a job to deploy the website # If we don't have an SSL domain, that means we need to get a cert through an
# http challenge.
if ( ! $ssl_domain ) { if ( ! $ssl_domain ) {
my $ssl_job_id = $c->minion->enqueue( 'create_ssl_cert', [ $blog->id ], { # Do not run this job in test mode.
notes => { '_bid_' . $blog->id => 1 }, if ( ! $c->is_testmode ) {
priority => $blog->build_priority, my $ssl_job_id = $c->minion->enqueue( 'create_ssl_cert', [ $blog->id ], {
queue => 'certbot', notes => { '_bid_' . $blog->id => 1 },
}); priority => $blog->build_priority,
$blog->create_related( 'jobs', { minion_job_id => $ssl_job_id } ); queue => 'certbot',
});
$blog->create_related( 'jobs', { minion_job_id => $ssl_job_id } );
}
} }
$c->redirect_to( $c->url_for( 'show_blog_settings', { id => $blog->id } ) ); $c->redirect_to( $c->url_for( 'show_blog_settings', { id => $blog->id } ) );

@ -3,6 +3,11 @@ use Mojo::Base 'Mojolicious::Plugin', -signatures;
use MJB::Web::Plugin::Jekyll::Blog; use MJB::Web::Plugin::Jekyll::Blog;
sub register ( $self, $app, $config ) { sub register ( $self, $app, $config ) {
my %opts = ();
if ( $app->is_testmode ) {
$opts{push_on_change} = 0;
$opts{root} = $ENV{MJB_TESTMODE_TEMPDIR},
}
$app->helper( jekyll => sub ($c, $domain) { $app->helper( jekyll => sub ($c, $domain) {
return MJB::Web::Plugin::Jekyll::Blog->new( return MJB::Web::Plugin::Jekyll::Blog->new(
@ -10,8 +15,10 @@ sub register ( $self, $app, $config ) {
domain => $domain, domain => $domain,
init_from => $c->config->{jekyll_init_repo}, init_from => $c->config->{jekyll_init_repo},
repo => $c->config->{store_repo_base} . "$domain.git", repo => $c->config->{store_repo_base} . "$domain.git",
%opts,
); );
}); });
} }
1; 1;

@ -44,6 +44,14 @@ has repo => (
required => 1, required => 1,
); );
#==
# When set to true, push the repo after any action that changes it.
#==
has push_on_change => (
is => 'ro',
default => sub { 1 },
);
#== #==
# The full local path (i.e. /var/repos/domain.com ) to the git repository that we will execute git commands in. # The full local path (i.e. /var/repos/domain.com ) to the git repository that we will execute git commands in.
#== #==
@ -117,7 +125,7 @@ sub init {
# Push the repo to the store # Push the repo to the store
$self->system_command( [ qw( git push origin master ) ], { $self->system_command( [ qw( git push origin master ) ], {
chdir => $self->repo_path, chdir => $self->repo_path,
}); }) if $self->push_on_change;
return $self; return $self;
} }
@ -206,7 +214,7 @@ sub remove_markdown_file {
# Push the changes # Push the changes
$self->system_command( [ qw( git push origin master ) ], { $self->system_command( [ qw( git push origin master ) ], {
chdir => $self->repo_path, chdir => $self->repo_path,
}); }) if $self->push_on_change;
return $self; return $self;
} }
@ -349,7 +357,7 @@ sub write_config {
# Push the repo to the store server # Push the repo to the store server
$self->system_command( [ qw( git push origin master ) ], { $self->system_command( [ qw( git push origin master ) ], {
chdir => $self->repo_path, chdir => $self->repo_path,
}); }) if $self->push_on_change;
return 1; return 1;
} }
@ -382,7 +390,7 @@ sub write_post {
# Push the repo to the store server # Push the repo to the store server
$self->system_command( [ qw( git push origin master ) ], { $self->system_command( [ qw( git push origin master ) ], {
chdir => $self->repo_path, chdir => $self->repo_path,
}); }) if $self->push_on_change;
return 1; return 1;
} }
@ -412,7 +420,7 @@ sub commit_file {
# Push the repo to the store server # Push the repo to the store server
$self->system_command( [ qw( git push origin master ) ], { $self->system_command( [ qw( git push origin master ) ], {
chdir => $self->repo_path, chdir => $self->repo_path,
}); }) if $self->push_on_change;
return 1; return 1;
@ -443,7 +451,7 @@ sub remove_file {
# Push the repo to the store server # Push the repo to the store server
$self->system_command( [ qw( git push origin master ) ], { $self->system_command( [ qw( git push origin master ) ], {
chdir => $self->repo_path, chdir => $self->repo_path,
}); }) if $self->push_on_change;
return 1; return 1;
@ -471,7 +479,7 @@ sub restore_commit {
# Push the repo to the store server # Push the repo to the store server
$self->system_command( [ qw( git push origin master ) ], { $self->system_command( [ qw( git push origin master ) ], {
chdir => $self->repo_path, chdir => $self->repo_path,
}); }) if $self->push_on_change;
return 1; return 1;
@ -578,11 +586,11 @@ sub system_command {
die "Error: directory " . $settings->{chdir} . "doesn't exist." die "Error: directory " . $settings->{chdir} . "doesn't exist."
unless -d $settings->{chdir}; unless -d $settings->{chdir};
$settings->{return_chdir} = getcwd();
# Change to that directory, or die with error. # Change to that directory, or die with error.
chdir $settings->{chdir} chdir $settings->{chdir}
or die "Failed to chdir to " . $settings->{chdir} . ": $!"; or die "Failed to chdir to " . $settings->{chdir} . ": $!";
$settings->{return_chdir} = getcwd();
} }
# Mask values we don't want exposed in the logs. # Mask values we don't want exposed in the logs.
@ -634,7 +642,7 @@ sub system_command {
# Return to the directory we started in if we chdir'ed. # Return to the directory we started in if we chdir'ed.
if ( $settings->{return_chdir} ) { if ( $settings->{return_chdir} ) {
chdir $settings->{return_chdir} chdir $settings->{return_chdir}
or die "Failed to chdir to " . $settings->{chdir} . ": $!"; or die "Failed to chdir to " . $settings->{return_chdir} . ": $!";
} }
if ( $ENV{MJB_DEBUG} ) { if ( $ENV{MJB_DEBUG} ) {

@ -4,6 +4,7 @@ use Test::More;
use Test::Deep; use Test::Deep;
use Test::Mojo::MJB; use Test::Mojo::MJB;
use Test::Postgresql58; use Test::Postgresql58;
use Mojo::File;
push our @ISA, qw( Exporter ); push our @ISA, qw( Exporter );
push our @EXPORT, qw( $run_code ); push our @EXPORT, qw( $run_code );
@ -25,8 +26,9 @@ our $pgsql = Test::Postgresql58->new()
load_psql_file("../DB/etc/schema.sql"); load_psql_file("../DB/etc/schema.sql");
$ENV{MJB_TESTMODE} = 1; $ENV{MJB_TESTMODE} = 1;
$ENV{MJB_DSN} = $pgsql->dsn; $ENV{MJB_DSN} = $pgsql->dsn;
$ENV{MJB_TESTMODE_TEMPDIR} = Mojo::File::tempdir->to_string;
sub load_psql_file { sub load_psql_file {
my ( $file ) = @_; my ( $file ) = @_;

@ -34,6 +34,7 @@ use parent 'Test::Mojo';
use Data::Dumper; use Data::Dumper;
use Test::Deep; use Test::Deep;
use Test::More; use Test::More;
use File::Path qw( remove_tree );
sub new { sub new {
my $class = shift; my $class = shift;
@ -150,4 +151,11 @@ sub _sg {
return shift->{data_stack}; return shift->{data_stack};
} }
sub clear_tempdir {
my ( $t ) = @_;
remove_tree( $ENV{MJB_TESTMODE_TEMPDIR} )
if -d $ENV{MJB_TESTMODE_TEMPDIR};
}
1; 1;

Loading…
Cancel
Save