-
Notifications
You must be signed in to change notification settings - Fork 9
/
virtual_feature.pl
330 lines (287 loc) · 8.74 KB
/
virtual_feature.pl
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
do 'virtualmin-nginx-fpm-lib.pl';
sub feature_name {
return $text{'feat_name'};
}
sub feature_label {
return $text{'feat_label'};
}
sub feature_losing {
return $text{'feat_loosing'};
}
sub feature_disname {
return $text{'feat_disname'};
}
sub feature_check {
if (! &has_command($config{'nginx_cmd'})) {
return $text{'feat_req_nginx'};
}
if (! &has_command($config{'fpm_cmd'})) {
return $text{'feat_req_fpm'};
} else {
return undef;
}
}
sub feature_clash {
my ($d) = @_;
if ($d->{'virtualmin-nginx'} or $d{'web'}) {
return $text{'feat_clash'};
}
if (-r "$config{'nginx_sites_available'}/$d->{'dom'}.conf") {
return $text{'feat_nginx_exists'};
}
if (-r "$config{'fpm_poold'}/$d->{'dom'}.conf") {
return $text{'feat_fpm_exists'};
} else {
return undef;
}
}
sub feature_depends {
my ($d) = @_;
return $text{'feat_edepunix'} if (!$d->{'unix'} && !$d->{'parent'});
return $text{'feat_edepdir'} if (!$d->{'dir'} && !$d->{'alias'});
return $text{'feat_eapache'} if ($d->{'web'});
return $text{'feat_nginx'} if ($d->{'virtualmin-nginx'});
return undef;
}
sub feature_suitable {
my ($parentdom, $aliasdom, $subdom) = @_;
return $aliasdom ? 0 : 1;
}
sub feature_setup {
my ($d) = @_;
%conf = ();
$conf{'pm'} = 'dynamic';
$conf{'pm.max_children'} = 5;
$conf{'pm.start_servers'} = 2;
$conf{'pm.min_spare_servers'} = 1;
$conf{'pm.max_spare_servers'} = 3;
$conf{'pm.max_requests'} = 500;
&virtual_server::generate_default_certificate($d);
&$virtual_server::first_print($text{'feat_setup'});
&create_nginx_config($d);
&create_fpm_pool($d, \%conf);
&$virtual_server::second_print($virtual_server::text{'setup_done'});
# Add nginx user to domain group
my $web_user = $config{'nginx_user'};
if ($web_user && $web_user ne 'none') {
&virtual_server::add_user_to_domain_group($d, $web_user, 'setup_webuser');
}
&reload_services;
}
sub feature_delete {
my ($d) = @_;
&$virtual_server::first_print($text{'feat_delete'});
&unlink_file(
"$config{'nginx_sites_available'}/$d->{'dom'}.conf",
"$config{'fpm_poold'}/$d->{'dom'}.conf"
);
if($config{'nginx_sites_enabled'}) {
&unlink_file(
"$config{'nginx_sites_enabled'}/$d->{'dom'}.conf",
);
}
&reload_services;
&$virtual_server::second_print($virtual_server::text{'setup_done'});
}
sub feature_modify {
my ($d, $oldd) = @_;
# if ($d->{'dom'} ne $oldd->{'dom'}) {
&$virtual_server::first_print($text{'feat_modify_domain'});
%conf = get_fpm_config($d);
&unlink_file(
"$config{'nginx_sites_available'}/$d->{'dom'}.conf",
"$config{'fpm_poold'}/$d->{'dom'}.conf"
);
if($config{'nginx_sites_enabled'}) {
&unlink_file(
"$config{'nginx_sites_enabled'}/$d->{'dom'}.conf",
);
}
&create_nginx_config($d);
&create_fpm_pool($d, \%conf);
&reload_services;
&$virtual_server::second_print($virtual_server::text{'setup_done'});
# }
}
sub feature_disable {
my ($d) = @_;
&$virtual_server::first_print($text{'feat_disable'});
if($config{'nginx_sites_enabled'}) {
&unlink_file("$config{'nginx_sites_enabled'}/$d->{'dom'}.conf");
} else {
&unlink_file("$config{'nginx_sites_available'}/$d->{'dom'}.conf");
}
&rename_file("$config{'fpm_poold'}/$d->{'dom'}.conf", "$config{'fpm_poold'}/$d->{'dom'}.conf.dis");
&reload_services;
&$virtual_server::second_print($virtual_server::text{'setup_done'});
}
sub feature_enable {
my ($d) = @_;
&$virtual_server::first_print($text{'feat_enable'});
if($config{'nginx_sites_enabled'}) {
&symlink_file("$config{'nginx_sites_available'}/$d->{'dom'}.conf", "$config{'nginx_sites_enabled'}/$d->{'dom'}.conf");
} else {
&create_nginx_config($d);
}
&rename_file("$config{'fpm_poold'}/$d->{'dom'}.conf.dis", "$config{'fpm_poold'}/$d->{'dom'}.conf");
&reload_services;
&$virtual_server::second_print($virtual_server::text{'setup_done'});
}
sub feature_import {
my ($dname, $user, $db) = @_;
if (-r "$config{'nginx_sites_available'}/$dname.conf" or -r "$config{'fpm_poold'}/$dname.conf") {
return 1;
} else {
return 0;
}
}
sub feature_links {
local ($d) = @_;
return ( { 'mod' => $module_name,
'desc' => $text{'feat_manage'},
'page' => 'edit_domain.cgi?dom='.$d->{'dom'},
'cat' => 'services',
} );
}
sub feature_validate {
my ($d) = @_;
if (!-r "$config{'nginx_sites_available'}/$d->{'dom'}.conf") {
return $text{'feat_validate_nginx'};
}
if (!-r "$config{'fpm_poold'}/$d->{'dom'}.conf") {
return $text{'feat_validate_fpm'};
} else {
return undef;
}
}
sub feature_backup {
my ($d, $file) = @_;
&$virtual_server::first_print($text{'feat_backup'});
$tmp_file = transname();
$command = "tar czfP $tmp_file ";
$command = "$command \"$config{'nginx_sites_available'}/$d->{'dom'}.conf\"";
if($config{'nginx_sites_enabled'}) {
$command = "$command \"$config{'nginx_nginx_sites_enabled'}/$d->{'dom'}.conf\"";
}
$command = "$command \"$config{'fpm_poold'}/$d->{'dom'}.conf\"";
system($command);
my $ok = ©_source_dest($tmp_file, $file);
if ($ok) {
&$virtual_server::second_print($virtual_server::text{'setup_done'});
return 1;
}
else {
&$virtual_server::second_print($text{'feat_backup_failed'});
return 0;
}
}
sub feature_restore {
my ($d, $file) = @_;
&$virtual_server::first_print($text{'feat_restore'});
my $ok = system("tar xzf $file -C /");
# if ($ok) {
&$virtual_server::second_print($virtual_server::text{'setup_done'});
&reload_services();
return 1;
# } else {
# &$virtual_server::second_print($text{'feat_restore_failed'}." tar xzf $file");
# return 0;
# }
}
sub feature_provides_web {
return 1;
}
sub feature_provides_ssl {
return 1;
}
sub feature_get_web_ssl_file {
my ($d, $mode) = @_;
if ($mode eq 'cert') {
return $d->{'ssl_cert'};
}
elsif ($mode eq 'key') {
return $d->{'ssl_key'};
}
elsif ($mode eq 'ca') {
# Always appeneded to the cert file
return $d->{'ssl_chain'};
}
return undef;
}
sub feature_web_supports_suexec {
return -1;
}
sub feature_web_supports_cgi {
return 1;
}
sub feature_web_supported_php_modes
{
return ('cgi');
}
sub feature_get_web_php_mode
{
return 'cgi';
}
sub feature_save_web_php_mode
{
my ($d, $mode) = @_;
$mode eq 'cgi' || &error($text{'feat_ephpmode'});
}
# feature_list_web_php_directories(&domain)
# Only one version is supported in Nginx
# Taken form virtualmin-nginx
sub feature_list_web_php_directories
{
my ($d) = @_;
my ($defver) = &get_default_php_version();
return ( { 'dir' => &virtual_server::public_html_dir($d),
'mode' => 'cgi',
'version' => $defver } );
}
sub feature_restart_web_php {
system("$config{'fpm_reload'} >/dev/null 2>&1");
}
sub feature_restart_web {
system("$config{'nginx_reload_cmd'} >/dev/null 2>&1");
}
sub feature_restart_web_command {
return $config{'nginx_reload_cmd'};
}
sub feature_get_web_suexec {
return 1;
}
sub feature_inputs_show {
return 0;
}
=begin
sub feature_inputs {
@types= (
['dynamic', 'Dynamic'],
['static', 'Static'],
['ondemand', 'On demand']
);
# $result = &ui_table_row($text{'fpm_pm_type'}, &ui_select("fpm_pm_type", $types[0]->[0], \@types));
# $result.= &ui_table_row($text{'fpm_pm_max_children'}, &ui_textbox("fpm_pm_max_children", 5, 50));
# $result.= &ui_table_row($text{'fpm_pm_start_servers'}, &ui_textbox("fpm_pm_start_servers", 2, 50));
# $result.= &ui_table_row($text{'fpm_pm_min_spare_servers'}, &ui_textbox("fpm_pm_min_spare_servers", 1, 50));
# $result.= &ui_table_row($text{'fpm_pm_max_spare_servers'}, &ui_textbox("fpm_pm_max_spare_servers", 3, 50));
# $result.= &ui_table_row($text{'fpm_pm_max_requests'}, &ui_textbox("fpm_pm_max_requests", 500, 50));
$result = &ui_table_row($text{'nginx_ssl'}, &ui_checkbox("nginx_ssl", "nginx_ssl", undef, 0));
return $result;
}
sub feature_inputs_parse {
return undef;
}
=cut
sub feature_start_service {
system("$config{'nginx_start_cmd'} >/dev/null 2>&1");
system("$config{'fpm_start'} >/dev/null 2>&1");
}
sub feature_stop_service {
system("$config{'nginx_stop_cmd'} >/dev/null 2>&1");
system("$config{'fpm_stop'} >/dev/null 2>&1");
}
sub feature_hlink
{
return "index";
}