Out of the box, this package supports UpCloud, but it's easy to support your favourite hosting service as well.
##Community maintained drivers
##Do you want to create a driver?
If you have created a custom server provider, do not PR it to this package. We don't want to maintain it.
Instead, put it in a repository of your own, and mail us a link to it. We'll put the link here in our docs, so others can make use of it.
##Creating a server provider class
First, you need a server provider class, which is any class that extends the abstract Spatie\DynamicServers\ServerProviders\ServerProvider
class.
By extending that class, you'll need to implement a couple of methods,
such as createServer
, hasStarted
, ... In these methods you'll probably need to use the API of your hosting service to do the work that's necessary.
Here's an example:
namespace App\Support;
use Spatie\DynamicServers\ServerProviders\ServerProvider;
class YourServerProvider extends ServerProvider
{
public function createServer(): void
{
}
public function hasStarted(): bool
{
}
public function stopServer(): void
{
}
public function hasStopped(): bool
{
}
public function deleteServer(): void
{
}
public function hasBeenDeleted(): bool
{
}
public function rebootServer(): void
{
}
public function currentServerCount(): int
{
}
}
The package stores information locally in the dynamic_servers
on all server that should be created and have been created.
Your server has access to a property, $server
, that you can use in your API calls. The configuration
property will return the array that was returned by the configuration
function when you configure a server type.
Let's create a dummy implementation of createServer
and hasStarted
.
use \Illuminate\Support\Facades\Http;
public function createServer(): void
{
$serverConfiguration = $this->server->configuration;
$response = Http::::withBasicAuth(
$this->server->option('username'),
$this->server->option('password')
)->post('https://your-provider.com/api/create-server', $serverConfiguration);
$this->server->addMeta('your_provider_server_properties', $response->toArray());
}
public function hasStarted(): bool
{
$serverId = $this->server->meta('your_provider_server_properties.server_id');
$serverDetails = Http::(
$this->server->option('username'),
$this->server->option('password')
)->post("https://your-provider.com/api/servers/{$serverId}")->json();
return $serverDetails['status'] === 'running';
}
All other methods should be implemented in a similar fashion.
##Register your server provider class
Next, you'll need to register your provider in the config file. Make sure to define any options that you are using in your server provider with $server->config(...)
return [
'providers' => [
'your_provider' => [
'class' => App\Support\YourServerProvider::class,
'options' => [
'username' => env('YOUR_PROVIDER_USER_NAME'),
'password' => env('YOUR_PROVIDER_USER_NAME'),
],
],
],
##Registering a server type
With this out of the way you can register a server type that makes use of your server provider.
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('your_provider')
->configuration(function (Server $server) {
return [
];
});
DynamicServers::registerServerType($serverType);