1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Laravel\Forge; |
4
|
|
|
|
5
|
|
|
use Laravel\Forge\Exceptions\Servers\PublicKeyWasNotFound; |
6
|
|
|
use Laravel\Forge\Exceptions\Servers\ServerWasNotFoundException; |
7
|
|
|
use Psr\Http\Message\ResponseInterface; |
8
|
|
|
use Laravel\Forge\Contracts\ResourceContract; |
9
|
|
|
|
10
|
|
|
class Server extends ApiResource |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Resource type. |
14
|
|
|
* |
15
|
|
|
* @return string |
16
|
|
|
*/ |
17
|
|
|
public static function resourceType() |
18
|
|
|
{ |
19
|
|
|
return 'server'; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Resource path (relative to owner or API root). |
24
|
|
|
* |
25
|
|
|
* @return string |
26
|
|
|
*/ |
27
|
|
|
public function resourcePath() |
28
|
|
|
{ |
29
|
|
|
return 'servers'; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Throw HTTP Not Found exception. |
34
|
|
|
* |
35
|
|
|
* @throws ServerWasNotFoundException |
36
|
|
|
*/ |
37
|
|
|
protected static function throwNotFoundException() |
38
|
|
|
{ |
39
|
|
|
throw new ServerWasNotFoundException('Given response is not a server response.'); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Credential ID. |
44
|
|
|
* |
45
|
|
|
* @return int |
46
|
|
|
*/ |
47
|
|
|
public function credentialId(): int |
48
|
|
|
{ |
49
|
|
|
return intval($this->getData('credential_id')); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Human readable server size. |
54
|
|
|
* |
55
|
|
|
* @return string|null |
56
|
|
|
*/ |
57
|
|
|
public function size() |
58
|
|
|
{ |
59
|
|
|
return $this->getData('size'); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Server region. |
64
|
|
|
* |
65
|
|
|
* @return string|null |
66
|
|
|
*/ |
67
|
|
|
public function region() |
68
|
|
|
{ |
69
|
|
|
return $this->getData('region'); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Server's PHP version. |
74
|
|
|
* |
75
|
|
|
* @return string|null |
76
|
|
|
*/ |
77
|
|
|
public function phpVersion() |
78
|
|
|
{ |
79
|
|
|
return $this->getData('php_version'); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Server public IP address. |
84
|
|
|
* |
85
|
|
|
* @return string|null |
86
|
|
|
*/ |
87
|
|
|
public function ip() |
88
|
|
|
{ |
89
|
|
|
return $this->getData('ip_address'); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Server private IP address. |
94
|
|
|
* |
95
|
|
|
* @return string|null |
96
|
|
|
*/ |
97
|
|
|
public function privateIp() |
98
|
|
|
{ |
99
|
|
|
return $this->getData('private_ip_address'); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Server sudo password - only set on server save. |
104
|
|
|
* |
105
|
|
|
* @return string|null |
106
|
|
|
*/ |
107
|
|
|
public function sudoPassword() |
108
|
|
|
{ |
109
|
|
|
return $this->getData('sudo_password'); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Server sudo password - only set on server save. |
114
|
|
|
* |
115
|
|
|
* @return string|null |
116
|
|
|
*/ |
117
|
|
|
public function databasePassword() |
118
|
|
|
{ |
119
|
|
|
return $this->getData('database_password'); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Blackfire service status. |
124
|
|
|
* |
125
|
|
|
* @return string|null |
126
|
|
|
*/ |
127
|
|
|
public function blackfireStatus() |
128
|
|
|
{ |
129
|
|
|
return $this->getData('blackfire_status'); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Papertrail service status. |
134
|
|
|
* |
135
|
|
|
* @return string |
136
|
|
|
*/ |
137
|
|
|
public function papertrailStatus() |
138
|
|
|
{ |
139
|
|
|
return $this->getData('papertrail_status'); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Determines if server access was revoked from Forge. |
144
|
|
|
* |
145
|
|
|
* @return bool |
146
|
|
|
*/ |
147
|
|
|
public function isRevoked(): bool |
148
|
|
|
{ |
149
|
|
|
return intval($this->getData('revoked')) === 1; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Determines if server was provisioned and ready to use. |
154
|
|
|
* |
155
|
|
|
* @return bool |
156
|
|
|
*/ |
157
|
|
|
public function isReady(): bool |
158
|
|
|
{ |
159
|
|
|
return intval($this->getData('is_ready')) === 1; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Network status. |
164
|
|
|
* |
165
|
|
|
* @return array|null |
166
|
|
|
*/ |
167
|
|
|
public function network() |
168
|
|
|
{ |
169
|
|
|
return $this->getData('network'); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Server tags. |
174
|
|
|
* |
175
|
|
|
* @return array |
176
|
|
|
*/ |
177
|
|
|
public function tags() |
178
|
|
|
{ |
179
|
|
|
return $this->getData('tags'); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Server ssh port. |
184
|
|
|
* |
185
|
|
|
* @return int |
186
|
|
|
*/ |
187
|
|
|
public function sshPort() |
188
|
|
|
{ |
189
|
|
|
return $this->getData('ssh_port'); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* The server provider. |
194
|
|
|
* |
195
|
|
|
* @return string |
196
|
|
|
*/ |
197
|
|
|
public function provider() |
198
|
|
|
{ |
199
|
|
|
return $this->getData('provider'); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* The server provider id. |
204
|
|
|
* |
205
|
|
|
* @return string |
206
|
|
|
*/ |
207
|
|
|
public function providerId() |
208
|
|
|
{ |
209
|
|
|
return $this->getData('provider_id'); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Reboot the server. |
214
|
|
|
* |
215
|
|
|
* @return bool |
216
|
|
|
*/ |
217
|
|
|
public function reboot(): bool |
218
|
|
|
{ |
219
|
|
|
$this->getHttpClient()->request('POST', $this->apiUrl('reboot')); |
220
|
|
|
|
221
|
|
|
return true; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Enable PHP OPCache on the server. |
226
|
|
|
* |
227
|
|
|
* @return bool |
228
|
|
|
*/ |
229
|
|
|
public function enableOPCache(): bool |
230
|
|
|
{ |
231
|
|
|
$this->getHttpClient()->request('POST', $this->apiUrl('/php/opcache')); |
232
|
|
|
|
233
|
|
|
return true; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* Disable PHP OPCache on the server. |
238
|
|
|
* |
239
|
|
|
* @return bool |
240
|
|
|
*/ |
241
|
|
|
public function disableOPCache(): bool |
242
|
|
|
{ |
243
|
|
|
$this->getHttpClient()->request('DELETE', $this->apiUrl('/php/opcache')); |
244
|
|
|
|
245
|
|
|
return true; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Revoke Forge access to server. |
250
|
|
|
* |
251
|
|
|
* @return bool |
252
|
|
|
**/ |
253
|
|
|
public function revokeAccess(): bool |
254
|
|
|
{ |
255
|
|
|
$this->getHttpClient()->request('POST', $this->apiUrl('/revoke')); |
256
|
|
|
|
257
|
|
|
return true; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Reconnect revoked server. |
262
|
|
|
* |
263
|
|
|
* @return string Public SSH key. |
264
|
|
|
*/ |
265
|
|
|
public function reconnect(): string |
266
|
|
|
{ |
267
|
|
|
$response = $this->getHttpClient()->request('POST', $this->apiUrl('/reconnect')); |
268
|
|
|
$json = json_decode((string) $response->getBody(), true); |
269
|
|
|
|
270
|
|
|
if (empty($json['public_key'])) { |
271
|
|
|
throw new PublicKeyWasNotFound( |
272
|
|
|
'Public key was not found after reconnecting revoked server (ID: '.$this->id().').', |
273
|
|
|
404 |
274
|
|
|
); |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
return $json['public_key']; |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* Reactivate revoked server. Make sure you've installed public SSH key |
282
|
|
|
* before calling this method. |
283
|
|
|
* |
284
|
|
|
* @return bool |
285
|
|
|
*/ |
286
|
|
|
public function reactivate(): bool |
287
|
|
|
{ |
288
|
|
|
$this->getHttpClient()->request('POST', $this->apiUrl('/reactivate')); |
289
|
|
|
|
290
|
|
|
return true; |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
/** |
294
|
|
|
* Create new Resource instance from HTTP response. |
295
|
|
|
* |
296
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response |
297
|
|
|
* @param \Laravel\Forge\ApiProvider $api |
298
|
|
|
* @param \Laravel\Forge\Contracts\ResourceContract $owner = null |
299
|
|
|
* |
300
|
|
|
* @return static |
301
|
|
|
*/ |
302
|
|
|
public static function createFromResponse(ResponseInterface $response, ApiProvider $api, ResourceContract $owner = null) |
303
|
|
|
{ |
304
|
|
|
$json = json_decode((string) $response->getBody(), true); |
305
|
|
|
$result = $json['server'] ?? null; |
306
|
|
|
|
307
|
|
|
if (is_null($result)) { |
308
|
|
|
static::throwNotFoundException(); |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
if (!empty($json['sudo_password'])) { |
312
|
|
|
$result['sudo_password'] = $json['sudo_password']; |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
if (!empty($json['database_password'])) { |
316
|
|
|
$result['database_password'] = $json['database_password']; |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
return new static($api, $result, $owner); |
320
|
|
|
} |
321
|
|
|
} |
322
|
|
|
|