Service.php 9.87 KB
Newer Older
1
<?php
2 3 4 5
/**
 * @file
 * The base Provision service class.
 */
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26

require_once DRUSH_BASE_PATH . '/commands/core/rsync.core.inc';


class Provision_Service extends Provision_ChainedState {

  /**
   * The server this service is associated to
   */
  protected $server = '@server_master';

  /**
   * The context in which this service stores its data
   *
   * This is usually an object made from a class derived from the
   * Provision_Context base class
   *
   * @see Provision_Context
   */
  public $context;

27 28
  protected $service = NULL;
  protected $application_name = NULL;
29 30 31 32 33 34

  protected $has_restart_cmd = FALSE;
  protected $has_port = FALSE;

  protected $configs = array();

35

36
  protected $config_cache = array();
37
  private $_config = array();
38 39 40 41 42


  /**
   * Implement the __call magic method.
   *
43
   * This implementation is really simple. It simply return NULL if the
44 45 46 47 48 49 50 51 52 53 54 55 56 57
   * method doesn't exist.
   *
   * This is used so that we can create methods for drush commands, and
   * can fail safely.
   */
  function __call($name, $args = array()) {
    return provision::method_invoke($this, $name, $args);
  }


  function init() {

  }

helmo's avatar
helmo committed
58 59 60
  /**
   * All services have the ability to have an associated restart command and listen port.
   *
61 62 63
   * If services require a fixed path config file, implement the function symlink_service() inside this method.
   *
   * @See Provision_Service_http_public::init_server()
helmo's avatar
helmo committed
64
   */
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
  function init_server() {
    if (!is_null($this->service)) {
      if ($this->has_port) {
        $this->server->setProperty($this->service . '_port', $this->default_port());
      }
      if ($this->has_restart_cmd) {
        $this->server->setProperty($this->service . '_restart_cmd', $this->default_restart_cmd());
      }
    }
    return TRUE;
  }

  function init_platform() {

  }

  function init_site() {

  }


  function default_port() {
    return false;
  }

  function default_restart_cmd() {
    return false;
  }

  /**
   * Set the currently active configuration object.
   *
   * @param $config
   *   String: Name of config file. The key to the $this->configs array.
   * @param $data
   *   Any optional information to be made available to templates. If a string, it will be
   *   turned into an array with the 'name' property the value of the string.
   */
  function config($config, $data = array()) {
104
    $this->_config = array();
105

106 107 108 109 110 111 112 113 114 115 116 117 118 119
    if (!isset($this->configs[$config])) {
      $service = (!is_null($this->application_name)) ? $this->application_name : $this->service;
      drush_log(dt('%service has no %name config file', array(
        '%service' => $service,
        '%name' => $config))
      );
      return $this;
    }

    if (!is_array($data) && is_string($data)) {
      $data = array('name' => $data);
    }

    if (!isset($this->config_cache[$this->context->name][$config])) {
120
      $this->config_cache[$this->context->name][$config] = array();
121
      foreach ((array) $this->configs[$config] as $class) {
122
        $this->config_cache[$this->context->name][$config][] = new $class($this->context, array_merge($this->config_data($config), $data));
123 124 125 126 127 128 129 130 131 132 133 134 135 136
      }
    }

    if (isset($this->config_cache[$this->context->name][$config])) {
      $this->_config = $this->config_cache[$this->context->name][$config];
    }

    return $this;
  }

  /**
   * Unlink the currently active config file.
   */
  function unlink() {
137 138 139 140
    foreach ($this->_config as $config) {
      if (is_object($config)) {
        $config->unlink();
      }
141 142 143 144 145 146 147 148 149
    }

    return $this;
  }

  /**
   * Write the currently active config file.
   */
  function write() {
150 151 152 153
    foreach ($this->_config as $config) {
      if (is_object($config)) {
        $config->write();
      }
154 155 156 157 158 159 160 161
    }

    return $this;
  }

  /**
   * Set a record on the data store of the currently active config file (if applicable).
   */
162
  function record_set($arg1, $arg2 = NULL) {
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
    foreach ($this->_config as $config) {
      if (is_object($config)) {
        if (is_object($config->store)) {
          if (is_array($arg1)) {
            $config->store->records = array_merge($config->store->records, $arg1);
          }
          elseif (!is_numeric($arg1)) {
            if (is_array($arg2)) {
              if (!isset($config->store->loaded_records[$arg1])
                  || !is_array($config->store->loaded_records[$arg1])) {
                $config->store->loaded_records[$arg1] = array();
              }
              if (!isset($config->store->records[$arg1])
                  || !is_array($config->store->records[$arg1])) {
                $config->store->records[$arg1] = array();
              }
              $config->store->records[$arg1] = array_merge($config->store->loaded_records[$arg1], $config->store->records[$arg1], $arg2);
180
            }
181 182
            else {
              $config->store->records[$arg1] = $arg2;
183 184 185 186 187 188 189 190 191 192 193 194
            }
          }
        }
      }
    }
    return $this;
  }

