From 61e08e761ef6f29cbc230c54f7e67014b79518aa Mon Sep 17 00:00:00 2001 From: Florian Schaal <florian@schaal-24.de> Date: Tue, 20 Oct 2015 07:27:11 -0400 Subject: [PATCH] openvz: free all ip-addresses if the vm was deleted --- interface/lib/plugins/vm_openvz_plugin.inc.php | 42 +++++++++++++++++++++++++++++++++++++----- 1 files changed, 37 insertions(+), 5 deletions(-) diff --git a/interface/lib/plugins/vm_openvz_plugin.inc.php b/interface/lib/plugins/vm_openvz_plugin.inc.php index 73cc9cd..5f6e3bb 100644 --- a/interface/lib/plugins/vm_openvz_plugin.inc.php +++ b/interface/lib/plugins/vm_openvz_plugin.inc.php @@ -60,6 +60,14 @@ // Set the IP address $app->db->query("UPDATE openvz_ip SET vm_id = ? WHERE ip_address = ?", $this->id, $this->dataRecord['ip_address']); + // Set additional IPs + if (isset($this->dataRecord['additional_ip'])) { + $app->db->query("UPDATE openvz_ip SET vm_id = 0, additional = 'n' WHERE vm_id = ? AND additional='y'", $this->id); + foreach ($this->dataRecord['additional_ip'] as $idx => $rec) { + $app->db->query("UPDATE openvz_ip SET vm_id = ?, additional = 'y' WHERE ip_address = ?", $this->id, $rec); + } + } + // Create the OpenVZ config file and store it in config field $this->makeOpenVZConfig(); @@ -94,7 +102,18 @@ } // Set the IP address - if(isset($this->dataRecord['ip_address'])) $app->db->query("UPDATE openvz_ip SET vm_id = ? WHERE ip_address = ?", $this->id, $this->dataRecord['ip_address']); + if(isset($this->dataRecord['ip_address'])) { + $app->db->query("UPDATE openvz_ip SET vm_id = 0 WHERE vm_id = ? AND additional='n'", $this->id); + $app->db->query("UPDATE openvz_ip SET vm_id = ?, additional = 'n' WHERE ip_address = ?", $this->id, $this->dataRecord['ip_address']); + } + + // Set additional IPs + if (isset($this->dataRecord['additional_ip'])) { + $app->db->query("UPDATE openvz_ip SET vm_id = 0, additional = 'n' WHERE (vm_id = ? AND additional='y')", $this->id); + foreach ($this->dataRecord['additional_ip'] as $idx => $rec) { + $app->db->query("UPDATE openvz_ip SET vm_id = ?, additional = 'y' WHERE ip_address = ?", $this->id, $rec); + } + } // Create the OpenVZ config file and store it in config field $this->makeOpenVZConfig(); @@ -111,10 +130,12 @@ global $app, $conf; //* Free the IP address - $tmp = $app->db->queryOneRecord("SELECT ip_address_id FROM openvz_ip WHERE vm_id = ?", $page_form->id); - $app->db->datalogUpdate('openvz_ip', array('vm_id' => 0), 'ip_address_id', $tmp['ip_address_id']); + $tmp_rec = $app->db->queryAllRecords("SELECT ip_address_id FROM openvz_ip WHERE vm_id = ?", $page_form->id); + foreach ($tmp_rec as $tmp) { + $app->db->datalogUpdate('openvz_ip', array('vm_id' => 0), 'ip_address_id', $tmp['ip_address_id']); + } unset($tmp); - + unset($tmp_rec); } private function applyTemplate() { @@ -192,6 +213,17 @@ $hostname = str_replace('{VEID}', $vm['veid'], $vm['hostname']); $tpl->setVar('hostname', $hostname); + + $additional_ips = $app->db->queryAllRecords("SELECT * FROM openvz_ip WHERE vm_id = ?",$this->id); + if (isset($additional_ips)) { + $vm['ip_address']=''; + foreach ($additional_ips as $ip) { + $vm['ip_address'] .= " ".$ip['ip_address']; + } + $vm['ip_address'] = substr($vm['ip_address'],1); + } + $tpl->setVar('ip_address', $vm['ip_address']); + $tpl->setVar('ip_address', $vm['ip_address']); $tpl->setVar('nameserver', $vm['nameserver']); $tpl->setVar('capability', $vm['capability']); @@ -249,7 +281,7 @@ "server_id" => $server_id, "zone" => $dns_soa_id, "name" => $hostname, - "type" => 'A', + "type" => @(preg_match("/^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/", $ip_address))?'A':'AAAA', "data" => $ip_address, "aux" => '0', "ttl" => '3600', -- Gitblit v1.9.1