Added DNS migration

This commit is contained in:
Deon George 2012-07-12 15:10:53 +10:00
parent 42c7f29665
commit 0a211b4677
6 changed files with 204 additions and 150 deletions

View File

@ -0,0 +1,44 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This class extends the Kohana XML
*
* @package OSB
* @subpackage XML
* @category XML
* @author Deon George
* @copyright (c) 2010 Deon George
* @license http://dev.leenooks.net/license.html
*/
class XML extends XML_Core {
/**
* Collapse an XML object into a simple array
*/
public function collapse(XML $xml=NULL) {
if (is_null($xml))
$xml = $this;
$result = array();
foreach ($xml->as_array() as $j=>$k) {
$v = $xml->$j->value();
if (count($k) > 1) {
foreach ($xml->get($j,1) as $k)
if (isset($k['name'][0]))
$result[$j][$k['name'][0]] = (isset($k['value'][0]) ? $k['value'][0] : '');
else
$result[$j][] = $k;
} elseif (! is_null($v))
$result[$j] = $v;
else {
$result[$j] = $this->collapse($xml->$j);
}
}
if (array_key_exists('name',$result) AND array_key_exists('value',$result))
$result = array($result['name']=>$result['value']);
return $result;
}
}
?>

View File

@ -23,6 +23,7 @@ class Controller_Task_Host extends Controller_Task {
case 'getdns':
case 'getreseller':
case 'gettraffic':
case 'migrate':
case 'provision':
$this->so = ORM::factory('service',$this->request->param('id'));
@ -71,7 +72,7 @@ class Controller_Task_Host extends Controller_Task {
if ($result->loaded())
$this->verify('c',$result);
print_r($result);
echo (string)$result->_object;
}
/**
@ -83,7 +84,7 @@ class Controller_Task_Host extends Controller_Task {
if ($result->loaded())
$this->verify('d',$result);
print_r($result);
echo (string)$result->_object;
}
/**
@ -95,16 +96,16 @@ class Controller_Task_Host extends Controller_Task {
#if ($result->loaded())
# $this->verify('d',$result);
print_r($result);
echo (string)$result->_object;
}
/**
* Get Reseller
* Get Reseller
*/
public function action_getreseller() {
$result = $this->hpo->cmd_getreseller($this->so);
print_r($result);
echo (string)$result->_object;
}
/**
@ -113,7 +114,97 @@ class Controller_Task_Host extends Controller_Task {
public function action_gettraffic() {
$result = $this->hpo->cmd_gettraffic($this->so);
print_r($result);
echo (string)$result->_object;
}
public function action_migrate() {
if (! preg_match('/^[0-9]+:[0-9]+-[0-9]+$/',$ids=$this->request->param('id')))
throw new Kohana_Exception('Need to specify 2 site for a service to migrate, ie: svc:a-b');
list($sid,$svrs) = preg_split('/:/',$ids,2);
list($fsid,$tsid) = preg_split('/-/',$svrs,2);
$so = ORM::factory('service',$sid);
if (! $so->loaded())
throw new Kohana_Exception('Service :sid doesnt exist?',array(':sid'=>$sid));
printf("Migrate: %s\n",$so->plugin()->name());
// Validation, make sure the target is as configured
if ($so->plugin()->host_server_id != $tsid)
throw new Kohana_Exception('Service :sid is defined to server :tsid?',array(':sid'=>$sid,':tsid'=>$tsid));
// Validation, make sure the target is defined
$domain = clone $this->hpo->cmd_getdomain($this->so);
if (! $domain->loaded())
throw new Kohana_Exception('Service :sid is not on server :tsid?',array(':sid'=>$sid,':tsid'=>$tsid));
// Temporarily set our host_server_id to $fsid
$hpo = ORM::factory('host_server',$fsid);
if (! $hpo->loaded())
throw new Kohana_Exception('Host server :fsid not defined?',array(':fsid'=>$fsid));
$result = $hpo->plugin()->cmd_getdns($this->so);
// During migration we are only interested in the data.
$k = array(
'host_plugin_plesk_10'=>array('type','host','value','opt'), // From PLESK 9.
);
$t = array(
'host_plugin_plesk_10'=>array('domain_id'=>array('site-id'=>'id')), // From PLESK 9.
);
$f = array(
'host_plugin_plesk_10'=>array('host'=>'NODOMAIN','value'=>'NOTRAILDOT'),
);
$dns = $this->hpo->newitem($this->so,'dns');
foreach ($result->_object->get('result',FALSE) as $key => $o) {
$rec = $o->data->collapse();
$add = $dns->add_node('add_rec');
// Translate old values to new ones.
foreach ($t[strtolower(get_class($this->hpo))] as $a=>$b) {
if (isset($rec[$a])) {
unset($rec[$a]);
foreach ($b as $c=>$d) {
$add->add_node($c,$domain->$d->value());
}
}
}
// Import the values we are interested in
foreach ($k[strtolower(get_class($this->hpo))] as $a=>$b)
if (isset($rec[$b])) {
if (isset($f[strtolower(get_class($this->hpo))][$b])) {
switch ($f[strtolower(get_class($this->hpo))][$b]) {
case 'NODOMAIN':
$rec[$b] = preg_replace('/.?'.$so->plugin()->name().'.$/i','',$rec[$b]);
break;
case 'NOTRAILDOT':
$rec[$b] = preg_replace('/.$/','',$rec[$b]);
break;
default:
throw new Kohana_Exception('Unknown filter action :filter',array(':filter'=>$f[strtolower(get_class($this->hpo))][$b]));
}
}
$add->add_node($b,$rec[$b]);
}
}
printf("DNS: %s\n",(string)$dns);
$result = $this->hpo->add_dnsdata($this->so,$dns);
echo (string)$result->_object;
}
/**

View File

@ -27,18 +27,8 @@ abstract class Host_Plugin implements Serializable {
CURLOPT_VERBOSE => FALSE,
);
public function serialize() {
return serialize($this->_object);
}
public function unserialize($s) {
$this->_object = unserialize($s);
}
public function __get($key) {
return isset($this->_object[$key]) ? $this->_object[$key] : NULL;
}
// Required abstract classes
abstract public function __get($key);
abstract public function admin_update();
abstract public function manage_button(Model_Service_Plugin_Host $spho,$t);

View File

@ -16,7 +16,7 @@ abstract class Host_Plugin_Plesk extends Host_Plugin {
protected $path = 'enterprise/control/agent.php';
protected $packet;
protected $xml;
private $_loaded;
protected $_loaded = FALSE;
// Manage UI Login Attributes
protected $url = 'login_up.php3';
@ -25,6 +25,31 @@ abstract class Host_Plugin_Plesk extends Host_Plugin {
protected $login_pass_field = 'passwd';
// Our required abstract classes
public function serialize() {
return (string)$this->_object;
}
public function unserialize($s) {
$this->_object = XML::factory(NULL,NULL,$s);
}
public function __get($index) {
// We need to find out the command key, so we can get the status result.
if (count($a=array_keys($this->xml->packet->as_array())) != 1)
throw new Kohana_Exception('XML command malformed? :xml',array(':xml'=>(string)$this->xml));
$key = array_shift($a);
if (count($a=array_keys($this->xml->packet->$key->as_array())) != 1)
throw new Kohana_Exception('XML command malformed? :xml',array(':xml'=>(string)$this->xml));
$get = array_shift($a);
if (! $this->_loaded OR $this->_object->$key->$get->result->status->value() != 'ok' )
return $this->_object;
if ($index == '_object')
return $this->_object->$key->$get;
else
return is_object($this->_object->$key->$get->result->$index) ? $this->_object->$key->$get->result->$index : NULL;
}
public function admin_update() {
return View::factory('host/admin/plugin/plesk')
->set('o',$this);
@ -55,6 +80,7 @@ abstract class Host_Plugin_Plesk extends Host_Plugin {
protected function init() {
$this->_loaded = FALSE;
$this->_object = XML::factory(NULL,'plesk');
$this->xml = XML::factory(NULL,'plesk');
$this->packet = $this->xml->add_node('packet','',array('version'=>$this->protocol));
}
@ -82,37 +108,12 @@ abstract class Host_Plugin_Plesk extends Host_Plugin {
$response = $request->execute();
$result = XML::factory(NULL,'plesk',$response->body());
$this->_object = XML::factory(NULL,'plesk',$response->body());
if ($result->status->value() == 'ok')
if ($this->_object->status->value() == 'ok')
$this->_loaded = TRUE;
return $result;
}
protected function collapse(XML $xml) {
$result = array();
foreach ($xml->as_array() as $j=>$k) {
$v = $xml->$j->value();
if (count($k) > 1) {
foreach ($xml->get($j,1) as $k)
if (isset($k['name'][0]))
$result[$j][$k['name'][0]] = (isset($k['value'][0]) ? $k['value'][0] : '');
else
$result[$j][] = $k;
} elseif (! is_null($v))
$result[$j] = $v;
else
$result[$j] = $this->collapse($xml->$j);
}
if (array_key_exists('name',$result) AND array_key_exists('value',$result))
$result = array($result['name']=>$result['value']);
return $result;
return $this;
}
public function loaded() {

View File

@ -12,7 +12,6 @@
*/
class Host_Plugin_Plesk_10 extends Host_Plugin_Plesk {
protected $protocol = '1.6.3.0';
private $_loaded = FALSE;
// @todo Get these default "templates" values out of the DB
private $_template = array(
@ -53,40 +52,22 @@ class Host_Plugin_Plesk_10 extends Host_Plugin_Plesk {
foreach ($items as $k)
$dataset->add_node($k);
$result = $this->server_command($this->xml);
if (! $this->loaded())
throw new Kohana_Exception('Unable to get PLESK Client data - Error :error',array(':error'=>(string)$result));
$this->_object['id'] = $result->id->value();
foreach ($items as $k)
foreach ($result->get($k) as $a=>$b)
$this->_object[$k] = $this->collapse($b);
return $this;
return $this->server_command($this->xml);
}
/**
* Get a DNS Configuration
*/
public function cmd_getdns(Model_Service $so,$file='') {
$do = $this->cmd_getdomain($so);
public function cmd_getdns(Model_Service $so) {
$do = clone $this->cmd_getdomain($so);
$this->init();
$domain = $this->packet->add_node('dns');
$get = $domain->add_node('get_rec');
$filter = $get->add_node('filter');
$filter->add_node('site-id',$do->id);
$filter->add_node('site-id',$do->id->value());
$result = $this->server_command($this->xml);
if (! $this->loaded())
throw new Kohana_Exception('Unable to get PLESK Domain data - Error :error',array(':error'=>(string)$result));
$this->_object = $this->collapse($result);
if ($file)
file_put_contents($file,(string)$result);
return $this;
return $this->server_command($this->xml);
}
/**
@ -104,17 +85,7 @@ class Host_Plugin_Plesk_10 extends Host_Plugin_Plesk {
foreach ($items as $k)
$dataset->add_node($k);
$result = $this->server_command($this->xml);
if (! $this->loaded())
throw new Kohana_Exception('Unable to get PLESK Domain data - Error :error',array(':error'=>(string)$result));
$this->_object['id'] = $result->id->value();
foreach ($items as $k)
foreach ($result->get($k) as $a=>$b)
$this->_object[$k] = $this->collapse($b);
return $this;
return $this->server_command($this->xml);
}
/**
@ -137,17 +108,7 @@ class Host_Plugin_Plesk_10 extends Host_Plugin_Plesk {
foreach ($items as $k)
$dataset->add_node($k);
$result = $this->server_command($this->xml);
if (! $this->loaded())
throw new Kohana_Exception('Unable to get PLESK Reseller data - Error :error',array(':error'=>(string)$result));
$this->_object['id'] = $result->id->value();
foreach ($items as $k)
foreach ($result->get($k) as $a=>$b)
$this->_object[$k] = $this->collapse($b);
return $this;
return $this->server_command($this->xml);
}
/**
@ -163,10 +124,10 @@ class Host_Plugin_Plesk_10 extends Host_Plugin_Plesk {
*/
public function provision(Model_Service $so) {
/*
$ro = $this->cmd_getreseller($so);
$ro = clone $this->cmd_getreseller($so);
// Make sure our reseller exists
if ($ro AND ! $ro->id)
if ($ro AND ! $ro->id->value())
throw new Kohana_Exception('Add Reseller - Not Implemented');
$result = $this->add_client($so);
@ -202,7 +163,7 @@ class Host_Plugin_Plesk_10 extends Host_Plugin_Plesk {
}
public function add_client(Model_Service $so) {
$ro = $this->cmd_getreseller($so);
$ro = clone $this->cmd_getreseller($so);
$this->init();
$client = $this->packet->add_node('customer');
@ -218,19 +179,21 @@ class Host_Plugin_Plesk_10 extends Host_Plugin_Plesk {
$gen_info->add_node('email',$so->account->email);
if ($ro->id)
$gen_info->add_node('owner-id',$ro->id);
$gen_info->add_node('owner-id',$ro->id->value());
$result = $this->server_command($this->xml);
return $this->server_command($this->xml);
}
if (! $this->loaded())
throw new Kohana_Exception('Unable to add PLESK Client - Error :error',array(':error'=>(string)$result));
public function add_dnsdata(Model_Service $so,XML $dns) {
$this->init();
$this->packet->import($dns);
return $result;
return $this->server_command($this->xml);
}
public function add_domain(Model_Service $so) {
$plan = 'Default Domain';
$co = $this->cmd_getclient($so);
$plan = 'Default Domain'; //@todo Get from other means.
$co = clone $this->cmd_getclient($so);
$ip = '223.27.16.147'; //@todo Get from other means.
if (! $co->loaded())
@ -241,7 +204,7 @@ class Host_Plugin_Plesk_10 extends Host_Plugin_Plesk {
$add = $domain->add_node('add');
$gen_setup = $add->add_node('gen_setup');
$gen_setup->add_node('name',strtolower($so->name()));
$gen_setup->add_node('owner-id',$co->id);
$gen_setup->add_node('owner-id',$co->id->value());
$gen_setup->add_node('htype','vrt_hst');
$gen_setup->add_node('ip_address',$ip);
$gen_setup->add_node('status','0');
@ -258,12 +221,13 @@ class Host_Plugin_Plesk_10 extends Host_Plugin_Plesk {
$add->add_node('plan-name',$plan);
$result = $this->server_command($this->xml);
return $this->server_command($this->xml);
}
if (! $this->loaded())
throw new Kohana_Exception('Unable to add PLESK Domain - Error :error',array(':error'=>(string)$result));
public function newitem(Model_Service $so,$type) {
$this->init();
return $result;
return $this->packet->add_node($type);
}
}
?>

View File

@ -12,7 +12,6 @@
*/
class Host_Plugin_Plesk_9 extends Host_Plugin_Plesk {
protected $protocol = '1.6.0.1';
private $_loaded = FALSE;
// @todo Get these default "templates" values out of the DB
private $_template = array(
@ -94,40 +93,22 @@ class Host_Plugin_Plesk_9 extends Host_Plugin_Plesk {
foreach ($items as $k)
$dataset->add_node($k);
$result = $this->server_command($this->xml);
if (! $this->loaded())
throw new Kohana_Exception('Unable to get PLESK Client data - Error :error',array(':error'=>(string)$result));
$this->_object['id'] = $result->id->value();
foreach ($items as $k)
foreach ($result->get($k) as $a=>$b)
$this->_object[$k] = $this->collapse($b);
return $this;
return $this->server_command($this->xml);
}
/**
* Get a DNS Configuration
*/
public function cmd_getdns(Model_Service $so,$file='') {
$do = $this->cmd_getdomain($so);
public function cmd_getdns(Model_Service $so) {
$do = clone $this->cmd_getdomain($so);
$this->init();
$domain = $this->packet->add_node('dns');
$get = $domain->add_node('get_rec');
$filter = $get->add_node('filter');
$filter->add_node('domain_id',$do->id);
$filter->add_node('domain_id',$do->id->value());
$result = $this->server_command($this->xml);
if (! $this->loaded())
throw new Kohana_Exception('Unable to get PLESK Domain data - Error :error',array(':error'=>(string)$result));
$this->_object = $this->collapse($result);
if ($file)
file_put_contents($file,(string)$result);
return $this;
return $this->server_command($this->xml);
}
/**
@ -145,17 +126,7 @@ class Host_Plugin_Plesk_9 extends Host_Plugin_Plesk {
foreach ($items as $k)
$dataset->add_node($k);
$result = $this->server_command($this->xml);
if (! $this->loaded())
throw new Kohana_Exception('Unable to get PLESK Domain data - Error :error',array(':error'=>(string)$result));
$this->_object['id'] = $result->id->value();
foreach ($items as $k)
foreach ($result->get($k) as $a=>$b)
$this->_object[$k] = $this->collapse($b);
return $this;
return $this->server_command($this->xml);
}
/**
@ -172,14 +143,7 @@ class Host_Plugin_Plesk_9 extends Host_Plugin_Plesk {
# $get->add_node('since_date','2012-04-01');
# $get->add_node('end_date','2012-04-02');
$result = $this->server_command($this->xml);
if (! $this->loaded())
throw new Kohana_Exception('Unable to get PLESK Traffic data - Error :error',array(':error'=>(string)$result));
$this->_object = $this->collapse($result);
return $this;
return $this->server_command($this->xml);
}
/**