-
Notifications
You must be signed in to change notification settings - Fork 0
/
webdevhelper.php
executable file
·256 lines (216 loc) · 6.27 KB
/
webdevhelper.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
<?php
$short_opts = '';
$short_opts .= 'n:'; // Required value: sandbox name.
$short_opts .= 'd';
$short_opts .= 'h';
$long_opts = array(
'nodb',
'name:',
'delete',
'help'
);
$cli_opts = getopt($short_opts, $long_opts);
if (empty($cli_opts) || isset($cli_opts['h']) || isset($cli_opts['help'])) {
print_help();
exit;
}
if (empty($cli_opts['n']) && empty($cli_opts['name'])) {
print_error('You must specify sandbox name with "-n" or "--name"!');
exit(1);
}
$name = isset($cli_opts['name']) ? $cli_opts['name'] : $cli_opts['n'];
$sandbox = new WebDevHelper();
$sandbox->setSandboxName($name);
if (isset($cli_opts['d']) || isset($cli_opts['delete'])) {
print_msg('Deleting sandbox: ' . $name);
$sandbox->removeSandbox();
exit;
}
if (isset($cli_opts['nodb'])){
$sandbox->noDB();
}
$sandbox->createSandbox();
class WebDevHelper {
private $name;
private $dir;
private $suffix;
private $dbName;
public function __construct() {
$this->dir = '/home/rosko/websites';
$this->suffix = '.dev';
}
public function setSandboxName($name) {
if (strpos($name, ' ') == FALSE) {
$this->name = $this->dbName = $name;
}
else {
print_error('There must be NO SPACES in sandbox name.');
exit(1);
}
}
public function createSandbox() {
$this->createSandboxDir($this->getSandboxDir());
if ($this->dbName) {
$this->createDB($this->getSandboxName());
}
$this->createHTTPDConf();
$this->enableSandbox();
}
public function createSandboxDir() {
if (!is_dir($this->getSandboxDir())) {
system('mkdir -p ' . $this->getSandboxDir(), $ret);
if ($ret != 0) {
print_error('Fatal error!!! Could not create directory for sandbox.');
exit(1);
}
else {
print_msg('Directory "' . $this->getSandboxDir() . '" successfully created.');
}
$ret = chown($this->getSandboxDir(), $_SERVER['SUDO_USER']);
if ($ret) {
print_msg('Owner for directory "' . $this->getSandboxDir() . '" changed to "' . $_SERVER['SUDO_USER'] .'"');
}
else {
print_error('Owner for directory "' . $this->getSandboxDir() . '" didn\'t changed.');
}
}
}
public function createDB() {
system('mysqladmin create ' . $this->getSandboxName(), $ret);
if ($ret != 0) {
print_error('Fatal error!!! Could not create DB for sandbox.');
exit(1);
}
else {
print_msg('DB "' . $this->getSandboxName() . '" successfully created.');
}
}
public function createHTTPDConf() {
$name = '/etc/apache2/sites-available/' . $this->getSandboxName();
$configFile = new SplFileObject($name,'w');
$ret = $configFile->fwrite($this->apacheConfig());
if ($ret) {
print_msg('HTTPD conf written successfully.');
}
else {
print_error('Could not write HTTPD config.');
}
}
public function getWebSitesDir() {
return $this->dir;
}
public function getSandboxDir() {
return $this->dir . '/' . $this->name . $this->suffix;
}
public function getSandboxName() {
return $this->name . $this->suffix;
}
public function apacheConfig() {
$template = $this->apacheConfigTemplate();
$replacement = array(
'[sandboxname]' => $this->getSandboxName(),
'[sandbox_dir]' => $this->getSandboxDir(),
);
return strtr($template,$replacement);
}
public function apacheConfigTemplate() {
return "<VirtualHost *:80>
ServerName [sandboxname]
ServerAdmin webmaster@localhost
DocumentRoot [sandbox_dir]
<Directory [sandbox_dir]>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
#CustomLog [sandbox_dir]/access.log combined
#ErrorLog [sandbox_dir]/error.log
</VirtualHost>
";
}
public function noDB() {
$this->dbName = FALSE;
}
public function enableSandbox() {
system('a2ensite ' . $this->getSandboxName(), $ret);
if ($ret != 0) {
print_error('Fatal error!!! Could not enable sandbox config in HTTPD.');
exit(1);
}
else {
print_msg('Site "' . $this->getSandboxName() . '" successfully enabled.');
}
system('service apache2 reload', $ret);
if ($ret != 0) {
print_error('Fatal error!!! Could not reload HTTPD configs.');
exit(1);
}
else {
print_msg('HTTPD config successfully reloaded.');
}
}
public function removeSandbox() {
$this->disableSandbox();
$this->removeHTTPDConf();
$this->removeDB();
$this->removeSandboxDir();
}
public function disableSandbox() {
system('a2dissite ' . $this->getSandboxName(), $ret);
if ($ret != 0) {
print_error('Fatal error!!! Could not disable sandbox config in HTTPD.');
}
else {
print_msg('Site "' . $this->getSandboxName() . '" successfully disabled.');
}
system('service apache2 reload', $ret);
if ($ret != 0) {
print_error('Fatal error!!! Could not reload HTTPD configs.');
}
else {
print_msg('HTTPD config successfully reloaded.');
}
}
public function removeHTTPDConf() {
$name = '/etc/apache2/sites-available/' . $this->getSandboxName();
$ret = unlink($name);
if ($ret) {
print_msg('Config for sandbox "' .$this->getSandboxName() . '" successfully removed.');
}
else {
print_error('Could not delete config for sandbox "' .$this->getSandboxName() . '".');
}
}
public function removeDB() {
system('mysqladmin drop -f ' . $this->getSandboxName(), $ret);
if ($ret != 0) {
print_error('Fatal error!!! Could not remove DB for sandbox "' .$this->getSandboxName() . '".');
}
}
public function removeSandboxDir() {
system('rm -rf ' . $this->getSandboxDir(), $ret);
if ($ret != 0) {
print_error('Fatal error!!! Could not remove directory for sandbox "' .$this->getSandboxName() . '".');
exit(1);
}
else {
print_msg('Directory "' . $this->getSandboxDir() . '" successfully deleted.');
}
}
}
function print_help() {
print_msg('HELP');
print_msg('HELP');
print_msg('HELP');
//var_dump($_SERVER['SCRIPT_NAME']);
}
function print_error($message) {
print('ERROR: ' . $message . PHP_EOL);
}
function print_msg($message) {
print($message . PHP_EOL);
}