Browse Source

Add metadata dir health checks

Jordi Boggiano 6 years ago
parent
commit
2321a16f6e

+ 6 - 1
src/Packagist/WebBundle/Controller/HealthCheckController.php

@@ -9,6 +9,7 @@ use Symfony\Bundle\FrameworkBundle\Controller\Controller;
 use ZendDiagnostics\Check;
 use ZendDiagnostics\Runner\Runner;
 use Packagist\WebBundle\HealthCheck\RedisHealthCheck;
+use Packagist\WebBundle\HealthCheck\MetadataDirCheck;
 use Psr\Log\LoggerInterface;
 use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
 
@@ -56,10 +57,14 @@ class HealthCheckController
             $machineName = 'dev';
         }
 
-        $runner->addCheck($d = new Check\DiskUsage(80, 90, '/'));
+        $runner->addCheck(new Check\DiskUsage(80, 90, '/'));
+        if ($this->awsMeta['primary'] === true) {
+            $runner->addCheck(new Check\DiskUsage(80, 90, '/mnt/sdephemeral'));
+        }
         $runner->addCheck(new Check\DiskFree(100 * 1024 * 1024, '/tmp'));
         $runner->addCheck(new Check\PDOCheck('mysql:dbname='.$dbname.';host='.$dbhost, $dbuser, $dbpass));
         $runner->addCheck(new RedisHealthCheck($this->redisClient));
+        $runner->addCheck(new MetadataDirCheck($this->awsMeta));
 
         $results = $runner->run();
 

+ 60 - 0
src/Packagist/WebBundle/HealthCheck/MetadataDirCheck.php

@@ -0,0 +1,60 @@
+<?php declare(strict_types = 1);
+
+namespace Packagist\WebBundle\HealthCheck;
+
+use ZendDiagnostics\Check\AbstractCheck;
+use ZendDiagnostics\Result\Failure;
+use ZendDiagnostics\Result\Success;
+use ZendDiagnostics\Result\Warning;
+use Symfony\Component\Process\Process;
+
+class MetadataDirCheck extends AbstractCheck
+{
+    /** @var array */
+    private $awsMeta;
+
+    public function __construct(array $awsMetadata)
+    {
+        $this->awsMeta = $awsMetadata;
+    }
+
+    public function check()
+    {
+        if ($this->awsMeta['primary']) {
+            // TODO in symfony4, use fromShellCommandline
+            $proc = new Process('lsblk -io NAME,TYPE,SIZE,MOUNTPOINT,FSTYPE,MODEL | grep Instance | grep sdeph');
+            if (0 === $proc->run()) {
+                return new Success('Instance store is in use');
+            }
+
+            return new Failure('Instance store needs configuring');
+        } else {
+            $packagesJson = __DIR__ . '/../../../web/packages.json';
+            if (!file_exists($packagesJson)) {
+                return new Failure($packagesJson.' not found');
+            }
+            if (!is_link($packagesJson)) {
+                return new Failure($packagesJson.' is not a symlink');
+            }
+            if (substr(file_get_contents($packagesJson), 0, 1) !== '{') {
+                return new Failure($packagesJson.' does not look like it has json in it');
+            }
+            $metaDir = __DIR__ . '/../../../web/p';
+            $metaV2Dir = __DIR__ . '/../../../web/p2';
+            if (!is_dir($metaDir)) {
+                return new Failure($metaDir.' not found');
+            }
+            if (!is_link($metaDir)) {
+                return new Failure($metaDir.' is not a symlink');
+            }
+            if (!is_dir($metaV2Dir)) {
+                return new Failure($metaV2Dir.' not found');
+            }
+            if (!is_link($metaV2Dir)) {
+                return new Failure($metaV2Dir.' is not a symlink');
+            }
+
+            return new Success('Metadata mirror is symlinked');
+        }
+    }
+}