Skip to content
Snippets Groups Projects
Commit 8bb8c24b authored by jurgenhaas's avatar jurgenhaas
Browse files

gitlab-ci-cd/general#2 Add optional support for healthcehck-io on cronjobs

parent 622dc381
No related branches found
No related tags found
1 merge request!7Merging develop into main
Pipeline #521954 passed
......@@ -25,15 +25,17 @@
"docs": "https://devops-tools.docs.lakedrops.com/composer/plugin/d4d/"
},
"require": {
"ext-json": "*",
"php": ">=7.2",
"ext-json": "*",
"composer-plugin-api": "^1||^2",
"henrywhitaker3/healthchecks-io": "^1.0",
"lakedrops/ahoy": "^1.5||dev-main",
"lakedrops/composer-json-utils": "^2.0||dev-main",
"lakedrops/docker-traefik": "^3.0||dev-main"
},
"require-dev": {
"composer/composer": "^1||^2",
"lakedrops/drupal-environment": "^3.0",
"phpunit/phpunit": "^8.4"
},
"minimum-stability": "dev",
......@@ -45,5 +47,10 @@
},
"extra": {
"class": "LakeDrops\\Docker4Drupal\\Plugin"
},
"config": {
"allow-plugins": {
"lakedrops/ahoy": true
}
}
}
......@@ -2,6 +2,11 @@
namespace LakeDrops\Docker4Drupal;
use Henrywhitaker3\Healthchecks\Exceptions\HealthchecksAccountLimitReachedException;
use Henrywhitaker3\Healthchecks\Exceptions\HealthchecksFailureException;
use Henrywhitaker3\Healthchecks\Exceptions\HealthchecksUnauthorisedException;
use Henrywhitaker3\Healthchecks\Exceptions\HealthchecksUuidNotFoundException;
use Henrywhitaker3\Healthchecks\HealthchecksManager;
use LakeDrops\Component\Composer\BaseHandler;
use LakeDrops\DockerTraefik\Traefik;
use LakeDrops\DrupalEnvironment\Handler as DrupalEnvironment;
......@@ -47,7 +52,7 @@ class Handler extends BaseHandler {
$this->getDockerGateway() :
$this->getLocalIpv4('docker0'),
'proxy' => ($this->isCiContext() || $this->isLocalDevMode()) ?
$this->getDockerProxy($projectname) :
$this->getDockerProxy() :
FALSE,
],
'traefik' => [
......@@ -514,8 +519,85 @@ class Handler extends BaseHandler {
'dest' => $projectRoot . '/backup',
'condition' => $this->config->readValue(['backup', 'enable']),
];
// Manage crontabs and optionally add them to heathcheck-io
$hj_api_url = getenv('HEALTHCHECK_API_URL');
$hj_api_key = getenv('HEALTHCHECK_API_KEY');
$hj_api_channels = getenv('HEALTHCHECK_API_CHANNELS');
$hj_project = $this->config->readValue('projectname');
$hj_branch = getenv('CI_COMMIT_BRANCH');
$hj_checks = [];
$hj_manager = NULL;
if (!empty($hj_api_url) && !empty($hj_api_key)) {
$hj_manager = new HealthchecksManager($hj_api_key, $hj_api_url);
try {
$hj_checks = $hj_manager->listChecks();
}
catch (HealthchecksFailureException | HealthchecksUnauthorisedException $e) {
// Ignoring this for now.
}
}
foreach ($this->config->readValue('crontabs') ?? [] as $user => $tasks) {
if (empty($tasks) || !is_array($tasks)) {
if (!is_array($tasks)) {
continue;
}
$activeTasks = [];
foreach ($tasks as $task) {
$disabled = !empty($task['disabled']);
$command = $task['command'];
if (isset($hj_manager)) {
unset($task['disabled'], $task['command']);
$task['channels'] = $hj_api_channels;
$task['tags'] = implode(' ', ['d4d', $hj_project, $hj_branch]);
$check = NULL;
foreach ($hj_checks as $existing) {
$tags = explode(' ', $existing['tags']);
if ($task['name'] === $existing['name'] && in_array('d4d', $tags, TRUE) && in_array($hj_project, $tags, TRUE) && in_array($hj_branch, $tags, TRUE)) {
$check = $existing;
break;
}
}
if (!$check) {
if ($disabled) {
// This task is disabled. As it doesn't exist yet, nothing to do.
continue;
}
try {
$check = $hj_manager->createCheck($task);
}
catch (HealthchecksAccountLimitReachedException | HealthchecksUnauthorisedException $e) {
// Ignoring this for now.
}
}
else {
$changed = FALSE;
foreach ($task as $key => $value) {
if (!isset($check[$key]) || $check[$key] !== $value) {
$changed = TRUE;
}
}
if ($changed) {
$parts = explode('/', $check['ping_url']);
$uuid = array_pop($parts);
try {
$check = $hj_manager->updateCheck($uuid, $task);
}
catch (HealthchecksFailureException | HealthchecksUuidNotFoundException | HealthchecksAccountLimitReachedException | HealthchecksUnauthorisedException $e) {
// Ignoring this for now.
}
}
}
if ($check) {
$command .= ' && curl -fsS --retry 5 -o /dev/null ' . $check['ping_url'];
}
}
if (!$disabled) {
$activeTasks[] = $command;
}
}
if (empty($activeTasks)) {
$files[$user] = [
'dest' => $projectRoot . '/crontabs',
'delete' => TRUE,
......@@ -525,7 +607,7 @@ class Handler extends BaseHandler {
$files[$user] = [
'source' => 'crontabs/template.twig',
'dest' => $projectRoot . '/crontabs',
'options' => $tasks,
'options' => $activeTasks,
];
}
}
......@@ -574,11 +656,9 @@ class Handler extends BaseHandler {
}
/**
* @param string $projectname
*
* @return string
*/
private function getDockerProxy(string $projectname): string {
private function getDockerProxy(): string {
foreach ($this->readNetworkConfig()['Containers'] as $container) {
if (isset($container['Name']) && in_array($container['Name'], ['traefik_traefik_1', 'traefik-traefik-1'])) {
return explode('/', $container['IPv4Address'])[0];
......@@ -623,7 +703,7 @@ class Handler extends BaseHandler {
exec('docker container inspect ' . $id, $output);
return json_decode(implode('', $output), TRUE)[0];
}
catch (Exception $ex) {
catch (\Exception $ex) {
// Ignore.
}
return [
......@@ -643,7 +723,7 @@ class Handler extends BaseHandler {
exec('docker network inspect traefik-public', $output);
return json_decode(implode('', $output), TRUE)[0];
}
catch (Exception $ex) {
catch (\Exception $ex) {
// Ignore.
}
return [
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment