commit | author | age
|
b74ef5
|
1 |
<?php |
T |
2 |
|
|
3 |
/* |
5a43e7
|
4 |
Copyright (c) 2011-2012, Till Brehm, projektfarm Gmbh |
b74ef5
|
5 |
All rights reserved. |
T |
6 |
|
|
7 |
Redistribution and use in source and binary forms, with or without modification, |
|
8 |
are permitted provided that the following conditions are met: |
|
9 |
|
|
10 |
* Redistributions of source code must retain the above copyright notice, |
|
11 |
this list of conditions and the following disclaimer. |
|
12 |
* Redistributions in binary form must reproduce the above copyright notice, |
|
13 |
this list of conditions and the following disclaimer in the documentation |
|
14 |
and/or other materials provided with the distribution. |
|
15 |
* Neither the name of ISPConfig nor the names of its contributors |
|
16 |
may be used to endorse or promote products derived from this software without |
|
17 |
specific prior written permission. |
|
18 |
|
|
19 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
|
20 |
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
|
21 |
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
|
22 |
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
|
23 |
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
|
24 |
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
25 |
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
|
26 |
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
|
27 |
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
|
28 |
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
29 |
*/ |
|
30 |
|
|
31 |
class openvz_plugin { |
b1a6a5
|
32 |
|
b74ef5
|
33 |
var $plugin_name = 'openvz_plugin'; |
T |
34 |
var $class_name = 'openvz_plugin'; |
b1a6a5
|
35 |
|
b74ef5
|
36 |
//* This function is called during ispconfig installation to determine |
T |
37 |
// if a symlink shall be created for this plugin. |
|
38 |
function onInstall() { |
|
39 |
global $conf; |
b1a6a5
|
40 |
|
b74ef5
|
41 |
//* this is only true on openvz host servers, not in openvz guests |
T |
42 |
if(@file_exists('/proc/vz/version')) { |
|
43 |
return true; |
|
44 |
} else { |
|
45 |
return false; |
|
46 |
} |
b1a6a5
|
47 |
|
b74ef5
|
48 |
} |
b1a6a5
|
49 |
|
MC |
50 |
|
b74ef5
|
51 |
/* |
T |
52 |
This function is called when the plugin is loaded |
|
53 |
*/ |
b1a6a5
|
54 |
|
b74ef5
|
55 |
function onLoad() { |
T |
56 |
global $app; |
b1a6a5
|
57 |
|
b74ef5
|
58 |
/* |
T |
59 |
Register for the events |
|
60 |
*/ |
b1a6a5
|
61 |
|
b74ef5
|
62 |
//* Virtual machine |
b1a6a5
|
63 |
$app->plugins->registerEvent('openvz_vm_insert', $this->plugin_name, 'vm_insert'); |
MC |
64 |
$app->plugins->registerEvent('openvz_vm_update', $this->plugin_name, 'vm_update'); |
|
65 |
$app->plugins->registerEvent('openvz_vm_delete', $this->plugin_name, 'vm_delete'); |
|
66 |
|
5a43e7
|
67 |
//* Register for actions |
b1a6a5
|
68 |
$app->plugins->registerAction('openvz_start_vm', $this->plugin_name, 'actions'); |
MC |
69 |
$app->plugins->registerAction('openvz_stop_vm', $this->plugin_name, 'actions'); |
|
70 |
$app->plugins->registerAction('openvz_restart_vm', $this->plugin_name, 'actions'); |
|
71 |
$app->plugins->registerAction('openvz_create_ostpl', $this->plugin_name, 'actions'); |
|
72 |
|
|
73 |
|
|
74 |
|
b74ef5
|
75 |
} |
b1a6a5
|
76 |
|
MC |
77 |
|
|
78 |
function vm_insert($event_name, $data) { |
b74ef5
|
79 |
global $app, $conf; |
b1a6a5
|
80 |
|
b74ef5
|
81 |
$veid = intval($data['new']['veid']); |
b1a6a5
|
82 |
|
b74ef5
|
83 |
if($veid == 0) { |
b1a6a5
|
84 |
$app->log("VEID = 0, we stop here.", LOGLEVEL_WARN); |
b74ef5
|
85 |
return; |
T |
86 |
} |
b1a6a5
|
87 |
|
cc7a82
|
88 |
$tmp = $app->db->queryOneRecord("SELECT template_file FROM openvz_ostemplate WHERE ostemplate_id = ?", $data['new']['ostemplate_id']); |
b74ef5
|
89 |
$ostemplate = escapeshellcmd($tmp['template_file']); |
T |
90 |
unset($tmp); |
b1a6a5
|
91 |
|
b74ef5
|
92 |
//* Create the virtual machine |
T |
93 |
exec("vzctl create $veid --ostemplate $ostemplate"); |
b1a6a5
|
94 |
$app->log("Create OpenVZ VM: vzctl create $veid --ostemplate $ostemplate", LOGLEVEL_DEBUG); |
MC |
95 |
|
b74ef5
|
96 |
//* Write the configuration of the VM |
b1a6a5
|
97 |
file_put_contents('/etc/vz/conf/'.$veid.'.conf', $data['new']['config']); |
MC |
98 |
|
b74ef5
|
99 |
//* Start the VM |
T |
100 |
if($data['new']['active'] == 'y') { |
|
101 |
exec("vzctl start $veid"); |
b1a6a5
|
102 |
$app->log("Starting OpenVZ VM: vzctl start $veid", LOGLEVEL_DEBUG); |
b74ef5
|
103 |
} |
b1a6a5
|
104 |
|
b74ef5
|
105 |
//* Set the root password in the virtual machine |
T |
106 |
exec("vzctl set $veid --userpasswd root:".escapeshellcmd($data['new']['vm_password'])); |
b1a6a5
|
107 |
|
b74ef5
|
108 |
} |
b1a6a5
|
109 |
|
MC |
110 |
function vm_update($event_name, $data) { |
b74ef5
|
111 |
global $app, $conf; |
b1a6a5
|
112 |
|
b74ef5
|
113 |
$veid = intval($data['new']['veid']); |
b1a6a5
|
114 |
|
b74ef5
|
115 |
if($veid == 0) { |
b1a6a5
|
116 |
$app->log("VEID = 0, we stop here.", LOGLEVEL_WARN); |
b74ef5
|
117 |
return; |
T |
118 |
} |
b1a6a5
|
119 |
|
b74ef5
|
120 |
//* Write the configuration of the VM |
b1a6a5
|
121 |
file_put_contents('/etc/vz/conf/'.$veid.'.conf', $data['new']['config']); |
MC |
122 |
$app->log("Writing new configuration for $veid", LOGLEVEL_DEBUG); |
|
123 |
|
da9e99
|
124 |
//* new diskspace for ploop-containers requieres "vzctl set" |
FS |
125 |
if($data['new']['diskspace'] != $data['old']['diskspace']) { |
|
126 |
exec("vzctl set ".$veid." --diskspace ".$data['new']['diskspace']."G --save"); |
|
127 |
} |
|
128 |
|
b74ef5
|
129 |
//* Apply config changes to the VM |
T |
130 |
if($data['new']['active'] == 'y' && $data['old']['active'] == 'y') { |
|
131 |
exec("vzctl restart $veid"); |
b1a6a5
|
132 |
$app->log("Restarting OpenVZ VM: vzctl restart $veid", LOGLEVEL_DEBUG); |
b74ef5
|
133 |
} elseif ($data['new']['active'] == 'y' && $data['old']['active'] == 'n') { |
T |
134 |
exec("vzctl start $veid"); |
b1a6a5
|
135 |
$app->log("Starting OpenVZ VM: vzctl start $veid", LOGLEVEL_DEBUG); |
b74ef5
|
136 |
} elseif ($data['new']['active'] == 'n' && $data['old']['active'] == 'y') { |
T |
137 |
exec("vzctl stop $veid"); |
b1a6a5
|
138 |
$app->log("Stopping OpenVZ VM: vzctl stop $veid", LOGLEVEL_DEBUG); |
b74ef5
|
139 |
} |
b1a6a5
|
140 |
|
b74ef5
|
141 |
//* Set the root password in the virtual machine |
T |
142 |
if($data['new']['vm_password'] != $data['old']['vm_password']) { |
|
143 |
exec("vzctl set $veid --userpasswd root:".escapeshellcmd($data['new']['vm_password'])); |
|
144 |
} |
b1a6a5
|
145 |
|
MC |
146 |
|
b74ef5
|
147 |
} |
b1a6a5
|
148 |
|
MC |
149 |
function vm_delete($event_name, $data) { |
b74ef5
|
150 |
global $app, $conf; |
b1a6a5
|
151 |
|
b74ef5
|
152 |
$veid = intval($data['old']['veid']); |
b1a6a5
|
153 |
|
b74ef5
|
154 |
if($veid == 0) { |
b1a6a5
|
155 |
$app->log("VEID = 0, we stop here.", LOGLEVEL_WARN); |
b74ef5
|
156 |
return; |
T |
157 |
} |
b1a6a5
|
158 |
|
c5d17d
|
159 |
exec("vzctl stop $veid"); |
b74ef5
|
160 |
exec("vzctl destroy $veid"); |
b1a6a5
|
161 |
$app->log("Destroying OpenVZ VM: vzctl destroy $veid", LOGLEVEL_DEBUG); |
MC |
162 |
|
b74ef5
|
163 |
} |
b1a6a5
|
164 |
|
MC |
165 |
function actions($action_name, $data) { |
5a43e7
|
166 |
global $app, $conf; |
b1a6a5
|
167 |
|
5a43e7
|
168 |
if ($action_name == 'openvz_start_vm') { |
T |
169 |
$veid = intval($data); |
|
170 |
if($veid > 0) { |
|
171 |
exec("vzctl start $veid"); |
b1a6a5
|
172 |
$app->log("Start VM: vzctl start $veid", LOGLEVEL_DEBUG); |
5a43e7
|
173 |
} |
T |
174 |
return 'ok'; |
|
175 |
} |
|
176 |
if ($action_name == 'openvz_stop_vm') { |
|
177 |
$veid = intval($data); |
|
178 |
if($veid > 0) { |
|
179 |
exec("vzctl stop $veid"); |
b1a6a5
|
180 |
$app->log("Stop VM: vzctl stop $veid", LOGLEVEL_DEBUG); |
5a43e7
|
181 |
} |
T |
182 |
return 'ok'; |
|
183 |
} |
|
184 |
if ($action_name == 'openvz_restart_vm') { |
|
185 |
$veid = intval($data); |
|
186 |
if($veid > 0) { |
|
187 |
exec("vzctl restart $veid"); |
b1a6a5
|
188 |
$app->log("Restart VM: vzctl restart $veid", LOGLEVEL_DEBUG); |
5a43e7
|
189 |
} |
T |
190 |
return 'ok'; |
|
191 |
} |
|
192 |
if ($action_name == 'openvz_create_ostpl') { |
b1a6a5
|
193 |
$parts = explode(':', $data); |
5a43e7
|
194 |
$veid = intval($parts[0]); |
T |
195 |
$template_cache_dir = '/vz/template/cache/'; |
|
196 |
$template_name = escapeshellcmd($parts[1]); |
|
197 |
if($veid > 0 && $template_name != '' && is_dir($template_cache_dir)) { |
|
198 |
$command = "vzdump --suspend --compress --stdexcludes --dumpdir $template_cache_dir $veid"; |
|
199 |
exec($command); |
|
200 |
exec("mv ".$template_cache_dir."vzdump-openvz-".$veid."*.tgz ".$template_cache_dir.$template_name.".tar.gz"); |
|
201 |
exec("rm -f ".$template_cache_dir."vzdump-openvz-".$veid."*.log"); |
|
202 |
} |
b1a6a5
|
203 |
$app->log("Created OpenVZ OStemplate $template_name from VM $veid", LOGLEVEL_DEBUG); |
5a43e7
|
204 |
return 'ok'; |
T |
205 |
} |
b1a6a5
|
206 |
|
5a43e7
|
207 |
} |
b1a6a5
|
208 |
|
b74ef5
|
209 |
|
T |
210 |
} // end class |
|
211 |
|
|
212 |
?> |