Skip to content

Commit b0a4976

Browse files
Added service commands
Signed-off-by: Bastian Schwarz <bastian@codename-php.de>
1 parent 36b9b04 commit b0a4976

5 files changed

Lines changed: 224 additions & 0 deletions

File tree

src/service/Service.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* Copyright 2022 Bastian Schwarz <bastian@codename-php.de>.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
namespace de\codenamephp\deployer\command\service;
19+
20+
use de\codenamephp\deployer\command\Command;
21+
use de\codenamephp\deployer\command\runner\iRunner;
22+
use de\codenamephp\deployer\command\runner\WithDeployerFunctions;
23+
24+
/**
25+
* Uses service to handle the service. The command is executed using an iRunner instance
26+
*/
27+
final class Service implements iService {
28+
29+
public function __construct(public iRunner $runner = new WithDeployerFunctions()) {}
30+
31+
public function run(string $serviceName, string $action, bool $sudo = true) : string {
32+
return $this->runner->run(new Command(binary: 'service', arguments: [$serviceName, $action], sudo: $sudo));
33+
}
34+
}

src/service/Systemctl.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* Copyright 2022 Bastian Schwarz <bastian@codename-php.de>.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
namespace de\codenamephp\deployer\command\service;
19+
20+
use de\codenamephp\deployer\command\Command;
21+
use de\codenamephp\deployer\command\runner\iRunner;
22+
use de\codenamephp\deployer\command\runner\WithDeployerFunctions;
23+
24+
/**
25+
* Uses systemctl to handle the service. The command is executed using an iRunner instance
26+
*/
27+
final class Systemctl implements iService {
28+
29+
public function __construct(public iRunner $runner = new WithDeployerFunctions()) {}
30+
31+
public function run(string $serviceName, string $action, bool $sudo = true) : string {
32+
return $this->runner->run(new Command(binary: 'systemctl', arguments: [$action, $serviceName], sudo: $sudo));
33+
}
34+
}

src/service/iService.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* Copyright 2022 Bastian Schwarz <bastian@codename-php.de>.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
namespace de\codenamephp\deployer\command\service;
19+
20+
use Deployer\Exception\Exception;
21+
use Deployer\Exception\RunException;
22+
use Deployer\Exception\TimeoutException;
23+
24+
/**
25+
* Interface to control services, e.g. through service, init.d or systemctl
26+
*/
27+
interface iService {
28+
29+
/**
30+
* Performs the given action for the given service. Implementations should use an iRunner implementation to execute the command
31+
*
32+
* @param string $serviceName The name of the service to handle, e.g. apache2, docker, ...
33+
* @param string $action The action to perform, e.g. restart, reload, ...
34+
* @param bool $sudo To use sudo or not, defaults to true
35+
* @return string The output of the command
36+
*
37+
* @throws Exception|RunException|TimeoutException
38+
*/
39+
public function run(string $serviceName, string $action, bool $sudo = true) : string;
40+
}

test/service/ServiceTest.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* Copyright 2022 Bastian Schwarz <bastian@codename-php.de>.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
namespace de\codenamephp\deployer\command\test\service;
19+
20+
use de\codenamephp\deployer\command\Command;
21+
use de\codenamephp\deployer\command\runner\iRunner;
22+
use de\codenamephp\deployer\command\runner\WithDeployerFunctions;
23+
use de\codenamephp\deployer\command\service\Service;
24+
use PHPUnit\Framework\TestCase;
25+
26+
final class ServiceTest extends TestCase {
27+
28+
private Service $sut;
29+
30+
protected function setUp() : void {
31+
parent::setUp();
32+
33+
$this->sut = new Service();
34+
}
35+
36+
public function test__construct() : void {
37+
$this->sut = new Service();
38+
39+
self::assertInstanceOf(WithDeployerFunctions::class, $this->sut->runner);
40+
}
41+
42+
public function test__construct_withArguments() : void {
43+
$runner = $this->createMock(iRunner::class);
44+
45+
$this->sut = new Service($runner);
46+
47+
self::assertSame($runner, $this->sut->runner);
48+
}
49+
50+
public function testRun() : void {
51+
$command = new Command(binary: 'service', arguments: ['apache2', 'restart'], sudo: true);
52+
53+
$this->sut->runner = $this->createMock(iRunner::class);
54+
$this->sut->runner->expects(self::once())->method('run')->with($command)->willReturn('some output');
55+
56+
self::assertEquals('some output', $this->sut->run('apache2', 'restart'));
57+
}
58+
}

test/service/SystemctlTest.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* Copyright 2022 Bastian Schwarz <bastian@codename-php.de>.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
namespace de\codenamephp\deployer\command\test\service;
19+
20+
use de\codenamephp\deployer\command\Command;
21+
use de\codenamephp\deployer\command\runner\iRunner;
22+
use de\codenamephp\deployer\command\runner\WithDeployerFunctions;
23+
use de\codenamephp\deployer\command\service\Systemctl;
24+
use PHPUnit\Framework\TestCase;
25+
26+
final class SystemctlTest extends TestCase {
27+
28+
private Systemctl $sut;
29+
30+
protected function setUp() : void {
31+
parent::setUp();
32+
33+
$this->sut = new Systemctl();
34+
}
35+
36+
public function test__construct() : void {
37+
$this->sut = new Systemctl();
38+
39+
self::assertInstanceOf(WithDeployerFunctions::class, $this->sut->runner);
40+
}
41+
42+
public function test__construct_withArguments() : void {
43+
$runner = $this->createMock(iRunner::class);
44+
45+
$this->sut = new Systemctl($runner);
46+
47+
self::assertSame($runner, $this->sut->runner);
48+
}
49+
50+
public function testRun() : void {
51+
$command = new Command(binary: 'systemctl', arguments: ['restart', 'apache2'], sudo: true);
52+
53+
$this->sut->runner = $this->createMock(iRunner::class);
54+
$this->sut->runner->expects(self::once())->method('run')->with($command)->willReturn('some output');
55+
56+
self::assertEquals('some output', $this->sut->run('apache2', 'restart'));
57+
}
58+
}

0 commit comments

Comments
 (0)