Skip to content
This repository was archived by the owner on Apr 26, 2020. It is now read-only.

Commit 1c93b31

Browse files
Justin991qantonmedv
authored andcommitted
DirectAdmin recipe (#218)
* Added DirectAdmin recipe * Added DirectAdmin docs
1 parent 26ccc3a commit 1c93b31

2 files changed

Lines changed: 259 additions & 0 deletions

File tree

docs/directadmin.md

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# DirectAdmin recipe
2+
3+
### Installing
4+
5+
Install with composer
6+
7+
```bash
8+
composer require deployer/recipes --dev
9+
```
10+
11+
Add to your _deploy.php_
12+
13+
```php
14+
require 'recipe/directadmin.php';
15+
```
16+
17+
### Configuration
18+
- `directadmin` – array with configuration for DirectAdmin
19+
- `host` – DirectAdmin host
20+
- `port` – DirectAdmin port (default: 2222, not required)
21+
- `scheme` – DirectAdmin scheme (default: http, not required)
22+
- `username` – DirectAdmin username
23+
- `password` – DirectAdmin password (it is recommended to use login keys!)
24+
- `db_user` – Database username (required when using directadmin:createdb or directadmin:deletedb)
25+
- `db_name` – Database namse (required when using directadmin:createdb)
26+
- `db_password` – Database password (required when using directadmin:createdb)
27+
- `domain_name` – Domain to create, delete or edit (required when using directadmin:createdomain, directadmin:deletedomain, directadmin:symlink-private-html or directadmin:php-version)
28+
- `domain_ssl` – Enable SSL, options: ON/OFF, default: ON (optional when using directadmin:createdb)
29+
- `domain_cgi` – Enable CGI, options: ON/OFF, default: ON (optional when using directadmin:createdb)
30+
- `domain_php` – Enable PHP, options: ON/OFF, default: ON (optional when using directadmin:createdb)
31+
- `domain_php_version` – Domain PHP Version, default: 1 (required when using directadmin:php-version)
32+
33+
34+
### Tasks
35+
- `directadmin:createdb` Create a database on DirectAdmin
36+
- `directadmin:deletedb` Delete a database on DirectAdmin
37+
- `directadmin:createdomain` Create a domain on DirectAdmin
38+
- `directadmin:deletedomain` Delete a domain on DirectAdmin
39+
- `directadmin:symlink-private-html` Symlink your private_html to public_html
40+
- `directadmin:php-version` Change the PHP version from a domain
41+
42+
### Usage
43+
44+
A complete example with configs, staging and deployment
45+
46+
```
47+
<?php
48+
namespace Deployer;
49+
50+
require 'recipe/directadmin.php';
51+
52+
// Project name
53+
set('application', 'myproject.com');
54+
// Project repository
55+
set('repository', 'git@github.com:myorg/myproject.com');
56+
57+
// DirectAdmin config
58+
set('directadmin', [
59+
'host' => 'example.com',
60+
'scheme' => 'https', // Optional
61+
'port' => 2222, // Optional
62+
'username' => 'admin',
63+
'password' => 'Test1234' // It is recommended to use login keys!
64+
]);
65+
66+
add('directadmin', [
67+
'db_name' => 'website',
68+
'db_user' => 'website',
69+
'db_password' => 'Test1234',
70+
71+
'domain_name' => 'test.example.com'
72+
]);
73+
74+
75+
host('example.com')
76+
->stage('review')
77+
->user('admin')
78+
->set('deploy_path', '~/domains/test.example.com/repository')
79+
80+
81+
// Tasks
82+
desc('Create directadmin domain and database');
83+
task('directadmin:prepare', [
84+
'directadmin:createdomain',
85+
'directadmin:symlink-private-html',
86+
'directadmin:createdb',
87+
])->onStage('review');
88+
89+
task('deploy', [
90+
'deploy:info',
91+
'directadmin:prepare',
92+
'deploy:prepare',
93+
'deploy:lock',
94+
'deploy:release',
95+
'deploy:update_code',
96+
'deploy:shared',
97+
'deploy:vendors',
98+
'deploy:writable',
99+
'deploy:symlink',
100+
'deploy:unlock',
101+
'cleanup',
102+
'success'
103+
])->desc('Deploy your project');
104+
```

recipe/directadmin.php

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
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

Comments
 (0)