Switchboard
  • Namespace
  • Class
  • Tree

Namespaces

  • Fluxsauce
    • Switchboard

Classes

  • EnvDb
  • Environment
  • Persistent
  • Project
  • Provider
  • ProviderAcquia
  • ProviderPantheon
  • Site
  • Sqlite
  1 <?php
  2 /**
  3  * @file
  4  * Acquia specific API interactions.
  5  */
  6 
  7 namespace Fluxsauce\Switchboard;
  8 
  9 /**
 10  * Acquia specific API interactions.
 11  * @package Fluxsauce\Switchboard
 12  */
 13 class ProviderAcquia extends Provider {
 14   /**
 15    * @var string Machine name of the Provider.
 16    */
 17   protected $name = 'acquia';
 18 
 19   /**
 20    * @var string Human readable label for the Provider.
 21    */
 22   protected $label = 'Acquia';
 23 
 24   /**
 25    * @var string Homepage URL for the provider.
 26    */
 27   protected $homepage = 'http://www.acquia.com/';
 28 
 29   /**
 30    * @var string Endpoint URL for the provider.
 31    */
 32   protected $endpoint = 'https://cloudapi.acquia.com/v1';
 33 
 34   /**
 35    * A mapping function that calls the appropriate API to populate a field.
 36    *
 37    * @param string $site_name
 38    *   Machine name of the site in question.
 39    * @param string $field
 40    *   Name of the field to populate.
 41    *
 42    * @return string
 43    *   The value of the field.
 44    * @throws \Exception
 45    *   Unknown field name.
 46    */
 47   public function siteGetField($site_name, $field) {
 48     switch ($field) {
 49       // No API required.
 50       case 'name':
 51       case 'provider':
 52         break;
 53 
 54       case 'unixUsername':
 55       case 'vcsUrl':
 56       case 'vcsType':
 57       case 'vcsProtocol':
 58       case 'uuid':
 59       case 'title':
 60       case 'sshPort':
 61         $this->apiGetSite($site_name);
 62         break;
 63 
 64       case 'realm':
 65         $this->apiGetSites();
 66         break;
 67 
 68       case 'environments':
 69         $this->apiGetSiteEnvironments($site_name);
 70         break;
 71 
 72       default:
 73         throw new \Exception('Unknown field ' . $field . ' in ' . __CLASS__);
 74     }
 75     return $this->sites[$site_name]->$field;
 76   }
 77 
 78   /**
 79    * Provider specific options for Requests.
 80    *
 81    * @return array
 82    *   Options for the request; see Requests::request for details.
 83    */
 84   public function requestsOptionsCustom() {
 85     $email = drush_cache_get('email', $this->drushCacheBinAuthName());
 86     $password = drush_cache_get('password', $this->drushCacheBinAuthName());
 87     $options = array(
 88       'auth' => new \Requests_Auth_Basic(array(
 89         $email->data,
 90         $password->data,
 91       )),
 92     );
 93     return $options;
 94   }
 95 
 96   /**
 97    * Log in to target Provider.
 98    *
 99    * @param string $email
100    *   The email address of the user.
101    * @param string $password
102    *   The password of the user.
103    *
104    * @return bool
105    *   Indicates success.
106    */
107   public function authLogin($email, $password) {
108     drush_cache_clear_all('*', $this->drushCacheBinAuthName(), TRUE);
109     drush_cache_set('email', $email, $this->drushCacheBinAuthName());
110     drush_cache_set('password', $password, $this->drushCacheBinAuthName());
111     return TRUE;
112   }
113 
114   /**
115    * Helper function to get the cached email.
116    *
117    * @return mixed
118    *   The password in question or NULL.
119    */
120   protected function authEmailGet() {
121     $email = drush_cache_get('email', $this->drushCacheBinAuthName());
122     if (isset($email->data)) {
123       return $email->data;
124     }
125   }
126 
127   /**
128    * Helper function to get the cached password.
129    *
130    * @return mixed
131    *   The password in question or NULL.
132    */
133   protected function authPasswordGet() {
134     $password = drush_cache_get('password', $this->drushCacheBinAuthName());
135     if (isset($password->data)) {
136       return $password->data;
137     }
138   }
139 
140   /**
141    * Determine whether a user is logged-in to a Provider.
142    *
143    * @return bool
144    *   TRUE if they are.
145    */
146   public function authIsLoggedIn() {
147     $email = drush_cache_get('email', $this->drushCacheBinAuthName());
148     $password = drush_cache_get('password', $this->drushCacheBinAuthName());
149     return (isset($email->data) && isset($password->data)) ? TRUE : FALSE;
150   }
151 
152   /**
153    * Populate available Sites from a Provider.
154    */
155   public function apiGetSites() {
156     $result = switchboard_request($this, array(
157       'method' => 'GET',
158       'resource' => '/sites',
159     ));
160     $site_names = json_decode($result->body);
161     $sites = array();
162     foreach ($site_names as $site_data) {
163       list($realm, $site_name) = explode(':', $site_data);
164       $site = new Site($this->name, $site_name);
165       $site->realm = $realm;
166       $site->update();
167       $this->sites[$site->name] = $site;
168     }
169   }
170 
171   /**
172    * Perform an API call to get site information from a Provider.
173    *
174    * @param string $site_name
175    *   The name of the site in question.
176    */
177   public function apiGetSite($site_name) {
178     $site = new Site('acquia', $site_name);
179     $result = switchboard_request($this, array(
180       'method' => 'GET',
181       'resource' => '/sites/' . $site->realm . ':' . $site_name,
182     ));
183     $site_info = json_decode($result->body);
184     $site->update(array(
185       'unixUsername' => $site_info->unix_username,
186       'vcsUrl' => $site_info->vcs_url,
187       'vcsType' => $site_info->vcs_type,
188       'vcsProtocol' => 'git',
189       'uuid' => $site_info->uuid,
190       'title' => $site_info->title,
191       'sshPort' => 22,
192     ));
193     $this->sites[$site_name] = $site;
194   }
195 
196   /**
197    * Populate available Site Environments from a Provider.
198    *
199    * @param string $site_name
200    *   The machine name of the site in question.
201    */
202   public function apiGetSiteEnvironments($site_name) {
203     $site =& $this->sites[$site_name];
204     $result = switchboard_request($this, array(
205       'method' => 'GET',
206       'resource' => '/sites/' . $site->realm . ':' . $site_name . '/envs',
207     ));
208     $environment_data = json_decode($result->body);
209     foreach ($environment_data as $environment) {
210       $new_environment = new Environment($site->id, $environment->name);
211       $new_environment->branch = $environment->vcs_path;
212       $new_environment->host = $environment->ssh_host;
213       $new_environment->username = "$site_name.$environment->name";
214       $new_environment->update();
215       $site->environmentAdd($new_environment);
216     }
217   }
218 
219   /**
220    * Get and populate list of Databases for a particular Environment.
221    *
222    * @param string $site_name
223    *   The machine name of the Site.
224    * @param string $env_name
225    *   The machine name of the Site Environment.
226    */
227   public function apiGetSiteEnvDbs($site_name, $env_name) {
228     $site =& $this->sites[$site_name];
229     $env =& $site->environments[$env_name];
230     $result = switchboard_request($this, array(
231       'method' => 'GET',
232       'resource' => '/sites/' . $site->realm . ':' . $site_name . '/envs/' . $env_name . '/dbs',
233     ));
234     $db_data = json_decode($result->body);
235     foreach ($db_data as $db) {
236       $new_db = new EnvDb($env->id, $db->instance_name);
237       $new_db->update();
238       $env->dbAdd($new_db);
239     }
240   }
241 
242   /**
243    * Get a list of database backups for a particular Site Environment.
244    *
245    * @param string $site_name
246    *   The machine name of the Site.
247    * @param string $env_name
248    *   The machine name of the Site Environment.
249    *
250    * @return array
251    *   An array of Backup arrays keyed by the timestamp. Each Backup
252    *   array has the following keys:
253    *   - 'filename'
254    *   - 'url'
255    *   - 'timestamp'
256    */
257   public function apiGetSiteEnvDbBackups($site_name, $env_name) {
258     $site = $this->sites[$site_name];
259     $result = switchboard_request($this, array(
260       'method' => 'GET',
261       'resource' => '/sites/' . $site->realm . ':' . $site_name . '/envs/' . $env_name . '/dbs/' . $site_name . '/backups',
262     ));
263     $backup_data = json_decode($result->body);
264 
265     $backups = array();
266     foreach ($backup_data as $backup) {
267       $backups[$backup->completed] = array(
268         'filename' => end(explode('/', $backup->path)),
269         'url' => '',
270         'timestamp' => $backup->completed,
271         'id' => $backup->id,
272       );
273     }
274     arsort($backups);
275     return $backups;
276   }
277 
278   /**
279    * Helper function to get the latest database backup.
280    *
281    * @param string $site_name
282    *   The machine name of the Site in question.
283    * @param string $env_name
284    *   The machine name of the Site Environment in question.
285    *
286    * @return array
287    *   A backup array as defined in apiGetSiteEnvDbBackups().
288    */
289   public function getSiteEnvDbBackupLatest($site_name, $env_name) {
290     $site = $this->sites[$site_name];
291     $backup = parent::getSiteEnvDbBackupLatest($site_name, $env_name);
292     $backup['url'] = 'https://cloudapi.acquia.com/v1/sites/' . $site->realm . ':' . $site_name . '/envs/' . $env_name . '/dbs/' . $site_name . '/backups/' . $backup['id'] . '/download.json';
293     unset($backup['id']);
294     return $backup;
295   }
296 
297   /**
298    * Download a backup.
299    *
300    * @param array $backup
301    *   An array from apiGetSiteEnvDbBackups().
302    * @param string $destination
303    *   The path to the destination.
304    *
305    * @return string
306    *   The full path to the downloaded backup.
307    */
308   public function apiDownloadBackup($backup, $destination) {
309     drush_log(var_export($backup, TRUE));
310     $destination_tmp = drush_tempnam('download_file');
311     drush_shell_exec("curl --fail -s -L -u " . $this->authEmailGet() . ":" . $this->authPasswordGet() . " --connect-timeout 30 -o %s %s", $destination_tmp, $backup['url']);
312     if (!drush_file_not_empty($destination_tmp) && $file = @file_get_contents($backup['url'])) {
313       @file_put_contents($destination_tmp, $file);
314     }
315     if (!drush_file_not_empty($destination_tmp)) {
316       return drush_set_error('SWITCHBOARD_ACQUIA_BACKUP_DL_FAIL', dt('Unable to download!'));
317     }
318     $destination_path = $destination . DIRECTORY_SEPARATOR . $backup['filename'];
319     drush_move_dir($destination_tmp, $destination_path, TRUE);
320     return $destination_path;
321   }
322 
323   /**
324    * Get the remote path to files for a particular Site Environment.
325    *
326    * @param string $site_name
327    *   The machine name of the Site in question.
328    * @param string $env_name
329    *   The machine name of the Site Environment in question.
330    *
331    * @return string
332    *   The full path of the files directory.
333    */
334   public function getFilesPath($site_name, $env_name) {
335     return "/mnt/files/$site_name.$env_name/sites/default/files";
336   }
337 }
338 
Switchboard API documentation generated by ApiGen 2.8.0