-
Notifications
You must be signed in to change notification settings - Fork 2
/
lib.php
163 lines (137 loc) · 4.1 KB
/
lib.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
<?php
class DeploySlack {
protected $slack;
protected $channelName;
public function __construct(Slack $slack, $channelName) {
$this->slack = $slack;
$this->channelName = $channelName;
}
private function execute($cmd, $stdoutCallback = null, $stderrCallback = null, $bufferLength = 128) {
$descriptorspec = array(
1 => array('pipe', 'w'), // stdout
2 => array('pipe', 'w'), // stderr
);
$process = proc_open($cmd, $descriptorspec, $pipes);
if (!is_resource($process))
{
throw new RuntimeException('Unable to execute the command.');
}
stream_set_blocking($pipes[1], false);
stream_set_blocking($pipes[2], false);
$output = '';
$err = '';
while (!feof($pipes[1]) || !feof($pipes[2]))
{
foreach ($pipes as $key => $pipe)
{
if (!$line = fread($pipe, $bufferLength))
{
continue;
}
if (1 == $key)
{
// stdout
$output .= $line;
if ($stdoutCallback)
{
call_user_func($stdoutCallback, $line);
}
}
else
{
// stderr
$err .= $line;
if ($stderrCallback)
{
call_user_func($stderrCallback, $line);
}
}
}
usleep(100000);
}
fclose($pipes[1]);
fclose($pipes[2]);
if (($return = proc_close($process)) > 0)
{
throw new RuntimeException('Problem executing command.', $return);
}
return array($output, $err);
}
public function executeAndSendToSlack(Slack $slack, $command) {
$message = "Log du déploiement :";
$resultArray = $this->slack->postMessage($this->channelName, $message);
$logContent = '';
$callbackStdout = function($line) use(&$logContent, $resultArray, $message) {
$logContent .= $line;
$lines = explode("\n", $logContent);
$nbDisplayedLines = 5;
$displayedLines = array_fill(0, $nbDisplayedLines, " ");
$lastLines = array_slice($lines, count($lines) - $nbDisplayedLines + 1, count($lines) - 1);
$displayedLines = $lastLines + $displayedLines;
$this->slack->updateMessage($resultArray['channel'], $resultArray['ts'], $message, implode($displayedLines, "\n"));
};
$this->execute($command, $callbackStdout);
$this->slack->updateMessage($resultArray['channel'], $resultArray['ts'], $message, $logContent);
return $logContent;
}
}
class Slack {
protected $token;
protected $username;
protected $iconUrl;
public function __construct($token, $username, $iconUrl) {
$this->token = $token;
$this->username = $username;
$this->iconUrl = $iconUrl;
}
public function postMessage($channelName, $message, array $links = []) {
$parameters = [
"token" => $this->token,
"channel" => $channelName,
"text" => $message,
"icon_url" => $this->iconUrl,
'username' => $this->username,
];
$attachments = [];
foreach ($links as $title => $href) {
$attachments[] = [
"title_link" => $href,
"text" => $title,
];
}
if (count($attachments)) {
$parameters['attachments'] = json_encode($attachments);
}
$url = "https://slack.com/api/chat.postMessage";
$curl = curl_init($url);
curl_setopt_array($curl, [
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_POSTFIELDS => http_build_query($parameters),
]);
$result = curl_exec($curl);
return json_decode($result, true);
}
public function updateMessage($channelId, $ts = null, $message, $text = "") {
$url = "https://slack.com/api/chat.update";
$parameters = [
"token" => $this->token,
"channel" => $channelId,
"text" => $message,
"icon_url" => $this->iconUrl,
"username" => $this->username,
"attachments" => json_encode([
[
"text" => $text,
]
]),
"ts" => $ts,
];
$curl = curl_init($url);
curl_setopt_array($curl, [
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_POSTFIELDS => http_build_query($parameters),
]);
$result = curl_exec($curl);
return json_decode($result, true);
}
}