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