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

  # 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;
  
  # Parameter
  my $op = param('op');
  
  # Setup script absolute path
  my $setup_script_abs_path = url_for->to_string;
  
  # Application script absolute path
  my $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");
  
  # Outputs
  my @outputs;
  
  # Error
  my $error;
  if ($op == 'setup') {
    
    # 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 Tool</title>
    %= javascript '/js/jquery-1.9.0.min.js';
    %= javascript '/js/bootstrap.js';
    %= stylesheet '/css/bootstrap.css';
    %= stylesheet '/css/bootstrap-responsive.css';
  </head>
  <body>
    <div class="container">
      <div class="text-center"><h1>Setup Tool</h1></div>
    </div>
    <hr style="margin-top:0;margin-bottom:0">
    <div class="container">
      <div class="text-center"><b><h3>Click!</h3></b></div>
      <form action="<%= url_for->query(op => 'setup') %>" method="post">
        <div class="text-center" style="margin-bottom:10px">
          <input type="submit" style="width:200px;height:50px;font-size:200%" value="Setup">
        </div>
      </form>

      % if ($op eq 'setup') {
        <span class="label">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>