From 24e3e7f25569b4ccf6188dd0a5b2179c9aa2b3e6 Mon Sep 17 00:00:00 2001 From: David Cook Date: Thu, 23 Jul 2020 03:54:54 +0000 Subject: [PATCH] WIP https://bugs.koha-community.org/show_bug.cgi?id=25671 --- Koha/Plugins/Utils.pm | 69 +++++++++++++++++++++ debian/scripts/koha-plugin | 123 ++++++++++++++++++++++++++++++++++++++ misc/koha_plugin.pl | 146 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 338 insertions(+) create mode 100644 Koha/Plugins/Utils.pm create mode 100644 debian/scripts/koha-plugin create mode 100644 misc/koha_plugin.pl diff --git a/Koha/Plugins/Utils.pm b/Koha/Plugins/Utils.pm new file mode 100644 index 0000000000..7d92989418 --- /dev/null +++ b/Koha/Plugins/Utils.pm @@ -0,0 +1,69 @@ +package Koha::Plugins::Utils; + +# Copyright 2020 Prosentient Systems +# +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +use Modern::Perl; +use Archive::Extract; +use File::Temp; +use File::Copy; + +sub download_plugin { + my ($class, $args) = @_; + my $plugin; + my $url = $args->{url}; + if ($url){ + + my ( $tfh, $tempfile ) = File::Temp::tempfile( UNLINK => 1 ); + + my $ua = Mojo::UserAgent->new(max_redirects => 5); + my $tx = $ua->get($url); + $tx->result->content->asset->move_to($tempfile); + + $plugin = $tempfile; + } + return $plugin; +} + +sub unzip_plugin { + my ($class, $args) = @_; + my $unzipped_plugin; + my $zip = $args->{zip}; + if ($zip){ + my $staging_dir = File::Temp::tempdir( CLEANUP => 1 ); + my $ae = Archive::Extract->new( archive => $zip, type => 'zip' ); + $ae->extract( to => $staging_dir ); + $unzipped_plugin = $staging_dir; + } + return $unzipped_plugin; +} + +sub copy_unzipped_plugin { + my ($class, $args) = @_; + my $copied; + my $unzipped_plugin = $args->{unzipped_plugin}; + my $plugin_dir = $args->{plugin_dir}; + if ($unzipped_plugin && -d $unzipped_plugin && $plugin_dir && -d $plugin_dir ){ + #NOTE: Perl doesn't have a built-in recursive copy, so we use system instead + if ( system("cp","-r","$unzipped_plugin/.",$plugin_dir) == 0 ){ + $copied = 1; + } + } + return $copied; +} + +1; diff --git a/debian/scripts/koha-plugin b/debian/scripts/koha-plugin new file mode 100644 index 0000000000..9a49055ea8 --- /dev/null +++ b/debian/scripts/koha-plugin @@ -0,0 +1,123 @@ +#!/usr/bin/perl + +# +# Copyright 2020 Prosentient Systems +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +use Getopt::Long; +use Modern::Perl; +use Pod::Usage; +use URI; +use LWP::UserAgent; + +my $help = 0; +my $install = 0; +my $uninstall = 0; +GetOptions( + 'help' => \$help, + 'install' => \$install, + 'uninstall' => \$uninstall, +); +usage() if $help; +usage() unless ($install || $uninstall); + +my $plugin_arg = shift @ARGV; +my @instances = @ARGV; + +if ($install){ + my $plugin_uri = URI->new($plugin_arg); + if ($plugin_uri){ + if ( _is_file_uri($plugin_uri) ){ + warn "file"; + } + elsif ( _is_http_uri($plugin_uri) ){ + warn "http"; + my $ua = LWP::UserAgent->new(); + my $response = $ua->get($plugin_uri); + if ($response->is_success){ + warn "Success"; + } + else { + warn "Unable to GET $plugin_uri"; + die $response->status_line; + } + + } + } +} +elsif ($uninstall){ + my $plugin_class = $plugin_arg; #Koha::Plugin::Com::Company::Plugin + #Switch to + warn $plugin_class; + +} + +sub _is_http_uri { + my ($uri) = @_; + my $is_http = 0; + #NOTE: This includes both http and https URIs + if ($uri->isa("URI::http")){ + $is_http = 1; + } + return $is_http; +} + +sub _is_file_uri { + my ($uri) = @_; + my $is_file = 0; + if ( (! defined $uri->scheme) || ($uri->scheme eq 'file') ){ + $is_file = 1; + } + return $is_file; +} + +sub usage { + pod2usage( -verbose => 1, -message => "koha-plugin\n\nThis script lets you manage Koha plugins for your Koha instances.\n", -width => 90 ); + exit; +} + +=head1 NAME + +koha-plugin - This script lets you manage Koha plugins for your Koha instances + +=head1 USAGE + +=over + +=item koha-plugin --install file:///koha-plugin.kpz instancename1 [instancename2] + +=item koha-plugin --install https://localhost/koha-plugin.kpz instancename1 [instancename2] + +=item koha-plugin --uninstall plugin_class instancename1 [instancename2] + +=item koha-plugin -h|--help + +=back + +=over + +=item --install + +Install the plugin for the specified instances + +=item --uninstall + +Uninstall the plugin for the specified instances + +=item --help|h + +Display this help message + +=back diff --git a/misc/koha_plugin.pl b/misc/koha_plugin.pl new file mode 100644 index 0000000000..9072c5c149 --- /dev/null +++ b/misc/koha_plugin.pl @@ -0,0 +1,146 @@ +#!/usr/bin/perl + +# +# Copyright 2020 Prosentient Systems +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +use Getopt::Long; +use Modern::Perl; +use Pod::Usage; +use URI; +use Mojo::UserAgent; + +use Koha::Plugins; +use Koha::Plugins::Handler; +use Koha::Plugins::Utils; + +my $plugins_dir = C4::Context->config("pluginsdir"); + +my $help = 0; +my $install = ''; +my $uninstall = ''; +GetOptions( + 'help' => \$help, + 'install=s' => \$install, + 'uninstall=s' => \$uninstall, +); +usage() if $help; +usage() unless ($install || $uninstall); + +my $plugin_arg = shift @ARGV; +my @instances = @ARGV; + +if ($install){ + my $plugin_arg = $install; + my $plugin_uri = URI->new($plugin_arg); + if ($plugin_uri){ + my $plugin; + if ( _is_file_uri($plugin_uri) ){ + + warn "file"; + } + elsif ( _is_http_uri($plugin_uri) ){ + $plugin = Koha::Plugins::Utils->download_plugin({ + url => $plugin_uri->as_string, + }); + } + if ($plugin){ + my $unzipped_plugin; + if ( -f $plugin ){ + $unzipped_plugin = Koha::Plugins::Utils->unzip_plugin({ + zip => $plugin, + }); + } + elsif ( -d $plugin ){ + $unzipped_plugin = $plugin; + } + if ($unzipped_plugin){ + my $copied = Koha::Plugins::Utils->copy_unzipped_plugin({ + unzipped_plugin => $unzipped_plugin, + plugin_dir => $plugins_dir, + }); + if ($copied){ + my @installed_plugins = Koha::Plugins->new()->InstallPlugins(); + warn scalar @installed_plugins; + } + } + } + } +} +elsif ($uninstall){ + my $class = $uninstall; + Koha::Plugins::Handler->delete( { class => $class } ); + #FIXME: The above method should return more meaningful output +} + +sub _is_http_uri { + my ($uri) = @_; + my $is_http = 0; + #NOTE: This includes both http and https URIs + if ($uri->isa("URI::http")){ + $is_http = 1; + } + return $is_http; +} + +sub _is_file_uri { + my ($uri) = @_; + my $is_file = 0; + if ( (! defined $uri->scheme) || ($uri->scheme eq 'file') ){ + $is_file = 1; + } + return $is_file; +} + +sub usage { + pod2usage( -verbose => 1, -message => "koha-plugin\n\nThis script lets you manage Koha plugins for your Koha instances.\n", -width => 90 ); + exit; +} + +=head1 NAME + +koha_plugin.pl - This script lets you manage Koha plugins for your Koha instance + +=head1 USAGE + +=over + +=item koha_plugin.pl --install file:///unzipped_koha-plugin + +=item koha_plugin.pl --install file:///koha-plugin.kpz + +=item koha_plugin.pl --install https://localhost/koha-plugin.kpz + +=item koha_plugin.pl --uninstall plugin_class + +=item koha_plugin.pl -h|--help + +=back + +=over + +=item --install + +Install the plugin for the specified instances + +=item --uninstall + +Uninstall the plugin for the specified instances + +=item --help|h + +Display this help message + +=back -- 2.11.0