gitprep / setup / templates / index.html.ep /
9cfec17 11 years ago
1 contributor
153 lines | 4.019kb
<%
  use Cwd qw/getcwd realpath/;
  use File::Copy 'copy';

  # Parameter
  my $op = param('op') || '';

  # Outputs
  my @outputs;
  
  # Error
  my $error;
  
  # App absolute path
  my $app_abs_path;
  
  if ($op eq 'setup') {

    # Setup directory
    my $setup_dir = getcwd;

    # Home directory
    my $home_dir = realpath($setup_dir . '/..');
    
    # Script directory
    my $script_dir = realpath($setup_dir . '/../script');
    
    # cpanm Path
    my $cpanm_path = "$home_dir/cpanm";
    
    # cpanm home directory
    $ENV{PERL_CPANM_HOME} = $setup_dir;
    
    # Setup script absolute path
    my $setup_script_abs_path = url_for->to_string;
    
    # Application script absolute path
    $app_abs_path = $setup_script_abs_path;
    $app_abs_path =~ s#\Q/setup/setup.cgi#.cgi#;
    
    # Application script name
    my ($app_name) = $app_abs_path =~ /([0-9a-zA-Z-_]+\.cgi)$/;
    
    # Application script file
    my $app_file = "$script_dir/$app_name";
    
    # Place application script is moved to 
    my $app_to = realpath("$home_dir/../$app_name");
    
    # Change directory
    if (chdir $home_dir) {
    
      # Install Module::CoreList
      my $cmd = 'perl cpanm -n -l extlib Module::CoreList 2>&1';
      if (open my $fh, "$cmd |") {
        local $/;
        my $output = <$fh>;
        push @outputs, $output;
        if (close $fh) {
        
          # Install modules
          my $cmd = 'perl -Iextlib/lib/perl5 '
            . 'cpanm -n -L extlib --installdeps . 2>&1';
          if (open my $fh, "$cmd |") {
            local $/;
            my $output = <$fh>;
            push @outputs, $output;
            if (close $fh) {
              
              # Copy application file
              if (copy $app_file, $app_to) {
                push @outputs, "$app_file is moved to $app_to";
                
                # Change mode
                if (chmod(0755, $app_to)) {
                  push @outputs, "change $app_to mode to 755";
                }
                else {
                  $error = 1;
                  app->log->error("Can't change mode $app_to");
                }
              }
              else {
                $error = 1;
                app->log->error("Can't move $app_file to $app_to");
              }
            }
            else {
              $error = 1;
              app->log->error("Can't close pipe install modules: $!");
            }
          }
          else {
            my $error = 1;
            app->log->error("Can't open pipe install modules: $!");
          }
        }
        else {
          $error = 1;
          app->log->error("Can't close pipe install Module::CoreList: $!");
        }
      }
      else {
        $error = 1;
        app->log->error("Can't open pipe install Module::CoreList: $!");
      }
    }
    else {
      $error = 1;
      app->log->error("Can't cahgne directory: $!");
    }
  }
%>

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>Setup</title>
  </head>
  <body>
    <div>
      <div style="text-align:center"><h1>Setup</h1></div>
    </div>
    <hr style="margin-top:0;margin-bottom:0">
    <div>
      <div style="text-align:center"><b><h3>Click only once. Wait for a few minutes.</h3></b></div>
      <form action="<%= url_for->query(op => 'setup') %>" method="post">
        <div style="text-align:center;margin-bottom:10px">
          <input type="submit" style="width:200px;height:50px;font-size:200%" value="Setup">
        </div>
      </form>

      % if ($op eq 'setup') {
        <span">Result</span>
<pre style="height:300px;overflow:auto;margin-bottom:30px">
% if ($error) {
<span style="color:red">Error, Setup failed. See setup/log/production.log</span>
% }
% unless ($error) {
% for my $line (@outputs) {
%= $line
% }
% }
</pre>
      % }

      % if ($op eq 'setup' && !$error) {
        <div style="font-size:150%;margin-bottom:30px;">Go to <a href="<%= $app_abs_path %>">Application</a></div>
      % }
    </div>
  </body>
</html>