1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Laravel\Forge; |
4
|
|
|
|
5
|
|
|
use Laravel\Forge\Exceptions\Servers\PublicKeyWasNotFound; |
6
|
|
|
use Laravel\Forge\Exceptions\Servers\ServerWasNotFoundException; |
7
|
|
|
|
8
|
|
|
class Server extends ApiResource |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Resource type. |
12
|
|
|
* |
13
|
|
|
* @return string |
14
|
|
|
*/ |
15
|
|
|
public static function resourceType() |
16
|
|
|
{ |
17
|
|
|
return 'server'; |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Resource path (relative to owner or API root). |
22
|
|
|
* |
23
|
|
|
* @return string |
24
|
|
|
*/ |
25
|
|
|
public function resourcePath() |
26
|
|
|
{ |
27
|
|
|
return 'servers'; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Throw HTTP Not Found exception. |
32
|
|
|
* |
33
|
|
|
* @throws \Exception |
34
|
|
|
*/ |
35
|
|
|
protected static function throwNotFoundException() |
36
|
|
|
{ |
37
|
|
|
throw new ServerWasNotFoundException('Given response is not a server response.'); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Credential ID. |
42
|
|
|
* |
43
|
|
|
* @return int |
44
|
|
|
*/ |
45
|
|
|
public function credentialId(): int |
46
|
|
|
{ |
47
|
|
|
return intval($this->getData('credential_id')); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Human readable server size. |
52
|
|
|
* |
53
|
|
|
* @return string|null |
54
|
|
|
*/ |
55
|
|
|
public function size() |
56
|
|
|
{ |
57
|
|
|
return $this->getData('size'); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Server region. |
62
|
|
|
* |
63
|
|
|
* @return string|null |
64
|
|
|
*/ |
65
|
|
|
public function region() |
66
|
|
|
{ |
67
|
|
|
return $this->getData('region'); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Server's PHP version. |
72
|
|
|
* |
73
|
|
|
* @return string|null |
74
|
|
|
*/ |
75
|
|
|
public function phpVersion() |
76
|
|
|
{ |
77
|
|
|
return $this->getData('php_version'); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Server public IP address. |
82
|
|
|
* |
83
|
|
|
* @return string|null |
84
|
|
|
*/ |
85
|
|
|
public function ip() |
86
|
|
|
{ |
87
|
|
|
return $this->getData('ip_address'); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Server private IP address. |
92
|
|
|
*/ |
93
|
|
|
public function privateIp() |
94
|
|
|
{ |
95
|
|
|
return $this->getData('private_ip_address'); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Blackfire service status. |
100
|
|
|
* |
101
|
|
|
* @return string|null |
102
|
|
|
*/ |
103
|
|
|
public function blackfireStatus() |
104
|
|
|
{ |
105
|
|
|
return $this->getData('blackfire_status'); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Papertrail service status. |
110
|
|
|
* |
111
|
|
|
* @return string |
112
|
|
|
*/ |
113
|
|
|
public function papertrailStatus() |
114
|
|
|
{ |
115
|
|
|
return $this->getData('papertrail_status'); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Determines if server access was revoked from Forge. |
120
|
|
|
* |
121
|
|
|
* @return bool |
122
|
|
|
*/ |
123
|
|
|
public function isRevoked(): bool |
124
|
|
|
{ |
125
|
|
|
return intval($this->getData('revoked')) === 1; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Determines if server was provisioned and ready to use. |
130
|
|
|
* |
131
|
|
|
* @return bool |
132
|
|
|
*/ |
133
|
|
|
public function isReady(): bool |
134
|
|
|
{ |
135
|
|
|
return intval($this->getData('is_ready')) === 1; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Network status. |
140
|
|
|
* |
141
|
|
|
* @return array|null |
142
|
|
|
*/ |
143
|
|
|
public function network() |
144
|
|
|
{ |
145
|
|
|
return $this->getData('network'); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Reboot the server. |
150
|
|
|
* |
151
|
|
|
* @return bool |
152
|
|
|
*/ |
153
|
|
|
public function reboot(): bool |
154
|
|
|
{ |
155
|
|
|
$this->getHttpClient()->request('POST', $this->apiUrl('reboot')); |
156
|
|
|
|
157
|
|
|
return true; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Revoke Forge access to server. |
162
|
|
|
* |
163
|
|
|
* @return bool |
164
|
|
|
**/ |
165
|
|
|
public function revokeAccess(): bool |
166
|
|
|
{ |
167
|
|
|
$this->getHttpClient()->request('POST', $this->apiUrl('/revoke')); |
168
|
|
|
|
169
|
|
|
return true; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Reconnect revoked server. |
174
|
|
|
* |
175
|
|
|
* @return string Public SSH key. |
176
|
|
|
*/ |
177
|
|
|
public function reconnect(): string |
178
|
|
|
{ |
179
|
|
|
$response = $this->getHttpClient()->request('POST', $this->apiUrl('/reconnect')); |
180
|
|
|
$json = json_decode((string) $response->getBody(), true); |
181
|
|
|
|
182
|
|
|
if (empty($json['public_key'])) { |
183
|
|
|
throw new PublicKeyWasNotFound( |
184
|
|
|
'Public key was not found after reconnecting revoked server (ID: '.$this->id().').', |
185
|
|
|
404 |
186
|
|
|
); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
return $json['public_key']; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Reactivate revoked server. Make sure you've installed public SSH key |
194
|
|
|
* before calling this method. |
195
|
|
|
* |
196
|
|
|
* @return bool |
197
|
|
|
*/ |
198
|
|
|
public function reactivate(): bool |
199
|
|
|
{ |
200
|
|
|
$this->getHttpClient()->request('POST', $this->apiUrl('/reactivate')); |
201
|
|
|
|
202
|
|
|
return true; |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
|