Skip to content

Commit a1bc6fa

Browse files
committed
Push changes that support creations of service packs and basic listing
1 parent 1f47eda commit a1bc6fa

File tree

15 files changed

+654
-49
lines changed

15 files changed

+654
-49
lines changed
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?php
2+
/**
3+
* Pterodactyl - Panel
4+
* Copyright (c) 2015 - 2016 Dane Everitt <dane@daneeveritt.com>
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
namespace Pterodactyl\Http\Controllers\Admin;
25+
26+
use Alert;
27+
use Log;
28+
use Storage;
29+
30+
use Pterodactyl\Models;
31+
use Pterodactyl\Repositories\ServiceRepository\Pack;
32+
use Pterodactyl\Http\Controllers\Controller;
33+
use Pterodactyl\Exceptions\DisplayValidationException;
34+
use Pterodactyl\Exceptions\DisplayException;
35+
36+
use Illuminate\Http\Request;
37+
38+
class PackController extends Controller
39+
{
40+
public function __construct()
41+
{
42+
//
43+
}
44+
45+
public function list(Request $request, $id)
46+
{
47+
$option = Models\ServiceOptions::findOrFail($id);
48+
return view('admin.services.packs.index', [
49+
'packs' => Models\ServicePack::where('option', $option->id)->get(),
50+
'service' => Models\Service::findOrFail($option->parent_service),
51+
'option' => $option
52+
]);
53+
}
54+
55+
public function new(Request $request, $opt = null)
56+
{
57+
$options = Models\ServiceOptions::select(
58+
'services.name AS p_service',
59+
'service_options.id',
60+
'service_options.name'
61+
)->join('services', 'services.id', '=', 'service_options.parent_service')->get();
62+
63+
$array = [];
64+
foreach($options as &$option) {
65+
if (!array_key_exists($option->p_service, $array)) {
66+
$array[$option->p_service] = [];
67+
}
68+
69+
$array[$option->p_service] = array_merge($array[$option->p_service], [[
70+
'id' => $option->id,
71+
'name' => $option->name
72+
]]);
73+
}
74+
75+
return view('admin.services.packs.new', [
76+
'services' => $array,
77+
'packFor' => $opt,
78+
]);
79+
}
80+
81+
public function create(Request $request)
82+
{
83+
// dd($request->all());
84+
try {
85+
$repo = new Pack;
86+
$id = $repo->create($request->except([
87+
'_token'
88+
]));
89+
Alert::success('Successfully created new service!')->flash();
90+
return redirect()->route('admin.services.packs.edit', $id)->withInput();
91+
} catch (DisplayValidationException $ex) {
92+
return redirect()->route('admin.services.packs.new', $request->input('option'))->withErrors(json_decode($ex->getMessage()))->withInput();
93+
} catch (DisplayException $ex) {
94+
Alert::danger($ex->getMessage())->flash();
95+
} catch (\Exception $ex) {
96+
Log::error($ex);
97+
Alert::danger('An error occured while attempting to add a new service pack.')->flash();
98+
}
99+
return redirect()->route('admin.services.packs.new', $request->input('option'))->withInput();
100+
101+
}
102+
103+
public function edit(Request $request, $id)
104+
{
105+
$pack = Models\ServicePack::findOrFail($id);
106+
dd($pack, Storage::url('packs/' . $pack->uuid));
107+
}
108+
}

app/Http/Routes/AdminRoutes.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,32 @@ public function map(Router $router) {
430430
]);
431431
});
432432

433+
// Service Packs
434+
$router->group([
435+
'prefix' => 'admin/services/packs',
436+
'middleware' => [
437+
'auth',
438+
'admin',
439+
'csrf'
440+
]
441+
], function () use ($router) {
442+
$router->get('/new/{option?}', [
443+
'as' => 'admin.services.packs.new',
444+
'uses' => 'Admin\PackController@new'
445+
]);
446+
$router->post('/new', [
447+
'uses' => 'Admin\PackController@create'
448+
]);
449+
$router->get('/for/{option}', [
450+
'as' => 'admin.services.packs.for',
451+
'uses' => 'Admin\PackController@list'
452+
]);
453+
$router->get('/edit/{pack}', [
454+
'as' => 'admin.services.packs.edit',
455+
'uses' => 'Admin\PackController@edit'
456+
]);
457+
});
458+
433459
}
434460

435461
}

app/Models/ServicePack.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
/**
3+
* Pterodactyl - Panel
4+
* Copyright (c) 2015 - 2016 Dane Everitt <dane@daneeveritt.com>
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
namespace Pterodactyl\Models;
25+
26+
use Illuminate\Database\Eloquent\Model;
27+
28+
class ServicePack extends Model
29+
{
30+
31+
/**
32+
* The table associated with the model.
33+
*
34+
* @var string
35+
*/
36+
protected $table = 'service_packs';
37+
38+
/**
39+
* Fields that are not mass assignable.
40+
*
41+
* @var array
42+
*/
43+
protected $guarded = ['id', 'created_at', 'updated_at'];
44+
45+
/**
46+
* Cast values to correct type.
47+
*
48+
* @var array
49+
*/
50+
protected $casts = [
51+
'option' => 'integer',
52+
'build_memory' => 'integer',
53+
'build_swap' => 'integer',
54+
'build_cpu' => 'integer',
55+
'build_io' => 'integer',
56+
'selectable' => 'boolean',
57+
'visible' => 'boolean'
58+
];
59+
60+
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<?php
2+
/**
3+
* Pterodactyl - Panel
4+
* Copyright (c) 2015 - 2016 Dane Everitt <dane@daneeveritt.com>
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
namespace Pterodactyl\Repositories\ServiceRepository;
25+
26+
use DB;
27+
use Storage;
28+
use Uuid;
29+
use Validator;
30+
31+
use Pterodactyl\Models;
32+
use Pterodactyl\Services\UuidService;
33+
use Pterodactyl\Exceptions\DisplayException;
34+
use Pterodactyl\Exceptions\DisplayValidationException;
35+
36+
class Pack
37+
{
38+
39+
public function __construct()
40+
{
41+
//
42+
}
43+
44+
public function create(array $data)
45+
{
46+
$validator = Validator::make($data, [
47+
'name' => 'required|string',
48+
'version' => 'required|string',
49+
'description' => 'string',
50+
'option' => 'required|exists:service_options,id',
51+
'selectable' => 'sometimes|boolean',
52+
'visible' => 'sometimes|boolean',
53+
'build_memory' => 'required|integer|min:0',
54+
'build_swap' => 'required|integer|min:0',
55+
'build_cpu' => 'required|integer|min:0',
56+
'build_io' => 'required|integer|min:10|max:1000',
57+
'build_container' => 'required|string',
58+
'build_script' => 'sometimes|string'
59+
]);
60+
61+
if ($validator->fails()) {
62+
throw new DisplayValidationException($validator->errors());
63+
}
64+
65+
if (isset($data['file_upload'])) {
66+
if (!$data['file_upload']->isValid()) {
67+
throw new DisplayException('The file provided does not appear to be valid.');
68+
}
69+
70+
if (!in_array($data['file_upload']->getMimeType(), [
71+
'application/zip',
72+
'application/gzip'
73+
])) {
74+
throw new DisplayException('The file provided does not meet the required filetypes of application/zip or application/gzip.');
75+
}
76+
}
77+
78+
DB::transaction(function () use ($data) {
79+
$uuid = new UuidService;
80+
$pack = Models\ServicePack::create([
81+
'option' => $data['option'],
82+
'uuid' => $uuid->generate('servers', 'uuid'),
83+
'build_memory' => $data['build_memory'],
84+
'build_swap' => $data['build_swap'],
85+
'build_cpu' => $data['build_swap'],
86+
'build_io' => $data['build_io'],
87+
'build_script' => (empty($data['build_script'])) ? null : $data['build_script'],
88+
'build_container' => $data['build_container'],
89+
'name' => $data['name'],
90+
'version' => $data['version'],
91+
'description' => (empty($data['description'])) ? null : $data['description'],
92+
'selectable' => isset($data['selectable']),
93+
'visible' => isset($data['visible'])
94+
]);
95+
96+
$filename = ($data['file_upload']->getMimeType() === 'application/zip') ? 'archive.zip' : 'archive.tar.gz';
97+
$data['file_upload']->storeAs('packs/' . $pack->uuid, $filename);
98+
99+
$pack->save();
100+
101+
return $pack->id;
102+
});
103+
}
104+
105+
}

app/Repositories/ServiceRepository/Service.php

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ public function delete($id)
101101

102102
DB::beginTransaction();
103103
try {
104+
Storage::deleteDirectory('services/' . $service->file);
105+
104106
Models\ServiceVariables::whereIn('option_id', $options->get()->toArray())->delete();
105107
$options->delete();
106108
$service->delete();
@@ -128,23 +130,10 @@ public function updateFile($id, array $data)
128130
$filepath = 'services/' . $service->file . '/' . $filename;
129131
$backup = 'services/.bak/' . str_random(12) . '.bak';
130132

131-
DB::beginTransaction();
132-
133133
try {
134134
Storage::move($filepath, $backup);
135135
Storage::put($filepath, $data['contents']);
136-
137-
$checksum = Models\Checksum::firstOrNew([
138-
'service' => $service->id,
139-
'filename' => $filename
140-
]);
141-
142-
$checksum->checksum = sha1_file(storage_path('app/' . $filepath));
143-
$checksum->save();
144-
145-
DB::commit();
146136
} catch(\Exception $ex) {
147-
DB::rollback();
148137
Storage::move($backup, $filepath);
149138
throw $ex;
150139
}

database/migrations/2016_11_09_163911_add_checksums_table.php

Lines changed: 0 additions & 36 deletions
This file was deleted.

0 commit comments

Comments
 (0)