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