Before starting to use the package, you should configure the default
server type. This server type defines which hosting provider should be used, and the payload that should be sent to your hosting provider when creating a new service.
Here's an example that sets up a default server type using the up_cloud
server provider. Want to use another server provider? No problem, here's how you can add support for your favourite provider.
The following configuration has a payload that will set up a new server by cloning a given image.
use Spatie\DynamicServers\Facades\DynamicServers;
use Spatie\DynamicServers\Models\Server;
use Spatie\DynamicServers\Support\ServerTypes\ServerType;
use \Spatie\DynamicServers\Support\DynamicServersManager;
$serverType = ServerType::default()
*
->provider('up_cloud')
->configuration(function (Server $server) {
return [
'server' => [
'zone' => 'de-fra1',
'title' => $server->name,
'hostname' => Str::slug($server->name),
'plan' => '2xCPU-4GB',
'storage_devices' => [
'storage_device' => [
[
'action' => 'clone',
'storage' => $server->option('disk_image'),
'title' => Str::slug($server->name) . '-disk',
'tier' => 'maxiops',
],
],
],
],
];
});
DynamicServers::registerServerType($serverType);
You can test if everything is configured correctly by manually increase the number of dynamic servers. Run this piece of code somewhere (maybe in a Tinker session or something similar).
Spatie\DynamicServers\Facades\DynamicServers::increase();
If everything is set up correctly, you should see a server spinning up at your hosting server. If nothing should happen, make sure your queues are running as starting/stopping servers uses the queue.
When the server has been started, you can use this code to destroy it. Rest assured that the package will only destroy servers that it has created itself. Any pre-existing servers in your hosting provider account will not be touched.
Spatie\DynamicServers\Facades\DynamicServers::decrease();