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