1 | <?php |
||
2 | |||
3 | namespace Deployer; |
||
4 | |||
5 | use Deployer\Task\Context; |
||
6 | use \Gufy\CpanelPhp\Cpanel; |
||
7 | |||
8 | /** |
||
9 | * @return Cpanel |
||
10 | * @throws Exception\Exception |
||
11 | */ |
||
12 | function getCpanel() |
||
13 | { |
||
14 | $config = get('cpanel', []); |
||
15 | $allowInStage = $config['allowInStage']; |
||
16 | $stage = Context::get()->getInput()->getArgument('stage'); |
||
17 | |||
18 | if (!class_exists('\Gufy\CpanelPhp\Cpanel')) { |
||
19 | throw new \RuntimeException("<comment>Please install php package</comment> <info>gufy/cpanel-php</info> <comment>to use CPanel API</comment>"); |
||
20 | } |
||
21 | |||
22 | if (!in_array($stage, $allowInStage)) { |
||
23 | throw new \RuntimeException(sprintf("Since it creates addon domains and databases, CPanel recipe is available only in the %s environments", implode($allowInStage))); |
||
24 | } |
||
25 | |||
26 | |||
27 | if (!is_array($config) || |
||
28 | !isset($config['host']) || |
||
29 | !isset($config['port']) || |
||
30 | !isset($config['username']) || |
||
31 | !isset($config['token']) || |
||
32 | !isset($config['user']) ) { |
||
33 | 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>"); |
||
34 | } |
||
35 | |||
36 | $cpanel = new Cpanel([ |
||
37 | 'host' => 'https://' . $config['host'] . ':' . $config['port'], |
||
38 | 'username' => $config['username'], |
||
39 | 'auth_type' => $config['auth_type'], |
||
40 | 'password' => $config['token'], |
||
41 | ]); |
||
42 | |||
43 | $cpanel->setTimeout($config['timeout']); |
||
44 | |||
45 | return $cpanel; |
||
46 | } |
||
47 | |||
48 | function getDomainInfo() |
||
49 | { |
||
50 | $domain = vsprintf(get('cpanel')['create_domain_format'], get('cpanel')['create_domain_values']); |
||
51 | $cleanDomain = str_replace(['.', ',', ' ', '/', '-'], '', $domain); |
||
52 | $subDomain = get('cpanel')['subdomain_prefix'] . $cleanDomain; |
||
53 | |||
54 | return [ |
||
55 | 'domain' => $domain, |
||
56 | 'subDomain' => $subDomain, |
||
57 | 'subDomainWithSuffix' => $subDomain . get('cpanel')['subdomain_suffix'] |
||
58 | ]; |
||
59 | } |
||
60 | |||
61 | desc('Creating database though CPanel API'); |
||
62 | task('cpanel:createdb', function () { |
||
63 | |||
64 | $cpanel = getCPanel(); |
||
65 | $config = get('cpanel', []); |
||
66 | 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)) { |
||
67 | return; |
||
68 | } |
||
69 | |||
70 | $createDbDataResult = $cpanel->cpanel('MysqlFE', 'createdb', $config['user'], ['db' => get('cpanel_createdb')]); |
||
71 | $addPrivilegesDataResult = $cpanel->cpanel('MysqlFE', 'setdbuserprivileges', $config['user'], ['privileges' => $config['db_user_privileges'], 'db'=> get('cpanel_createdb'), 'dbuser' => $config['db_user']]); |
||
72 | |||
73 | $createDbData = json_decode($createDbDataResult, true); |
||
74 | $addPrivilegesData = json_decode($addPrivilegesDataResult, true); |
||
75 | |||
76 | if (isset($createDbData['cpanelresult']['error'])) { |
||
77 | writeln($createDbData['cpanelresult']['error']); |
||
78 | } else { |
||
79 | writeln('Successfully created database!'); |
||
80 | } |
||
81 | |||
82 | if (isset($addPrivilegesData['cpanelresult']['error'])) { |
||
83 | writeln($addPrivilegesData['cpanelresult']['error']); |
||
84 | } else { |
||
85 | writeln('Successfully added privileges to database!'); |
||
86 | } |
||
87 | }); |
||
88 | |||
89 | desc('Creating addon domain though CPanel API'); |
||
90 | task('cpanel:createaddondomain', function () { |
||
91 | $cpanel = getCPanel(); |
||
92 | $config = get('cpanel', []); |
||
93 | $domain = getDomainInfo()['domain']; |
||
94 | $subDomain = getDomainInfo()['subDomain']; |
||
95 | 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)) { |
||
96 | return; |
||
97 | } |
||
98 | |||
99 | writeln(sprintf('Creating addon domain %s and pointing it to %s', $domain, get('addondir'))); |
||
100 | |||
101 | $addAddonDomainResult = $cpanel->cpanel('AddonDomain', 'addaddondomain', $config['user'], ['dir' => get('addondir'), 'newdomain'=> $domain, 'subdomain' => $subDomain]); |
||
102 | $addAddonDomainData = json_decode($addAddonDomainResult, true); |
||
103 | |||
104 | if (isset($delAddonDomainResult['cpanelresult']['error'])) { |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Loading history...
|
|||
105 | writeln($addAddonDomainData['cpanelresult']['error']); |
||
106 | } else { |
||
107 | writeln('Successfully created addon domain!'); |
||
108 | writeln($addAddonDomainData['cpanelresult']['data'][0]['reason']); |
||
109 | } |
||
110 | }); |
||
111 | |||
112 | desc('Delete addon domain though CPanel API'); |
||
113 | task('cpanel:deleteaddondomain', function () { |
||
114 | $cpanel = getCPanel(); |
||
115 | $config = get('cpanel', []); |
||
116 | $domain = getDomainInfo()['domain']; |
||
117 | $subDomain = getDomainInfo()['subDomain']; |
||
118 | $subDomainWithSuffix = getDomainInfo()['subDomainWithSuffix']; |
||
119 | |||
120 | if (!askConfirmation(sprintf('This will delete the addon domain %s with corresponding subdomain %s, ok?', $domain, $subDomain), true)) { |
||
121 | return; |
||
122 | } |
||
123 | |||
124 | writeln(sprintf('Deleting addon domain %s', $domain)); |
||
125 | |||
126 | $delAddonDomainResult = $cpanel->cpanel('AddonDomain', 'deladdondomain', $config['user'], ['domain'=> $domain, 'subdomain' => $subDomainWithSuffix]); |
||
127 | $delAddonDomainResult = json_decode($delAddonDomainResult, true); |
||
128 | |||
129 | if (isset($delAddonDomainResult['cpanelresult']['error'])) { |
||
130 | writeln($delAddonDomainResult['cpanelresult']['error']); |
||
131 | } else { |
||
132 | writeln('Successfully deleted addon domain!'); |
||
133 | writeln($delAddonDomainResult['cpanelresult']['data'][0]['reason']); |
||
134 | } |
||
135 | }); |
||
136 |