Skip to content
This repository has been archived by the owner on Oct 19, 2021. It is now read-only.

Commit

Permalink
Fixes #91: Bring in updated example scripts. (#92)
Browse files Browse the repository at this point in the history
* Fixes #91: Bring in updated example scripts.

* Run code formatter.

* Don't codesniff examples.

* Remove project specific scripted item.

* Fix some formatting/help issues introduced with making the script generic.

* Additional fixes after doing a prod release.
  • Loading branch information
swichers committed Dec 30, 2020
1 parent 84ec385 commit ec50f1d
Show file tree
Hide file tree
Showing 13 changed files with 564 additions and 402 deletions.
27 changes: 18 additions & 9 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
# Library Usage Examples

This folder contains some examples of how to use the library to script ACSF deployments and associated tasks. To use these examples copy the `.env.dist` file to `.env` in this folder. Then edit and configure each value in that file.
This folder contains some examples of how to use the library to script ACSF deployments and associated tasks. To use
these examples copy the `.env.dist` file to `.env` in this folder. Then edit and configure each value in that file.

```sh
cp ../.env.dist .env
```

The provided scripts allow you to perform many of the common tasks you might need right out of the box, but the real power in this library comes from using these scripts as a starting point for your own more complex scripts.
The provided scripts allow you to perform many of the common tasks you might need right out of the box, but the real
power in this library comes from using these scripts as a starting point for your own more complex scripts.

## Examples provided

Expand All @@ -16,11 +18,17 @@ Usage: `php backport.php test tags/2.7.0-beta.1-build`

Copies the production environment down to the given environment, and then deploys the tag or branch provided.

### backup.php
### backups-create.php

Usage: `php backup.php live`
Usage: `php backups-create.php live database`

Creates a backup of each site on the given environment.
Creates a database backup of each site on the given environment.

### backups-prune.php

Usage: `php backups-prune.php live 14`

Delete backups older than the given number of days.

### cc.php

Expand All @@ -32,13 +40,14 @@ Runs the ACSF cache clear process on the given environment.

Usage: `php deploy.php live tags/2.7.0-beta.1-build`

Deploys a new tag or branch to the target environment. Creates a backup in the process if the target environment is `live`.
Deploys a new tag or branch to the target environment.

### deploy-uat.php
### prod-release.php

Usage: `php deploy-uat.php tags/2.7.0-beta.1-build`
Usage: `php prod-release.php example tags/2.7.0-build tags/2.7.0-beta.1-build`

Deploys a new tag or branch to the UAT environment. Backports production first.
Perform a production release. Deploys code to both the production and UAT environments after creating a backup. Shows
how to combine the example scripts to perform complex tasks.

### redeploy.php

Expand Down
66 changes: 19 additions & 47 deletions examples/backport.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* @file
* Backport production to the target environment.
Expand All @@ -10,36 +11,39 @@

declare(strict_types = 1);

use swichers\Acsf\Client\Endpoints\Entity\EntityInterface;
use swichers\Acsf\Client\ClientFactory;
use swichers\Acsf\Client\Endpoints\Entity\EntityInterface;

require __DIR__ . '/../vendor/autoload.php';
require __DIR__ . '/utils.php';

// The environment to backport to.
define('TARGET_ENV', $argv[1] ?? '');
// The code to deploy after the backport.
define('DEPLOY_REF', $argv[2] ?? '');
$DEPLOY_REF = $argv[2] ?? '';
// The ACSF stack to target.
define('STACK_ID', $argv[3] ?? 1);
// The environment to copy down to the TARGET_ENV.
define('SOURCE_ENV', 'live');

if (empty(TARGET_ENV) || empty(DEPLOY_REF)) {
echo "Must supply a target environment and a code reference.\n\n";
printf(
"Example: php %s test tags/2.4.2-build\n",
basename(__FILE__)
);
if (empty(TARGET_ENV)) {
echo "Must supply a target environment and an optional code reference.\n\n";
printf("Example: php %s test tags/2.4.2-build\n", basename(__FILE__));
die(1);
}

$start_time = new DateTime();

$client = ClientFactory::createFromEnvironment(SOURCE_ENV);

$sites = $client->getAction('Sites')->listAll();
$site_ids = array_column($sites['sites'], 'id');
// If no code reference was supplied we default to the current code.
if (empty($DEPLOY_REF)) {
$client->setEnvironment(TARGET_ENV);
$DEPLOY_REF = $client->getAction('Vcs')->list()['current'];
$client->setEnvironment(SOURCE_ENV);
}

$site_ids = array_column($client->getAction('Sites')->listAll()['sites'], 'id');
printf(
"Backporting %d sites from %s to %s: %s\n",
count($site_ids),
Expand All @@ -58,47 +62,15 @@
]
);

$client->getEntity('Task', intval($task_info['task_id']))->wait(
30,
function (EntityInterface $task, $task_status) {

printf(
"Backport (%d): %s\n",
$task->id(),
$task_status['status_string']
);
}
);
$client->getEntity('Task', (int) $task_info['task_id'])->wait(
60,
static function (EntityInterface $task, array $taskStatus) {

// Change to the target environment.
$client->setEnvironment(TARGET_ENV);

$refs = $client->getAction('Vcs')->list(['stack_id' => STACK_ID]);
if (!in_array(DEPLOY_REF, $refs['available'])) {
printf("Unable to find %s in list of available refs.\n", DEPLOY_REF);
die(1);
}

printf("Current code: %s\n", $refs['current']);
printf("Deploying: %s\n", DEPLOY_REF);

$task_info = $client->getAction('Update')->updateCode(
DEPLOY_REF,
['stack_id' => STACK_ID]
);
$client->getEntity('Task', intval($task_info['task_id']))->wait(
30,
function (EntityInterface $task, $task_status) {

printf(
"Code Deploy (%d): %s\n",
$task->id(),
$task_status['status_string']
);
printf("Backport (%d): %s\n", $task->id(), $taskStatus['status_string']);
}
);

printf("Code deploy completed.\n");
run_script('deploy', TARGET_ENV, $DEPLOY_REF, STACK_ID);

$diff = $start_time->diff(new DateTime());
printf("Script complete. Time elapsed: %s\n", $diff->format('%H:%I:%S'));
Expand Down
50 changes: 0 additions & 50 deletions examples/backup.php

This file was deleted.

61 changes: 61 additions & 0 deletions examples/backups-create.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

/**
* @file
* Backs up all sites on the target environment.
*
* Backs up db, theme, code, etc.
*
* Usage:
* php backup.php live 'database,public files'
* php backup.php live 'database,public files,private files'
* php backup.php live
*/

declare(strict_types = 1);

use swichers\Acsf\Client\ClientFactory;
use swichers\Acsf\Client\Endpoints\Entity\EntityInterface;

require __DIR__ . '/../vendor/autoload.php';
require __DIR__ . '/utils.php';

// The environment to back up (dev, live, etc.)
define('TARGET_ENV', $argv[1] ?? '');
// The comma separated list of components to backup.
define('BACKUP_COMPONENTS', $argv[2] ?? 'database,public files,private files');

if (empty(TARGET_ENV)) {
echo "Must supply an environment to back up.\n\n";
printf("Example: php %s live\n", basename(__FILE__));
die(1);
}

$start_time = new DateTime();

$client = ClientFactory::createFromEnvironment(TARGET_ENV);

printf("Creating backups including %s\n", BACKUP_COMPONENTS);

$client->getAction('Sites')->backupAll(
[
'components' => explode(',', BACKUP_COMPONENTS),
],
TRUE,
60,
static function (EntityInterface $task, $taskStatus) use ($client) {

$site_name = get_site_name($client, (int) $taskStatus['nid']);
printf(
"Backup (%d, %s): %s\n",
$task->id(),
$site_name,
$taskStatus['status_string']
);
}
);

$diff = $start_time->diff(new DateTime());
printf("Script complete. Time elapsed: %s\n", $diff->format('%H:%I:%S'));

exit(0);
112 changes: 112 additions & 0 deletions examples/backups-prune.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php

/**
* @file
* Deletes backups older than the given date.
*
* Usage:
* php backups-prune.php dev 14
*/

declare(strict_types = 1);

use swichers\Acsf\Client\ClientFactory;
use swichers\Acsf\Client\ClientInterface;
use swichers\Acsf\Client\Endpoints\Entity\Site;
use Symfony\Component\HttpClient\Exception\ClientException;

require __DIR__ . '/../vendor/autoload.php';
require __DIR__ . '/utils.php';

// The environment to remove backups from.
define('TARGET_ENV', $argv[1] ?? '');
// The age of backups to remove. Backups older than this will be deleted.
define('MAX_AGE_DAYS', (int) ($argv[2] ?? 14));

if (empty(TARGET_ENV)) {
echo "Must supply an environment to back up.\n\n";
printf("Example: php %s live\n", basename(__FILE__));
die(1);
}

$start_time = new DateTime();

$client = ClientFactory::createFromEnvironment(TARGET_ENV);

printf("Removing site backups older than %d days.\n", MAX_AGE_DAYS);

/** @var \swichers\Acsf\Client\Endpoints\Entity\Site $site */
foreach ($client->getAction('Sites')->getAll() as $site) {
prune_site_backups($client, $site, MAX_AGE_DAYS);
}

$diff = $start_time->diff(new DateTime());
printf("Script complete. Time elapsed: %s\n", $diff->format('%H:%I:%S'));

exit(0);

/**
* Prune old backups for the given site.
*
* @param \swichers\Acsf\Client\ClientInterface $client
* The ACSF client.
* @param \swichers\Acsf\Client\Endpoints\Entity\Site $site
* The Site to prune backups for.
* @param int $maxAge
* The age of backups to remove. Defaults to 2 weeks.
* @param int|null $currentPage
* The current page.
* @param int $perPage
* The amount of backups to list per page.
*
* @throws \swichers\Acsf\Client\Exceptions\InvalidOptionException
*/
function prune_site_backups(ClientInterface $client, Site $site, int $maxAge = 14, int $currentPage = NULL, int $perPage = 20) {

if (is_null($currentPage)) {
// We need to calculate what the last page is so we can work our way
// backwards. If we start at the front we may miss items as page numbers
// shift during the loop.
$currentPage =
(int) ceil($site->listBackups(['limit' => 1])['count'] / $perPage) ?: 1;
}

try {
$backups = $site->listBackups(
[
'limit' => $perPage,
'page' => $currentPage,
]
);
}
catch (ClientException $x) {
fwrite(STDERR, $x->getMessage() . PHP_EOL);
return;
}

$prunables = array_filter(
$backups['backups'],
static function ($backup) use ($maxAge) {

return $backup['timestamp'] <= strtotime(sprintf('-%d days', $maxAge));
}
);

if (!empty($prunables)) {
foreach ($prunables as $prunable) {
printf(
"%s: Delete %s %s\n",
$site->details()['site'],
$prunable['label'],
date('c', $prunable['timestamp'])
);
/** @var \swichers\Acsf\Client\Endpoints\Entity\Backup $backup */
$backup = $client->getEntity('Backup', $prunable['id'], $site);
$backup->delete();
}
}

if ($currentPage > 1) {
prune_site_backups($client, $site, $maxAge, $currentPage - 1, $perPage);
}
}
Loading

0 comments on commit ec50f1d

Please sign in to comment.