1. Perl
  2. Mojolicious

Script to install CPAN module with Sakura rental server light plan

I wrote a web application (CGI script) to install the CPAN module with Sakura's rental server light plan (125 yen per month). With Sakura's rental server light plan, SSH and Telnet cannot be used, so installing the CPAN module is a high barrier, but with this tool it is easy to install the module from a web browser. can do. Twice

[f: id: perlcodeexample: 20110323012954p: image]

Start developing Mojolicious on Windows and upload it to Sakura's rental server by FTP using the same procedure as please. The permissions on app.cgi should be 755.

The directory structure is as follows.

sakuracpanm - app.cgi

To install a module, simply enter the module name in the text box and press the install button. The first CPAN module installation is time consuming (5-10 minutes) as it requires the installation of cpanm's dependent modules. Please wait patiently. Anyone can use it, so remove this tool as soon as the installation is complete, or set permissions to 000, etc.

# !/usr/bin/perl

use FindBin;
use lib "$FindBin::Bin/lib";

use Mojolicious::Lite;
use File::Copy 'move';
use LWP::UserAgent;
use utf8;

# top page
get'/' => sub {
    my $self = shift;
    
    my $module = $self->req->param('module');
    
    $self->stash(error =>'');
    $self->stash(output =>'');
    
    if ($module && $module =~ /^[:\w]+$/) {
        my $home = $ENV{DOCUMENT_ROOT};
        $home =~ s #/www $# #;
        
        my @output;
        my @cpanm;
        eval {
            chdir $home
              or die qq {Can't change directory "$home":$!};
            
            my $ua = LWP::UserAgent->new;
            my $res = $ua->get('http://xrl.us/cpanm/');
            my $cpanm_content;
            if ($res->is_success) {
                $cpanm_content = $res->content;
            }
            else {
                die qq {Can't donwlaod "cpanm"};
            }
            
            push @cpanm, '#!/usr/bin/perl';
            push @cpanm, "BEGIN {".
                             "\$ENV{HOME} =" $home'; ".
                             "use lib '$home/perl5/lib/perl5';".
                         "}";
            push @cpanm, $cpanm_content;
            
            open my $write_fh, '>', 'cpanm'
              or die qq {Can't open "cpanm" for write:$!};
            
            print $write_fh join("\n", @cpanm);
            
            close $write_fh;
            
            my $output = `perl cpanm - local-lib = $home/perl5 $module 2> & 1`;
            push @output, split /\n /, $output;
        };
        
        return $self->render(error => $@) if $@;
        
        $self->render(output => \@output);
    }
    elsif ($module) {
        $self->stash(error =>'Enter module name correctly');
        $self->render;
    }
    else {
        $self->render;
    }
} =>'index';

app->start;

__DATA__

@@index.html.ep
<html>
  <head>
    <meta http-equiv = "Content-type" content = "text/html; charset = UTF-8">    <title> Sakura Rental Server Light CPAN Module Installation </title>
  </head>
  <body>
    <h1>Sakura rental server light CPAN module installation</h1>
    <pre>
    # If you want to use the installed module, write as follows in the script (app.cgi).
    use FindBin;
    use lib "$FindBin::Bin/../../perl5/lib/perl5";
    
    # This script is available to everyone and should be removed as soon as the installation is complete.
    </pre>
    <form method = "get" action = "<%= url_for'/' %>">      <div> Module <input type = "text" name = "module"><input type = "submit" value = "install"></div>
      <div style = "color: red"><%= $error %> </div>
      <div>
        %if ($output) {
          %foreach my $line (@$output) {
            <p style = "margin: 0; padding: 0"><%= $line %> </p>
          %}
        %}
      </div>
    </form>
  </body>
</html>

(Reference) File::Copy, FindBin

Related Informatrion