diff --git a/.gitattributes b/.gitattributes index 6f7cb903..f24dcf29 100644 --- a/.gitattributes +++ b/.gitattributes @@ -599,6 +599,7 @@ src/functions.sh -text src/functions_dns.sh -text src/functions_hosting.sh -text src/inotify_update_domains.sh -text +src/lxc_stopexpired.php -text src/mail_dodelete.php -text src/mem_add -text src/mem_del -text diff --git a/bureau/class/m_lxc.php b/bureau/class/m_lxc.php index 04a4bdac..4d72636c 100644 --- a/bureau/class/m_lxc.php +++ b/bureau/class/m_lxc.php @@ -14,6 +14,7 @@ class m_lxc implements vm $this->IP = variable_get('lxc_ip', '', "IP address of the Alternc's LXC server. If empty, no LXC server."); $this->PORT = variable_get('lxc_port', '6504', "Port of the Alternc's LXC server"); $this->KEY = variable_get('lxc_key', '', "Shared key with the Alternc's LXC server"); + $this->maxtime = variable_get('lxc_maxtime', '4', "How many hours do we allow to have a server before shutting it down"); } @@ -37,6 +38,29 @@ class m_lxc implements vm return true; } + // Stop VMs running since more than MAXTIME hours + function stop_expired($force=false) { + global $mem,$err; + # Need to be distinct of $db (calling subfunctions) + $db2 = new DB_system(); + + if (! $mem->checkright() ) { + $err->raise("lxc",_("-- Only administrators can do that! --")); + return false; + } + + # If force, maxtime = 0 + $time = ($force?0:$this->maxtime); + + $db2->query("select * from vm_history where date_end is null and date_start < subdate( now() , interval $time hour);"); + + while ($db2->next_record()) { + $mem->su($db2->Record['uid']); + $this->stop(); + $mem->unsu(); + } + } + private function sendMessage($params) { $fp = fsockopen($this->IP, $this->PORT, $errno, $errstr, $this->TIMEOUT); @@ -121,20 +145,32 @@ class m_lxc implements vm public function getvm() { - global $db, $mem; + global $db, $mem, $cuid; - $uid = $mem->user['uid']; - $res = array(); + $db->query("SELECT * FROM vm_history WHERE date_end IS NULL AND uid= $cuid ORDER BY id DESC LIMIT 1"); - $res = $db->query("SELECT * FROM vm_history WHERE date_end IS NULL AND uid= '$uid' ORDER BY id DESC LIMIT 1"); - - if ($db->next_record()) - { + if ($db->next_record()){ $db->Record['serialized_object'] = unserialize($db->Record['serialized_object']); return $db->Record; } - else + else { return FALSE; + } + } + + # Stop all VMs + public function stopall() { + global $mem, $db, $err; + + if (! $mem->checkright() ) { + $err->raise("lxc",_("-- Only administrators can do that! --")); + return false; + } + + if ($this->sendMessage(array('action' => 'stopall' )) === FALSE) + return FALSE; + + return $db->query("UPDATE vm_history SET date_end = NOW() WHERE date_end is null;"); } public function stop() diff --git a/debian/alternc.cron.d b/debian/alternc.cron.d index 1c8c3b84..1cd1a666 100644 --- a/debian/alternc.cron.d +++ b/debian/alternc.cron.d @@ -26,3 +26,6 @@ # Every 30 minutes, do cron_users actions 00,30 * * * * alterncpanel /usr/lib/alternc/cron_users.sh + +# Every hour, stop expired VMs +10 * * * * alterncpanel /usr/lib/alternc/lxc_stopexpired.php diff --git a/src/Makefile b/src/Makefile index b5b92ea7..60555b76 100644 --- a/src/Makefile +++ b/src/Makefile @@ -19,7 +19,7 @@ # ---------------------------------------------------------------------- # Purpose of file: Makefile des binaires de /usr/lib/alternc # ---------------------------------------------------------------------- -SCRIPTS=sqlbackup.sh quota_init quota_delete update_domains.sh slave_dns sendmail spoolsize.php fixperms.sh alternc-dboptimize export_account.php cron_users_doit.sh cron_users.sh compress_logs.sh delete_logs.sh quota-warning.sh update_mails.sh alternc_add_policy_dovecot rebuild_all_webconf.sh courier-dovecot-migrate.pl popimap-log-login.sh mem_add mem_del quota_edit quota_get du.pl update_quota_mail.sh inotify_update_domains.sh functions.sh functions_hosting.sh functions_dns.sh mail_dodelete.php procmail_to_sieve.php +SCRIPTS=sqlbackup.sh quota_init quota_delete update_domains.sh slave_dns sendmail spoolsize.php fixperms.sh alternc-dboptimize export_account.php cron_users_doit.sh cron_users.sh compress_logs.sh delete_logs.sh quota-warning.sh update_mails.sh alternc_add_policy_dovecot rebuild_all_webconf.sh courier-dovecot-migrate.pl popimap-log-login.sh mem_add mem_del quota_edit quota_get du.pl update_quota_mail.sh inotify_update_domains.sh functions.sh functions_hosting.sh functions_dns.sh mail_dodelete.php procmail_to_sieve.php lxc_stopexpired.php BIN=$(DESTDIR)/usr/lib/alternc/ install: diff --git a/src/lxc_stopexpired.php b/src/lxc_stopexpired.php new file mode 100755 index 00000000..67eaea45 --- /dev/null +++ b/src/lxc_stopexpired.php @@ -0,0 +1,38 @@ +#!/usr/bin/php -q +setid(2000); +$admin->enabled=1; + +# Stop expired vms +$lxc->stop_expired();