- remove multiserver-stuff from current-trunk

- fix FroxlorSshTransport
This commit is contained in:
Michael Kaufmann (d00p)
2010-10-24 19:16:26 +00:00
parent 0be29498b0
commit 58b3a19ae7
6 changed files with 0 additions and 969 deletions

View File

@@ -1,107 +0,0 @@
<?php
/**
* This file is part of the Froxlor project.
* Copyright (c) 2010 the Froxlor Team (see authors).
*
* For the full copyright and license information, please view the COPYING
* file that was distributed with this source code. You can also view the
* COPYING file online at http://files.froxlor.org/misc/COPYING.txt
*
* @copyright (c) the authors
* @author Froxlor team <team@froxlor.org> (2010-)
* @license GPLv2 http://files.froxlor.org/misc/COPYING.txt
* @package Settings
* @version $Id$
*/
return array(
'groups' => array(
'froxlorclient' => array(
'title' => $lng['admin']['froxlorclient'],
'fields' => array(
'froxlorclient_ipaddress' => array(
'label' => $lng['froxlorclient']['ipaddress'],
'settinggroup' => 'client',
'varname' => 'ipaddress',
'type' => 'string',
'default' => '',
/* 'plausibility_check_method' => 'validateIP', */
'save_method' => 'storeSettingField',
),
'froxlorclient_hostname' => array(
'label' => $lng['froxlorclient']['hostname'],
'settinggroup' => 'client',
'varname' => 'hostname',
'type' => 'string',
'default' => '',
'save_method' => 'storeSettingField'
),
'froxlorclient_deploy_mode' => array(
'label' => $lng['froxlorclient']['deploy_mode'],
'settinggroup' => 'client',
'varname' => 'deploy_mode',
'type' => 'option',
'default' => 'pubkey',
'option_mode' => 'one',
'option_options' => array('pubkey' => 'PublicKey', 'plainpass' => 'Passphrase'),
'save_method' => 'storeSettingField',
),
'froxlorclient_ssh_port' => array(
'label' => $lng['froxlorclient']['ssh_port'],
'settinggroup' => 'client',
'varname' => 'ssh_port',
'type' => 'int',
'int_min' => 1,
'int_max' => 65535,
'default' => 22,
'save_method' => 'storeSettingField',
),
'froxlorclient_ssh_user' => array(
'label' => $lng['froxlorclient']['ssh_user'],
'settinggroup' => 'client',
'varname' => 'ssh_user',
'type' => 'string',
'default' => '',
'save_method' => 'storeSettingField'
),
'froxlorclient_ssh_passphrase' => array(
'label' => $lng['froxlorclient']['ssh_passphrase'],
'settinggroup' => 'client',
'varname' => 'ssh_passphrase',
'type' => 'string',
'default' => '',
'save_method' => 'storeSettingField'
),
'froxlorclient_ssh_pubkey' => array(
'label' => $lng['froxlorclient']['ssh_pubkey'],
'settinggroup' => 'client',
'varname' => 'ssh_pubkey',
'type' => 'string',
'string_type' => 'file',
'default' => '',
'string_emptyallowed' => true,
'save_method' => 'storeSettingField'
),
'froxlorclient_ssh_privkey' => array(
'label' => $lng['froxlorclient']['ssh_privkey'],
'settinggroup' => 'client',
'varname' => 'ssh_privkey',
'type' => 'string',
'string_type' => 'file',
'default' => '',
'string_emptyallowed' => true,
'save_method' => 'storeSettingField'
),
'froxlorclient_install_destination' => array(
'label' => $lng['froxlorclient']['install_destination'],
'settinggroup' => 'client',
'varname' => 'install_destination',
'type' => 'string',
'default' => '/usr/share/froxlor/',
'save_method' => 'storeSettingField'
),
)
)
)
);

View File

@@ -1,105 +0,0 @@
<?php
/**
* Deploy File Creator Class
*
* This class creates the deploy file.
*
* PHP version 5
*
* This file is part of the Froxlor project.
* Copyright (c) 2010 the Froxlor Team (see authors).
*
* For the full copyright and license information, please view the COPYING
* file that was distributed with this source code. You can also view the
* COPYING file online at http://files.froxlor.org/misc/COPYING.txt
*
* @category FroxlorCore
* @package Classes
* @subpackage System
* @author Froxlor Team <team@froxlor.org>
* @copyright 2010 the authors
* @license GPLv2 http://files.froxlor.org/misc/COPYING.txt
* @version SVN: $Id$
* @link http://www.froxlor.org/
*/
/**
* Class FroxlorDeployfileCreator
*
* This class creates the deploy file.
*
* @category FroxlorCore
* @package Classes
* @subpackage System
* @author Froxlor Team <team@froxlor.org>
* @copyright 2010 the authors
* @license GPLv2 http://files.froxlor.org/misc/COPYING.txt
* @link http://www.froxlor.org/
*/
class FroxlorDeployfileCreator
{
/**
* Contains the file listing.
*
* @var array
*/
public static $_list = null;
/**
* Excluded dirs, seperated with | (for RegEx)
*
* @var string
*/
public static $_exclude = "userdata.inc.php|navigation|configfiles";
/**
* This function iterates through the $dir and generates the deploy list.
*
* @param array $dir dir to deploy
*
* @return array file listing
*/
public static function createList($dirList)
{
$list = array();
foreach ($dirList as $dir) {
if (is_dir($dir)) {
$its = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($dir)
);
foreach ($its as $fullFileName => $it ) {
if (!preg_match("/(".self::$_exclude.")/i", $fullFileName)) {
$list[] = $fullFileName;
}
}
} else {
throw new Exception($dir." is not a directory!");
}
}
self::$_list = $list;
return $list;
}
/**
* This function saves the deploy list to a file.
*
* @param string $toPath file path
* @param array $list array list with all needed files
*
* @return boolean
*/
public static function saveListTo($toPath, $list = null)
{
if (is_null($list)) {
$list = self::$_list;
}
return file_put_contents($toPath, implode("\n", $list));
}
}

View File

@@ -1,176 +0,0 @@
<?php
/**
* Package Creator Class
*
* This class creates packages to send over ssh.
*
* PHP version 5
*
* This file is part of the Froxlor project.
* Copyright (c) 2010 the Froxlor Team (see authors).
*
* For the full copyright and license information, please view the COPYING
* file that was distributed with this source code. You can also view the
* COPYING file online at http://files.froxlor.org/misc/COPYING.txt
*
* @category FroxlorCore
* @package Classes
* @subpackage System
* @author Froxlor Team <team@froxlor.org>
* @copyright 2010 the authors
* @license GPLv2 http://files.froxlor.org/misc/COPYING.txt
* @version SVN: $Id$
* @link http://www.froxlor.org/
*/
/**
* Class FroxlorPkgCreator
*
* This class creates packages to send over ssh.
*
* @category FroxlorCore
* @package Classes
* @subpackage System
* @author Froxlor Team <team@froxlor.org>
* @copyright 2010 the authors
* @license GPLv2 http://files.froxlor.org/misc/COPYING.txt
* @link http://www.froxlor.org/
*/
class FroxlorPkgCreator
{
/**
* Path to save file.
*
* @var string
*/
private $_path = null;
/**
* Contains the file-list as an array.
*
* @var array
*/
private $_config = array();
/**
* Manual added files.
* Contains an array with an array (name, data);
*
* @var array
*/
private $_manualFiles = array();
/**
* Constructor.
*
* @param string $incListPath contains the path to include-configuration
* @param string $toPath path where the package is saved
*/
public function __construct($incListPath, $toPath)
{
$this->_path = $toPath;
// load the config
$this->_config = $this->_readConfig($incListPath);
// parse the config
if (!$this->_checkConfig()) {
throw new Exception("Error in FroxlorPkgCreator::_checkConfig()");
}
}
/**
* Loads the config to an array.
*
* @param string $path path to inc-list
*
* @return array pathes to files
*/
private function _readConfig($path)
{
$arr = array();
if (is_readable($path)) {
$arr = file($path);
}
return $arr;
}
/**
* This function checks the files for readability and prohibted files.
*
* @return boolean check result
*/
private function _checkConfig()
{
foreach ($this->_config as $key => $var) {
if (strstr($var, "userdata.inc")) {
// delete this entry
unset($this->_config[$key]);
} else {
$this->_config[$key] = trim($var);
}
}
// no items, can't pack them
if (count($this->_config) == 0) {
return false;
}
// everything checked
return true;
}
/**
* Adds a "file".
*
* @param string $name filename (containg path like lib/userdata.inc.php)
* @param string $data data to write
*/
public function addFile($name, $data)
{
$this->_manualFiles[] = array(
'name' => $name,
'data' => $data
);
}
/**
* This functions creates the package.
*
* @param string $toPath the path where this file should be saved
*
* @return string path
*/
public function pack()
{
$toPath = $this->_path;
$zip = new ZipArchive;
// create archive
if ($zip->open($toPath, ZIPARCHIVE::OVERWRITE)) {
// write data
foreach ($this->_config as $var) {
$name = str_replace("froxlor/", "", strstr($var, "froxlor/"));
$zip->addFile($var, $name);
}
// add manual files
foreach ($this->_manualFiles as $var) {
$zip->addFromString($var['name'], $var['data']);
}
// close it
$zip->close();
return true;
}
// return
return false;
}
}
?>

View File

@@ -1,315 +0,0 @@
<?php
/**
* ssh Transport class
*
* This class handles remote server related stuff.
*
* PHP version 5
*
* This file is part of the Froxlor project.
* Copyright (c) 2010 the Froxlor Team (see authors).
*
* For the full copyright and license information, please view the COPYING
* file that was distributed with this source code. You can also view the
* COPYING file online at http://files.froxlor.org/misc/COPYING.txt
*
* @category FroxlorCore
* @package Classes
* @subpackage System
* @author Froxlor Team <team@froxlor.org>
* @copyright 2010 the authors
* @license GPLv2 http://files.froxlor.org/misc/COPYING.txt
* @version SVN: $Id$
* @link http://www.froxlor.org/
*/
/**
* Class FroxlorSshTransport
*
* This class handles remote server related stuff.
*
* @category FroxlorCore
* @package Classes
* @subpackage System
* @author Froxlor Team <team@froxlor.org>
* @copyright 2010 the authors
* @license GPLv2 http://files.froxlor.org/misc/COPYING.txt
* @link http://www.froxlor.org/
*/
class FroxlorSshTransport
{
/**
* Clas object.
*
* @var FroxlorSshTransport
*/
private static $_instance = null;
/**
* Contains the connection handle.
*
* @var resource
*/
private $_connection = null;
/**
* Contains all information for the ssh connection.
*
* @var array
*/
private $_settings = array();
/**
* Contains the shell resource.
*
* @var resource
*/
private $_shell = null;
/**
* Constructor (Singleton)
*
* @param array $settings contains an array of settings for the connection
*
* @return FroxlorSshTransport
*
* @throws Exception if the connection could not be established
*/
private function __construct($settings)
{
// settings
$this->_settings = $settings;
// try to connect
$this->_connect();
// get a shell stream
$this->_shell = $this->_getShell();
}
/**
* Destructor.
*
*/
public function __destruct()
{
$this->close();
}
/**
* Constructor for publickey auth
*
* @param string $ip ip toremote server
* @param string $port remote port
* @param string $username ssh username
* @param string $pubkeyfile path to pubkeyfile
* @param string $privkeyfile path to privatekeyfile
* @param string $passphrase passphrase
*
* @return FroxlorSshTransport
*/
public static function usePublicKey($ip, $port, $username, $pubkeyfile, $privkeyfile , $passphrase)
{
$settings = array(
'ip' => $ip,
'port' => $port,
'username' => $username,
'pubkeyfile' => $pubkeyfile,
'privkeyfile' => $privkeyfile,
'passphrase' => $passphrase
);
if (is_null(self::$_instance)) {
self::$_instance = new FroxlorSshTransport($settings);
}
return self::$_instance;
}
/**
* Constructor for plain password auth
*
* @param string $ip ip to remote server
* @param string $port remote port
* @param string $username ssh username
* @param string $password ssh password
*
* @return FroxlorSshTransport
*/
public static function usePlainPassword($ip, $port, $username, $password)
{
$settings = array(
'ip' => $ip,
'port' => $port,
'username' => $username,
'password' => $password
);
if (is_null(self::$_instance)) {
self::$_instance = new FroxlorSshTransport($settings);
}
return self::$_instance;
}
/**
* Send a command to the shell session.
*
* @param string $cmd command to send (without EOL)
*/
public function sendCmd($cmd)
{
// writes the command to the shell
fwrite($this->_shell, $cmd.PHP_EOL);
}
/**
* Sends a file to the remote server.
* The function checks if fopen() is enabled to use a faster way
* to send the file. Otherwise ssh2_scp_send() is used.
*
* @param string $localFile path to the local file
* @param string $remoteFile remote file path
* @param string $chmod file rights (default: 0644)
*
* @return boolean
*/
public function sendFile($localFile, $remoteFile, $chmod = 0644)
{
// check if file exists
if (@!is_readable($localFile)) {
return false;
}
// send file with scp if fopen() is disabled
if (!function_exists("fopen") && ssh2_scp_send($this->_connection, $localFile, $remoteFile, $chmod)) {
return true;
} else {
return false;
}
// open a sftp cnnection
$sftp = ssh2_sftp($this->_connection);
// connect to it
$sftpStream = @fopen('ssh2.sftp://'.$sftp.$remoteFile, 'w');
try {
if (!$sftpStream) {
throw new Exception("Could not open remote file: $remoteFile");
}
$data_to_send = @file_get_contents($localFile);
if ($data_to_send === false) {
throw new Exception("Could not open local file: $localFile.");
}
if (@fwrite($sftpStream, $data_to_send) === false) {
throw new Exception("Could not send data from file: $localFile.");
}
fclose($sftpStream);
return true;
} catch (Exception $e) {
// TODO log to error log?
fclose($sftpStream);
return false;
}
}
/**
* This reads all available output.
*
* @return array
*/
public function readAll()
{
if (is_null($this->_shell)) {
return array();
}
$output = array();
while($line = fgets($this->_shell)) {
$output[] = $line;
}
return $output;
}
/**
* Clean up.
*/
public function close()
{
// close the session and force flushing file content to disk
if (!is_null($this->_connection)) {
ssh2_exec($this->_connection, 'exit');
}
// set null values
$this->_shell = null;
$this->_settings = null;
$this->_connection = null;
$this->_instance = null;
}
/**
* This function connects to the remote server.
*
* @return void
*/
private function _connect()
{
$callbacks = array('disconnect' => array('FroxlorSshTransport', 'disconnected'));
if ($this->_settings['pubkeyfile']) {
// pubkey authentication
$this->_connection = ssh2_connect($this->_settings['ip'], $this->_settings['port'], array('hostkey'=>'ssh-rsa'), $callbacks);
$success = ssh2_auth_pubkey_file($this->_connection, $this->_settings['username'], $pubkeyfile, $privkeyfile, $passphrase);
} else {
// plain password authentication
$this->_connection = ssh2_connect($this->_settings['ip'], $this->_settings['port'], array(), $callbacks);
$success = ssh2_auth_password($this->_connection, $this->_settings['username'], $this->_settings['password']);
}
// check connection
if (!$success) {
// TODO change this to an Exception for froxlor 1.0
throw new Exception("Connection to ssh could not be established!");
}
}
/**
* Returns a shell.
*
* @return resource
*/
private function _getShell()
{
return ssh2_shell($this->_connection, 'vt102', null, 80, 24, SSH2_TERM_UNIT_CHARS);
}
/**
* Callback function if the connection disconnects.
*
* @param string $reason reason
* @param string $message message
* @param string $language language
*
* @return void
*/
public static function disconnected($reason, $message, $language)
{
// try reconnecting
try{
self::$_instance->_connect();
} catch (Exception $e) {
throw new Exception("Connection lost and could not re-established! \n".$reason."\n".$message."\n".$language."\n".$e->getMessage());
}
}
}

View File

@@ -1,89 +0,0 @@
<?php
/**
* This file is part of the Froxlor project.
* Copyright (c) 2010 the Froxlor Team (see authors).
*
* For the full copyright and license information, please view the COPYING
* file that was distributed with this source code. You can also view the
* COPYING file online at http://files.froxlor.org/misc/COPYING.txt
*
* @copyright (c) the authors
* @author Froxlor team <team@froxlor.org> (2010-)
* @license GPLv2 http://files.froxlor.org/misc/COPYING.txt
* @package Panel
* @version $Id$
*
* This file is for demonstration purpose and should NEVER be enabled
* in a live enviroment.
*
*/
exit();
/* get a FroxlorSshTransport Object with plain password authentication
* Note: to get this working you have to enable plain password authentication in
* your sshd config!
*/
$transport = FroxlorSshTransport::usePlainPassword("test.froxlor.org", 22, "testSshUser", "plainpassword");
/*
* Send a command to the ssh shell.
*/
$transport->sendCmd("ls -alF");
/*
* The return result is an array with lines read from stdin
*/
$outputArray = $transport->readAll();
/*
* Let's copy our sshd_config to the remote host.
*/
$transport->sendFile("/etc/ssh/sshd_config", "/etc/ssh/sshd_config", 0644);
/*
* Close this session.
*/
$transport->close();
/*
* Create a new ssh session with pubkey authentication.
*/
$transport = FroxlorSshTransport::usePublicKey("test.froxlor.org", 22, "testUserSSh", "/path/to/pubkey.key", "/path/to/private.key", "myPassphrase1337");
/*
* Clean up and finish.
*/
$transport->close();
/*
* *********************************************************************************************************************
*
* Demousage about deploying
*/
/*
* create a file list and save it internaly
*/
FroxlorDeployfileCreator::createList(
array(
"/var/www/froxlor/lib/",
"/var/www/froxlor/lng/",
"/var/www/froxlor/scripts/",
"/var/www/froxlor/actions/",
"/var/www/froxlor/templates/"
)
);
/*
* save it to disk file
*/
FroxlorDeployfileCreator::saveListTo("deploy.txt");
/*
* and create a zip archive
*/
$pkg = new FroxlorPkgCreator("deploy.txt", "deploy.zip");
$pkg->pack();

View File

@@ -1,177 +0,0 @@
<?php
/**
* This file is part of the Froxlor project.
* Copyright (c) 2010 the Froxlor Team (see authors).
*
* For the full copyright and license information, please view the COPYING
* file that was distributed with this source code. You can also view the
* COPYING file online at http://files.froxlor.org/misc/COPYING.txt
*
* @copyright (c) the authors
* @author Froxlor team <team@froxlor.org> (2010-)
* @license GPLv2 http://files.froxlor.org/misc/COPYING.txt
* @package Cron
* @version $Id$
*/
/*
* This script prepares data for froxlor-clients (multserver-mode)
* and transfers them via ssh/scp to the destination-server
*/
if(@php_sapi_name() != 'cli'
&& @php_sapi_name() != 'cgi'
&& @php_sapi_name() != 'cgi-fcgi')
{
die('This script only works in the shell.');
}
class client_deployer
{
/**
* object of froxlor-client
* @var froxlorclient
*/
private $_client = null;
public function __construct($client = null)
{
$this->_client = $client;
}
/**
* This functions deploys the needed files
* to the client destination server
*
* @return bool
*/
public function Deploy()
{
// get FroxlorSshTransport-object
$ssh = null;
if($this->_client->getSetting('client', 'deploy_mode') !== null
&& $this->_client->getSetting('client', 'deploy_mode') == 'pubkey'
) {
$ssh = FroxlorSshTransport::usePublicKey(
$this->_client->getSetting('client', 'hostname'),
$this->_client->getSetting('client', 'ssh_port'),
$this->_client->getSetting('client', 'ssh_user'),
$this->_client->getSetting('client', 'ssh_pubkey'),
$this->_client->getSetting('client', 'ssh_privkey'),
$this->_client->getSetting('client', 'ssh_passphrase')
);
}
else if($this->_getSetting('client', 'deploy_mode') !== null)
{
$ssh = FroxlorSshTransport::usePlainPassword(
$this->_client->getSetting('client', 'hostname'),
$this->_client->getSetting('client', 'ssh_port'),
$this->_client->getSetting('client', 'ssh_user'),
$this->_client->getSetting('client', 'ssh_passphrase')
);
} else {
throw new Exception('NO_DEPLOY_METHOD_GIVEN');
}
if($ssh instanceof FroxlorSshTransport)
{
$time = time();
$deployList = "/tmp/froxlor_deploy_".$time.".txt";
$zipPath = "/tmp/froxlor_deploy_".$time.".zip";
$remoteTo = $this->_client->getSetting('client', 'install_destination');
// TODO get a deploy configuration from database/panel?
// create the deploy list
$mypath = dirname(dirname((dirname(dirname(__FILE)))));
FroxlorDeployfileCreator::createList(
array(
makeCorrectDir($mypath."/lib/"),
makeCorrectDir($mypath."/lng/"),
makeCorrectDir($mypath."/scripts/"),
makeCorrectDir($mypath."/actions/"),
makeCorrectDir($mypath."/templates/")
)
);
FroxlorDeployfileCreator::saveListTo($deployList);
// prepare and pack files
$this->_prepareFiles($deployList, $zipPath);
// transfer the data
$bytes = $this->_transferArchive($ssh, $zipPath, $remoteTo);
// close the session
$ssh->close();
}
}
/**
* transfer the created archive to
* the destination host
*
* @return double amount of bytes transferd
*/
private function _transferArchive($ssh, $from, $to)
{
if ($ssh->sendFile($from, $to)) {
$filenfo = stat($from);
return $filenfo['7'];
} else {
return 0.0;
}
}
/**
* create an archive of all needed files listed
* in a list/xml file (@TODO)
*
* @return string path to the created archive
*/
private function _prepareFiles($deployList, $toPath)
{
$pkg = new FroxlorPkgCreator($deployList, $toPath);
/**
* create userdata file which
* has to be included to the archive
*/
$userdatafile = $this->_createUserdataFile();
// add userdata.inc.php
$pkg->addFile("lib/userdata.inc.php", $userdatafile);
// pack it
$pkg->pack();
}
/**
* create the userdata.inc.php file filled
* with necessary data, like database-host,
* username, etc.
* !!! don't forget to set $server_id to the correct value !!!
*
* @return string full path to the created file
*
* @TODO master-db settings
*/
private function _createUserdataFile()
{
$userdata = '';
$userdata .= "<?php\n";
$userdata .= "// automatically generated userdata.inc.php for Froxlor-Client '".$this->_client->Get('name')."'\n";
$userdata .= "\$sql['host']='".$this->_client->getSetting('client', 'masterdb_host')."';\n";
$userdata .= "\$sql['user']='".$this->_client->getSetting('client', 'masterdb_usr')."';\n";
$userdata .= "\$sql['password']='".$this->_client->getSetting('client', 'masterdb_pwd')."';\n";
$userdata .= "\$sql['db']='".$this->_client->getSetting('client', 'masterdb_db')."';\n";
$userdata .= "\$sql_root[0]['caption']='Master database';\n";
$userdata .= "\$sql_root[0]['host']='".$this->_client->getSetting('client', 'masterdb_host')."';\n";
$userdata .= "\$sql_root[0]['user']='".$this->_client->getSetting('client', 'masterdb_rootusr')."';\n";
$userdata .= "\$sql_root[0]['password']='".$this->_client->getSetting('client', 'masterdb_rootpwd')."';\n";
$userdata .= "// Define our system id\n";
$userdata .= "\$server_id = ".(int)$this->_client->Get('id').";\n";
return $userdata;
}
}