function _varnish_terminal_run in Varnish 6
Same name and namespace in other branches
- 8 varnish.module \_varnish_terminal_run()
- 5 varnish.module \_varnish_terminal_run()
- 7 varnish.module \_varnish_terminal_run()
Helper function that sends commands to Varnish
Utilizes sockets to talk to varnish terminal.
5 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.
- varnish_requirements in ./
varnish.module - Implementation of hook_requirements()
File
- ./
varnish.module, line 291 - varnish.module Provide drupal hooks for integration with the Varnish control layer.
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'));
$timeout = variable_get('varnish_socket_timeout', VARNISH_DEFAULT_TIMETOUT);
$seconds = (int) ($timeout / 1000);
$milliseconds = (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' => $milliseconds,
));
socket_set_option($client, SOL_SOCKET, SO_RCVTIMEO, array(
'sec' => $seconds,
'usec' => $milliseconds,
));
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[] = 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?
if ($status['code'] == 107) {
// Require authentication
$secret = variable_get('varnish_control_key', '');
$challenge = substr($status['msg'], 0, 32);
$pack = $challenge . "\n" . $secret . "\n" . $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;
}