/** * Requests API data and returns aliases * * @return string */ private function getAliases() { $request = new Request(); $user = new User(); $path = 'drush_aliases'; $method = 'GET'; $response = $request->request('users', Session::getValue('user_id'), $path, $method); eval(str_replace('<?php', '', $response['data']->drush_aliases)); $formatted_aliases = substr($response['data']->drush_aliases, 0, -1); $sites_object = new Sites(); $sites = $sites_object->all(); foreach ($sites as $site) { $environments = $site->environments->all(); foreach ($environments as $environment) { $key = $site->get('name') . '.' . $environment->get('id'); if (isset($aliases[$key])) { break; } try { $formatted_aliases .= PHP_EOL . " \$aliases['{$key}'] = "; $formatted_aliases .= $this->constructAlias($environment); } catch (TerminusException $e) { continue; } } } $formatted_aliases .= PHP_EOL; return $formatted_aliases; }
/** * Retrieves current version number from repository and saves it to the cache * * @return string The version number */ function checkCurrentVersion() { $request = new Request(); $url = 'https://api.github.com/repos/pantheon-systems/cli/releases'; $url .= '?per_page=1'; $response = $request->simpleRequest($url, array('absolute_url' => true)); $release = array_shift($response['data']); Terminus::getCache()->putData('latest_release', array('version' => $release->name, 'check_date' => time())); return $release->name; }
/** * Fetches this object from Pantheon * * @param array $options Params to pass to url request * @return TerminusModel $this */ public function fetch(array $options = array()) { $fetch_args = array(); if (isset($options['fetch_args'])) { $fetch_args = $options['fetch_args']; } $options = array_merge(array('options' => array('method' => 'get')), $this->getFetchArgs(), $fetch_args); $results = $this->request->simpleRequest($this->getFetchUrl(), $options); $this->attributes = $results['data']; return $this; }
/** * Execute the login via email/password * * @param string $email Email address associated with a Pantheon account * @param string $password Password for the account * @return bool True if login succeeded * @throws TerminusException */ public function logInViaUsernameAndPassword($email, $password) { if (!Terminus\Utils\isValidEmail($email)) { throw new TerminusException('{email} is not a valid email address.', compact('email'), 1); } $options = array('form_params' => array('email' => $email, 'password' => $password)); $response = $this->request->request('login', '', '', 'POST', $options); if ($response['status_code'] != '200') { throw new TerminusException('Login unsuccessful for {email}', compact('email'), 1); } $this->logger->info('Logged in as {uuid}.', array('uuid' => $response['data']->user_id)); $this->setInstanceData($response['data']); return true; }
/** * Fetches organizational memberships for user * * @return array $data Properties below: * [string] id UUID of membership join * [string] name Name of organization * [string] type Always "organization" */ private function fetchUserOrganizations() { $response = $this->request->pagedRequest('users/' . Session::getValue('user_uuid') . '/memberships/organizations'); $data = array(); foreach ($response['data'] as $membership) { if ($membership->role == 'unprivileged') { // Users with unprivileged role in organizations can't see organization // sites, but must be added to the team continue; } $data[] = array('id' => $membership->id, 'name' => $membership->organization->profile->name, 'type' => 'organization'); } return $data; }
/** * Simplified request method for Pantheon API * * @param [string] $path API path (URL) * @param [array] $options Options for the request * [string] method GET is default * [mixed] data Native PHP data structure (e.g. int, string array, or * simple object) to be sent along with the request. Will be encoded as * JSON for you. * @return [array] $data */ public static function simple_request($path, $options = array()) { $req_options = array(); $method = 'get'; if (isset($options['method'])) { $method = $options['method']; } if (isset($options['data'])) { $req_options['body'] = json_encode($options['data']); $req_options['headers'] = array('Content-type' => 'application/json'); } $url = 'https://' . TERMINUS_HOST . '/api/' . $path; if (Session::getValue('session')) { $req_options['cookies'] = array('X-Pantheon-Session' => Session::getValue('session')); $req_options['verify'] = false; } try { $resp = Request::send($url, $method, $req_options); } catch (Guzzle\Http\Exception\BadResponseException $e) { \Terminus::error('Request Failure: %s', $e->getMessage()); return; } $json = $resp->getBody(true); $data = array('info' => $resp->getInfo(), 'headers' => $resp->getRawHeaders(), 'json' => $json, 'data' => json_decode($json), 'status_code' => $resp->getStatusCode()); return $data; }
/** * Retrieves a single backup or downloads it as requested * * @params [array] $assoc_args Parameters and flags from the command line * @return [string] $url->url */ private function getBackup($assoc_args) { $site = $this->sites->get(Input::sitename($assoc_args)); $env = $site->environments->get(Input::env(array('args' => $assoc_args, 'site' => $site))); $file = Input::optional('file', $assoc_args, false); if ($file) { $backup = $env->backups->getBackupByFileName($file); $element = $backup->getElement(); } else { $element = Input::backupElement(array('args' => $assoc_args)); $latest = (bool) Input::optional('latest', $assoc_args, false); $backups = $env->backups->getFinishedBackups($element); if ($latest) { $backup = array_pop($backups); } else { $context = array('site' => $site->get('name'), 'env' => $env->get('id')); $backup = Input::backup(array('backups' => $backups, 'context' => $context)); } } $url = $backup->getUrl(); if (isset($assoc_args['to'])) { $target = str_replace('~', $_SERVER['HOME'], $assoc_args['to']); if (is_dir($target)) { $filename = Utils\getFilenameFromUrl($url); $target = sprintf('%s/%s', $target, $filename); } $this->log()->info('Downloading ... please wait ...'); if (Request::download($url, $target)) { $this->log()->info('Downloaded {target}', compact('target')); return $target; } else { $this->failure('Could not download file'); } } return $url; }
public static function download($url, $target) { try { $response = Request::download($url, $target); return $target; } catch (Exception $e) { Terminus::error($e->getMessage()); } }
/** * "Wake" a site * * @return [array] $return_data */ public function wake() { $hostnames = $this->getHostnames(); $target = key($hostnames); $response = Request::send("http://{$target}/pantheon_healthcheck", 'GET'); $return_data = array('success' => $response->isSuccessful(), 'time' => $response->getInfo('total_time'), 'styx' => $response->getHeader('X-Pantheon-Styx-Hostname'), 'response' => $response, 'target' => $target); return $return_data; }
/** * Downloads the given URL to the given target * * @param [string] $url Location of file to download * @param [string] $target Location to download file to * @return [void] */ protected function download($url, $target) { try { $response = Request::download($url, $target); return $target; } catch (Exception $e) { $this->log()->error($e->getMessage()); } }
/** * Retrieves current version number from repository and saves it to the cache * * @return [string] $response->name The version number */ protected function checkCurrentVersion() { $url = 'https://api.github.com/repos/pantheon-systems/cli/releases?per_page=1'; $response = Request::send($url, 'GET'); $json = $response->getBody(true); $data = json_decode($json); $release = array_shift($data); $this->cache->put_data('latest_release', array('version' => $release->name, 'check_date' => time())); return $release->name; }