  /**
   * Delete a record from the data store of the currently active config file (if applicable).
   */
  function record_del($record) {
195
    return $this->record_set($record, NULL);
196 197 198 199 200 201
  }

  /**
   * Check if a record exists in the data store of the currently active config file (if applicable).
   */
  function record_exists($record) {
202 203 204 205 206
    foreach ($this->_config as $config) {
      if (is_object($config)) {
        if (is_object($config->store)) {
          return array_key_exists($record, $config->store->merged_records());
        }
207 208 209 210 211 212 213 214
      }
    }
    return FALSE;
  }

  /**
   * Fetch record(s) from the data store of the currently active config file (if applicable).
   */
215
  function record_get($key = NULL, $default = NULL) {
216 217 218 219
    foreach ($this->_config as $config) {
      if (is_object($config)) {
        if (is_object($config->store)) {
          $records = $config->store->merged_records();
220

221 222 223
          if (is_null($key)) {
            return $records;
          }
224

225 226 227
          if (isset($records[$key])) {
            return $records[$key];
          }
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
        }
      }
    }
    return $default;
  }


  /**
   * Generate a configuration file.
   *
   * This method will fetch the class to instantiate from the internal
   * $this->configs control array.
   */
  function create_config($config, $data = array()) {
    $this->config($config, $data)->write();
  }

  /**
   * Delete a configuration file.
247
   *
248 249
   * This method will fetch the class to instantiate from the internal
   * $this->configs control array.
250 251
   *
   * @return the return value of unlink(), which is usually the file object
252 253
   */
  function delete_config($config, $data = array()) {
254
    return $this->config($config, $data)->unlink();
255 256 257
  }

  /**
anarcat's avatar
anarcat committed
258
   * Fetch extra information the service wants to pass to the config file classes.
259
   */
260
  function config_data($config = NULL, $class = NULL) {
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
    $data = array();
    // Always pass the server this service is running on to configs.
    $data['server'] = $this->server;

    if (!is_null($this->application_name)) {
      // This value may be useful to standardize paths in config files.
      $data['application_name'] = $this->application_name;
    }
    return $data;
  }


  /**
   * Restart the service using the provided restart command.
   */
  function restart() {
    // Only attempt to restart real services can have restart commands.
    if (!is_null($this->service) && $this->has_restart_cmd) {
      $service = (!is_null($this->application_name)) ? $this->application_name : $this->service;

      // Only attempt to restart if the command has been filled in.
      if ($cmd = $this->server->{"{$this->service}_restart_cmd"}) {
        if ($this->server->shell_exec($cmd)) {
          drush_log(dt('%service on %server has been restarted', array(
            '%service' => $service,
            '%server' => $this->server->remote_host))
          );

          return TRUE;
        }
        else {
292
          drush_log(dt('%service on %server could not be restarted.' .
293 294
            ' Changes might not be available until this has been done. (error: %msg)', array(
            '%service' => $service,
295
            '%server' => $this->server->remote_host,
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
            '%msg' => join("\n", drush_shell_exec_output()))), 'warning');
        }
      }
    }
    return FALSE;
  }

  function __construct($server) {
    $this->server = is_object($server) ? $server : d($server);
  }

  /**
   * Set the currently active context of the service.
   *
   * @arg mixed $context
   *    the context to store this services data into. this can be an
   *    object, or a string in which case the object will be loaded
   *    dynamically with d()
   *
   * @see d()
   */
  function setContext($context) {
    $this->context = is_object($context) ? $context : d($context);
  }

  /**
   * Sync filesystem changes to the server hosting this service.
   */
  function sync($path = NULL, $additional_options = array()) {
    return $this->server->sync($path, $additional_options);
  }

  function fetch($path = NULL) {
    return $this->server->fetch($path);
  }

  function verify() {
    return TRUE;
  }

  /**
   * Return service-specific configuration options for help.
   *
   * @return
340
   *   array('option' => 'description')
341 342 343 344
   */
  static function option_documentation() {
    return array();
  }
345 346 347

  /**
   * Save symlink for this server from /var/aegir/config/APPLICATION_NAME.conf -> /var/aegir/config/SERVER/APPLICATION_NAME.conf
348 349 350 351
   *
   * If service requires a fixed path config file, implement this function in the init_server() method.
   *
   * @See Provision_Service_http_public::init_server()
352 353 354 355 356 357 358 359 360 361 362 363 364
   */
  function symlink_service() {
    $file = $this->application_name . '.conf';
    // We link the app_name.conf file on the remote server to the right version.
    $cmd = sprintf('ln -sf %s %s',
      escapeshellarg($this->server->config_path . '/' . $file),
      escapeshellarg($this->server->aegir_root . '/config/' . $file)
    );

    if ($this->server->shell_exec($cmd)) {
      drush_log(dt("Created symlink for %file on %server", array(
        '%file' => $file,
        '%server' => $this->server->remote_host,
365
      )), 'notice');
366 367
    };
  }
368
}