MetadataDirCheck.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 (empty($awsMeta)) {
  19. return true;
  20. }
  21. if ($awsMeta['primary'] && $awsMeta['has_instance_store']) {
  22. // TODO in symfony4, use fromShellCommandline
  23. $proc = new Process('lsblk -io NAME,TYPE,SIZE,MOUNTPOINT,FSTYPE,MODEL | grep Instance | grep sdeph');
  24. if (0 !== $proc->run()) {
  25. return false;
  26. }
  27. }
  28. return true;
  29. }
  30. public function check()
  31. {
  32. if (empty($this->awsMeta)) {
  33. return new Success('No AWS metadata given');
  34. }
  35. if ($this->awsMeta['primary']) {
  36. if ($this->awsMeta['has_instance_store']) {
  37. if (!self::isMetadataStoreMounted($this->awsMeta)) {
  38. return new Failure('Instance store needs configuring');
  39. }
  40. }
  41. $packagesJson = __DIR__ . '/../../../../web/packages.json';
  42. if (!file_exists($packagesJson)) {
  43. return new Failure($packagesJson.' not found on primary server');
  44. }
  45. return new Success('Primary server metadata has been dumped');
  46. }
  47. $packagesJson = __DIR__ . '/../../../../web/packages.json';
  48. if (!file_exists($packagesJson)) {
  49. return new Failure($packagesJson.' not found');
  50. }
  51. if (!is_link($packagesJson)) {
  52. return new Failure($packagesJson.' is not a symlink');
  53. }
  54. if (substr(file_get_contents($packagesJson), 0, 1) !== '{') {
  55. return new Failure($packagesJson.' does not look like it has json in it');
  56. }
  57. $metaDir = __DIR__ . '/../../../../web/p';
  58. $metaV2Dir = __DIR__ . '/../../../../web/p2';
  59. if (!is_dir($metaDir)) {
  60. return new Failure($metaDir.' not found');
  61. }
  62. if (!is_link($metaDir)) {
  63. return new Failure($metaDir.' is not a symlink');
  64. }
  65. if (!is_dir($metaV2Dir)) {
  66. return new Failure($metaV2Dir.' not found');
  67. }
  68. if (!is_link($metaV2Dir)) {
  69. return new Failure($metaV2Dir.' is not a symlink');
  70. }
  71. return new Success('Metadata mirror is symlinked');
  72. }
  73. }