-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrestart-php7.1-fpm.php
94 lines (83 loc) · 2.67 KB
/
restart-php7.1-fpm.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
<?php
namespace samyapp;
use function Deployer\get;
use function Deployer\desc;
use function Deployer\task;
/**
* Gets an oauth access token from Cloudways
* Requires a valid cloudways account email and api key
*/
function cloudways_oauth($email, $api_key)
{
$url = 'https://api.cloudways.com/api/v1/oauth/access_token';
$data = [
'email' => $email, get('cloudways_email'),
'api_key' => $api_key,
];
// use key 'http' even if you send the request to https://...
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data)
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
if ($result !== FALSE) {
$json = json_decode($result);
$token = $json->access_token;
return $token;
}
return false;
}
/**
* Makes an api request to cloudways api to restart php-fpm on a specific server
* @param string $server - The ID of the server to restart
* @param string $token - oauth access token granted by cloudways_oauth()
*/
function cloudways_restartphp($server, $token) {
$url = 'https://api.cloudways.com/api/v1/service/state';
$data = [
'server_id' => $server,
'service' => 'php7.1-fpm',
'state' => 'restart',
];
// use key 'http' even if you send the request to https://...
$options = array(
'http' => array(
'header' => "Authorization: Bearer $token\r\nContent-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data)
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
if ($result !== FALSE) {
$json = json_decode($result);
return $json->service_status && $json->service_status->status == 'running';
}
return false;
}
desc('Restart php 7.1 fpm on cloudways server');
task('deploy:restart-php7.1-fpm', function() {
$token = cloudways_oauth(get('cloudways_email'), get('cloudways_api_key'));
if($token) {
if(!cloudways_restartphp(get('cloudways_server_id'), $token)) {
throw new \Exception('unable to restart php');
}
}
else {
throw new \Exception('unable to restart php - oauth failure');
}
});
/* Example Usage
// require this file in your deploy.php after requiring your recipe,
// set the needed variables and schedule the task.
set('cloudways_email', '[email protected]');
set('cloudways_server_id', 42); // the id of the cloudways server to restart
// your cloudways api key - you could hard code this in your deploy script of
// just set it in your environment instead.
set('cloudways_api_key', $_ENV['CLOUDWAYS_API_KEY']);
after('deploy:symlink', 'deploy:restart-php7.1-fpm');
*/