Commit 5bfb1067 authored by Adrian Rossouw's avatar Adrian Rossouw

still a work in progress, but we now at least record the SOA record for the zone.

parent 797299a1
<?php print "<?php \n"; ?>
<?php foreach ($records as $key => $record) {
print "\n\${$this->key}['{$key}'] = ". var_export($record, TRUE) .';';
}
?>
<?php
foreach ($records as $key => $name) {
printf('zone "%s" { type master; file "%s/%s.zone"; allow-query { any; }; };' . "\n", $name, $dns_zoned_path, $name);
}
?>
......@@ -72,12 +72,30 @@ class provisionConfig_dns_server extends provisionConfig_dns {
}
}
class provisionConfig_dns_zone_store extends provisionConfig_data_store {
function filename() {
return "{$this->data['server']->dns_data_path}/{$this->data['name']}.zone.inc";
}
}
class provisionConfig_dns_zone extends provisionConfig_dns {
public $template = 'zone.tpl.php';
public $description = 'Zone-wide DNS configuration';
public $data_store_class = 'provisionConfig_dns_zone_store';
function filename() {
return "{$this->data['server']->dns_zoned_path}/{$this->uri}.zone";
return "{$this->data['server']->dns_zoned_path}/{$this->data['name']}.zone";
}
function write() {
// lock the store until we are done generating our config.
$this->store->lock();
parent::write();
$this->store->write();
$this->store->close();
}
}
......@@ -93,9 +111,3 @@ class provisionConfig_dns_host extends provisionConfig_dns {
}
class provisionConfig_dns_zone_store extends provisionConfig_data_store {
function filename() {
return $this->data['server']->dns_data_path;
}
}
......@@ -32,16 +32,11 @@ function drush_dns_provision_zone($action, $zone, $name = null, $type = null, $d
$status = d()->service('dns')->delete_host($zone);
break;
case 'verify':
case 'create':
$config = d()->service('dns')->get_config('server');
$config->store->records[$zone] = $zone;
$config->write();
## $status = d()->service('dns')->create_zone($zone);
case 'create':
$status = d()->service('dns')->create_zone($zone);
break;
case 'delete':
$config = d()->service('dns')->get_config('server');
$config->store->records[$zone] = FALSE;
$config->write();
$status = d()->service('dns')->delete_zone($zone);
break;
case 'rr-add':
$status = d()->service('dns')->add_record($zone, $name, $type, $destination);
......@@ -128,6 +123,30 @@ class provisionService_dns_basic extends provisionService_dns {
return $data;
}
/**
* This creates a zone, which mostly consists of adding the SOA record.
*/
function create_zone($zone) {
$config = $this->config_load('server');
$config->store->records[$zone] = $zone;
$config->write();
$config = $this->config_load('zone', array('name' => $zone));
$config->store->records['SOA'] = $zone;
$config->write();
}
/**
* This completely drops a zone, without any checks.
*/
function delete_zone($zone) {
$config = d()->service('dns')->config_load('server');
$config->store->records[$zone] = FALSE;
$config->write();
}
}
class provisionService_dns_complex extends provisionService_dns_basic {
......@@ -232,20 +251,6 @@ class provisionService_dns_complex extends provisionService_dns_basic {
}
}
/**
* This creates a zone, which mostly consists of adding the SOA record.
*/
function create_zone($fqdn = null) {
$this->edit_record($fqdn, '@', 'SOA');
}
/**
* This completely drops a zone, without any checks.
*/
function delete_zone($fqdn) {
return FALSE;
}
/**
* This adds a (potentially) duplicate record (RR) in a zonefile
*
......
......@@ -3,4 +3,3 @@ foreach ($records as $key => $name) {
printf("conf-file=%s/%s.zone\n", $dns_zoned_path, $name);
}
?>
#conf-dir=<?php print "$dns_zoned_path"; ?>
......@@ -229,93 +229,6 @@ class provisionConfig {
function unlink() {
provision_file()->unlink($this->filename())->status();
}
/**
* add a line to a file if the file (or pattern) is not in the file
* already
*
* This is bound to be called repeatedly so it needs to be fast.
*/
function add_line_if_not_exists($line, $pattern = null) {
$file = $this->filename();
// XXX: we should do exponential backoff here to limit contention
$fd = fopen($file, 'r+'); // read/write, beginning of file
flock($fd, LOCK_EX);
$line = trim($line);
if (is_null($pattern)) {
$pattern = '/' . $line . '/';
}
$found = FALSE;
while ($l = fgets($fd)) {
if (preg_match($pattern, $l)) {
$found = TRUE;
break;
}
}
if (!$found) {
fseek($fd, 0, SEEK_END);
drush_log("pattern $pattern not found in file $file, adding at the end: $line");
fwrite($fd, $line . "\n");
}
fclose($fd);
return $found;
}
/**
* add a line to a file if the file (or pattern) is not in the file
* already
*
* This is bound to be called repeatedly so it needs to be fast.
*/
function add_line($line) {
$file = $this->filename();
// XXX: we should do exponential backoff here to limit contention
$fd = fopen($file, 'a'); // read/write, beginning of file
flock($fd, LOCK_EX);
$line = trim($line);
fwrite($fd, $line . "\n");
fclose($fd);
}
function replace_or_add_line($replacement, $pattern) {
$file = $this->filename();
if (!($fd = fopen($file, 'a+'))) {
drush_log("warning: cannot open $file");
}
flock($fd, LOCK_EX);
$body = fread($fd, filesize($file));
$newbody = preg_replace($pattern, $replacement, $body);
if ($body === $newbody) {
drush_log("pattern $pattern not found in $file, adding at the end: $replacement");
fseek($fd, 0, SEEK_END);
fwrite($fd, $replacement . "\n");
} else {
drush_log("pattern $pattern found and replaced with $replacement");
fseek($fd, 0);
fwrite($fd, $newbody);
ftruncate($fd, strlen($newbody));
}
fclose($fd);
}
function delete_line($pattern) {
$file = $this->filename();
$fd = fopen($file, 'r+');
flock($fd, LOCK_EX);
$body = fread($fd, filesize($file));
$newbody = preg_replace($pattern, '', $body);
if ($newbody != $body) {
drush_log("pattern $pattern found and replaced with $replacement");
} else {
drush_log("couldn't find line $pattern", 'warning');
}
fseek($fd, 0);
fwrite($fd, $newbody);
ftruncate($fd, strlen($newbody));
fclose($fd);
}
}
......
......@@ -17,6 +17,8 @@ class provisionService extends provisionChainedState {
protected $configs = array();
protected $config_cache = array();
// All services have the ability to have an associated restart command and listen port.
function init() {
if (!is_null($this->service)) {
......@@ -38,9 +40,15 @@ class provisionService extends provisionChainedState {
return false;
}
function get_config($config) {
foreach ((array) $this->configs[$config] as $class) {
return new $class($this->context, $this->config_data($config));
function &config_load($config, $data = array()) {
if (!isset($this->config_cache[$this->context->name][$config])) {
foreach ((array) $this->configs[$config] as $class) {
$this->config_cache[$this->context->name][$config] = new $class($this->context, array_merge($this->config_data($config), $data));
}
}
if (isset($this->config_cache[$this->context->name][$config])) {
return $this->config_cache[$this->context->name][$config];
}
return false;
}
......@@ -53,17 +61,14 @@ class provisionService extends provisionChainedState {
*/
function create_config($config) {
if (isset($this->configs[$config])) {
// We cast to array here to silently accept the non array version
foreach ((array) $this->configs[$config] as $class) {
try {
$object = new $class($this->context, $this->config_data($config));
$object->write();
}
catch (provisionException_continue $e) {
drush_log($e->getMessage());
// The service or configuration class has signaled that the config file
// was uneccesary, and we should continue normally.
}
try {
$object =& $this->config_load($config);
$object->write();
}
catch (provisionException_continue $e) {
drush_log($e->getMessage());
// The service or configuration class has signaled that the config file
// was uneccesary, and we should continue normally.
}
}
}
......@@ -76,17 +81,15 @@ class provisionService extends provisionChainedState {
*/
function delete_config($config) {
if (isset($this->configs[$config])) {
// We cast to array here to silently accept the non array version
foreach ((array) $this->configs[$config] as $class) {
try {
$object = new $class($this->context, $this->config_data($config, $class));
$object->unlink();
}
catch (provisionException_continue $e) {
drush_log($e->getMessage());
// The service or configuration class has signaled that the config file
// was uneccesary, and we should continue normally.
}
try {
$object =& $this->config_load($config);
$object->unlink();
}
catch (provisionException_continue $e) {
drush_log($e->getMessage());
// The service or configuration class has signaled that the config file
// was uneccesary, and we should continue normally.
}
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment