Skip to content

Commit

Permalink
Merge pull request #412 from pantheon-systems/site_workflows
Browse files Browse the repository at this point in the history
Added `site workflows`, removed `site jobs` and `site notifications`
  • Loading branch information
TeslaDethray committed Aug 20, 2015
2 parents 6b88af9 + 8ddc55b commit fbf9b7e
Show file tree
Hide file tree
Showing 9 changed files with 356 additions and 100 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ All notable changes to this project starting with the 0.6.0 release will be docu
- `site delete-env` now has an optional --remove-branch flag (#395)
- Environment variables for --site (TERMINUS_SITE), --org (TERMINUS_ORG), --env (TERMINUS_ENV), and user (TERMINUS_USER). User may import these themselves, or add them to the .env file in the user's current directory. (#407)
- `site tags <add|remove> --site=<site> --org=<org> --tag=<tag>` command will add tags to an organization site (#417)
- `site workflows` commmand will show all workflows run on the site and their statuses (replaces `site jobs` and `site notifications`) (#412)

### Fixed
- `organizations sites` shows all the organization's sites, not just the first 100 (#371)
Expand All @@ -28,6 +29,8 @@ All notable changes to this project starting with the 0.6.0 release will be docu

### Deprecated
- `sites delete` will be removed in v1.0.0 (#370)
- `site jobs` will be removed in v0.7.0 (#412)
- `site notifications` will be removed in v0.7.0 (#412)

##[0.6.1] - 2015-08-11
### Fixed
Expand Down
14 changes: 14 additions & 0 deletions php/Terminus/Collections/Workflows.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,20 @@ public function create($type, $options = array()) {
return $model;
}

public function fetchPaged() {
$results = TerminusCommand::paged_request($this->url());

foreach ($results['data'] as $model_data) {
$model = new Workflow($model_data, array(
'owner' => $this->owner,
'owner_type' => $this->owner_type,
));
$this->add($model);
}

return $this;
}

public function fetch() {
$results = TerminusCommand::simple_request($this->url());

Expand Down
8 changes: 8 additions & 0 deletions php/Terminus/Models/Workflow.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,12 @@ public function logMessages() {
\Terminus::error(sprintf('[%s] %s', $message->level, $message->message));
}
}

public function get($attribute) {
if(isset($this->attributes->$attribute)) {
return $this->attributes->$attribute;
}
return null;
}

}
33 changes: 11 additions & 22 deletions php/Terminus/Site.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ class Site {
public $environments = array();
public $environmentsCollection;
public $information;
public $jobs;
public $metadata;
public $workflows;

Expand Down Expand Up @@ -258,17 +257,6 @@ public function createBranch($branch) {
return $response['data'];
}

/**
* fetch jobs
**/
public function jobs() {
if (!$this->jobs) {
$response = \TerminusCommand::request('sites', $this->getId(), 'jobs', 'GET');
$this->jobs = $response['data'];
}
return $this->jobs;
}

/**
* Retrieve New Relic Info
*/
Expand All @@ -278,16 +266,6 @@ public function newRelic() {
return $response['data'];
}

/**
* fetch notifications
**/
public function notifications() {
$path = 'notifications';
$data = \TerminusCommand::request('sites', $this->getId(), $path, 'GET');
return $data['data'];
}


/**
* Import Archive
*/
Expand Down Expand Up @@ -548,6 +526,7 @@ public function get($attribute) {
}

/**
<<<<<<< HEAD
* Returns tags from the site/org join
*
* @return [array] $tags Tags in string format
Expand Down Expand Up @@ -584,4 +563,14 @@ public function getOrganizations() {
$orgs = $this->org_memberships->fetch()->all();
return $orgs;
}

/**
* Retrieves a list of workflows run and running on this site
*
* @return [array] $workflows An array of Workflow objects
*/
public function getWorkflows() {
$workflows = $this->workflows->fetchPaged()->all();
return $workflows;
}
}
2 changes: 1 addition & 1 deletion php/class-terminus-command.php
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ protected function waitOnWorkflow($object_name, $object_id, $workflow_id) {
}
} else {
Terminus::error(
PHP_EOL . "Couldn't complete jobs: '$type'" . PHP_EOL
PHP_EOL . "Couldn't complete workflows: '$type'" . PHP_EOL
);
}
}
Expand Down
139 changes: 63 additions & 76 deletions php/commands/site.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,33 @@ public function dashboard($args, $assoc_args) {
}
}

/**
* Delete a site from pantheon
*
* ## OPTIONS
* [--site=<site>]
* : ID of the site you want to delete
*
* [--force]
* : to skip the confirmations
*/
public function delete($args, $assoc_args) {
$sitename = Input::sitename($assoc_args);
$site_id = $this->sitesCache->findID($sitename);
$site_to_delete = new Site($site_id);

if (!isset($assoc_args['force']) AND !Terminus::get_config('yes')) {
// if the force option isn't used we'll ask you some annoying questions
Terminus::confirm( sprintf( "Are you sure you want to delete %s?", $site_to_delete->information->name ));
Terminus::confirm( "Are you really sure?" );
}
Terminus::line( sprintf( "Deleting %s ...", $site_to_delete->information->name ) );
$response = \TerminusCommand::request( 'sites', $site_to_delete->id, '', 'DELETE' );

$this->sitesCache->remove($sitename);
Terminus::success("Deleted %s!", $sitename);
}

/**
* Retrieve information about the site
*
Expand Down Expand Up @@ -1193,30 +1220,6 @@ public function instrument($args, $assoc_args) {
}
}

/**
* List a site's job history
*
* ## OPTIONS
*
* [--site=<site>]
* : Site to deploy from
**/
public function jobs($args, $assoc_args) {
$site = SiteFactory::instance(Input::sitename($assoc_args));
$jobs = $site->jobs();
$data = array();
foreach ($jobs as $job) {
$data[] = array(
'slot' => $job->slot,
'name' => $job->human_name,
'env' => @$job->environment,
'status' => $job->status,
'updated' => $job->changed
);
}
$this->handleDisplay($data,$args);
}

/**
* Mount a site with sshfs
*
Expand Down Expand Up @@ -1283,32 +1286,6 @@ public function new_relic($args, $assoc_args) {
}
}

/**
* Open the Pantheon site dashboard a browser
*
* ## OPTIONS
*
* [--site=<site>]
* : site for which to retreive notifications
*
*/
public function notifications($args, $assoc_args) {
$site = SiteFactory::instance(Input::sitename($assoc_args));
$notifications = $site->notifications();
$data = array();
foreach ($notifications as $note) {
$data[] = array(
'time' => $note->start,
'name' => $note->name,
'id' => $note->build->number."@".$note->build->environment->HOSTNAME,
'status'=> $note->build->status,
'phase' => $note->build->phase,
'duration' => $note->build->estimated_duration,
);
}
$this->handleDisplay($data);
}

/**
* Get or set owner
*
Expand Down Expand Up @@ -1683,32 +1660,42 @@ public function wipe($args, $assoc_args) {
Terminus::success(sprintf("Successfully wiped %s - %s", $site->getName(), $environment_id));
}

/**
* Delete a site from pantheon
*
* ## OPTIONS
* [--site=<site>]
* : ID of the site you want to delete
*
* [--force]
* : to skip the confirmations
*/
function delete($args, $assoc_args) {
$sitename = Input::sitename($assoc_args);
$site_id = $this->sitesCache->findID($sitename);
$site_to_delete = new Site($site_id);

if (!isset($assoc_args['force']) AND !Terminus::get_config('yes')) {
// if the force option isn't used we'll ask you some annoying questions
Terminus::confirm( sprintf( "Are you sure you want to delete %s?", $site_to_delete->information->name ));
Terminus::confirm( "Are you really sure?" );
}
Terminus::line( sprintf( "Deleting %s ...", $site_to_delete->information->name ) );
$response = \TerminusCommand::request( 'sites', $site_to_delete->id, '', 'DELETE' );

$this->sitesCache->remove($sitename);
Terminus::success("Deleted %s!", $sitename);
}
/**
* List a site's workflows
*
* ## OPTIONS
* [--site=<site>]
* : Site to check
*
* @subcommand workflows
*/
public function workflows($args, $assoc_args) {
$site = SiteFactory::instance(Input::sitename($assoc_args));
$workflows = $site->getWorkflows();
$data = array();
foreach($workflows as $workflow) {
$user = 'Pantheon';
if (isset($workflow->get('user')->email)) {
$user = $workflow->get('user')->email;
}
$data[] = array(
'workflow' => $workflow->get('description'),
'user' => $user,
'status' => $workflow->get('phase'),
'last_update' => date(
'Y-m-dTH:i:s',
($workflow->get('created_at') + $workflow->get('total_time'))
),
'tasks/complete' =>
$workflow->get('step') . '/' . $workflow->get('number_of_tasks'),
);
}
if (count($data) > 0) {
$this->constructTableForResponse($data);
} else {
Terminus::error('No workflows have been run on ' . $site->getName());
}
}
}

\Terminus::add_command( 'site', 'Site_Command' );
10 changes: 10 additions & 0 deletions tests/features/site.feature
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,13 @@ Feature: site
"""
Git
"""

Scenario: Site Workflows
@vcr site-workflows
Given I am authenticated
And a site named "[[test_site_name]]"
When I run "terminus site workflows --site=[[test_site_name]]"
Then I should get:
"""
Converge "dev"
"""
Loading

0 comments on commit fbf9b7e

Please sign in to comment.