provision.service.inc 5.04 KB
Newer Older
Adrian Rossouw's avatar
Adrian Rossouw committed
1 2
<?php

3
require_once('provision.inc');
4

5 6 7 8
require_once DRUSH_BASE_PATH . '/commands/core/rsync.core.inc';


class provisionService extends provisionChainedState {
9
  protected $server = '@server_master';
10
  public $context;
11

12
  protected $service = null;
13
  protected $application_name = null;
14 15 16 17

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

18
  protected $configs = array();
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40

  // All services have the ability to have an associated restart command and listen port.
  function init() {
    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 default_port() {
    return false;
  }

  function default_restart_cmd() {
    return false;
  }

41 42 43 44 45 46 47
  function get_config($config) {
    foreach ((array) $this->configs[$config] as $class) {
      return new $class($this->context, $this->config_data($config));
    }
    return false;
  }

48 49 50 51 52 53 54 55 56 57
  /**
   * Generate a configuration file.
   *
   * This method will fetch the class to instantiate from the internal
   * $this->configs control array.
   */
  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) {
58
        try {
59 60
          $object = new $class($this->context, $this->config_data($config));
          $object->write();
61 62
        }
        catch (provisionException_continue $e) {
63
          drush_log($e->getMessage());
64 65 66
          // The service or configuration class has signaled that the config file
          // was uneccesary, and we should continue normally.
        }
67 68 69 70 71
      }
    }
  }

  /**
72
   * Delete a configuration file.
73 74 75 76 77 78 79 80
   * 
   * This method will fetch the class to instantiate from the internal
   * $this->configs control array.
   */
  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) {
81
        try {
82 83
          $object = new $class($this->context, $this->config_data($config, $class));
          $object->unlink();
84 85
        }
        catch (provisionException_continue $e) {
86
          drush_log($e->getMessage());
87 88 89
          // The service or configuration class has signaled that the config file
          // was uneccesary, and we should continue normally.
        }
90 91 92 93 94 95 96 97 98
      }
    }
  }

  /**
   * Fetch extra information the service wants to pass to he config file classes.
   */
  function config_data($config = null, $class = null) {
    $data = array();
99
    // Always pass the server this service is running on to configs.
100
    $data['server'] = $this->server;
101 102 103 104 105

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

109

110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
  /**
   * 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 {
          drush_log(dt('%service on %server could not be restarted.'.
            ' Changes might not be available until this has been done. (error: %msg)', array(
            '%service' => $service,
            '%server' => $this->server->remote_host, 
            '%msg' => join("\n", drush_shell_exec_output()))), 'warning');
        }
      }
    }
    return FALSE;
  }
139

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

144 145
  function setContext($context) {
    $this->context = is_object($context) ? $context : d($context);
146 147
  }

drumm's avatar
drumm committed
148 149
  function sync($path = NULL, $additional_options = array()) {
    return $this->server->sync($path, $additional_options);
150 151
  }

152 153
  function verify() {
    return TRUE;
154 155
  }

drumm's avatar
drumm committed
156 157 158 159 160 161 162 163 164
  /**
   * Return service-specific configuration options for help.
   *
   * @return
   *   array('--option' => 'description')
   */
  static function option_documentation() {
    return array();
  }
drumm's avatar
drumm committed
165
}
166

Adrian Rossouw's avatar
Adrian Rossouw committed
167
class provisionService_null extends provisionService {
168 169 170 171 172 173 174 175 176

  function __get($name) {
    return false;
  }

  function __call($name, $args) {
    return false;
  }

177 178 179 180 181 182 183
  /**
   * Null services do not synch files to the remote server,
   * because they have no associated config files.
   */
  function sync() {
    return null;
  }
Adrian Rossouw's avatar
Adrian Rossouw committed
184 185
}