1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
### Installing |
4
|
|
|
|
5
|
|
|
Add to your _deploy.php_ |
6
|
|
|
|
7
|
|
|
```php |
8
|
|
|
require 'contrib/cpanel.php'; |
9
|
|
|
``` |
10
|
|
|
|
11
|
|
|
### Description |
12
|
|
|
This is a recipe that uses the [cPanel 2 API](https://documentation.cPanel.net/display/DD/Guide+to+cPanel+API+2). |
13
|
|
|
|
14
|
|
|
Unfortunately the [UAPI](https://documentation.cPanel.net/display/DD/Guide+to+UAPI) that is recommended does not have support for creating addon domains. |
15
|
|
|
The main idea behind is for staging purposes but I guess you can use it for other interesting concepts. |
16
|
|
|
|
17
|
|
|
The idea is, every branch possibly has its own staging domain/subdomain (staging-neat-feature.project.com) and database db_neat-feature_project so it can be tested. |
18
|
|
|
This recipe can make the domain/subdomain and database creation part of the deployment process so you don't have to manually create them through an interface. |
19
|
|
|
|
20
|
|
|
|
21
|
|
|
### Configuration |
22
|
|
|
The example uses a .env file and Dotenv for configuration, but you can set the parameters as you wish |
23
|
|
|
``` |
24
|
|
|
set('cpanel', [ |
25
|
|
|
'host' => getenv('CPANEL_HOST'), |
26
|
|
|
'port' => getenv('CPANEL_PORT'), |
27
|
|
|
'username' => getenv('CPANEL_USERNAME'), |
28
|
|
|
'auth_type' => getenv('CPANEL_AUTH_TYPE'), |
29
|
|
|
'token' => getenv('CPANEL_TOKEN'), |
30
|
|
|
'user' => getenv('CPANEL_USER'), |
31
|
|
|
'db_user' => getenv('CPANEL_DB_USER'), |
32
|
|
|
'db_user_privileges' => getenv('CPANEL_DB_PRIVILEGES'), |
33
|
|
|
'timeout' => 500, |
34
|
|
|
|
35
|
|
|
'allowInStage' => ['staging', 'beta', 'alpha'], |
36
|
|
|
|
37
|
|
|
'create_domain_format' => '%s-%s-%s', |
38
|
|
|
'create_domain_values' => ['staging', 'master', get('application')], |
39
|
|
|
'subdomain_prefix' => substr(md5(get('application')), 0,4) . '-', |
40
|
|
|
'subdomain_suffix' => getenv('SUDOMAIN_SUFFIX'), |
41
|
|
|
|
42
|
|
|
|
43
|
|
|
'create_db_format' => '%s_%s-%s-%s', |
44
|
|
|
'create_db_values' => ['apps', 'staging','master', get('application')], |
45
|
|
|
|
46
|
|
|
]); |
47
|
|
|
``` |
48
|
|
|
|
49
|
|
|
- `cpanel` – array with configuration for cPanel |
50
|
|
|
- `username` – WHM account |
51
|
|
|
- `user` – cPanel account that you want in charge of the domain |
52
|
|
|
- `token` – WHM API token |
53
|
|
|
- `create_domain_format` – Format for name creation of domain |
54
|
|
|
- `create_domain_values` – The actual value reference for naming |
55
|
|
|
- `subdomain_prefix` – cPanel has a weird way of dealing with addons and subdomains, you cannot create 2 addons with the same subdomain, so you need to change it in some way, example uses first 4 chars of md5(app_name) |
56
|
|
|
- `subdomain_suffix` – cPanel has a weird way of dealing with addons and subdomains, so the suffix needs to be your main domain for that account for deletion purposes |
57
|
|
|
- `addondir` – addon dir is different from the deploy path because cPanel "injects" /home/user/ into the path, so tilde cannot be used |
58
|
|
|
- `allowInStage` – Define the stages that cPanel recipe actions are allowed in |
59
|
|
|
|
60
|
|
|
|
61
|
|
|
#### .env file example |
62
|
|
|
``` |
63
|
|
|
CPANEL_HOST=xxx.xxx.xxx.xxx |
64
|
|
|
CPANEL_PORT=2087 |
65
|
|
|
CPANEL_USERNAME=root |
66
|
|
|
CPANEL_TOKEN=xxxx |
67
|
|
|
CPANEL_USER=xxx |
68
|
|
|
CPANEL_AUTH_TYPE=hash |
69
|
|
|
CPANEL_DB_USER=db_user |
70
|
|
|
CPANEL_DB_PRIVILEGES="ALL PRIVILEGES" |
71
|
|
|
SUDOMAIN_SUFFIX=.mymaindomain.com |
72
|
|
|
|
73
|
|
|
``` |
74
|
|
|
|
75
|
|
|
### Tasks |
76
|
|
|
|
77
|
|
|
- `cpanel:createaddondomain` Creates an addon domain |
78
|
|
|
- `cpanel:deleteaddondomain` Removes an addon domain |
79
|
|
|
- `cpanel:createdb` Creates a new database |
80
|
|
|
|
81
|
|
|
### Usage |
82
|
|
|
|
83
|
|
|
A complete example with configs, staging and deployment |
84
|
|
|
|
85
|
|
|
``` |
86
|
|
|
<?php |
87
|
|
|
|
88
|
|
|
namespace Deployer; |
89
|
|
|
use Dotenv\Dotenv; |
90
|
|
|
|
91
|
|
|
require 'vendor/autoload.php'; |
92
|
|
|
(Dotenv::create(__DIR__))->load(); // this is used just so an .env file can be used for credentials |
93
|
|
|
|
94
|
|
|
require 'cpanel.php'; |
95
|
|
|
|
96
|
|
|
|
97
|
|
|
// Project name |
98
|
|
|
set('application', 'myproject.com'); |
99
|
|
|
// Project repository |
100
|
|
|
set('repository', '[email protected]:myorg/myproject.com'); |
101
|
|
|
|
102
|
|
|
|
103
|
|
|
|
104
|
|
|
|
105
|
|
|
|
106
|
|
|
set('cpanel', [ |
107
|
|
|
'host' => getenv('CPANEL_HOST'), |
108
|
|
|
'port' => getenv('CPANEL_PORT'), |
109
|
|
|
'username' => getenv('CPANEL_USERNAME'), |
110
|
|
|
'auth_type' => getenv('CPANEL_AUTH_TYPE'), |
111
|
|
|
'token' => getenv('CPANEL_TOKEN'), |
112
|
|
|
'user' => getenv('CPANEL_USER'), |
113
|
|
|
'db_user' => getenv('CPANEL_DB_USER'), |
114
|
|
|
'db_user_privileges' => getenv('CPANEL_DB_PRIVILEGES'), |
115
|
|
|
'timeout' => 500, |
116
|
|
|
'allowInStage' => ['staging', 'beta', 'alpha'], |
117
|
|
|
|
118
|
|
|
'create_domain_format' => '%s-%s-%s', |
119
|
|
|
'create_domain_values' => ['staging', 'master', get('application')], |
120
|
|
|
'subdomain_prefix' => substr(md5(get('application')), 0,4) . '-', |
121
|
|
|
'subdomain_suffix' => getenv('SUDOMAIN_SUFFIX'), |
122
|
|
|
|
123
|
|
|
|
124
|
|
|
'create_db_format' => '%s_%s-%s-%s', |
125
|
|
|
'create_db_values' => ['apps', 'staging','master', get('application')], |
126
|
|
|
|
127
|
|
|
]); |
128
|
|
|
|
129
|
|
|
host('myproject.com') |
130
|
|
|
->stage('staging') |
131
|
|
|
->set('cpanel_createdb', vsprintf(get('cpanel')['create_db_format'], get('cpanel')['create_db_values'])) |
132
|
|
|
->set('branch', 'dev-branch') |
133
|
|
|
->set('deploy_path', '~/staging/' . vsprintf(get('cpanel')['create_domain_format'], get('cpanel')['create_domain_values'])) |
134
|
|
|
->set('addondir', 'staging/' . vsprintf(get('cpanel')['create_domain_format'], get('cpanel')['create_domain_values'])); |
135
|
|
|
// Tasks |
136
|
|
|
task('build', function () { |
137
|
|
|
run('cd {{release_path}} && build'); |
138
|
|
|
}); |
139
|
|
|
|
140
|
|
|
after('deploy:prepare', 'cpanel:createaddondomain'); |
141
|
|
|
after('deploy:prepare', 'cpanel:createdb'); |
142
|
|
|
``` |
143
|
|
|
*/ |
144
|
|
|
namespace Deployer; |
145
|
|
|
|
146
|
|
|
use Deployer\Task\Context; |
147
|
|
|
use \Gufy\CpanelPhp\Cpanel; |
|
|
|
|
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @return Cpanel |
151
|
|
|
* @throws Exception\Exception |
152
|
|
|
*/ |
153
|
|
|
function getCpanel() |
154
|
|
|
{ |
155
|
|
|
$config = get('cpanel', []); |
156
|
|
|
$allowInStage = $config['allowInStage']; |
157
|
|
|
$stage = Context::get()->getInput()->getArgument('stage'); |
158
|
|
|
|
159
|
|
|
if (!class_exists('\Gufy\CpanelPhp\Cpanel')) { |
160
|
|
|
throw new \RuntimeException("<comment>Please install php package</comment> <info>gufy/cpanel-php</info> <comment>to use CPanel API</comment>"); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
if (!in_array($stage, $allowInStage)) { |
164
|
|
|
throw new \RuntimeException(sprintf("Since it creates addon domains and databases, CPanel recipe is available only in the %s environments", implode($allowInStage))); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
|
168
|
|
|
if (!is_array($config) || |
169
|
|
|
!isset($config['host']) || |
170
|
|
|
!isset($config['port']) || |
171
|
|
|
!isset($config['username']) || |
172
|
|
|
!isset($config['token']) || |
173
|
|
|
!isset($config['user']) ) { |
174
|
|
|
throw new \RuntimeException("<comment>Please configure CPanel config:</comment> <info>set('cpanel', array('host' => 'xxx.xxx.xxx.xxx:', 'port' => 2087 , 'username' => 'root', 'token' => 'asdfasdf', 'cpaneluser' => 'guy'));</info>"); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
$cpanel = new Cpanel([ |
178
|
|
|
'host' => 'https://' . $config['host'] . ':' . $config['port'], |
179
|
|
|
'username' => $config['username'], |
180
|
|
|
'auth_type' => $config['auth_type'], |
181
|
|
|
'password' => $config['token'], |
182
|
|
|
]); |
183
|
|
|
|
184
|
|
|
$cpanel->setTimeout($config['timeout']); |
185
|
|
|
|
186
|
|
|
return $cpanel; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
function getDomainInfo() |
190
|
|
|
{ |
191
|
|
|
$domain = vsprintf(get('cpanel')['create_domain_format'], get('cpanel')['create_domain_values']); |
192
|
|
|
$cleanDomain = str_replace(['.', ',', ' ', '/', '-'], '', $domain); |
193
|
|
|
$subDomain = get('cpanel')['subdomain_prefix'] . $cleanDomain; |
194
|
|
|
|
195
|
|
|
return [ |
196
|
|
|
'domain' => $domain, |
197
|
|
|
'subDomain' => $subDomain, |
198
|
|
|
'subDomainWithSuffix' => $subDomain . get('cpanel')['subdomain_suffix'] |
199
|
|
|
]; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
desc('Creating database though CPanel API'); |
203
|
|
|
task('cpanel:createdb', function () { |
204
|
|
|
|
205
|
|
|
$cpanel = getCPanel(); |
206
|
|
|
$config = get('cpanel', []); |
207
|
|
|
if (!askConfirmation(sprintf('This will try to create the database %s on the host though CPanel API, ok?', get('cpanel_createdb'), get('deploy_path')), true)) { |
208
|
|
|
return; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
$createDbDataResult = $cpanel->cpanel('MysqlFE', 'createdb', $config['user'], ['db' => get('cpanel_createdb')]); |
212
|
|
|
$addPrivilegesDataResult = $cpanel->cpanel('MysqlFE', 'setdbuserprivileges', $config['user'], ['privileges' => $config['db_user_privileges'], 'db'=> get('cpanel_createdb'), 'dbuser' => $config['db_user']]); |
213
|
|
|
|
214
|
|
|
$createDbData = json_decode($createDbDataResult, true); |
215
|
|
|
$addPrivilegesData = json_decode($addPrivilegesDataResult, true); |
216
|
|
|
|
217
|
|
|
if (isset($createDbData['cpanelresult']['error'])) { |
218
|
|
|
writeln($createDbData['cpanelresult']['error']); |
219
|
|
|
} else { |
220
|
|
|
writeln('Successfully created database!'); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
if (isset($addPrivilegesData['cpanelresult']['error'])) { |
224
|
|
|
writeln($addPrivilegesData['cpanelresult']['error']); |
225
|
|
|
} else { |
226
|
|
|
writeln('Successfully added privileges to database!'); |
227
|
|
|
} |
228
|
|
|
}); |
229
|
|
|
|
230
|
|
|
desc('Creating addon domain though CPanel API'); |
231
|
|
|
task('cpanel:createaddondomain', function () { |
232
|
|
|
$cpanel = getCPanel(); |
233
|
|
|
$config = get('cpanel', []); |
234
|
|
|
$domain = getDomainInfo()['domain']; |
235
|
|
|
$subDomain = getDomainInfo()['subDomain']; |
236
|
|
|
if (!askConfirmation(sprintf('This will try to create the addon domain %s and point it to %s and subdomain %s, ok?', $domain, get('addondir'), $subDomain), true)) { |
237
|
|
|
return; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
writeln(sprintf('Creating addon domain %s and pointing it to %s', $domain, get('addondir'))); |
241
|
|
|
|
242
|
|
|
$addAddonDomainResult = $cpanel->cpanel('AddonDomain', 'addaddondomain', $config['user'], ['dir' => get('addondir'), 'newdomain'=> $domain, 'subdomain' => $subDomain]); |
243
|
|
|
$addAddonDomainData = json_decode($addAddonDomainResult, true); |
244
|
|
|
|
245
|
|
|
if (isset($delAddonDomainResult['cpanelresult']['error'])) { |
|
|
|
|
246
|
|
|
writeln($addAddonDomainData['cpanelresult']['error']); |
247
|
|
|
} else { |
248
|
|
|
writeln('Successfully created addon domain!'); |
249
|
|
|
writeln($addAddonDomainData['cpanelresult']['data'][0]['reason']); |
250
|
|
|
} |
251
|
|
|
}); |
252
|
|
|
|
253
|
|
|
desc('Delete addon domain though CPanel API'); |
254
|
|
|
task('cpanel:deleteaddondomain', function () { |
255
|
|
|
$cpanel = getCPanel(); |
256
|
|
|
$config = get('cpanel', []); |
257
|
|
|
$domain = getDomainInfo()['domain']; |
258
|
|
|
$subDomain = getDomainInfo()['subDomain']; |
259
|
|
|
$subDomainWithSuffix = getDomainInfo()['subDomainWithSuffix']; |
260
|
|
|
|
261
|
|
|
if (!askConfirmation(sprintf('This will delete the addon domain %s with corresponding subdomain %s, ok?', $domain, $subDomain), true)) { |
262
|
|
|
return; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
writeln(sprintf('Deleting addon domain %s', $domain)); |
266
|
|
|
|
267
|
|
|
$delAddonDomainResult = $cpanel->cpanel('AddonDomain', 'deladdondomain', $config['user'], ['domain'=> $domain, 'subdomain' => $subDomainWithSuffix]); |
268
|
|
|
$delAddonDomainResult = json_decode($delAddonDomainResult, true); |
269
|
|
|
|
270
|
|
|
if (isset($delAddonDomainResult['cpanelresult']['error'])) { |
271
|
|
|
writeln($delAddonDomainResult['cpanelresult']['error']); |
272
|
|
|
} else { |
273
|
|
|
writeln('Successfully deleted addon domain!'); |
274
|
|
|
writeln($delAddonDomainResult['cpanelresult']['data'][0]['reason']); |
275
|
|
|
} |
276
|
|
|
}); |
277
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths