Skip to content

Commit f61d1a9

Browse files
committed
Introduce UnitInstaller
Resolve #1 This introduces the UnitInstaller and UnitTemplates. These can be used to create new units on your need and install them into your system.
1 parent 697c84e commit f61d1a9

23 files changed

+609
-99
lines changed

README.md

Lines changed: 112 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,85 @@
55
66
PHP wrapper for systemctl (PHP7.1)
77

8-
## Current supported units
8+
# Table of Contents
9+
10+
- [Static Methods](#static-methods)
11+
- [::setBinary(string $binary)](#setbinarystring-binary)
12+
- [::setTimeout(int $timeout)](#settimeoutint-timeout)
13+
- [::setInstallPath(string $installPath)](#setinstallpathstring-installpath)
14+
- [::setAssetPath(string $assetPath)](#setassetpathstring-assetpath)
15+
- ["I need sudo to run commands"](#i-need-sudo-to-run-commands)
16+
- [How do I start/stop/restart a unit?](#how-do-i-startstoprestart-a-unit)
17+
- [Managing units](#managing-units)
18+
- [Supported units](#supported-units)
19+
- [Handling unit commands](#handling-unit-commands)
20+
- [Install new units](#install-new-units)
21+
- [UnitSection](#unitsection)
22+
- [InstallSection](#installsection)
23+
- [TypeSpecificSection](#typespecificsection)
24+
- [How to Contribute](#how-to-contribute)
25+
26+
# Static Methods
27+
### ::setBinary(string $binary)
28+
Change the binary executable of `SystemCtl`
29+
30+
```php
31+
SystemCtl::setBinary('/bin/systemctl');
32+
```
33+
34+
### ::setTimeout(int $timeout)
35+
Change the timeout for each command like `start()` on units and `SystemCtl`
36+
37+
```php
38+
SystemCtl::setTimeout(3);
39+
```
40+
41+
### ::setInstallPath(string $installPath)
42+
Change the install path for new units
43+
44+
```php
45+
SystemCtl::setInstallPath('/etc/systemd/system');
46+
```
47+
48+
### ::setAssetPath(string $assetPath)
49+
Change the asset path to look for unit file templates.
50+
The `default` path is relative to the `SystemCtl` vendor package
51+
52+
```php
53+
SystemCtl::setAssetPath('assets');
54+
```
55+
56+
## "I need sudo to run commands"
57+
If you need sudo, you should execute the bin executable with sudo.
58+
The incode support was dropped due to security reason.
59+
60+
## How do I start/stop/restart a unit?
61+
Simply is that. First we instantiate a `SystemCtl` instance an load a unit from a specific type.
62+
Here we use a `Service`. You will always get back `true` if the command succeeded.
63+
Otherwise the method will throw a `CommandFailedException`.
64+
65+
```php
66+
$systemCtl = new SystemCtl();
67+
68+
// start/stop/enable/disable/reload/restart
69+
$systemCtl->getService('nginx')->start();
70+
$systemCtl->getService('nginx')->stop();
71+
```
72+
73+
# Managing units
74+
To manage any unit u want simply use the proper getter to receive an `Unit` object from `SystemCtl`
75+
76+
## Supported units
977
- service
1078
- timer
1179

12-
> If you like to add support for more units, feel free to contribute.
80+
> If you like to see more units feel free to contribute. Other units will be added in the future.
81+
82+
## Handling unit commands
83+
Each unit comes with a range of methods you can invoke on them (like `start()`).
84+
These methods will be dispatched to the `SystemCtl::$binary` you set before hand (or default).
1385

14-
## Current supported commands
86+
Available unit commands are:
1587
- start
1688
- stop
1789
- enable
@@ -21,35 +93,50 @@ PHP wrapper for systemctl (PHP7.1)
2193
- isEnabled
2294
- isActive
2395

24-
> If you like to add support for more commands, feel free to contribute.
96+
# Install new units
2597

26-
## How to change the binary
98+
> To see a full documentation on how unit files are structure see [Redhat Systemd Documentation](https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/7/html/System_Administrators_Guide/sect-Managing_Services_with_systemd-Unit_Files.html)
99+
100+
To install new units you will first need to create a specific `UnitTemplate` (like `ServiceUnitTemplate`).
101+
After you have created a template with a `$name`. You need to define needed values inside the
102+
`Sections` of a unit.
27103

28104
```php
29-
SystemCtl::setBinary('/bin/systemctl');
105+
$unitTemplate = new ServiceTemplate('myService');
30106
```
31107

32-
## How to change command timeout
33-
To change command tmeout simply call the static method `setTimeout`.
108+
Each unit consists of three sections: The `UnitSection`, `InstallSection` and a `TypeSpecificSection`
109+
110+
## UnitSection
111+
To change the values needed fore the `UnitSection` simple get the section from the template
112+
and set needed values.
113+
114+
**Beware that only set values will be rendered into the template**
115+
116+
> For a full documentation on available methods see [UnitSection](src/Template/Section/UnitSection.php)
34117
```php
35-
SystemCtl::setTimeout(10);
118+
$unitTemplate->getUnitSection()->setDescription('My test service');
36119
```
37120

38-
> The default timeout is set to `3` seconds
121+
## InstallSection
122+
To change the value needed for the `InstallSection` get the section from the template and set
123+
the needed values.
39124

40-
## "I need sudo to run commands"
41-
If you need sudo, you should execute the bin executable with sudo.
42-
The incode support was dropped due to security reason.
43-
44-
## How do I start/stop/restart a unit?
45-
Simply is that. First we instantiate a `SystemCtl` instance an load a unit from a specific type. Here we use a `Service`. You will always get back `true` if the command succeeded. Otherwise the method will throw a `CommandFailedException`.
125+
> For a full documentation on available methods see [InstallSection](src/Template/Section/InstallSection.php)
46126
47127
```php
48-
$systemCtl = new SystemCtl();
128+
$unitTemplate->getInstallSection()->setWantedBy(['multi-user.target']);
129+
```
49130

50-
// start/stop/enable/disable/reload/restart
51-
$systemCtl->getService('nginx')->start();
52-
$systemCtl->getService('nginx')->stop();
131+
## TypeSpecificSection
132+
Each unit will have a type specific section. These sections are named after the `Type` of the Unit (e.g. `Service`).
133+
To change them, you simply to the same thing as for the others. In case of a `Service` you will do
134+
the following:
135+
136+
> For a full documentation on available methods see [Serviceection](src/Template/Section/ServiceSection.php)
137+
138+
```php
139+
$unitTemplate->getServiceSection()->setType(ServiceSection::TYPE_SIMPLE);
53140
```
54141

55142
# How to Contribute
@@ -64,13 +151,14 @@ Make your changes and make sure you run *test* and *codesniffer*.
64151
```bash
65152
$ composer test
66153
> vendor/bin/phpunit tests/
67-
PHPUnit 6.1.4 by Sebastian Bergmann and contributors.
154+
PHPUnit 6.3.0 by Sebastian Bergmann and contributors.
68155

69-
........ 8 / 8 (100%)
156+
................................................................. 65 / 89 ( 73%)
157+
........................ 89 / 89 (100%)
70158

71-
Time: 130 ms, Memory: 2.00MB
159+
Time: 1.65 seconds, Memory: 8.00MB
72160

73-
OK (8 tests, 13 assertions)
161+
OK (89 tests, 169 assertions)
74162

75163
$ composer cs
76164
> vendor/bin/phpcs --standard=PSR2 src/ && vendor/bin/phpcs --standard=PSR2 tests/

phpunit.xml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,16 @@
1818
<testsuite name="integration-tests">
1919
<directory>./tests/Integration</directory>
2020
</testsuite>
21+
<testsuite name="functional-tests">
22+
<directory>./tests/Functional</directory>
23+
</testsuite>
2124
</testsuites>
2225

2326
<filter>
2427
<blacklist>
2528
<directory suffix=".php">./vendor</directory>
2629
</blacklist>
27-
<whitelist processUncoveredFilesFromWhitelist="false">
30+
<whitelist processUncoveredFilesFromWhitelist="true">
2831
<directory suffix=".php">./src</directory>
2932
</whitelist>
3033
</filter>

src/Exception/CommandFailedException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77
*
88
* @package SystemCtl\Exception
99
*/
10-
class CommandFailedException extends \Exception
10+
class CommandFailedException extends \RuntimeException
1111
{
1212
}

src/Exception/ConfigurationNotSupported.php

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/Exception/ExceptionInterface.php

Lines changed: 0 additions & 14 deletions
This file was deleted.

src/Exception/PropertyNotSupportedException.php

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,24 @@
22

33
namespace SystemCtl\Exception;
44

5-
use Throwable;
6-
7-
class PropertyNotSupportedException extends \Exception
5+
/**
6+
* PropertyNotSupportedException
7+
*
8+
* @package SystemCtl\Exception
9+
* @author icanhazstring <[email protected]>
10+
*/
11+
class PropertyNotSupportedException extends \LogicException
812
{
913
/**
10-
* PropertyNotSupportedException constructor.
1114
* @param string $property
1215
* @param string $class
13-
* @param int $code
14-
* @param Throwable|null $previous
16+
*
17+
* @return PropertyNotSupportedException
1518
*/
16-
public function __construct(string $property, string $class, $code = 0, Throwable $previous = null)
19+
public static function create(string $property, string $class): self
1720
{
18-
parent::__construct("Property '{$property}' not supported in {$class}", $code, $previous);
21+
return new self(
22+
sprintf('Property "%s" not supported in %s', $property, $class)
23+
);
1924
}
2025
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace SystemCtl\Exception;
4+
5+
/**
6+
* UnitFileExistsException
7+
*
8+
* @package SystemCtl\Exception
9+
* @author icanhazstring <[email protected]>
10+
*/
11+
class UnitFileExistsException extends \RuntimeException
12+
{
13+
/**
14+
* @param string $unitName
15+
* @param string $unitSuffix
16+
*
17+
* @return UnitFileExistsException
18+
*/
19+
public static function create(string $unitName, string $unitSuffix): self
20+
{
21+
return new self(
22+
sprintf('Unit file %s.%s already exists', $unitName, $unitSuffix)
23+
);
24+
}
25+
}

src/Exception/UnitNotFoundException.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@
22

33
namespace SystemCtl\Exception;
44

5-
use RuntimeException;
6-
75
/**
86
* Class UnitNotFoundException
97
*
108
* @package SystemCtl\Exception
119
*/
12-
class UnitNotFoundException extends RuntimeException implements ExceptionInterface
10+
class UnitNotFoundException extends \RuntimeException
1311
{
1412
/**
1513
* @param string $type

src/Exception/UnitTypeNotSupportedException.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,17 @@
77
*
88
* @package SystemCtl\Exception
99
*/
10-
class UnitTypeNotSupportedException extends \Exception
10+
class UnitTypeNotSupportedException extends \LogicException
1111
{
12+
/**
13+
* @param string $unitSuffix
14+
*
15+
* @return UnitTypeNotSupportedException
16+
*/
17+
public static function create(string $unitSuffix): self
18+
{
19+
return new self(
20+
sprintf('Given unit type "%s" not supported', $unitSuffix)
21+
);
22+
}
1223
}

0 commit comments

Comments
 (0)