1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CSFCloud\Containers; |
4
|
|
|
|
5
|
|
|
use CSFCloud\KeyManager; |
6
|
|
|
use CSFCloud\Resource; |
7
|
|
|
use Httpful\Request; |
8
|
|
|
|
9
|
|
|
class Container extends Resource { |
10
|
|
|
|
11
|
|
|
private $containerId; |
12
|
|
|
private $statusCache; |
13
|
|
|
|
14
|
2 |
|
public function __construct (KeyManager $km, string $id) { |
15
|
2 |
|
parent::__construct($km); |
16
|
2 |
|
$this->containerId = $id; |
17
|
2 |
|
$this->FetchStatus(); |
18
|
2 |
|
} |
19
|
|
|
|
20
|
2 |
|
private function BuildUrl (string $api, array $query = []) : string { |
21
|
2 |
|
$query["key"] = $this->keymanager->GetServerKey(); |
22
|
2 |
|
$url = "https://api.csfcloud.com/container/" . urlencode($this->containerId) . $api . "?" . http_build_query($query); |
23
|
|
|
//echo "URL: " . $url . PHP_EOL; |
24
|
2 |
|
return $url; |
25
|
|
|
} |
26
|
|
|
|
27
|
2 |
|
public function FetchStatus () { |
28
|
2 |
|
$request = Request::get($this->BuildUrl(""))->expectsText()->send(); |
29
|
2 |
|
$this->statusCache = json_decode($request->body, true); |
30
|
2 |
|
} |
31
|
|
|
|
32
|
2 |
|
public function GetId () : string { |
33
|
2 |
|
return $this->containerId; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function GetConfiguration () : array { |
37
|
|
|
return $this->statusCache["configuration"]; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function SetConfiguration (array $cnf) { |
41
|
|
|
$this->statusCache["configuration"] = $cnf; |
42
|
|
|
} |
43
|
|
|
|
44
|
2 |
|
public function UpdateChanges () { |
45
|
2 |
|
Request::put($this->BuildUrl("")) |
46
|
2 |
|
->sendsJson()->body(json_encode($this->statusCache["configuration"]))->send(); |
47
|
2 |
|
} |
48
|
|
|
|
49
|
2 |
|
public function GetContainerName () : string { |
50
|
2 |
|
return $this->statusCache["configuration"]["name"]; |
51
|
|
|
} |
52
|
|
|
|
53
|
2 |
|
public function SetContainerName (string $newname) { |
54
|
2 |
|
$this->statusCache["configuration"]["name"] = $newname; |
55
|
2 |
|
} |
56
|
|
|
|
57
|
|
|
public function IsRunning () : bool { |
58
|
|
|
return $this->statusCache["running"]; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function GetLastLogId () : ?string { |
62
|
|
|
return $this->statusCache["last_log"]; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function GetLastLogWebSocketUrl () : string { |
66
|
|
|
return "wss://dashboard-logs.csfcloud.com/log?id=" . $this->statusCache["last_log"]; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function Build () : string { |
70
|
|
|
$response = Request::get($this->BuildUrl("/build"))->expectsJson()->send(); |
71
|
|
|
$this->statusCache["last_log"] = $response->body->run_id; |
72
|
|
|
return $this->statusCache["last_log"]; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function Run () : string { |
76
|
|
|
$response = Request::get($this->BuildUrl("/run"))->expectsJson()->send(); |
77
|
|
|
$this->statusCache["last_log"] = $response->body->run_id; |
78
|
|
|
return $this->statusCache["last_log"]; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function Stop () { |
82
|
|
|
$response = Request::get($this->BuildUrl("/stop"))->expectsJson()->send(); |
|
|
|
|
83
|
|
|
$this->statusCache["last_log"] = null; |
84
|
|
|
} |
85
|
|
|
|
86
|
2 |
|
public function Delete () { |
87
|
2 |
|
Request::delete($this->BuildUrl(""))->send(); |
88
|
2 |
|
} |
89
|
|
|
|
90
|
2 |
|
private function GetFileUrl(string $name) : string { |
91
|
2 |
|
return $this->BuildUrl("/files", [ |
92
|
2 |
|
"filename" => $name |
93
|
|
|
]); |
94
|
|
|
} |
95
|
|
|
|
96
|
2 |
|
public function UploadFile (string $file, string $name) { |
97
|
2 |
|
Request::post($this->GetFileUrl($name))->body(file_get_contents($file))->send(); |
98
|
2 |
|
} |
99
|
|
|
|
100
|
|
|
public function DownloadFile (string $name, string $file) { |
101
|
|
|
shell_exec("wget " . escapeshellarg($this->GetFileUrl($name)) . " -O " . escapeshellarg($file)); |
102
|
|
|
} |
103
|
|
|
|
104
|
2 |
|
public function GetFileContents (string $name) : string { |
105
|
2 |
|
$response = Request::get($this->GetFileUrl($name))->send(); |
106
|
2 |
|
return $response->body; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function DeleteFile (string $name) { |
110
|
|
|
$url = $this->BuildUrl("/files", [ |
|
|
|
|
111
|
|
|
"filename" => $name |
112
|
|
|
]); |
113
|
|
|
|
114
|
|
|
Request::delete($this->BuildUrl(""))->send(); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
} |