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