You are here

function hosting_task_deploy_form in Hosting 7.4

Confirmation form for site deploy.

See also

hosting_task_confirm_form().

File

site/hosting_site.module, line 835
Contains hook implementations for Hosting site module.

Code

function hosting_task_deploy_form($node) {
  $form = array();
  if ($node->type == 'site') {
    try {
      $wrapper = new GitWrapper();
      $git = $wrapper
        ->workingCopy($node->git_root);
      $diff = $git
        ->diff()
        ->getOutput();
      $status = $git
        ->getStatus();
    } catch (GitException $e) {
      drupal_set_message($e
        ->getMessage(), 'error');
    }
    $two_col_classes = array(
      'col-sm-6',
      'col-md-6',
      'col-lg-6',
    );
    $form['info'] = array(
      '#type' => 'container',
      '#attributes' => array(
        'class' => array(
          'container',
        ),
      ),
      'current' => array(
        '#type' => 'container',
        '#attributes' => array(
          'class' => $two_col_classes,
        ),
      ),
      'target' => array(
        '#type' => 'container',
        '#attributes' => array(
          'class' => $two_col_classes,
        ),
      ),
    );
    $form['info']['current']['current_git_reference'] = array(
      '#title' => t('Current Git Reference'),
      '#type' => 'item',
      '#markup' => $node->git_reference,
      '#description' => t('The current branch, tag, or SHA of the site code at &path.', array(
        '&path' => $node->git_root,
      )),
      '#field_prefix' => '<div class="label label-default git-ref-label">',
      '#field_suffix' => '</div>',
    );

    // Allow the user to change git_reference here.
    $form['info']['target']['target_git_reference'] = array(
      '#title' => t('Target Git Reference'),
      '#type' => 'textfield',
      '#default_value' => $_GET['git_reference'] ?: $node->git_reference,
      '#description' => t('Enter the desired git branch, tag, or SHA. If a branch is entered, the git repository will pulled and reset to match the remote repository. If a tag or SHA is entered, it will be checked out, resulting in a "DETACHED HEAD" state.'),
      '#parents' => array(
        'parameters',
        'target_git_reference',
      ),
      '#attributes' => array(
        'class' => array(
          'col-md-6',
        ),
      ),
    );

    /**
     * @TODO:
    // Offer users ability to control commands that will run during this deployment.
    $form['hooks'] = array(
      '#type' => 'container',
      'title' => array(
        '#prefix' => '<h4>',
        '#markup' => t('Deployment Phases'),
        '#suffix' => '</h4>',
      ),
      'description' => array(
        '#markup' => t('Choose what commands will be run during this deploy.'),
      ),
      '#attributes' => array(
        'class' => array(
          'alert alert-info'
        ),
      ),
    );
    $form['hooks']['checkout'] = array(
      '#title' => _hosting_task_deploy_command_form_title('git', $node->commands),
      '#type' => 'checkbox',
      '#default_value' => TRUE,
      '#disabled' => TRUE,
      '#parents' => array(
        'parameters', 'build',
      ),
    );
    $form['hooks']['build'] = array(
      '#title' => _hosting_task_deploy_command_form_title('build', $node->commands),
      '#type' => 'checkbox',
      // @TODO: Use site default.
      '#default_value' => TRUE,
      '#parents' => array(
        'parameters', 'build',
      ),
    );
    $form['hooks']['install'] = array(
      '#title' => _hosting_task_deploy_command_form_title('install', $node->commands),
      '#type' => 'checkbox',
      '#parents' => array(
        'parameters', 'install',
      ),
    );
    $form['hooks']['deploy'] = array(
      '#title' => _hosting_task_deploy_command_form_title('deploy', $node->commands),
      '#type' => 'checkbox',
      '#default_value' => TRUE,
      '#parents' => array(
        'parameters', 'deploy',
      ),
    );
    $form['hooks']['test'] = array(
      '#title' => _hosting_task_deploy_command_form_title('test', $node->commands),
      '#type' => 'checkbox',
      '#parents' => array(
        'parameters', 'test',
      ),
    );

    // Check for working copy changes and offer to reset them for the user.
    $form['danger_zone'] = array(
      '#type' => 'container',
      'title' => array(
        '#prefix' => '<h4>',
        '#markup' => t('DANGER ZONE'),
        '#suffix' => '</h4>',
      ),
      '#attributes' => array(
        'class' => array(
          'alert alert-danger'
        ),
      ),
      '#states' => array(
        'visible' => array(
          ':input[name="parameters[install]"]' => array('checked' => TRUE),
        ),
      ),
    );
    $form['danger_zone']['git_reset'] = array(
      '#title' => t('Discard working copy changes.'),
      '#type' => 'checkbox',
      '#default_value' => FALSE,
      '#description' => t('Uncommitted changes were detected in the codebase. Check this box to discard changes to the code before attempting to deploy. If you do not, a "git checkout" command will still be attempted, and any errors will be logged.'),
      '#parents' => array(
        'parameters', 'git_reset'
      ),
      '#access' => !empty($diff) || !empty($status),
    );
    /**
     * @TODO: This was built with assumption deploy stages was available.
    $form['danger_zone']['confirm_destroy'] = array(
      '#title' => t('I understand that all data in the site !link will be destroyed.', array(
        '!link' => l($node->title, $node->title),
      )),
      '#type' => 'checkbox',
      '#default_value' => FALSE,
      '#description' => t('The site will be destroyed before the <code>install</code> command is run.'),
      '#parents' => array(
        'confirm_destroy'
      ),
      '#states' => array(
        'visible' => array(
          ':input[name="parameters[install]"]' => array('checked' => TRUE),
        ),
      ),
    );
    */
  }
  $form['git_info'] = array(
    '#title' => t('View git status in &path', array(
      '&path' => $node->git_root,
    )),
    '#type' => 'fieldset',
    '#collapsed' => true,
    '#collapsible' => true,
    'status' => array(
      '#prefix' => '<h4>' . t('Git Status') . '</h4><pre>',
      '#markup' => $status,
      '#suffix' => '</pre>',
      '#access' => !empty($status),
    ),
    'diff' => array(
      '#prefix' => '<h4>' . t('Git diff') . '</h4><pre>',
      '#markup' => $diff,
      '#suffix' => '</pre>',
      '#access' => !empty($diff),
    ),
    'clean' => array(
      '#prefix' => '<h4>',
      '#markup' => t('Git working copy is clean. No changes.'),
      '#suffix' => '</h4>',
      '#access' => empty($diff) && empty($status),
    ),
  );
  return $form;
}