You are here

function gardens_site_data_sites_json_issue_type_get in Acquia Cloud Site Factory Connector 8.2

Same name and namespace in other branches
  1. 8 acsf_init/lib/sites/g/sites.inc \gardens_site_data_sites_json_issue_type_get()

Re-checks for a fatal issue with the sites.json file.

This function is not the only location where issues are determined; it's used to doublecheck the exact type of issue / doublecheck for a race condition, after an issue was initially detected outside this function.

Return value

string Type of issue encountered. Empty string means the sites.json file is OK (or is missing, which is also OK).

2 calls to gardens_site_data_sites_json_issue_type_get()
gardens_site_data_json_alert_flag_clear in acsf_init/lib/sites/g/sites.inc
Clears a 'sites.json alert' flag.
gardens_site_data_json_alert_flag_set in acsf_init/lib/sites/g/sites.inc
Tries to set a flag, marking that an issue with sites.json exists.

File

acsf_init/lib/sites/g/sites.inc, line 617
ACSF helper functions for Drupal's multi-site directory aliasing feature.

Code

function gardens_site_data_sites_json_issue_type_get() {
  $issue_type = '';
  $sites_json_path = gardens_site_data_get_filepath();

  // If sites.json is missing completely then this script is being executed
  // outside of an ACSF infrastructure in which case no alert is needed.
  if (file_exists($sites_json_path)) {

    // Check if sites.json is readable.
    if (!is_readable($sites_json_path)) {
      $issue_type = 'file_unreadable';
    }

    // Check if the file's contents are inaccessible.
    if (!$issue_type) {

      // Try to read sites.json and see if it succeeds and what kind of error
      // we get back if it fails. There is a fail which we need to ignore: when
      // the sites.json is being rewritten for a short period of time the
      // following error will be returned:
      //
      // head: cannot open `/mnt/files/balazs.01live/files-private/sites.json'
      // for reading: Structure needs cleaning
      //
      // To make sure we are only triggering an alert in case of a Gluster split
      // brain, redirect the stderr to stdout and look for the indicator
      // message: 'Input/output error'.
      //
      // Acquia rules disallow exec() with dynamic arguments.
      // phpcs:disable
      exec(sprintf('head -n1 %s 2>&1', escapeshellarg($sites_json_path)), $output_array, $exit_code);

      // phpcs:enable
      $output = implode('', $output_array);
      if ($exit_code !== 0 && strpos($output, 'Input/output error') !== FALSE) {
        $issue_type = 'gluster_split_brain';
      }
    }

    // Check for invalid JSON data. We treat empty file as invalid too here,
    // because it doesn't matter much. It's not consistent with the initial
    // check, though. (An empty file does not cause
    // gardens_site_data_json_alert_flag_set() to be called.)
    if (!$issue_type) {
      $map = gardens_site_data_load_file();
      if (!$map) {
        $issue_type = 'invalid_json_data';
      }
    }
  }
  return $issue_type;
}