1 | <?php |
||
2 | /* |
||
3 | ### Installing |
||
4 | |||
5 | Add to your _deploy.php_ |
||
6 | |||
7 | ```php |
||
8 | require 'contrib/directadmin.php'; |
||
9 | ``` |
||
10 | |||
11 | ### Configuration |
||
12 | - `directadmin` – array with configuration for DirectAdmin |
||
13 | - `host` – DirectAdmin host |
||
14 | - `port` – DirectAdmin port (default: 2222, not required) |
||
15 | - `scheme` – DirectAdmin scheme (default: http, not required) |
||
16 | - `username` – DirectAdmin username |
||
17 | - `password` – DirectAdmin password (it is recommended to use login keys!) |
||
18 | - `db_user` – Database username (required when using directadmin:createdb or directadmin:deletedb) |
||
19 | - `db_name` – Database namse (required when using directadmin:createdb) |
||
20 | - `db_password` – Database password (required when using directadmin:createdb) |
||
21 | - `domain_name` – Domain to create, delete or edit (required when using directadmin:createdomain, directadmin:deletedomain, directadmin:symlink-private-html or directadmin:php-version) |
||
22 | - `domain_ssl` – Enable SSL, options: ON/OFF, default: ON (optional when using directadmin:createdb) |
||
23 | - `domain_cgi` – Enable CGI, options: ON/OFF, default: ON (optional when using directadmin:createdb) |
||
24 | - `domain_php` – Enable PHP, options: ON/OFF, default: ON (optional when using directadmin:createdb) |
||
25 | - `domain_php_version` – Domain PHP Version, default: 1 (required when using directadmin:php-version) |
||
26 | |||
27 | ### Usage |
||
28 | |||
29 | A complete example with configs, staging and deployment |
||
30 | |||
31 | ``` |
||
32 | <?php |
||
33 | namespace Deployer; |
||
34 | |||
35 | require 'recipe/directadmin.php'; |
||
36 | |||
37 | // Project name |
||
38 | set('application', 'myproject.com'); |
||
39 | // Project repository |
||
40 | set('repository', '[email protected]:myorg/myproject.com'); |
||
41 | |||
42 | // DirectAdmin config |
||
43 | set('directadmin', [ |
||
44 | 'host' => 'example.com', |
||
45 | 'scheme' => 'https', // Optional |
||
46 | 'port' => 2222, // Optional |
||
47 | 'username' => 'admin', |
||
48 | 'password' => 'Test1234' // It is recommended to use login keys! |
||
49 | ]); |
||
50 | |||
51 | add('directadmin', [ |
||
52 | 'db_name' => 'website', |
||
53 | 'db_user' => 'website', |
||
54 | 'db_password' => 'Test1234', |
||
55 | |||
56 | 'domain_name' => 'test.example.com' |
||
57 | ]); |
||
58 | |||
59 | |||
60 | host('example.com') |
||
61 | ->stage('review') |
||
62 | ->user('admin') |
||
63 | ->set('deploy_path', '~/domains/test.example.com/repository') |
||
64 | |||
65 | |||
66 | // Tasks |
||
67 | desc('Create directadmin domain and database'); |
||
68 | task('directadmin:prepare', [ |
||
69 | 'directadmin:createdomain', |
||
70 | 'directadmin:symlink-private-html', |
||
71 | 'directadmin:createdb', |
||
72 | ])->onStage('review'); |
||
73 | |||
74 | task('deploy', [ |
||
75 | 'deploy:info', |
||
76 | 'directadmin:prepare', |
||
77 | 'deploy:prepare', |
||
78 | 'deploy:lock', |
||
79 | 'deploy:release', |
||
80 | 'deploy:update_code', |
||
81 | 'deploy:shared', |
||
82 | 'deploy:vendors', |
||
83 | 'deploy:writable', |
||
84 | 'deploy:symlink', |
||
85 | 'deploy:unlock', |
||
86 | 'cleanup', |
||
87 | 'success' |
||
88 | ])->desc('Deploy your project'); |
||
89 | ``` |
||
90 | |||
91 | */ |
||
92 | namespace Deployer; |
||
93 | use Deployer\Task\Context; |
||
94 | use Deployer\Utility\Httpie; |
||
95 | |||
96 | /** |
||
97 | * getDirectAdminConfig |
||
98 | * |
||
99 | * @return array |
||
100 | */ |
||
101 | function getDirectAdminConfig() |
||
102 | { |
||
103 | $config = get('directadmin', []); |
||
104 | |||
105 | if (!is_array($config) || |
||
106 | !isset($config['host']) || |
||
107 | !isset($config['username']) || |
||
108 | !isset($config['password']) ) { |
||
109 | throw new \RuntimeException("Please set the following DirectAdmin config:" . PHP_EOL . "set('directadmin', ['host' => '127.0.0.1', 'port' => 2222, 'username' => 'admin', 'password' => 'password']);"); |
||
110 | } |
||
111 | |||
112 | return $config; |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * DirectAdmin |
||
117 | * |
||
118 | * @param string $action |
||
119 | * @param array $data |
||
120 | * |
||
121 | * @return void |
||
122 | */ |
||
123 | function DirectAdmin(string $action, array $data = []) |
||
124 | { |
||
125 | $config = getDirectAdminConfig(); |
||
126 | $scheme = $config['scheme'] ?? 'http'; |
||
127 | $port = $config['port'] ?? 2222; |
||
128 | |||
129 | $result = Httpie::post(sprintf('%s://%s:%s/%s', $scheme, $config['host'], $port, $action)) |
||
130 | ->form($data) |
||
131 | ->setopt(CURLOPT_USERPWD, $config['username'] . ':' . $config['password']) |
||
132 | ->send(); |
||
133 | |||
134 | parse_str($result, $resultData); |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
135 | |||
136 | if ($resultData['error'] === '1') { |
||
137 | $resultData['details'] = trim($resultData['details']); |
||
138 | $resultData['details'] = str_replace(['\\n', '\\r'], '', $resultData['details']); |
||
139 | $resultData['details'] = strip_tags($resultData['details']); |
||
140 | |||
141 | writeln('<error>DirectAdmin message: ' . $resultData['details'] . '</error>'); |
||
142 | } |
||
143 | } |
||
144 | |||
145 | desc('Create a database on DirectAdmin'); |
||
146 | task('directadmin:createdb', function () { |
||
147 | $config = getDirectAdminConfig(); |
||
148 | |||
149 | if (!is_array($config) || |
||
0 ignored issues
–
show
|
|||
150 | !isset($config['db_name']) || |
||
151 | !isset($config['db_user']) || |
||
152 | !isset($config['db_password']) ) { |
||
153 | throw new \RuntimeException("Please add the following DirectAdmin config:" . PHP_EOL . "add('directadmin', ['db_name' => 'test', 'db_user' => 'test', 'db_password' => '123456']);"); |
||
154 | } |
||
155 | |||
156 | DirectAdmin('CMD_API_DATABASES', [ |
||
157 | 'action' => 'create', |
||
158 | 'name' => $config['db_name'], |
||
159 | 'user' => $config['db_user'], |
||
160 | 'passwd' => $config['db_password'], |
||
161 | 'passwd2' => $config['db_password'], |
||
162 | ]); |
||
163 | }); |
||
164 | |||
165 | desc('Delete a database on DirectAdmin'); |
||
166 | task('directadmin:deletedb', function () { |
||
167 | $config = getDirectAdminConfig(); |
||
168 | |||
169 | if (!is_array($config) || |
||
0 ignored issues
–
show
|
|||
170 | !isset($config['db_user'])) { |
||
171 | throw new \RuntimeException("Please add the following DirectAdmin config:" . PHP_EOL . "add('directadmin', ['db_user' => 'test_database']);"); |
||
172 | } |
||
173 | |||
174 | DirectAdmin('CMD_API_DATABASES', [ |
||
175 | 'action' => 'delete', |
||
176 | 'select0' => $config['username'] . '_' . $config['db_user'], |
||
177 | ]); |
||
178 | }); |
||
179 | |||
180 | desc('Create a domain on DirectAdmin'); |
||
181 | task('directadmin:createdomain', function () { |
||
182 | $config = getDirectAdminConfig(); |
||
183 | |||
184 | if (!is_array($config) || |
||
0 ignored issues
–
show
|
|||
185 | !isset($config['domain_name'])) { |
||
186 | throw new \RuntimeException("Please add the following DirectAdmin config:" . PHP_EOL . "add('directadmin', ['domain_name' => 'test.example.com']);"); |
||
187 | } |
||
188 | |||
189 | DirectAdmin('CMD_API_DOMAIN', [ |
||
190 | 'action' => 'create', |
||
191 | 'domain' => $config['domain_name'], |
||
192 | 'ssl' => $config['domain_ssl'] ?? 'On', |
||
193 | 'cgi' => $config['domain_cgi'] ?? 'ON', |
||
194 | 'php' => $config['domain_php'] ?? 'ON', |
||
195 | ]); |
||
196 | }); |
||
197 | |||
198 | desc('Delete a domain on DirectAdmin'); |
||
199 | task('directadmin:deletedomain', function () { |
||
200 | $config = getDirectAdminConfig(); |
||
201 | |||
202 | if (!is_array($config) || |
||
0 ignored issues
–
show
|
|||
203 | !isset($config['domain_name'])) { |
||
204 | throw new \RuntimeException("Please add the following DirectAdmin config:" . PHP_EOL . "add('directadmin', ['domain_name' => 'test.example.com']);"); |
||
205 | } |
||
206 | |||
207 | DirectAdmin('CMD_API_DOMAIN', [ |
||
208 | 'delete' => 'anything', |
||
209 | 'confirmed' => 'anything', |
||
210 | 'select0' => $config['domain_name'], |
||
211 | ]); |
||
212 | }); |
||
213 | |||
214 | desc('Symlink your private_html to public_html'); |
||
215 | task('directadmin:symlink-private-html', function () { |
||
216 | $config = getDirectAdminConfig(); |
||
217 | |||
218 | if (!is_array($config) || |
||
0 ignored issues
–
show
|
|||
219 | !isset($config['domain_name'])) { |
||
220 | throw new \RuntimeException("Please add the following DirectAdmin config:" . PHP_EOL . "add('directadmin', ['domain_name' => 'test.example.com']);"); |
||
221 | } |
||
222 | |||
223 | DirectAdmin('CMD_API_DOMAIN', [ |
||
224 | 'action' => 'private_html', |
||
225 | 'domain' => $config['domain_name'], |
||
226 | 'val' => 'symlink', |
||
227 | ]); |
||
228 | }); |
||
229 | |||
230 | desc('Change the PHP version from a domain'); |
||
231 | task('directadmin:php-version', function () { |
||
232 | $config = getDirectAdminConfig(); |
||
233 | |||
234 | if (!is_array($config) || |
||
0 ignored issues
–
show
|
|||
235 | !isset($config['domain_name']) || |
||
236 | !isset($config['domain_php_version'])) { |
||
237 | throw new \RuntimeException("Please add the following DirectAdmin config:" . PHP_EOL . "add('directadmin', ['domain_name' => 'test.example.com', 'domain_php_version' => 1]);"); |
||
238 | } |
||
239 | |||
240 | DirectAdmin('CMD_API_DOMAIN', [ |
||
241 | 'action' => 'php_selector', |
||
242 | 'domain' => $config['domain_name'], |
||
243 | 'php1_select' => $config['domain_php_version'], |
||
244 | ]); |
||
245 | }); |
||
246 |