forked from virtualmin/virtualmin-gpl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create-plan.pl
executable file
·176 lines (161 loc) · 5.38 KB
/
create-plan.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
#!/usr/local/bin/perl
=head1 create-plan.pl
Creates a new account plan for use with virtual servers.
This command allows you to create a new account plan, which defines limits
that can be applied to new or existing virtual servers. The only mandatory
parameter is C<--name>, which must be followed by a unique name for the plan
to create.
Quotas for virtual servers on the plan can be set with the C<--quota> or
C<--admin-quota> flags, followed by a quota in blocks (typically 1k in size).
By default, plan quotas are unlimited.
Restrictions on the number of virtual servers, mailboxes, aliases and databases
can be set with the C<--max-doms>, C<--max-mailbox>, C<--max-alias> and
C<--max-dbs> parameters, followed by a number. By default, all of these are
unlimited.
Allowed features for new virtual servers can be set with the C<--features> flag,
followed by a space-separated feature code list like I<web dns mail>. Similarly,
allowed editing capabilities can be set with C<--capabilities> followed by
a list of codes like I<domain users aliases>. In both cases, the lists must
be a single quoted parameter.
Scripts that virtual servers on the plan can install can be restricted by
the C<--scripts> flag, followed by a quoted list of script codes. To find
available codes, use the C<list-available-scripts> API command.
To create a plan that is owned by a reseller, use the C<--owner> flag followed
by an existing reseller name. To limit use of the plan to only some resellers,
use C<--resellers> followed by a list of reseller names. Or use
C<--no-resellers> to prevent any resellers from seeing it.
=cut
package virtual_server;
if (!$module_name) {
$main::no_acl_check++;
$ENV{'WEBMIN_CONFIG'} ||= "/etc/webmin";
$ENV{'WEBMIN_VAR'} ||= "/var/webmin";
if ($0 =~ /^(.*)\/[^\/]+$/) {
chdir($pwd = $1);
}
else {
chop($pwd = `pwd`);
}
$0 = "$pwd/create-plan.pl";
require './virtual-server-lib.pl';
$< == 0 || die "create-plan.pl must be run as root";
}
@OLDARGV = @ARGV;
# Parse command-line args
$plan = { };
while(@ARGV > 0) {
local $a = shift(@ARGV);
if ($a eq "--name") {
$plan->{'name'} = shift(@ARGV);
}
elsif ($a eq "--owner") {
$plan->{'owner'} = shift(@ARGV);
&get_reseller($plan->{'owner'}) ||
&usage("Reseller owner $plan->{'owner'} does not exisst");
}
elsif ($a eq "--quota" || $a eq "--admin-quota") {
# Some quota
$q = shift(@ARGV);
$q =~ /^\d+$/ ||
&usage("$a must be followed by a quota in blocks");
$f = $a eq "--quota" ? "quota" : "uquota";
$plan->{$f} = $q;
}
elsif ($a =~ /^\-\-max\-(\S+)$/ && &indexof($1, @plan_maxes) >= 0) {
# Some limit on domains / etc
$l = $1; $q = shift(@ARGV);
$q =~ /^\d+$/ ||
&usage("$a must be followed by a numeric limit");
$plan->{$l.'limit'} = $q;
}
elsif ($a =~ /^\-\-(\S+)$/ && &indexof($1, @plan_restrictions) >= 0) {
# No db name or other binary limit
$plan->{$1} = 1;
}
elsif ($a eq "--features") {
# Allowed features
@fl = split(/\s+/, shift(@ARGV));
@allf = ( @opt_features, "virt", &list_feature_plugins() );
foreach $f (@fl) {
&indexof($f, @allf) >= 0 ||
&usage("Unknown feature $f - allowed options ".
"are : ".join(" ", @allf));
}
$plan->{'featurelimits'} = join(" ", @fl);
}
elsif ($a eq "--no-features") {
$plan->{'featurelimits'} = 'none';
}
elsif ($a eq "--capabilities") {
# Edit capabilities
@cl = split(/\s+/, shift(@ARGV));
foreach $c (@cl) {
&indexof($c, @edit_limits) >=0 ||
&usage("Unknown capability $c - allowed options ".
"are : ".join(" ", @edit_limits));
}
$plan->{'capabilities'} = join(" ", @cl);
}
elsif ($a eq "--scripts") {
# Allowed scripts
@sc = split(/\s+/, shift(@ARGV));
foreach $s (@sc) {
&get_script($s) ||
&usage("Unknown script code $s");
}
$plan->{'scripts'} = join(" ", @sc);
}
elsif ($a eq "--no-resellers") {
$plan->{'resellers'} = 'none';
}
elsif ($a eq "--resellers") {
@rl = split(/\s+/, shift(@ARGV));
foreach $r (@rl) {
&get_reseller($r) || &usage("Unknown reseller $r");
}
$plan->{'resellers'} = join(" ", @rl);
}
elsif ($a eq "--multiline") {
$multiline = 1;
}
elsif ($a eq "--help") {
&usage();
}
else {
&usage("Unknown parameter $a");
}
}
# Validate vital parameters
$plan->{'name'} || &usage("Missing --name flag");
($clash) = grep { lc($_->{'name'}) eq lc($plan->{'name'}) } &list_plans();
$clash && &usage("A plan name $plan->{'name'} already exists");
# Create it
&save_plan($plan);
print "Created plan $plan->{'name'} with ID $plan->{'id'}\n";
&run_post_actions_silently();
&virtualmin_api_log(\@OLDARGV);
sub usage
{
print $_[0],"\n\n" if ($_[0]);
print "Creates a new Virtualmin account plan with the given limits.\n";
print "\n";
print "virtualmin create-plan --name plan-name\n";
print " [--owner reseller]\n";
print " [--quota blocks]\n";
print " [--admin-quota blocks]\n";
foreach $l (@plan_maxes) {
print " [--max-$l limit]\n";
}
foreach $r (@plan_restrictions) {
print " [--$r]\n";
}
print " [--features \"web dns mail ...\" | --no-features]\n";
print " [--capabilities \"domain users aliases ...\"]\n";
if (defined(&list_scripts)) {
print " [--scripts \"name name ...\"]\n";
}
if (defined(&list_resellers)) {
print " [--no-resellers | --resellers \"name name..\"]\n";
}
exit(1);
}