This repository has been archived by the owner on Jan 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathService.php
200 lines (168 loc) · 3.79 KB
/
Service.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<?php
declare(strict_types=1);
/*
* This file is part of the systemctl PHP library.
*
* (c) Martin Janser <martin@duss-janser.ch>
*
* This source file is subject to the GPL license that is bundled
* with this source code in the file LICENSE.
*/
namespace SystemCtl;
use Symfony\Component\Process\Process;
class Service
{
const STATUS_STOPPED = 3;
/**
* @var string
*/
private static $command = 'systemctl';
/**
* @var bool
*/
private static $sudo = true;
/**
* @var int
*/
private static $timeout = 3;
/**
* @var string
*/
private $name;
/**
* Sets the systemctl command to use.
*
* @param string $command
*/
public static function setCommand($command)
{
self::$command = $command;
}
/**
* Specifies whether or not to use sudo to run the systemctl command.
*
* @param bool $flag
*/
public static function sudo($flag = true)
{
self::$sudo = (bool) $flag;
}
/**
* Specifies the timeout in seconds for the systemctl process.
*
* @param int $timeout
*/
public static function setTimeout($timeout)
{
self::$timeout = (int) $timeout;
}
/**
* @param string $name Name of the service to manage
*/
public function __construct($name)
{
$this->name = $name;
}
/**
* Checks whether or not the service is running.
*
* @throws CommandFailedException If the command failed
*
* @return bool
*/
public function isRunning()
{
$process = $this->getProcess([
'--lines=0',
'status',
$this->name,
]);
$process->run();
if ($process->isSuccessful()) {
return true;
}
if (self::STATUS_STOPPED === $process->getExitCode()) {
return false;
}
throw new CommandFailedException($process);
}
/**
* Starts the service.
*
* @throws CommandFailedException If the command failed
*/
public function start()
{
if ($this->isRunning()) {
return;
}
$process = $this->getProcess([
'start',
$this->name,
]);
$process->run();
if (!$process->isSuccessful()) {
throw new CommandFailedException($process);
}
}
/**
* Stops the service.
*
* @throws CommandFailedException If the command failed
*/
public function stop()
{
if (!$this->isRunning()) {
return;
}
$process = $this->getProcess([
'stop',
$this->name,
]);
$process->run();
if (!$process->isSuccessful()) {
throw new CommandFailedException($process);
}
}
/**
* Restarts the service.
*
* @throws CommandFailedException If the command failed
*/
public function restart()
{
$process = $this->getProcess([
'restart',
$this->name,
]);
$process->run();
if (!$process->isSuccessful()) {
throw new CommandFailedException($process);
}
}
/**
* @return string
*/
public function __toString()
{
return $this->name;
}
/**
* Creates and prepares a process.
*
* @param string[] $arguments
*
* @return Process
*/
private function getProcess(array $arguments)
{
$command = explode(' ', self::$command);
if (self::$sudo) {
array_unshift($command, 'sudo');
}
$command = array_merge($command, $arguments);
$process = new Process($command);
$process->setTimeout(self::$timeout);
return $process;
}
}