MetadataDirCheck.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php declare(strict_types = 1);
  2. namespace Packagist\WebBundle\HealthCheck;
  3. use ZendDiagnostics\Check\AbstractCheck;
  4. use ZendDiagnostics\Result\Failure;
  5. use ZendDiagnostics\Result\Success;
  6. use ZendDiagnostics\Result\Warning;
  7. use Symfony\Component\Process\Process;
  8. class MetadataDirCheck extends AbstractCheck
  9. {
  10. /** @var array */
  11. private $awsMeta;
  12. public function __construct(array $awsMetadata)
  13. {
  14. $this->awsMeta = $awsMetadata;
  15. }
  16. public static function isMetadataStoreMounted(array $awsMeta): bool
  17. {
  18. if ($awsMeta['primary'] && $awsMeta['has_instance_store']) {
  19. // TODO in symfony4, use fromShellCommandline
  20. $proc = new Process('lsblk -io NAME,TYPE,SIZE,MOUNTPOINT,FSTYPE,MODEL | grep Instance | grep sdeph');
  21. if (0 !== $proc->run()) {
  22. return false;
  23. }
  24. }
  25. return true;
  26. }
  27. public function check()
  28. {
  29. if (empty($this->awsMeta)) {
  30. return new Success('No AWS metadata given');
  31. }
  32. if ($this->awsMeta['primary']) {
  33. if ($this->awsMeta['has_instance_store']) {
  34. if (!self::isMetadataStoreMounted($this->awsMeta)) {
  35. return new Failure('Instance store needs configuring');
  36. }
  37. }
  38. $packagesJson = __DIR__ . '/../../../../web/packages.json';
  39. if (!file_exists($packagesJson)) {
  40. return new Failure($packagesJson.' not found on primary server');
  41. }
  42. return new Success('Primary server metadata has been dumped');
  43. }
  44. $packagesJson = __DIR__ . '/../../../../web/packages.json';
  45. if (!file_exists($packagesJson)) {
  46. return new Failure($packagesJson.' not found');
  47. }
  48. if (!is_link($packagesJson)) {
  49. return new Failure($packagesJson.' is not a symlink');
  50. }
  51. if (substr(file_get_contents($packagesJson), 0, 1) !== '{') {
  52. return new Failure($packagesJson.' does not look like it has json in it');
  53. }
  54. $metaDir = __DIR__ . '/../../../../web/p';
  55. $metaV2Dir = __DIR__ . '/../../../../web/p2';
  56. if (!is_dir($metaDir)) {
  57. return new Failure($metaDir.' not found');
  58. }
  59. if (!is_link($metaDir)) {
  60. return new Failure($metaDir.' is not a symlink');
  61. }
  62. if (!is_dir($metaV2Dir)) {
  63. return new Failure($metaV2Dir.' not found');
  64. }
  65. if (!is_link($metaV2Dir)) {
  66. return new Failure($metaV2Dir.' is not a symlink');
  67. }
  68. return new Success('Metadata mirror is symlinked');
  69. }
  70. }