Commit 9f0be0f6 authored by Adrian Rossouw's avatar Adrian Rossouw

new data store config class.

still a work in progress.
parent b0f026cb
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
*/ */
class provisionService_dns_bind extends provisionService_dns_complex { class provisionService_dns_bind extends provisionService_dns_complex {
protected $application_name = 'bind'; protected $application_name = 'bind';
protected $has_restart_command = TRUE; protected $has_restart_command = TRUE;
private $zone_cache = array(); private $zone_cache = array();
...@@ -16,34 +17,11 @@ class provisionService_dns_bind extends provisionService_dns_complex { ...@@ -16,34 +17,11 @@ class provisionService_dns_bind extends provisionService_dns_complex {
function default_restart_cmd() { function default_restart_cmd() {
return provisionService_dns_bind::bind_default_restart_cmd(); return provisionService_dns_bind::bind_default_restart_cmd();
function write() {
parent::write();
$this->data['server']->sync($this->filename());
} }
function unlink() {
parent::unlink();
$this->data['server']->sync($this->filename());
}}
function init() { function init() {
parent::init(); parent::init();
$this->server->setProperty('bind_conf_path', $this->server->config_path . '/bind.conf'); $this->configs['server'][] = 'provisionConfig_bind_server';
$this->server->setProperty('bind_zone_master_path', $this->server->config_path . '/zones/master');
$this->configs['server'][] = 'provisionConfig_bind_master_zone';
}
function config_data($config, $class) {
$data = parent::config_data($config, $class);
$data['bind_conf_path'] = $this->server->bind_config_path;
$data['bind_zone_master_path'] = $this->server->bind_zone_master_path;
return $data;
}
function verify() {
if ($this->context->type =='server') {
provision_file()->create_dir($this->server->bind_zone_master_path, dt("Bind zone files"), 0700);
}
} }
/** /**
...@@ -59,6 +37,7 @@ class provisionService_dns_bind extends provisionService_dns_complex { ...@@ -59,6 +37,7 @@ class provisionService_dns_bind extends provisionService_dns_complex {
if (!$zone->exists()) { if (!$zone->exists()) {
drush_log("creating zone"); drush_log("creating zone");
//$zone->add_line_if_not_exists($zone->zone_declaration(), '/zone\s*"'. $zonename . '"/'); //$zone->add_line_if_not_exists($zone->zone_declaration(), '/zone\s*"'. $zonename . '"/');
$zone->store->records[$zonename] = array('name' => $zonename);
} }
return parent::create_zone($zonename); return parent::create_zone($zonename);
} }
...@@ -183,7 +162,7 @@ class provisionConfig_bind_zone extends provisionConfig { ...@@ -183,7 +162,7 @@ class provisionConfig_bind_zone extends provisionConfig {
} }
function filename() { function filename() {
return $this->bind_zone_master_path . '/' . $this->data['name']; return $this->dns_zoned_path . '/' . $this->data['name'];
} }
function exists() { function exists() {
...@@ -275,13 +254,8 @@ class provisionConfig_bind_zone extends provisionConfig { ...@@ -275,13 +254,8 @@ class provisionConfig_bind_zone extends provisionConfig {
} }
} }
class provisionConfig_bind_master_zone { class provisionConfig_bind_server extends provisionConfig_dns_server {
function filename() {
return $this->bind_conf_path;
}
function zone_declaration() { function zone_declaration() {
return 'zone "' . $this->data['name'] . '" { type master; file "' . $this->bind_zone_master_path . '/' . $this->data['name'] . '"; allow-query { any; }; };'; return 'zone "' . $this->data['name'] . '" { type master; file "' . $this->bind_zone_master_path . '/' . $this->data['name'] . '"; allow-query { any; }; };';
} }
} }
...@@ -17,6 +17,15 @@ class provisionConfig_dns extends provisionConfig { ...@@ -17,6 +17,15 @@ class provisionConfig_dns extends provisionConfig {
} }
} }
// The data store for the server configuration
// contains a list of zones we manage.
class provisionConfig_dns_server_store extends provisionConfig_data_store {
function filename() {
return $this->data['server']->dns_data_path . '/zones.master.inc';
}
}
/** /**
* Base config class for the server level config. * Base config class for the server level config.
*/ */
...@@ -24,6 +33,8 @@ class provisionConfig_dns_server extends provisionConfig_dns { ...@@ -24,6 +33,8 @@ class provisionConfig_dns_server extends provisionConfig_dns {
public $template = 'server.tpl.php'; public $template = 'server.tpl.php';
public $description = 'Server-wide DNS configuration'; public $description = 'Server-wide DNS configuration';
public $data_store_class = 'provisionConfig_dns_server_store';
function filename() { function filename() {
if (isset($this->data['application_name'])) { if (isset($this->data['application_name'])) {
$file = $this->data['application_name'] . '.conf'; $file = $this->data['application_name'] . '.conf';
...@@ -36,7 +47,6 @@ class provisionConfig_dns_server extends provisionConfig_dns { ...@@ -36,7 +47,6 @@ class provisionConfig_dns_server extends provisionConfig_dns {
function write() { function write() {
parent::write(); parent::write();
if (isset($this->data['application_name'])) { if (isset($this->data['application_name'])) {
$file = $this->data['application_name'] . '.conf'; $file = $this->data['application_name'] . '.conf';
// We link the app_name.conf file on the remote server to the right version. // We link the app_name.conf file on the remote server to the right version.
...@@ -74,5 +84,12 @@ class provisionConfig_dns_host extends provisionConfig_dns { ...@@ -74,5 +84,12 @@ class provisionConfig_dns_host extends provisionConfig_dns {
function filename() { function filename() {
return "{$this->data['server']->dns_hostd_path}/{$this->uri}.hosts"; return "{$this->data['server']->dns_hostd_path}/{$this->uri}.hosts";
} }
}
class provisionConfig_dns_zone_store extends provisionConfig_data_store {
function filename() {
return $this->data['server']->dns_data_path;
}
} }
...@@ -62,6 +62,8 @@ function dns_provision_services() { ...@@ -62,6 +62,8 @@ function dns_provision_services() {
class provisionService_dns extends provisionService { class provisionService_dns extends provisionService {
public $service = 'dns'; public $service = 'dns';
public $slave = null;
} }
class provisionService_dns_basic extends provisionService_dns { class provisionService_dns_basic extends provisionService_dns {
...@@ -74,6 +76,9 @@ class provisionService_dns_basic extends provisionService_dns { ...@@ -74,6 +76,9 @@ class provisionService_dns_basic extends provisionService_dns {
function init() { function init() {
parent::init(); parent::init();
// Path for storing data store config files.
$this->server->dns_data_path = $this->server->aegir_root . '/dns.d';
if (!is_null($this->application_name)) { if (!is_null($this->application_name)) {
$app_dir = "{$this->server->config_path}/{$this->application_name}"; $app_dir = "{$this->server->config_path}/{$this->application_name}";
$this->server->dns_zoned_path = "{$app_dir}/zone.d"; $this->server->dns_zoned_path = "{$app_dir}/zone.d";
...@@ -83,6 +88,8 @@ class provisionService_dns_basic extends provisionService_dns { ...@@ -83,6 +88,8 @@ class provisionService_dns_basic extends provisionService_dns {
function verify() { function verify() {
if ($this->context->type === 'server') { if ($this->context->type === 'server') {
provision_file()->create_dir($this->server->dns_data_path, dt("DNS data store"), 0700);
if (!is_null($this->application_name)) { if (!is_null($this->application_name)) {
provision_file()->create_dir($this->server->dns_zoned_path, dt("DNS zone configuration"), 0755); provision_file()->create_dir($this->server->dns_zoned_path, dt("DNS zone configuration"), 0755);
$this->sync($this->server->dns_zoned_path, array( $this->sync($this->server->dns_zoned_path, array(
...@@ -94,6 +101,8 @@ class provisionService_dns_basic extends provisionService_dns { ...@@ -94,6 +101,8 @@ class provisionService_dns_basic extends provisionService_dns {
'exclude' => $this->server->dns_hostd_path . '/*', // Make sure remote directory is created 'exclude' => $this->server->dns_hostd_path . '/*', // Make sure remote directory is created
)); ));
# TODO: create a slave zone path too.
$this->create_config('server'); $this->create_config('server');
} }
} }
...@@ -103,6 +112,7 @@ class provisionService_dns_basic extends provisionService_dns { ...@@ -103,6 +112,7 @@ class provisionService_dns_basic extends provisionService_dns {
function config_data($config = null, $class = null) { function config_data($config = null, $class = null) {
$data = parent::config_data($config, $class); $data = parent::config_data($config, $class);
if (!is_null($this->application_name)) { if (!is_null($this->application_name)) {
$data['dns_data_path_path'] = $this->server->dns_zoned_path;
$data['dns_zoned_path'] = $this->server->dns_zoned_path; $data['dns_zoned_path'] = $this->server->dns_zoned_path;
$data['dns_hostd_path'] = $this->server->dns_hostd_path; $data['dns_hostd_path'] = $this->server->dns_hostd_path;
} }
...@@ -126,26 +136,15 @@ class provisionService_dns_complex extends provisionService_dns_basic { ...@@ -126,26 +136,15 @@ class provisionService_dns_complex extends provisionService_dns_basic {
$this->server->setProperty('dns_negativettl', 86400); # 24h $this->server->setProperty('dns_negativettl', 86400); # 24h
} }
function verify() { function verify() {
switch ($this->context->type) { switch ($this->context->type) {
case 'server' :
$this->create_server_config();
break;
case 'site' : case 'site' :
$this->create_host(); $this->create_host();
break; break;
} }
$this->parse_configs(); // This will restart the server if needed.
} parent::verify();
function create_server_config() {
return FALSE;
}
function delete_server_config() {
return FALSE;
} }
/** /**
...@@ -322,7 +321,6 @@ class provisionService_dns_complex extends provisionService_dns_basic { ...@@ -322,7 +321,6 @@ class provisionService_dns_complex extends provisionService_dns_basic {
if ($this->count_records($parts[1], null, array('NS', 'SOA'))) { if ($this->count_records($parts[1], null, array('NS', 'SOA'))) {
$this->delete_zone($parts[1]); $this->delete_zone($parts[1]);
} }
} }
/** /**
......
...@@ -40,6 +40,16 @@ class provisionConfig { ...@@ -40,6 +40,16 @@ class provisionConfig {
*/ */
protected $group = NULL; protected $group = NULL;
/**
* An optional data store class to instantiate for this config.
*/
protected $data_store_class = NULL;
/**
* The data store.
*/
protected $store = NULL;
/** /**
* Forward $this->... to $this->owner->... * Forward $this->... to $this->owner->...
* object. * object.
...@@ -71,8 +81,15 @@ class provisionConfig { ...@@ -71,8 +81,15 @@ class provisionConfig {
if (sizeof($data)) { if (sizeof($data)) {
$this->data = $data; $this->data = $data;
} }
$this->parsed_data = $this->parse(); //$this->parsed_data = $this->parse();
$this->data = array_merge($this->parsed_data, $this->data); //$this->data = array_merge($this->parsed_data, $this->data);
if (!is_null($this->data_store_class) && class_exists($this->data_store_class)) {
$class = $this->data_store_class;
$this->store = new $class($owner, $data);
}
} }
/** /**
...@@ -107,16 +124,29 @@ class provisionConfig { ...@@ -107,16 +124,29 @@ class provisionConfig {
* Load template from filename(). * Load template from filename().
*/ */
private function load_template() { private function load_template() {
$class_name = get_class($this);
$reflect = new reflectionObject($this); $reflect = new reflectionObject($this);
$base_dir = dirname($reflect->getFilename());
if (isset($this->template)) { if (isset($this->template)) {
$file = $base_dir . '/' . $this->template; while ($class_name) {
drush_log("Template loaded: $file"); // Iterate through the config file's parent classes until we
if (file_exists($file) && is_readable($file)) { // find the template file to use.
return file_get_contents($file); $reflect = new reflectionClass($class_name);
$base_dir = dirname($reflect->getFilename());
$file = $base_dir . '/' . $this->template;
if (file_exists($file) && is_readable($file)) {
drush_log("Template loaded: $file");
return file_get_contents($file);
}
$class_name = get_parent_class($class_name);
} }
}
}
return false; return false;
} }
...@@ -166,8 +196,7 @@ class provisionConfig { ...@@ -166,8 +196,7 @@ class provisionConfig {
->fail('Could not change permissions of @path to @perm'); ->fail('Could not change permissions of @path to @perm');
} }
provision_file()->file_put_contents($filename, $this->render_template($template, $this->data)) $this->file_put_contents($filename, $this->render_template($template, $this->data));
->succeed('Generated config ' . (empty($this->description) ? $filename : $this->description), 'success');
// Change the permissions of the file if needed // Change the permissions of the file if needed
if (!is_null($this->mode)) { if (!is_null($this->mode)) {
...@@ -184,6 +213,12 @@ class provisionConfig { ...@@ -184,6 +213,12 @@ class provisionConfig {
} }
} }
// allow overriding w.r.t locking
function file_put_contents($filename, $text) {
provision_file()->file_put_contents($filename, $text)
->succeed('Generated config ' . (empty($this->description) ? $filename : $this->description), 'success');
}
/** /**
* Remove configuration file as specified by filename(). * Remove configuration file as specified by filename().
*/ */
...@@ -371,3 +406,81 @@ class provisionConfig_drushrc_site extends provisionConfig_drushrc { ...@@ -371,3 +406,81 @@ class provisionConfig_drushrc_site extends provisionConfig_drushrc {
} }
} }
class provisionConfig_data_store extends provisionConfig {
public $template = 'data_store.tpl.php';
public $key = 'record';
private $locked = FALSE;
protected $fp = null;
public $records = array();
protected $loaded_records = array();
protected $mode = 0400;
function __construct($owner, $data = array()) {
parent::__construct($owner, $data);
$this->init_records = $data;
$this->load_data();
}
function __destruct() {
// release the file lock if we have it.
$this->close();
}
function open() {
if (!$this->fp) {
$this->fp = fopen($this->filename(), "w");
}
}
function lock() {
if (!$this->locked) {
$this->open();
flock($this->fp, LOCK_EX);
// Do one last load before setting our locked status.
$this->load_data();
$this->locked = TRUE;
}
}
function file_put_contents($filename, $text) {
$this->lock();
fwrite($this->fp, $text);
fflush($this->fp);
$this->close();
}
function unlock() {
if ($this->locked && $this->fp) {
flock($this->fp, LOCK_UN);
$this->locked = FALSE;
}
}
function close() {
if ($this->fp) {
fclose($this->fp);
}
}
function load_data() {
if (!$this->locked) {
// Once we have the lock we dont need to worry about it changing
// from under us.
if (file_exists($this->filename()) && is_readable($this->filename())) {
include($this->filename());
$this->loaded_records = ${$this->key};
}
}
}
function process() {
$this->data['records'] = array_merge($this->loaded_records, $this->records);
}
}
...@@ -4,5 +4,3 @@ ...@@ -4,5 +4,3 @@
print "\n\$options['$key'] = ". var_export(${$key}, TRUE) .';'; print "\n\$options['$key'] = ". var_export(${$key}, TRUE) .';';
} }
?> ?>
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