1 <?php
2 3 4 5
6
7 namespace Fluxsauce\Switchboard;
8
9 10 11 12
13 class ProviderAcquia extends Provider {
14 15 16
17 protected $name = 'acquia';
18
19 20 21
22 protected $label = 'Acquia';
23
24 25 26
27 protected $homepage = 'http://www.acquia.com/';
28
29 30 31
32 protected $endpoint = 'https://cloudapi.acquia.com/v1';
33
34 35 36 37 38 39 40 41 42 43 44 45 46
47 public function siteGetField($site_name, $field) {
48 switch ($field) {
49
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 80 81 82 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 98 99 100 101 102 103 104 105 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 116 117 118 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 129 130 131 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 142 143 144 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 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 173 174 175 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 198 199 200 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 221 222 223 224 225 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 244 245 246 247 248 249 250 251 252 253 254 255 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 280 281 282 283 284 285 286 287 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 299 300 301 302 303 304 305 306 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 325 326 327 328 329 330 331 332 333
334 public function getFilesPath($site_name, $env_name) {
335 return "/mnt/files/$site_name.$env_name/sites/default/files";
336 }
337 }
338