Marius Cramer
2015-08-06 37b29231e47a0c4458dc1c15d98588f16f07e1e2
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
b74ef5 124         //* Apply config changes to the VM
T 125         if($data['new']['active'] == 'y' && $data['old']['active'] == 'y') {
126             exec("vzctl restart $veid");
b1a6a5 127             $app->log("Restarting OpenVZ VM: vzctl restart $veid", LOGLEVEL_DEBUG);
b74ef5 128         } elseif ($data['new']['active'] == 'y' && $data['old']['active'] == 'n') {
T 129             exec("vzctl start $veid");
b1a6a5 130             $app->log("Starting OpenVZ VM: vzctl start $veid", LOGLEVEL_DEBUG);
b74ef5 131         } elseif ($data['new']['active'] == 'n' && $data['old']['active'] == 'y') {
T 132             exec("vzctl stop $veid");
b1a6a5 133             $app->log("Stopping OpenVZ VM: vzctl stop $veid", LOGLEVEL_DEBUG);
b74ef5 134         }
b1a6a5 135
b74ef5 136         //* Set the root password in the virtual machine
T 137         if($data['new']['vm_password'] != $data['old']['vm_password']) {
138             exec("vzctl set $veid --userpasswd root:".escapeshellcmd($data['new']['vm_password']));
139         }
b1a6a5 140
MC 141
b74ef5 142     }
b1a6a5 143
MC 144     function vm_delete($event_name, $data) {
b74ef5 145         global $app, $conf;
b1a6a5 146
b74ef5 147         $veid = intval($data['old']['veid']);
b1a6a5 148
b74ef5 149         if($veid == 0) {
b1a6a5 150             $app->log("VEID = 0, we stop here.", LOGLEVEL_WARN);
b74ef5 151             return;
T 152         }
b1a6a5 153
c5d17d 154         exec("vzctl stop $veid");
b74ef5 155         exec("vzctl destroy $veid");
b1a6a5 156         $app->log("Destroying OpenVZ VM: vzctl destroy $veid", LOGLEVEL_DEBUG);
MC 157
b74ef5 158     }
b1a6a5 159
MC 160     function actions($action_name, $data) {
5a43e7 161         global $app, $conf;
b1a6a5 162
5a43e7 163         if ($action_name == 'openvz_start_vm') {
T 164             $veid = intval($data);
165             if($veid > 0) {
166                 exec("vzctl start $veid");
b1a6a5 167                 $app->log("Start VM: vzctl start $veid", LOGLEVEL_DEBUG);
5a43e7 168             }
T 169             return 'ok';
170         }
171         if ($action_name == 'openvz_stop_vm') {
172             $veid = intval($data);
173             if($veid > 0) {
174                 exec("vzctl stop $veid");
b1a6a5 175                 $app->log("Stop VM: vzctl stop $veid", LOGLEVEL_DEBUG);
5a43e7 176             }
T 177             return 'ok';
178         }
179         if ($action_name == 'openvz_restart_vm') {
180             $veid = intval($data);
181             if($veid > 0) {
182                 exec("vzctl restart $veid");
b1a6a5 183                 $app->log("Restart VM: vzctl restart $veid", LOGLEVEL_DEBUG);
5a43e7 184             }
T 185             return 'ok';
186         }
187         if ($action_name == 'openvz_create_ostpl') {
b1a6a5 188             $parts = explode(':', $data);
5a43e7 189             $veid = intval($parts[0]);
T 190             $template_cache_dir = '/vz/template/cache/';
191             $template_name = escapeshellcmd($parts[1]);
192             if($veid > 0 && $template_name != '' && is_dir($template_cache_dir)) {
193                 $command = "vzdump --suspend --compress --stdexcludes --dumpdir $template_cache_dir $veid";
194                 exec($command);
195                 exec("mv ".$template_cache_dir."vzdump-openvz-".$veid."*.tgz ".$template_cache_dir.$template_name.".tar.gz");
196                 exec("rm -f ".$template_cache_dir."vzdump-openvz-".$veid."*.log");
197             }
b1a6a5 198             $app->log("Created OpenVZ OStemplate $template_name from VM $veid", LOGLEVEL_DEBUG);
5a43e7 199             return 'ok';
T 200         }
b1a6a5 201
5a43e7 202     }
b1a6a5 203
b74ef5 204
T 205 } // end class
206
207 ?>