|
| 1 | +<?php |
| 2 | +namespace Deployer; |
| 3 | +use Deployer\Task\Context; |
| 4 | +use Deployer\Utility\Httpie; |
| 5 | + |
| 6 | +/** |
| 7 | + * getDirectAdminConfig |
| 8 | + * |
| 9 | + * @return array |
| 10 | + */ |
| 11 | +function getDirectAdminConfig() |
| 12 | +{ |
| 13 | + $config = get('directadmin', []); |
| 14 | + |
| 15 | + if (!is_array($config) || |
| 16 | + !isset($config['host']) || |
| 17 | + !isset($config['username']) || |
| 18 | + !isset($config['password']) ) { |
| 19 | + throw new \RuntimeException("Please set the following DirectAdmin config:" . PHP_EOL . "set('directadmin', ['host' => '127.0.0.1', 'port' => 2222, 'username' => 'admin', 'password' => 'password']);"); |
| 20 | + } |
| 21 | + |
| 22 | + return $config; |
| 23 | +} |
| 24 | + |
| 25 | +/** |
| 26 | + * DirectAdmin |
| 27 | + * |
| 28 | + * @param string $action |
| 29 | + * @param array $data |
| 30 | + * |
| 31 | + * @return void |
| 32 | + */ |
| 33 | +function DirectAdmin(string $action, array $data = []) |
| 34 | +{ |
| 35 | + $config = getDirectAdminConfig(); |
| 36 | + $scheme = $config['scheme'] ?? 'http'; |
| 37 | + $port = $config['port'] ?? 2222; |
| 38 | + |
| 39 | + $result = Httpie::post(sprintf('%s://%s:%s/%s', $scheme, $config['host'], $port, $action)) |
| 40 | + ->form($data) |
| 41 | + ->setopt(CURLOPT_USERPWD, $config['username'] . ':' . $config['password']) |
| 42 | + ->send(); |
| 43 | + |
| 44 | + parse_str($result, $resultData); |
| 45 | + |
| 46 | + if ($resultData['error'] === '1') { |
| 47 | + $resultData['details'] = trim($resultData['details']); |
| 48 | + $resultData['details'] = str_replace(['\\n', '\\r'], '', $resultData['details']); |
| 49 | + $resultData['details'] = strip_tags($resultData['details']); |
| 50 | + |
| 51 | + writeln('<error>DirectAdmin message: ' . $resultData['details'] . '</error>'); |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +desc('Create a database on DirectAdmin'); |
| 56 | +task('directadmin:createdb', function () { |
| 57 | + $config = getDirectAdminConfig(); |
| 58 | + |
| 59 | + if (!is_array($config) || |
| 60 | + !isset($config['db_name']) || |
| 61 | + !isset($config['db_user']) || |
| 62 | + !isset($config['db_password']) ) { |
| 63 | + throw new \RuntimeException("Please add the following DirectAdmin config:" . PHP_EOL . "add('directadmin', ['db_name' => 'test', 'db_user' => 'test', 'db_password' => '123456']);"); |
| 64 | + } |
| 65 | + |
| 66 | + DirectAdmin('CMD_API_DATABASES', [ |
| 67 | + 'action' => 'create', |
| 68 | + 'name' => $config['db_name'], |
| 69 | + 'user' => $config['db_user'], |
| 70 | + 'passwd' => $config['db_password'], |
| 71 | + 'passwd2' => $config['db_password'], |
| 72 | + ]); |
| 73 | +}); |
| 74 | + |
| 75 | +desc('Delete a database on DirectAdmin'); |
| 76 | +task('directadmin:deletedb', function () { |
| 77 | + $config = getDirectAdminConfig(); |
| 78 | + |
| 79 | + if (!is_array($config) || |
| 80 | + !isset($config['db_user'])) { |
| 81 | + throw new \RuntimeException("Please add the following DirectAdmin config:" . PHP_EOL . "add('directadmin', ['db_user' => 'test_database']);"); |
| 82 | + } |
| 83 | + |
| 84 | + DirectAdmin('CMD_API_DATABASES', [ |
| 85 | + 'action' => 'delete', |
| 86 | + 'select0' => $config['username'] . '_' . $config['db_user'], |
| 87 | + ]); |
| 88 | +}); |
| 89 | + |
| 90 | +desc('Create a domain on DirectAdmin'); |
| 91 | +task('directadmin:createdomain', function () { |
| 92 | + $config = getDirectAdminConfig(); |
| 93 | + |
| 94 | + if (!is_array($config) || |
| 95 | + !isset($config['domain_name'])) { |
| 96 | + throw new \RuntimeException("Please add the following DirectAdmin config:" . PHP_EOL . "add('directadmin', ['domain_name' => 'test.example.com']);"); |
| 97 | + } |
| 98 | + |
| 99 | + DirectAdmin('CMD_API_DOMAIN', [ |
| 100 | + 'action' => 'create', |
| 101 | + 'domain' => $config['domain_name'], |
| 102 | + 'ssl' => $config['domain_ssl'] ?? 'On', |
| 103 | + 'cgi' => $config['domain_cgi'] ?? 'ON', |
| 104 | + 'php' => $config['domain_php'] ?? 'ON', |
| 105 | + ]); |
| 106 | +}); |
| 107 | + |
| 108 | +desc('Delete a domain on DirectAdmin'); |
| 109 | +task('directadmin:deletedomain', function () { |
| 110 | + $config = getDirectAdminConfig(); |
| 111 | + |
| 112 | + if (!is_array($config) || |
| 113 | + !isset($config['domain_name'])) { |
| 114 | + throw new \RuntimeException("Please add the following DirectAdmin config:" . PHP_EOL . "add('directadmin', ['domain_name' => 'test.example.com']);"); |
| 115 | + } |
| 116 | + |
| 117 | + DirectAdmin('CMD_API_DOMAIN', [ |
| 118 | + 'delete' => 'anything', |
| 119 | + 'confirmed' => 'anything', |
| 120 | + 'select0' => $config['domain_name'], |
| 121 | + ]); |
| 122 | +}); |
| 123 | + |
| 124 | +desc('Symlink your private_html to public_html'); |
| 125 | +task('directadmin:symlink-private-html', function () { |
| 126 | + $config = getDirectAdminConfig(); |
| 127 | + |
| 128 | + if (!is_array($config) || |
| 129 | + !isset($config['domain_name'])) { |
| 130 | + throw new \RuntimeException("Please add the following DirectAdmin config:" . PHP_EOL . "add('directadmin', ['domain_name' => 'test.example.com']);"); |
| 131 | + } |
| 132 | + |
| 133 | + DirectAdmin('CMD_API_DOMAIN', [ |
| 134 | + 'action' => 'private_html', |
| 135 | + 'domain' => $config['domain_name'], |
| 136 | + 'val' => 'symlink', |
| 137 | + ]); |
| 138 | +}); |
| 139 | + |
| 140 | +desc('Change the PHP version from a domain'); |
| 141 | +task('directadmin:php-version', function () { |
| 142 | + $config = getDirectAdminConfig(); |
| 143 | + |
| 144 | + if (!is_array($config) || |
| 145 | + !isset($config['domain_name']) || |
| 146 | + !isset($config['domain_php_version'])) { |
| 147 | + throw new \RuntimeException("Please add the following DirectAdmin config:" . PHP_EOL . "add('directadmin', ['domain_name' => 'test.example.com', 'domain_php_version' => 1]);"); |
| 148 | + } |
| 149 | + |
| 150 | + DirectAdmin('CMD_API_DOMAIN', [ |
| 151 | + 'action' => 'php_selector', |
| 152 | + 'domain' => $config['domain_name'], |
| 153 | + 'php1_select' => $config['domain_php_version'], |
| 154 | + ]); |
| 155 | +}); |
0 commit comments