Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
lib/LANraragi/Plugin/Managed

node_modules
content
thumb
Expand Down
10 changes: 10 additions & 0 deletions lib/LANraragi.pm
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ use LANraragi::Utils::I18NInitializer;

use LANraragi::Model::Search;
use LANraragi::Model::Config;
use LANraragi::Model::Plugins;
use LANraragi::Model::Server;
use LANraragi::Model::Setup qw(first_install_actions);
use LANraragi::Model::Metrics;

Expand Down Expand Up @@ -138,6 +140,14 @@ sub startup {
# through LRR's rotating logger pipeline.
$self->log( get_logger( "Mojolicious", "mojo" ) );

# Reconcile discovered plugins with Redis state.
my $redis_config = $self->LRR_CONF->get_redis_config;
LANraragi::Model::Plugins::scan_plugins($redis_config);

# Reset restart flag.
LANraragi::Model::Server::clear_restart_pending($redis_config);
$redis_config->quit();

#Plugin listing
my @plugins = get_plugins("metadata");
foreach my $pluginfo (@plugins) {
Expand Down
20 changes: 20 additions & 0 deletions lib/LANraragi/Controller/Api/Other.pm
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@ use Redis;

use LANraragi::Model::Stats;
use LANraragi::Model::Opds;
use LANraragi::Model::Server qw(is_restart_pending);
use LANraragi::Utils::Generic qw(render_api_response);
use LANraragi::Utils::Logging qw(get_logger);
use LANraragi::Utils::Plugins qw(get_plugin get_plugins use_plugin);

sub serve_serverinfo {
my $self = shift;

my $redis = $self->LRR_CONF->get_redis_config;
my $last_clear = $redis->hget( "LRR_SEARCHCACHE", "created" ) || time;
my $restart = is_restart_pending($redis);
my $arc_stat = LANraragi::Model::Stats::get_archive_count;
my $page_stat = LANraragi::Model::Stats::get_page_stat;
$redis->quit();
Expand All @@ -39,6 +42,7 @@ sub serve_serverinfo {
total_pages_read => $page_stat,
total_archives => $arc_stat,
cache_last_cleared => $last_clear,
restart_required => $restart ? \1 : \0,
excluded_namespaces => [
split( /\s*,\s*/, $self->LRR_CONF->get_excludednamespaces )
]
Expand Down Expand Up @@ -90,6 +94,8 @@ sub list_plugins {
my $type = $self->stash('type');

my @plugins = get_plugins($type);
my $redis = $self->LRR_CONF->get_redis_config;
my $logger = get_logger( "Plugins", "lanraragi" );

foreach my $plugin (@plugins) {
if ( ref( $plugin->{parameters} ) eq 'HASH' ) {
Expand All @@ -99,8 +105,22 @@ sub list_plugins {
}
$plugin->{parameters} = \@parameters_array;
}

my $namerds = "LRR_PLUGIN_" . uc( $plugin->{namespace} );
$plugin->{registry} = $redis->hget( $namerds, "installed_registry" );
$plugin->{sha256} = $redis->hget( $namerds, "installed_sha256" );

# Usually installed_version shouldn't be different from version.
my $installed_version = $redis->hget( $namerds, "installed_version" );
my $plugin_version = $plugin->{version};
if ( defined $installed_version && $installed_version ne $plugin_version ) {
$logger->warn(
"Plugin $plugin installed_version='$installed_version' but plugin->version='$plugin_version'"
);
}
}

$redis->quit();
$self->render( openapi => \@plugins );
}

Expand Down
59 changes: 59 additions & 0 deletions lib/LANraragi/Controller/Api/Plugins.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package LANraragi::Controller::Api::Plugins;
use Mojo::Base 'Mojolicious::Controller';

use LANraragi::Model::Plugins;
use LANraragi::Utils::Generic qw(render_api_response exec_with_lock);

# Install a managed plugin.
sub install_plugin {
my $self = shift->openapi->valid_input or return;
my $body = $self->req->json;
my $namespace = $body->{namespace};
my $registry_id = $body->{registry};
my $version = $body->{version};
my $force = $body->{force} // 0;

my $jobid = $self->minion->enqueue(
install_plugin => [ $namespace, $registry_id, $version, $force ] => { priority => 0, attempts => 1 } );

$self->render(
openapi => {
operation => "install_plugin",
namespace => $namespace,
success => 1,
job => $jobid,
}
);
}

# Uninstall a managed plugin.
sub uninstall_plugin {
my $self = shift->openapi->valid_input or return;
my $namespace = $self->stash('plugin_namespace');

return unless exec_with_lock(
$self,
"plugin-write:" . uc($namespace),
"uninstall_plugin",
$namespace,
sub {
my $redis = $self->LRR_CONF->get_redis_config;
my ( $status, $message ) = LANraragi::Model::Plugins::uninstall_plugin(
$namespace, $redis
);
$redis->quit();

unless ( $status == 200 ) {
$self->render(
openapi => { operation => "uninstall_plugin", error => $message, success => 0 },
status => $status
);
return;
}

render_api_response( $self, "uninstall_plugin" );
}
);
}

1;
Loading
Loading