m_lxc.php 5.56 KB
Newer Older
1 2
<?php

3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/* 
 ----------------------------------------------------------------------
 AlternC - Web Hosting System
 Copyright (C) 2000-2013 by the AlternC Development Team.
 https://alternc.org/
 ----------------------------------------------------------------------
 LICENSE

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License (GPL)
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 To read the license please visit http://www.gnu.org/copyleft/gpl.html
 ----------------------------------------------------------------------
 Purpose of file: Manage LXC-based virtual machine through an inetd-based protocol
 ----------------------------------------------------------------------
*/

François Serman's avatar
François Serman committed
27
include_once(dirname(__FILE__) . '/vm.class.php');
28

29 30 31 32 33 34 35

/**
 * Manage AlternC's virtual machine start/stop using our own inetd-based protocol.
 */
class m_lxc implements vm {


36
  public $IP;
Alan Garcia's avatar
Alan Garcia committed
37
  public $KEY;
38
  public $PORT;
Alan Garcia's avatar
Alan Garcia committed
39
  public $maxtime;
40 41 42
  public $TIMEOUT = 5;
  public $error = array();

43 44 45 46

  /** 
   * Constructor, initialize the class informations from AlternC's variables
   */
47
  function m_lxc() {
Alan Garcia's avatar
Alan Garcia committed
48 49 50 51
    $this->IP   = variable_get('lxc_ip', '', "IP address of the Alternc's LXC server. If empty, no LXC server.", array('desc'=>'IP address','type'=>'ip'));
    $this->PORT = variable_get('lxc_port', '6504', "Port of the Alternc's LXC server", array('desc'=>'Port','type'=>'integer'));
    $this->KEY  = variable_get('lxc_key', '', "Shared key with the Alternc's LXC server", array('desc'=>'Shared key','type'=>'string'));
    $this->maxtime = variable_get('lxc_maxtime', '4', "How many hours do we allow to have a server before shutting it down", array('desc'=>'Max time','type'=>'integer'));
52 53 54
  }


55 56 57
  /**
   * HOOK: add the "Console Access" to AlternC's main menu
   */
58
  function hook_menu() {
59
    if ( empty($this->IP)) return ; // No menu if no server
60 61

    $obj = array(
62
      'title'       => _("Console access"),
63 64 65 66 67 68 69 70
      'ico'         => 'images/ssh.png',
      'link'        => 'vm.php',
      'pos'         => 95,
     ) ;

     return $obj;
  }

71 72 73 74

  /**
   * HOOK: remove VM history for AlternC account
   */
75 76 77 78 79 80 81
  function hook_admin_del_member() {
    global $db,$err,$cuid;
    $err->log("lxc","alternc_del_member");
    $db->query("DELETE FROM vm_history WHERE uid='$cuid'");
    return true;
  }

82

83 84 85 86 87 88 89
  /** 
   * Send a message to a remote VM manager instance
   * $params are the parameters to send as serialized data
   * to the listening server. 
   * Return the unserialized response data, if the message has been sent successfully
   * or FALSE if an error occurred. In that case $error[] is set.
   */
90
  private function sendMessage($params) {
91
    global $L_FQDN,$hooks;
92
    $fp = @fsockopen($this->IP, $this->PORT, $errno, $errstr, $this->TIMEOUT);
93
    if (!$fp) {
94 95 96
      $this->error[] = 'Unable to connect';
      return FALSE;
    }
97 98 99
    // Authenticate:
    $params['server']=$L_FQDN;
    $params['key']=$this->KEY;
100 101 102 103 104 105
    // MySQL Host for this user ? 
    $moreparams=$hooks->invoke("lxc_params",array($params));
    foreach($moreparams as $p) {
      foreach($p as $k=>$v) 
	$params[$k]=$v;
    }
106

107 108
    $msg = serialize($params);
    if (fwrite ($fp, $msg."\n") < 0) {
109 110 111
      $this->error[] = 'Unable to send data';
      return FALSE;
    }
112
    $resp = fgets($fp, 8192);
113 114
    fclose ($fp);

115
    $data = @unserialize($resp);
116
  
117
    if (isset($data['error']) && $data['error']>0) {
118 119
      $this->error[] = $data['msg'];
      return FALSE;
120
    } else {
121 122 123 124
      return $resp;
    }
  }

125 126 127 128 129 130

  /** 
   * START a Virtual Machine on the remote VM manager
   * for user $login having hashed password $pass and uid $uid
   */
  public function start($login = FALSE, $pass = FALSE, $uid = FALSE) {
131 132
    global $mem, $db, $err, $mysql;

133
    if ($this->getvm() !== FALSE) {
134 135 136
      $err->raise('lxc', _('VM already started'));
      return FALSE;
    }
Benjamin Sonntag's avatar
Benjamin Sonntag committed
137
    unset($this->error);
138 139 140 141 142 143 144 145 146

    $login = $login ? $login : $mem->user['login'];
    $pass  = $pass  ? $pass  : $mem->user['pass'];
    $uid   = $uid   ? $uid   : $mem->user['uid'];

    $msgg = array('action'=>'start', 'login'=>$login, 'pass' => $pass, 'uid'=> $uid);  
    $msgg['mysql_host'] = $mysql->dbus->Host;

    $res = $this->sendMessage($msgg);
147
    if ($res === FALSE) {
148
      return $this->error;
149
    } else {
150
      $data = unserialize($res);
Alan Garcia's avatar
Alan Garcia committed
151
      $error = (int)$data['error'];
152 153 154 155 156
      $hostname = $data['hostname'];
      $msg = $data['msg'];
      $date_start = 'NOW()';
      $uid = $mem->user['uid'];

Alan Garcia's avatar
Alan Garcia committed
157 158
      if ($error != 0) {
        $err->raise('lxc', _($msg));
159 160 161 162 163 164 165 166
        return FALSE;
      }
      $db->query("INSERT INTO vm_history (ip,date_start,uid,serialized_object) VALUES ('$hostname', $date_start, '$uid', '$res')");
      return $res;
    }
  }


167 168 169 170
  /**
   * 
   */
  public function getvm($login = FALSE) {
Alan Garcia's avatar
Alan Garcia committed
171
    global $db, $mem, $cuid;
172

173 174 175 176 177
    $login = $login ? $login : $mem->user['login'];
    $msgg = array('action'=>'get', 'login'=>$login);  
    $res = $this->sendMessage($msgg);
    if (!$res) return FALSE;
    return unserialize($res);
Alan Garcia's avatar
Alan Garcia committed
178 179
  }

180

181 182 183 184
  /** 
   * Stop the currently running VM
   */
  public function stop() {
185 186 187 188 189
    global $db, $mem;
    $vm = $this->getvm();
    if ($vm === FALSE)
      return FALSE;

190 191 192
    if ($this->sendMessage(array('action' => 'stop', 'vm' => $vm['vm'])) === FALSE)
      return FALSE;
    return TRUE;
193
  }
194 195 196 197



} // class m_lxc