Support for uploading templates for installing packs

This commit is contained in:
Dane Everitt 2016-11-16 17:22:22 -05:00
parent e09659a88f
commit d4729427aa
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
6 changed files with 187 additions and 13 deletions

View file

@ -97,7 +97,6 @@ class PackController extends Controller
public function new(Request $request, $opt = null)
{
return view('admin.services.packs.new', [
'services' => $this->formatServices(),
'packFor' => $opt,
@ -106,7 +105,6 @@ class PackController extends Controller
public function create(Request $request)
{
// dd($request->all());
try {
$repo = new Pack;
$id = $repo->create($request->except([
@ -123,7 +121,6 @@ class PackController extends Controller
Alert::danger('An error occured while attempting to add a new service pack.')->flash();
}
return redirect()->route('admin.services.packs.new', $request->input('option'))->withInput();
}
public function edit(Request $request, $id)
@ -179,7 +176,7 @@ class PackController extends Controller
if ((bool) $files) {
$zip = new \ZipArchive;
if (!$zip->open($filename, \ZipArchive::CREATE)) {
exit("cannot open <$filename>\n");
abort(503, 'Unable to open file for writing.');
}
$files = Storage::files('packs/' . $pack->uuid);
@ -200,4 +197,31 @@ class PackController extends Controller
])->deleteFileAfterSend(true);
}
}
public function uploadForm(Request $request, $for = null) {
return view('admin.services.packs.upload', [
'services' => $this->formatServices(),
'for' => $for
]);
}
public function postUpload(Request $request)
{
try {
$repo = new Pack;
$id = $repo->createWithTemplate($request->except([
'_token'
]));
Alert::success('Successfully created new service!')->flash();
return redirect()->route('admin.services.packs.edit', $id)->withInput();
} catch (DisplayValidationException $ex) {
return redirect()->back()->withErrors(json_decode($ex->getMessage()))->withInput();
} catch (DisplayException $ex) {
Alert::danger($ex->getMessage())->flash();
} catch (\Exception $ex) {
Log::error($ex);
Alert::danger('An error occured while attempting to add a new service pack.')->flash();
}
return redirect()->back();
}
}

View file

@ -446,6 +446,13 @@ class AdminRoutes {
$router->post('/new', [
'uses' => 'Admin\PackController@create'
]);
$router->get('/upload/{option?}', [
'as' => 'admin.services.packs.uploadForm',
'uses' => 'Admin\PackController@uploadForm'
]);
$router->post('/upload', [
'uses' => 'Admin\PackController@postUpload'
]);
$router->get('/', [
'as' => 'admin.services.packs',
'uses' => 'Admin\PackController@listAll'

View file

@ -46,7 +46,7 @@ class Pack
$validator = Validator::make($data, [
'name' => 'required|string',
'version' => 'required|string',
'description' => 'string',
'description' => 'sometimes|nullable|string',
'option' => 'required|exists:service_options,id',
'selectable' => 'sometimes|boolean',
'visible' => 'sometimes|boolean',
@ -55,7 +55,7 @@ class Pack
'build_cpu' => 'required|integer|min:0',
'build_io' => 'required|integer|min:10|max:1000',
'build_container' => 'required|string',
'build_script' => 'sometimes|string'
'build_script' => 'sometimes|nullable|string'
]);
if ($validator->fails()) {
@ -75,7 +75,8 @@ class Pack
}
}
DB::transaction(function () use ($data) {
DB::beginTransaction();
try {
$uuid = new UuidService;
$pack = Models\ServicePack::create([
'option' => $data['option'],
@ -93,13 +94,94 @@ class Pack
'visible' => isset($data['visible'])
]);
Storage::makeDirectory('packs/' . $pack->uuid);
if (isset($data['file_upload'])) {
$filename = ($data['file_upload']->getMimeType() === 'application/zip') ? 'archive.zip' : 'archive.tar.gz';
$data['file_upload']->storeAs('packs/' . $pack->uuid, $filename);
}
$pack->save();
DB::commit();
} catch (\Exception $ex) {
DB::rollBack();
throw $ex;
}
return $pack->id;
});
}
public function createWithTemplate(array $data)
{
if (!isset($data['file_upload'])) {
throw new DisplayException('No template file was found submitted with this request.');
}
if (!$data['file_upload']->isValid()) {
throw new DisplayException('The file provided does not appear to be valid.');
}
if (!in_array($data['file_upload']->getMimeType(), [
'application/zip',
'text/plain',
'application/json'
])) {
throw new DisplayException('The file provided (' . $data['file_upload']->getMimeType() . ') does not meet the required filetypes of application/zip or application/json.');
}
if ($data['file_upload']->getMimeType() === 'application/zip') {
$zip = new \ZipArchive;
if (!$zip->open($data['file_upload']->path())) {
throw new DisplayException('The uploaded archive was unable to be opened.');
}
$isZip = $zip->locateName('archive.zip');
$isTar = $zip->locateName('archive.tar.gz');
if ($zip->locateName('import.json') === false || ($isZip === false && $isTar === false)) {
throw new DisplayException('This contents of the provided archive were in an invalid format.');
}
$json = json_decode($zip->getFromName('import.json'));
$id = $this->create([
'name' => $json->name,
'version' => $json->version,
'description' => $json->description,
'option' => $data['option'],
'selectable' => $json->selectable,
'visible' => $json->visible,
'build_memory' => $json->build->memory,
'build_swap' => $json->build->swap,
'build_cpu' => $json->build->cpu,
'build_io' => $json->build->io,
'build_container' => $json->build->container,
'build_script' => $json->build->script
]);
$pack = Models\ServicePack::findOrFail($id);
if (!$zip->extractTo(storage_path('app/packs/' . $pack->uuid), ($isZip === false) ? 'archive.tar.gz' : 'archive.zip')) {
$pack->delete();
throw new DisplayException('Unable to extract the archive file to the correct location.');
}
$zip->close();
return $pack->id;
} else {
$json = json_decode(file_get_contents($data['file_upload']->path()));
return $this->create([
'name' => $json->name,
'version' => $json->version,
'description' => $json->description,
'option' => $data['option'],
'selectable' => $json->selectable,
'visible' => $json->visible,
'build_memory' => $json->build->memory,
'build_swap' => $json->build->swap,
'build_cpu' => $json->build->cpu,
'build_io' => $json->build->io,
'build_container' => $json->build->container,
'build_script' => $json->build->script
]);
}
}
public function update($id, array $data)

View file

@ -58,7 +58,7 @@
<a href="{{ route('admin.services.packs.new', $option->id) }}">
<button class="pull-right btn btn-xxs btn-primary"><i class="fa fa-plus"></i></button>
</a>
<a href="{{ route('admin.services.packs.new', $option->id) }}">
<a href="#upload" id="toggleUpload">
<button class="pull-right btn btn-xxs btn-default" style="margin-right:5px;"><i class="fa fa-upload"></i> Install from Template</button>
</a>
</td>
@ -69,6 +69,22 @@
<script>
$(document).ready(function () {
$('#sidebar_links').find("a[href='/admin/services/packs']").addClass('active');
$('#toggleUpload').on('click', function (event) {
event.preventDefault();
var element = $(this);
element.find('button').addClass('disabled');
$.ajax({
method: 'GET',
url: '{{ route('admin.services.packs.uploadForm', $option->id) }}'
}).fail(function (jqXhr) {
console.error(jqXhr);
alert('There was an error trying to create the upload form.');
}).success(function (data) {
$(data).modal();
}).always(function () {
element.find('button').removeClass('disabled');
});
});
});
</script>
@endsection

View file

@ -152,7 +152,7 @@
<label class="control-label">Package Archive:</label>
<input name="file_upload" type="file" accept=".zip,.tar.gz, application/zip, application/gzip" />
<p class="text-muted"><small>This package file must either be a <code>.zip</code> or <code>.tar.gz</code> archive of files to use for either building or running this pack.<br /><br />If your file is larger than <code>20MB</code> we recommend uploading it using SFTP. Once you have added this pack to the system, a path will be provided where you should upload the file.
This is currently configured with the following limits: <code>upload_max_filesize={{ ini_get('upload_max_filesize') }}</code> and <code>post_max_size={{ ini_get('post_max_size') }}</code>. If your file is larger than either of those values this request will fail.</small></p>
This server is currently configured with the following limits: <code>upload_max_filesize={{ ini_get('upload_max_filesize') }}</code> and <code>post_max_size={{ ini_get('post_max_size') }}</code>. If your file is larger than either of those values this request will fail.</small></p>
</div>
</div>
</div>

View file

@ -0,0 +1,45 @@
<div class="modal fade" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<form action="{{ route('admin.services.packs.uploadForm') }}" method="POST" enctype="multipart/form-data">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
<h4 class="modal-title">Install Pack from Template</h4>
</div>
<div class="modal-body">
<div class="well" style="margin-bottom:0">
<div class="row">
<div class="col-md-12">
<label class="control-label">Associated Service Option:</label>
<select name="option" class="form-control">
@foreach($services as $service => $options)
<option disabled>{{ $service }}</option>
@foreach($options as $option)
<option value="{{ $option['id'] }}" @if((int) $for === (int) $option['id'])selected="selected"@endif>&nbsp;&nbsp; -- {{ $option['name'] }}</option>
@endforeach
@endforeach
</select>
</div>
</div>
<div class="row" style="margin-top:15px;">
<div class="col-md-12">
<div class="row">
<div class="form-group col-md-12">
<label class="control-label">Package Archive:</label>
<input name="file_upload" type="file" accept=".zip,.json, application/json, application/zip" />
<p class="text-muted"><small>This file should be either the <code>.json</code> template file, or a <code>.zip</code> pack archive containing <code>archive.(zip|tar.gz)</code> and <code>import.json</code> within.<br /><br />This server is currently configured with the following limits: <code>upload_max_filesize={{ ini_get('upload_max_filesize') }}</code> and <code>post_max_size={{ ini_get('post_max_size') }}</code>. If your file is larger than either of those values this request will fail.</small></p>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="modal-footer">
{!! csrf_field() !!}
<button type="button" class="btn btn-default btn-sm" data-dismiss="modal">Cancel</button>
<input type="submit" class="btn btn-primary btn-sm" value="Install" />
</div>
</form>
</div>
</div>
</div>