@@ 8-88 (lines=81) @@ | ||
5 | use GuzzleHttp\Client; |
|
6 | use Tylercd100\Rebooter\ApiRebooter; |
|
7 | ||
8 | class DigitalOceanRebooter extends ApiRebooter { |
|
9 | ||
10 | protected $token; |
|
11 | protected $server_id; |
|
12 | protected $host; |
|
13 | protected $client; |
|
14 | ||
15 | /** |
|
16 | * @param string $token API Token from DigitalOcean.com |
|
17 | * @param number $server_id The ID of the droplet you want to control |
|
18 | * @param string $host The api host |
|
19 | * @param Client $client The guzzle client to use |
|
20 | */ |
|
21 | public function __construct($token, $server_id, $host = "api.digitalocean.com", Client $client = null) { |
|
22 | ||
23 | if (!$client instanceof Client) { |
|
24 | $client = new Client(); |
|
25 | } |
|
26 | ||
27 | $this->client = $client; |
|
28 | $this->token = $token; |
|
29 | $this->server_id = $server_id; |
|
30 | $this->host = $host; |
|
31 | } |
|
32 | ||
33 | /** |
|
34 | * Executes a Boot command |
|
35 | * @return \Psr\Http\Message\ResponseInterface |
|
36 | */ |
|
37 | public function boot() { |
|
38 | return $this->exec("power_on"); |
|
39 | } |
|
40 | ||
41 | /** |
|
42 | * Executes a Reboot command |
|
43 | * @return \Psr\Http\Message\ResponseInterface |
|
44 | */ |
|
45 | public function reboot() { |
|
46 | return $this->exec("reboot"); |
|
47 | } |
|
48 | ||
49 | /** |
|
50 | * Executes a Shutdown command |
|
51 | * @return \Psr\Http\Message\ResponseInterface |
|
52 | */ |
|
53 | public function shutdown() { |
|
54 | return $this->exec("power_off"); |
|
55 | } |
|
56 | ||
57 | /** |
|
58 | * Builds the request URL for the API call |
|
59 | * @param string $action The DigitalOcean API action |
|
60 | * @return string |
|
61 | */ |
|
62 | protected function buildRequestUrl($action) { |
|
63 | return "https://{$this->host}/v2/droplets/{$this->server_id}/actions"; |
|
64 | } |
|
65 | ||
66 | /** |
|
67 | * Builds the request data for the API call |
|
68 | * @param string $action The DigitalOcean API action |
|
69 | * @return array |
|
70 | */ |
|
71 | protected function buildRequestData($action) { |
|
72 | return ["type"=>$action]; |
|
73 | } |
|
74 | ||
75 | /** |
|
76 | * Executes a command on the API |
|
77 | * @param string $action |
|
78 | * @return \Psr\Http\Message\ResponseInterface |
|
79 | */ |
|
80 | protected function exec($action) { |
|
81 | $url = $this->buildRequestUrl($action); |
|
82 | $data = $this->buildRequestData($action); |
|
83 | return $this->client->request('POST', $url, [ |
|
84 | 'auth' => [$this->token, ""], |
|
85 | 'form_params' => $data, |
|
86 | ]); |
|
87 | } |
|
88 | } |
@@ 8-91 (lines=84) @@ | ||
5 | use GuzzleHttp\Client; |
|
6 | use Tylercd100\Rebooter\ApiRebooter; |
|
7 | ||
8 | class VultrRebooter extends ApiRebooter { |
|
9 | ||
10 | protected $token; |
|
11 | protected $server_id; |
|
12 | protected $host; |
|
13 | protected $client; |
|
14 | ||
15 | /** |
|
16 | * @param string $token API Token from vultr.com |
|
17 | * @param number $server_id The ID of the server you want to control (Vultr calls it the `SUBID`) |
|
18 | * @param string $host The api host |
|
19 | * @param Client $client The guzzle client to use |
|
20 | */ |
|
21 | public function __construct($token, $server_id, $host = "api.vultr.com", Client $client = null){ |
|
22 | ||
23 | if(!$client instanceof Client){ |
|
24 | $client = new Client(); |
|
25 | } |
|
26 | ||
27 | $this->client = $client; |
|
28 | $this->token = $token; |
|
29 | $this->server_id = $server_id; |
|
30 | $this->host = $host; |
|
31 | } |
|
32 | ||
33 | /** |
|
34 | * Executes a Boot command |
|
35 | * @return \Psr\Http\Message\ResponseInterface |
|
36 | */ |
|
37 | public function boot(){ |
|
38 | return $this->exec("start"); |
|
39 | } |
|
40 | ||
41 | /** |
|
42 | * Executes a Reboot command |
|
43 | * @return \Psr\Http\Message\ResponseInterface |
|
44 | */ |
|
45 | public function reboot(){ |
|
46 | return $this->exec("reboot"); |
|
47 | } |
|
48 | ||
49 | /** |
|
50 | * Executes a Shutdown command |
|
51 | * @return \Psr\Http\Message\ResponseInterface |
|
52 | */ |
|
53 | public function shutdown(){ |
|
54 | return $this->exec("halt"); |
|
55 | } |
|
56 | ||
57 | /** |
|
58 | * Builds the request URL for the API call |
|
59 | * @param string $action The Vultr API action |
|
60 | * @return string |
|
61 | */ |
|
62 | protected function buildRequestUrl($action){ |
|
63 | return "https://{$this->host}/v1/server/{$action}"; |
|
64 | } |
|
65 | ||
66 | /** |
|
67 | * Builds the request data for the API call |
|
68 | * @param string $action The Vultr API action |
|
69 | * @return array |
|
70 | */ |
|
71 | protected function buildRequestData($action){ |
|
72 | return ["SUBID"=>$this->server_id]; |
|
73 | } |
|
74 | ||
75 | /** |
|
76 | * Executes a command on the API |
|
77 | * @param string $action |
|
78 | * @return \Psr\Http\Message\ResponseInterface |
|
79 | */ |
|
80 | protected function exec($action){ |
|
81 | $url = $this->buildRequestUrl($action); |
|
82 | $data = $this->buildRequestData($action); |
|
83 | ||
84 | return $this->client->request('POST', $url, [ |
|
85 | 'headers' => [ |
|
86 | 'API-Key' => $this->token, |
|
87 | ], |
|
88 | 'form_params' => $data, |
|
89 | ]); |
|
90 | } |
|
91 | } |