You are here

function _varnish_terminal_run in Varnish 7

Same name and namespace in other branches
  1. 8 varnish.module \_varnish_terminal_run()
  2. 5 varnish.module \_varnish_terminal_run()
  3. 6 varnish.module \_varnish_terminal_run()

Helper function that sends commands to Varnish.

Utilizes sockets to talk to varnish terminal.

4 calls to _varnish_terminal_run()
VarnishTestCase::assertVarnishCommand in ./varnish.test
Run a varnish command and make sure it worked for us.
varnish_admin_reports_page in ./varnish.admin.inc
Menu callback for varnish admin settings.
varnish_get_status in ./varnish.module
Get the status (up/down) of each of the varnish servers.
varnish_purge in ./varnish.module
Helper function to purge items for a host that matches the provided pattern.

File

./varnish.module, line 327
Common functions used for the module.

Code

function _varnish_terminal_run($commands) {
  if (!extension_loaded('sockets')) {

    // Prevent fatal errors if people don't have requirements.
    return FALSE;
  }

  // Convert single commands to an array so we can handle everything in the same
  // way.
  if (!is_array($commands)) {
    $commands = array(
      $commands,
    );
  }
  $ret = array();
  $terminals = explode(' ', variable_get('varnish_control_terminal', '127.0.0.1:6082'));

  // The variable varnish_socket_timeout defines the timeout in milliseconds.
  $timeout = variable_get('varnish_socket_timeout', VARNISH_DEFAULT_TIMEOUT);
  $seconds = (int) ($timeout / 1000);
  $microseconds = (int) ($timeout % 1000 * 1000);
  foreach ($terminals as $terminal) {
    list($server, $port) = explode(':', $terminal);
    $client = socket_create(AF_INET, SOCK_STREAM, getprotobyname('tcp'));
    socket_set_option($client, SOL_SOCKET, SO_SNDTIMEO, array(
      'sec' => $seconds,
      'usec' => $microseconds,
    ));
    socket_set_option($client, SOL_SOCKET, SO_RCVTIMEO, array(
      'sec' => $seconds,
      'usec' => $microseconds,
    ));
    if (@(!socket_connect($client, $server, $port))) {
      watchdog('varnish', 'Unable to connect to server socket @server:@port: %error', array(
        '@server' => $server,
        '@port' => $port,
        '%error' => socket_strerror(socket_last_error($client)),
      ), WATCHDOG_ERROR);
      $ret[$terminal] = FALSE;

      // If a varnish server is unavailable, move on to the next in the list.
      continue;
    }

    // If there is a CLI banner message (varnish >= 2.1.x), try to read it and
    // move on.
    if (floatval(variable_get('varnish_version', 2.1)) > 2.0) {
      $status = _varnish_read_socket($client);

      // Do we need to authenticate?
      // Require authentication.
      if ($status['code'] == 107) {
        $secret = variable_get('varnish_control_key', '');
        $challenge = substr($status['msg'], 0, 32);
        if (variable_get('varnish_control_key_appendnewline', TRUE)) {
          $pack = $challenge . "\n" . $secret . "\n" . $challenge . "\n";
        }
        else {
          $pack = $challenge . "\n" . $secret . $challenge . "\n";
        }
        $key = hash('sha256', $pack);
        socket_write($client, "auth {$key}\n");
        $status = _varnish_read_socket($client);
        if ($status['code'] != 200) {
          watchdog('varnish', 'Authentication to server failed!', array(), WATCHDOG_ERROR);
        }
      }
    }
    foreach ($commands as $command) {
      if ($status = _varnish_execute_command($client, $command)) {
        $ret[$terminal][$command] = $status;
      }
    }
  }
  return $ret;
}