Skip to content

Commit

Permalink
Modification to import process to (a) allow creating a site from and …
Browse files Browse the repository at this point in the history
…import

and (b) ensure wordpress import are not improperly tagged as drush archives. Also removes a deprecated function and fixes some spacing in the auth command.
  • Loading branch information
mikevanwinkle committed Jan 7, 2015
1 parent e71d962 commit 934340b
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 87 deletions.
8 changes: 8 additions & 0 deletions php/Terminus/Helpers/Input.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,12 @@ static function menu( $choices, $default = null, $text = "Select one", $return_v
}
return $index;
}

static function string( $args, $key, $label = "Enter") {
if ( isset($args[$key]) ) {
return $args[$key];
}
$string = \Terminus::prompt($label);
return $string;
}
}
6 changes: 5 additions & 1 deletion php/Terminus/Site.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,11 @@ public function notifications() {
*/
public function import($url) {
$path = 'environments/dev/import';
$data = array('url'=>$url,'drush_archive' => 1);
$drush_archive = 1;
if ("wordpress" == $this->information->framework) {
$drush_archive = 0;
}
$data = array('url'=>$url,'drush_archive' => $drush_archive);
$options = array( 'body' => json_encode($data) , 'headers'=>array('Content-type'=>'application/json') );
$response = \Terminus_Command::request('sites', $this->getId(), $path, 'POST', $options);
return $response['data'];
Expand Down
5 changes: 3 additions & 2 deletions php/class-terminus-command.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,14 +237,15 @@ protected function _constructTableForResponse($data,$headers = array()) {
* Example: $this->waitOnWorkflow( "sites", "68b99b50-8942-4c66-b7e3-22b67445f55d", "e4f7e832-5644-11e4-81d4-bc764e111d20");
*/
protected function waitOnWorkflow( $object_name, $object_id, $workflow_id ) {
print "working .";
print "Working .";
Terminus::set_config('nocache',true);
$workflow = self::request( $object_name, $object_id, "workflows/$workflow_id", 'GET' );
$result = $workflow['data']->result;
$desc = $workflow['data']->active_description;
$type = $workflow['data']->type;
$tries = 0;
while( $result !== 'succeeded' AND $tries < 100) {
if ( 'failed' == $result ) Terminus::error("Couldn't complete jobs '{$desc}'".PHP_EOL);
if ( 'failed' == $result ) Terminus::error(PHP_EOL."Couldn't complete jobs: '{$type}'".PHP_EOL);
$workflow = self::request( $object_name, $object_id, "workflows/{$workflow_id}", 'GET' );
$result = $workflow['data']->result;
if (Terminus::get_config('debug')) {
Expand Down
121 changes: 51 additions & 70 deletions php/commands/auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,40 +29,40 @@ class Auth_Command extends Terminus_Command {
* : dump call information when logging in.
*/
public function login( $args, $assoc_args ) {
if ( empty( $args ) ) {
$email = Terminus::prompt( "Your email address?", NULL );
if ( empty( $args ) ) {
$email = Terminus::prompt( "Your email address?", NULL );
}
else {
$email = $args[0];
}

if ( \Terminus\Utils\is_valid_email( $email ) ) {
if ( !isset( $assoc_args['password'] ) ) {
exec("stty -echo");
$password = Terminus::prompt( "Your dashboard password (input will not be shown)" );
exec("stty echo");
Terminus::line();
}
else {
$email = $args[0];
$password = $assoc_args['password'];
}
Terminus::line( "Logging in as $email" );
$data = $this->doLogin($email, $password);

if ( \Terminus\Utils\is_valid_email( $email ) ) {
if ( !isset( $assoc_args['password'] ) ) {
exec("stty -echo");
$password = Terminus::prompt( "Your dashboard password (input will not be shown)" );
exec("stty echo");
Terminus::line();
}
else {
$password = $assoc_args['password'];
}
Terminus::line( "Logging in as $email" );
$data = $this->doLogin($email, $password);

if ( $data != FALSE ) {
if (array_key_exists("debug", $assoc_args)){
$this->_debug(get_defined_vars());
}
//Terminus::line( "Success!" );
Terminus::launch_self("art", array("fist"));
}
else {
Terminus::error( "Login Failed!" );
if ( $data != FALSE ) {
if (array_key_exists("debug", $assoc_args)){
$this->_debug(get_defined_vars());
}
//Terminus::line( "Success!" );
Terminus::launch_self("art", array("fist"));
}
else {
Terminus::error( "Error: invalid email address" );
Terminus::error( "Login Failed!" );
}
}
else {
Terminus::error( "Error: invalid email address" );
}
}

/**
Expand Down Expand Up @@ -112,58 +112,39 @@ private function _checkSession() {
*/
private function doLogin($email,$password)
{
if (Terminus::is_test()) {
$data = array(
'user_uuid' => '77629472-3050-457c-8c3d-32b2cabf992b',
'session' => '77629472-3050-457c-8c3d-32b2cabf992b:7dc42f40-65f8-11e4-b314-bc764e100eb1:ZHR0TgtQYsKcOOwMOd0tk',
'session_expire_time' => '1417727066',
'email' => '[email protected]',
);
return $data;
}

$options = array(
'body' => json_encode(array(
'email' => $email,
'password' => $password,
)),
'headers' => array('Content-type'=>'application/json'),
);

$response = Terminus_Command::request('login','','','POST',$options);
if ( !$response OR '200' != @$response['info']['http_code'] ) {
\Terminus::error("[auth_error]: unsuccessful login");
}

// Prepare credentials for storage.
if (Terminus::is_test()) {
$data = array(
'user_uuid' => $response['data']->user_id,
'session' => $response['data']->session,
'session_expire_time' => $response['data']->expires_at,
'email' => $email,
'user_uuid' => '77629472-3050-457c-8c3d-32b2cabf992b',
'session' => '77629472-3050-457c-8c3d-32b2cabf992b:7dc42f40-65f8-11e4-b314-bc764e100eb1:ZHR0TgtQYsKcOOwMOd0tk',
'session_expire_time' => '1417727066',
'email' => '[email protected]',
);
// creates a session instance
Session::instance()->setData($data);
return $data;
}

public function getUUIDFromSession() {
if( !$this->session ) {
throw new Exception("Need a valid session.");
}

$endpoint = sprintf("https://%s/api/user",TERMINUS_HOST);
$response = Request::send($endpoint, "GET", array(
'allow_redirects'=>true,
'cookies'=>
array('X-Pantheon-Session'=>$this->session)
)
$options = array(
'body' => json_encode(array(
'email' => $email,
'password' => $password,
)),
'headers' => array('Content-type'=>'application/json'),
);

$user = json_decode($response->getBody(TRUE));
$this->cache->put_data("user",$user);
return $user->id;
$response = Terminus_Command::request('login','','','POST',$options);
if ( !$response OR '200' != @$response['info']['http_code'] ) {
\Terminus::error("[auth_error]: unsuccessful login");
}

// Prepare credentials for storage.
$data = array(
'user_uuid' => $response['data']->user_id,
'session' => $response['data']->session,
'session_expire_time' => $response['data']->expires_at,
'email' => $email,
);
// creates a session instance
Session::instance()->setData($data);
return $data;
}
}

Expand Down
8 changes: 5 additions & 3 deletions php/commands/site.php
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ public function clone_env($args, $assoc_args) {
\Terminus::confirm($confirm);

if ( !$this->envExists($site_id, $to_env) ) {
\Terminus::error("The %s environment has not been created yet. run `terminus site create-env [--site=<env>]`");
\Terminus::error("The %s environment has not been created yet. run `terminus site create-env [--site=<env>]`", $to_env);
}

if ($db) {
Expand All @@ -512,7 +512,7 @@ public function clone_env($args, $assoc_args) {
}


// @todo this should be moved to a namespaced class CloneObject
// @todo this should be moved to a namespaced class CloneResource
private function cloneObject($to_env, $from_env, $site_id, $object_type) {
$path = sprintf("environments/%s/%s", $to_env, $object_type);

Expand Down Expand Up @@ -843,7 +843,9 @@ public function import($args, $assoc_args) {
$url = $assoc_args['url'];
$import = $site->import($url);
if ($import) {
Terminus::success("Import queued");
Terminus::line('Import started, you can now safely kill this script without interfering.');
$this->waitOnWorkflow('sites', $site->getId(), $import->id);
Terminus::success("Import complete");
}
}

Expand Down
63 changes: 52 additions & 11 deletions php/commands/sites.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,10 @@ public function __construct() {
}

/**
* ## OPTIONS
* Show sites
*
* ## OPTIONS
*
* [--nocache]
* : Get a fresh list of sites from the server side.
* [--bash]
* : Get bash friendly output
* [--json]
* : Json output
*/
public function show($args, $assoc_args) {
$sites = SiteFactory::instance();
Expand All @@ -50,18 +46,19 @@ public function show($args, $assoc_args) {

/**
* Create a new site
* @package 2.0
*
* ## OPTIONS
*
* [--product]
* [--product=<productid>]
* : Specify the product to create
* [--name]
* [--name=<name>]
* : Name of the site to create (machine-readable)
* [--label]
* [--label=<label>]
* : Label for the site
* [--org=<org>]
* : UUID of organization to add this site to
* [--import=<url>]
* : A url to import a valid archive from
*/
public function create($args, $assoc_args) {
$sites = SiteFactory::instance();
Expand Down Expand Up @@ -91,13 +88,57 @@ public function create($args, $assoc_args) {
$site = $response['data'];
$workflow_id = $site->id;
$result = $this->waitOnWorkFlow( 'sites', $site->site_id, $workflow_id );

if( $result ) {
Terminus::success("Pow! You created a new site!");
$this->cache->flush('sites');
}

if (isset($assoc_args['import'])) {
Terminus::launch_self('site', array('import'), array('url'=>$assoc_args['import'], 'site'=>$data['name'], 'nocache' => True));
}

return true;
}

/**
* Import a new site
* @package 2.0
*
* ## OPTIONS
*
* [--url=<url>]
* : Url of archive to import
*
* [--name=<name>]
* : Name of the site to create (machine-readable)
*
* [--label=<label>]
* : Label for the site
*
* [--org=<org>]
* : UUID of organization to add this site to
*
* @subcommand create-from-import
*/
public function import($args, $assoc_args) {
$url = Input::string($assoc_args, 'url', "Url of archive to import");
$label = Input::string($assoc_args, 'label', "Human readable label for the site");
$slug = $this->sanitizeName( $label );
$name = Input::string($assoc_args, 'name', "Machine name of the site; used as part of the default URL [ if left blank will be $slug]");
$name = $name ? $name : $slug;
$organization = Terminus::menu(Input::orglist(), false, "Choose organization");
if (!$url) {
Terminus::error("Please enter a url.");
}
Terminus::launch_self('sites', array('create'), array(
'label' => $label,
'name' => $name,
'org' => $organization,
));
Terminus::launch_self('site', array('import'), array('url'=>$url, 'site'=>$name, 'nocache' => True));
}

/**
* Delete site
* --site=<site>
Expand Down

0 comments on commit 934340b

Please sign in to comment.