diff --git a/lib/Drupal/monitoring/Sensor/GitRepositorySensorException.php b/lib/Drupal/monitoring/Sensor/GitRepositorySensorException.php index 29b2d13..4a72a65 100644 --- a/lib/Drupal/monitoring/Sensor/GitRepositorySensorException.php +++ b/lib/Drupal/monitoring/Sensor/GitRepositorySensorException.php @@ -2,13 +2,13 @@ /** * @file - * Contains \Drupal\monitoring\Sensor\DisabledSensorException. + * Contains \Drupal\monitoring\Sensor\GitRepositorySensorException. */ namespace Drupal\monitoring\Sensor; /** - * Thrown if a disabled sensor is requested to be executed. + * Thrown if git repository error identified. */ class GitRepositorySensorException extends \InvalidArgumentException { diff --git a/lib/Drupal/monitoring/Sensor/SensorThresholds.php b/lib/Drupal/monitoring/Sensor/SensorThresholds.php index 9905a85..0b4692b 100644 --- a/lib/Drupal/monitoring/Sensor/SensorThresholds.php +++ b/lib/Drupal/monitoring/Sensor/SensorThresholds.php @@ -46,16 +46,11 @@ abstract class SensorThresholds extends SensorConfigurable implements SensorThre ), '#default_value' => $type, '#ajax' => array( - 'callback' => 'monitoring_sensor_thresholds_ajax', - 'wrapper' => 'monitoring-sensor-thresholds', + 'callback' => 'monitoring_sensor_settings_ajax', + 'wrapper' => 'monitoring-sensor-settings-wrapper', ), ); - $form['thresholds']['intervals'] = array( - '#prefix' => '
' . $this->exec('git status') . '',
+ $form['submit_repo_path'] = array(
+ '#type' => 'button',
+ '#value' => t('Update repository path'),
+ '#submit' => array('monitoring_sensor_settings_form_submit'),
+ '#validate' => array('monitoring_sensor_settings_form_validate'),
+ '#ajax' => array(
+ 'callback' => 'monitoring_sensor_settings_ajax',
+ 'wrapper' => 'monitoring-sensor-settings-wrapper',
+ ),
);
- $form['info']['available_branches'] = array(
- '#type' => 'item',
- '#title' => t('Available branches'),
- '#markup' => '' . $this->exec('git branch') . '',
- );
- $form['info']['available_tags'] = array(
- '#type' => 'item',
- '#title' => t('Available tags'),
- '#markup' => '' . $this->exec('git tag') . '',
+
+ if (isset($form_state['values'][monitoring_sensor_settings_key($this->getSensorName())]['repo_path'])) {
+ $repo_path = $form_state['values'][monitoring_sensor_settings_key($this->getSensorName())]['repo_path'];
+ }
+ else {
+ $repo_path = $this->info->getSetting('repo_path');
+ }
+
+ if ($this->getRepositoryPath($repo_path) == NULL) {
+ $form['missing_repo_path_info'] = array(
+ '#type' => 'markup',
+ '#markup' => ''
+ );
+ }
+
+ $form['branch'] = array(
+ '#type' => 'select',
+ '#title' => t('Git branch'),
+ '#description' => t('Select valid branch for the current deployment.'),
+ '#options' => $this->getBranches(),
+ '#empty_option' => '--',
+ '#default_value' => $this->info->getSetting('branch'),
);
- $form['cmd'] = array(
- '#type' => 'textarea',
- '#title' => t('Command'),
- '#description' => t('Enter one or more shell commands that will check the local git repository status. Any output printed to console will escalate the sensor status to critical.'),
- '#default_value' => $this->info->getSetting('cmd'),
+ $form['tag'] = array(
+ '#type' => 'select',
+ '#title' => t('Git tag'),
+ '#description' => t('Select valid tag for the current deployment.'),
+ '#options' => $this->getTags(),
+ '#empty_option' => '--',
+ '#default_value' => $this->info->getSetting('tag'),
);
return $form;
@@ -85,8 +129,8 @@ class SensorGit extends SensorConfigurable implements SensorExtendedInfoInterfac
*/
public function settingsFormValidate($form, &$form_state) {
$path = $form_state['values'][monitoring_sensor_settings_key($this->getSensorName())]['repo_path'];
- if (!file_exists($path)) {
- form_set_error('repo_path', t('The provided path %path does not exists.', array('%path' => $path)));
+ if ($this->getRepositoryPath($path) == NULL) {
+ form_set_error('repo_path', t('The provided path %path does not exists or is not a git repository.', array('%path' => $path)));
}
}
@@ -94,9 +138,7 @@ class SensorGit extends SensorConfigurable implements SensorExtendedInfoInterfac
* {@inheritdoc}
*/
public function resultVerbose(SensorResultInterface $result) {
- $output = 'CMD: ' . $this->exec($this->info->getSetting('cmd'));
- $output .= "\n\n" . $this->cmdOutput;
- return $output;
+ return format_string('Files in unexpected state: @files', array('@files' => $this->cmdOutput)); } /** @@ -108,9 +150,133 @@ class SensorGit extends SensorConfigurable implements SensorExtendedInfoInterfac * @return string * Shell command. */ - protected function exec($cmd) { - $repo_path = DRUPAL_ROOT . '/' . $this->info->getSetting('repo_path'); + protected function execAtGitRoot($cmd) { + $repo_path = escapeshellarg($this->getRepositoryPath($this->info->getSetting('repo_path'))); return trim(shell_exec("cd $repo_path\n$cmd 2>&1")); } + /** + * Checks if the current branch is the same as the provided. + * + * @param string $branch + * Expected branch name. + * + * @throws \Drupal\monitoring\Sensor\GitRepositorySensorException + * In case the current branch name is equal to the expected. + */ + protected function checkBranch($branch) { + $current_branch = NULL; + foreach ($this->getBranches() as $key => $value) { + if (strpos($value, '*') !== FALSE) { + $current_branch = $key; + } + } + + if ($current_branch != $branch) { + throw new GitRepositorySensorException(format_string('Unexpected branch @current expecting @expected', array('@expected' => $branch, '@current' => $current_branch))); + } + } + + /** + * Checks if the current tag is the same as the provided. + * + * @param string $tag + * Expected tag name. + * + * @throws \Drupal\monitoring\Sensor\GitRepositorySensorException + * In case the current tag name is equal to the expected. + */ + protected function checkTag($tag) { + $current_tag = NULL; + foreach ($this->getTags() as $key => $value) { + if (strpos($value, '*') !== FALSE) { + $current_tag = $key; + } + } + + if ($current_tag != $tag) { + throw new GitRepositorySensorException(format_string('Unexpected tag @current expecting @expected', array('@expected' => $tag, '@current' => $current_tag))); + } + } + + /** + * Gets absolute repository path. + * + * @param string $path + * Path relative to Drupal root. + * + * @return null|string + * If the resulting path is a valid git repository the absolute path to the + * repository is returned. + * If the resulting path is not a valid git repository NULL is returned. + */ + protected function getRepositoryPath($path) { + if (empty($this->repositoryPath)) { + $repo_path = DRUPAL_ROOT . '/' . $path; + $cmd = "cd " . escapeshellarg($repo_path) . "\ngit status 2>&1"; + if (file_exists($repo_path) && strpos(shell_exec($cmd), '# On branch') !== FALSE) { + $this->repositoryPath = $repo_path; + } + else { + $this->repositoryPath = NULL; + } + } + + return $this->repositoryPath; + } + + /** + * Gets branches of the current git repository. + * + * @return array + * List of branches where key as well as the value are branch names. The + * value of the current branch is prefixed with *. + */ + protected function getBranches() { + $branches = array(); + + $cmd_output = $this->execAtGitRoot('git branch'); + if (strpos($cmd_output, 'fatal') !== FALSE) { + return $branches; + } + + foreach (explode("\n", $cmd_output) as $branch) { + if (strpos($branch, '*') === 0) { + $key = trim(str_replace('*', '', $branch)); + } + else { + $key = trim($branch); + } + $branches[$key] = trim($branch); + } + return $branches; + } + + /** + * Gets tags of the current git repository. + * + * @return array + * List of tags where key as well as the value are tag names. The + * value of the current tag is prefixed with *. + */ + protected function getTags() { + $tags = array(); + + $cmd_output = $this->execAtGitRoot('git branch'); + if (strpos($cmd_output, 'fatal') !== FALSE) { + return $tags; + } + + foreach (explode("\n", $cmd_output) as $tag) { + if (strpos($tag, '*') === 0) { + $key = trim(str_replace('*', '', $tag)); + } + else { + $key = trim($tag); + } + $tags[$key] = trim($tag); + } + return $tags; + } + } diff --git a/monitoring.admin.inc b/monitoring.admin.inc index 53c52f1..50fa75b 100644 --- a/monitoring.admin.inc +++ b/monitoring.admin.inc @@ -275,6 +275,8 @@ function monitoring_sensor_settings_form($form, &$form_state, SensorInfo $sensor ); $form[$form_key] = $sensor->settingsForm($form[$form_key], $form_state); + $form[$form_key]['#prefix'] = '