1313use Deployer \Component \Ssh \Client ;
1414use Deployer \Component \Ssh \IOArguments ;
1515use Deployer \Deployer ;
16+ use Deployer \Exception \Exception ;
1617use Deployer \Host \Host ;
18+ use Deployer \Host \HostCollection ;
1719use Deployer \Host \Localhost ;
1820use Deployer \Selector \Selector ;
21+ use Deployer \Task \Context ;
1922use Deployer \Task \Task ;
2023use Symfony \Component \Console \Input \InputInterface ;
2124use Symfony \Component \Console \Output \OutputInterface ;
@@ -32,40 +35,21 @@ function spinner(string $message = ''): string
3235
3336class Master
3437{
35- /**
36- * @var InputInterface
37- */
38- private $ input ;
39-
40- /**
41- * @var OutputInterface
42- */
43- private $ output ;
44-
45- /**
46- * @var Server
47- */
48- private $ server ;
49-
50- /**
51- * @var Messenger
52- */
53- private $ messenger ;
54-
55- /**
56- * @var false|string
57- */
58- private $ phpBin ;
38+ private HostCollection $ hosts ;
39+ private InputInterface $ input ;
40+ private OutputInterface $ output ;
41+ private Messenger $ messenger ;
42+ private string |false $ phpBin ;
5943
6044 public function __construct (
45+ HostCollection $ hosts ,
6146 InputInterface $ input ,
6247 OutputInterface $ output ,
63- Server $ server ,
6448 Messenger $ messenger ,
6549 ) {
50+ $ this ->hosts = $ hosts ;
6651 $ this ->input = $ input ;
6752 $ this ->output = $ output ;
68- $ this ->server = $ server ;
6953 $ this ->messenger = $ messenger ;
7054 $ this ->phpBin = (new PhpExecutableFinder ())->find ();
7155 }
@@ -182,43 +166,76 @@ private function runTask(Task $task, array $hosts): int
182166 return 0 ;
183167 }
184168
185- $ callback = function (string $ output ) {
186- $ output = preg_replace ('/\n$/ ' , '' , $ output );
187- if (strlen ($ output ) !== 0 ) {
188- $ this ->output ->writeln ($ output );
189- }
190- };
169+ $ server = new Server ('127.0.0.1 ' , 0 , $ this ->output );
191170
192171 /** @var Process[] $processes */
193172 $ processes = [];
194173
195- $ this -> server ->loop -> futureTick (function () use (&$ processes , $ hosts , $ task ) {
174+ $ server ->afterRun (function (int $ port ) use (&$ processes , $ hosts , $ task ) {
196175 foreach ($ hosts as $ host ) {
197- $ processes [] = $ this ->createProcess ($ host , $ task );
176+ $ processes [] = $ this ->createProcess ($ host , $ task, $ port );
198177 }
199178
200179 foreach ($ processes as $ process ) {
201180 $ process ->start ();
202181 }
203182 });
204183
205- $ this ->server ->loop ->addPeriodicTimer (0.03 , function ($ timer ) use (&$ processes , $ callback ) {
206- $ this ->gatherOutput ($ processes , $ callback );
184+ $ echoCallback = function (string $ output ) {
185+ $ output = preg_replace ('/\n$/ ' , '' , $ output );
186+ if (strlen ($ output ) !== 0 ) {
187+ $ this ->output ->writeln ($ output );
188+ }
189+ };
190+
191+ $ server ->ticker (function () use (&$ processes , $ server , $ echoCallback ) {
192+ $ this ->gatherOutput ($ processes , $ echoCallback );
207193 if ($ this ->output ->isDecorated () && !getenv ('CI ' )) {
208194 $ this ->output ->write (spinner ());
209195 }
210196 if ($ this ->allFinished ($ processes )) {
211- $ this ->server ->loop ->stop ();
212- $ this ->server ->loop ->cancelTimer ($ timer );
197+ $ server ->stop ();
198+ }
199+ });
200+
201+ $ server ->router (function (string $ path , array $ payload ) {
202+ switch ($ path ) {
203+ case '/load ' :
204+ ['host ' => $ host ] = $ payload ;
205+
206+ $ host = $ this ->hosts ->get ($ host );
207+ $ config = $ host ->config ()->persist ();
208+
209+ return new Response (200 , $ config );
210+
211+ case '/save ' :
212+ ['host ' => $ host , 'config ' => $ config ] = $ payload ;
213+
214+ $ host = $ this ->hosts ->get ($ host );
215+ $ host ->config ()->update ($ config );
216+
217+ return new Response (200 , true );
218+
219+ case '/proxy ' :
220+ ['host ' => $ host , 'func ' => $ func , 'arguments ' => $ arguments ] = $ payload ;
221+
222+ Context::push (new Context ($ this ->hosts ->get ($ host )));
223+ $ answer = call_user_func ($ func , ...$ arguments );
224+ Context::pop ();
225+
226+ return new Response (200 , $ answer );
227+
228+ default :
229+ return new Response (404 , null );
213230 }
214231 });
215232
216- $ this -> server -> loop ->run ();
233+ $ server ->run ();
217234
218235 if ($ this ->output ->isDecorated () && !getenv ('CI ' )) {
219236 $ this ->output ->write (" \r" ); // clear spinner
220237 }
221- $ this ->gatherOutput ($ processes , $ callback );
238+ $ this ->gatherOutput ($ processes , $ echoCallback );
222239
223240 if ($ this ->cumulativeExitCode ($ processes ) !== 0 ) {
224241 $ this ->messenger ->endTask ($ task , true );
@@ -227,11 +244,11 @@ private function runTask(Task $task, array $hosts): int
227244 return $ this ->cumulativeExitCode ($ processes );
228245 }
229246
230- protected function createProcess (Host $ host , Task $ task ): Process
247+ protected function createProcess (Host $ host , Task $ task, int $ port ): Process
231248 {
232249 $ command = [
233250 $ this ->phpBin , DEPLOYER_BIN ,
234- 'worker ' , '--port ' , $ this -> server -> getPort () ,
251+ 'worker ' , '--port ' , $ port ,
235252 '--task ' , $ task ,
236253 '--host ' , $ host ->getAlias (),
237254 ];
0 commit comments