| 1 | # | ||
| 2 | # (c) Jan Gehring <jan.gehring@gmail.com> | ||
| 3 | # | ||
| 4 | # vim: set ts=2 sw=2 tw=0: | ||
| 5 | # vim: set expandtab: | ||
| 6 | |||
| 7 | package Rex::JobControl::Rexfile; | ||
| 8 | $Rex::JobControl::Rexfile::VERSION = '0.18.0'; | ||
| 9 | use Mojo::Base 'Mojolicious::Controller'; | ||
| 10 | use Cwd; | ||
| 11 | use File::Spec; | ||
| 12 | |||
| 13 | sub prepare_stash { | ||
| 14 | my $self = shift; | ||
| 15 | |||
| 16 | my $project = $self->project( $self->param("project_dir") ); | ||
| 17 | $self->stash( project => $project ); | ||
| 18 | |||
| 19 | my $rexfile = $project->get_rexfile( $self->param("rexfile_dir") ); | ||
| 20 | $self->stash( rexfile => $rexfile ); | ||
| 21 | } | ||
| 22 | |||
| 23 | sub index { | ||
| 24 | my $self = shift; | ||
| 25 | $self->render; | ||
| 26 | } | ||
| 27 | |||
| 28 | sub rexfile_new { | ||
| 29 | my $self = shift; | ||
| 30 | $self->render; | ||
| 31 | } | ||
| 32 | |||
| 33 | sub rexfile_new_create { | ||
| 34 | my $self = shift; | ||
| 35 | |||
| 36 | $self->app->log->debug( "Got project name: " . $self->param("project_dir") ); | ||
| 37 | $self->app->log->debug( "Got rexfile name: " . $self->param("rexfile_name") ); | ||
| 38 | |||
| 39 | my $pr = $self->project( $self->param("project_dir") ); | ||
| 40 | |||
| 41 | my $rexfile_archive = $self->param("rexfile_archive"); | ||
| 42 | |||
| 43 | if ( $rexfile_archive && $rexfile_archive->filename ) { | ||
| 44 | $self->app->log->debug( | ||
| 45 | "This is a fileupload: " . $rexfile_archive->filename ); | ||
| 46 | |||
| 47 | if ( $self->req->is_limit_exceeded ) { | ||
| 48 | |||
| 49 | # $ENV{MOJO_MAX_MESSAGE_SIZE} = 1073741824; | ||
| 50 | $self->flash( | ||
| 51 | { | ||
| 52 | title => "File too large.", | ||
| 53 | message => | ||
| 54 | "You have reached the upload file limit. You can set this limit higher in the configuration file.", | ||
| 55 | } | ||
| 56 | ); | ||
| 57 | return $self->redirect_to( "/project/" . $self->param("project_dir") ); | ||
| 58 | } | ||
| 59 | |||
| 60 | if ( $rexfile_archive->filename =~ m/\.tar\.gz$/ ) { | ||
| 61 | if ( !-d getcwd() . "/upload" ) { | ||
| 62 | mkdir getcwd() . "/upload"; | ||
| 63 | } | ||
| 64 | |||
| 65 | $rexfile_archive->move_to( | ||
| 66 | File::Spec->catdir( | ||
| 67 | $self->config->{upload_tmp_path}, | ||
| 68 | $rexfile_archive->filename | ||
| 69 | ) | ||
| 70 | ); | ||
| 71 | |||
| 72 | $self->minion->enqueue( | ||
| 73 | checkout_rexfile => [ | ||
| 74 | $pr->directory, | ||
| 75 | $self->param("rexfile_name"), | ||
| 76 | File::Spec->catdir( | ||
| 77 | $self->config->{upload_tmp_path}, | ||
| 78 | $rexfile_archive->filename | ||
| 79 | ), | ||
| 80 | $self->param("rexfile_description") | ||
| 81 | ] | ||
| 82 | ); | ||
| 83 | |||
| 84 | $self->flash( | ||
| 85 | { | ||
| 86 | title => "Rexfile will be extracted in background.", | ||
| 87 | message => | ||
| 88 | "Rexfile will be extracted in background. Once it it finished it will appear in the list." | ||
| 89 | } | ||
| 90 | ); | ||
| 91 | } | ||
| 92 | else { | ||
| 93 | $self->flash( | ||
| 94 | { | ||
| 95 | title => "Wrong filetype", | ||
| 96 | message => "Only .tar.gz files are allowed.", | ||
| 97 | } | ||
| 98 | ); | ||
| 99 | return $self->redirect_to( "/project/" . $self->param("project_dir") ); | ||
| 100 | } | ||
| 101 | } | ||
| 102 | else { | ||
| 103 | |||
| 104 | $self->minion->enqueue( | ||
| 105 | checkout_rexfile => [ | ||
| 106 | $pr->directory, $self->param("rexfile_name"), | ||
| 107 | $self->param("rexfile_url"), $self->param("rexfile_description") | ||
| 108 | ] | ||
| 109 | ); | ||
| 110 | |||
| 111 | $self->flash( | ||
| 112 | { | ||
| 113 | title => "Rexfile will be downloaded in background.", | ||
| 114 | message => | ||
| 115 | "Rexfile will be downloaded in background. Once it it finished it will appear in the list." | ||
| 116 | } | ||
| 117 | ); | ||
| 118 | |||
| 119 | } | ||
| 120 | |||
| 121 | $self->redirect_to( "/project/" . $self->param("project_dir") ); | ||
| 122 | } | ||
| 123 | |||
| 124 | sub view { | ||
| 125 | my $self = shift; | ||
| 126 | $self->render; | ||
| 127 | } | ||
| 128 | |||
| 129 | sub reload { | ||
| 130 | my $self = shift; | ||
| 131 | |||
| 132 | $self->app->log->debug( "Got project name: " . $self->param("project_dir") ); | ||
| 133 | $self->app->log->debug( "Got rexfile name: " . $self->param("rexfile_name") ); | ||
| 134 | |||
| 135 | my $pr = $self->project( $self->param("project_dir") ); | ||
| 136 | my $rexfile = $pr->get_rexfile( $self->param("rexfile_dir") ); | ||
| 137 | |||
| 138 | $rexfile->reload; | ||
| 139 | |||
| 140 | $self->redirect_to( "/project/" . $pr->directory ); | ||
| 141 | } | ||
| 142 | |||
| 143 | sub remove { | ||
| 144 | my $self = shift; | ||
| 145 | |||
| 146 | $self->app->log->debug( "Got project name: " . $self->param("project_dir") ); | ||
| 147 | $self->app->log->debug( "Got rexfile name: " . $self->param("rexfile_name") ); | ||
| 148 | |||
| 149 | my $pr = $self->project( $self->param("project_dir") ); | ||
| 150 | my $rexfile = $pr->get_rexfile( $self->param("rexfile_dir") ); | ||
| 151 | |||
| 152 | $rexfile->remove; | ||
| 153 | |||
| 154 | $self->flash( | ||
| 155 | { | ||
| 156 | title => "Rexfile removed", | ||
| 157 | message => "Rexfile <b>" . $rexfile->name . "</b> removed." | ||
| 158 | } | ||
| 159 | ); | ||
| 160 | |||
| 161 | $self->redirect_to( "/project/" . $pr->directory ); | ||
| 162 | } | ||
| 163 | |||
| 164 | 1; |