bareos.bsock.directorconsolejson.DirectorConsoleJson
- class bareos.bsock.directorconsolejson.DirectorConsoleJson(*args, **kwargs)[source]
Bases:
DirectorConsole
Communicate with the Bareos Director Daemon Console interface in API mode 2 (JSON).
Example
>>> import bareos.bsock >>> directorconsole = bareos.bsock.DirectorConsoleJson(address='localhost', port=9101, password='secret') >>> pools = directorconsole.call('list pools') >>> for pool in pools["pools"]: ... print(pool["name"]) ... Scratch Incremental Full Differential
The results the the call method is a
dict
object.In case of an error, an exception, derived from
bareos.exceptions.Error
is raised.Parameters: The parameter are identical to
bareos.bsock.directorconsole.DirectorConsole
.- Raises:
bareos.exceptions.JsonRpcInvalidJsonReceivedException – if the “.api” command is not available.
Methods
Extend argparser with
DirectorConsole
specific parameter.Extract arguments.
Login to a Bareos Daemon.
Calls a command on the Bareos Director and returns its result.
Calls a command on the Bareos Director and returns its result.
Close the connection.
Establish a network connection and authenticate.
If a encrypted connection is used, returns information about the encryption.
Get the Bareos Console protocol version that is used.
Bareos TLS-PSK excepts the identiy is a specific format.
Is readable data available?
Enter the interactive mode.
Verifes that last status still indicates connected.
Checks if a Bareos signal indicates the end of a message.
Checks if we have all required modules for TLS-PSK.
Retrieve a message and evaluate it.
Tries to reconnect.
Receive a single message.
Receive a number of bytes.
Receive a full message.
Retrieve a message of the specific length.
Send message to the Daemon.
Alias for
call()
.- static argparser_add_default_command_line_arguments(argparser)
Extend argparser with
DirectorConsole
specific parameter.Every command line program must offer a similar set of parameter to connect to a Bareos Director. This method adds the required parameter to an existing ArgParser instance. Parameter required to initialize a DirectorConsole class are stored in variables prefixed with
BAREOS_
.Use
bareos.bsock.lowlevel.LowLevel.argparser_get_bareos_parameter()
to retrieve the relevant parameter (with theBAREOS_
prefix removed).Example
>>> argparser = argparse.ArgumentParser(description="Console to Bareos Director.") >>> DirectorConsole.argparser_add_default_command_line_arguments(argparser) >>> args = argparser.parse_args() >>> bareos_args = DirectorConsole.argparser_get_bareos_parameter(args) >>> director = DirectorConsole(**bareos_args)
- Parameters:
argparser (ArgParser) – ArgParser instance.
- static argparser_get_bareos_parameter(args)
Extract arguments.
This method is usally used together with the method
argparser_add_default_command_line_arguments()
.- Parameters:
args (ArgParser.Namespace) – Arguments retrieved by
ArgumentParser.parse_args()
.- Returns:
The relevant parameter from args to initialize a connection.
- Return type:
- auth()
Login to a Bareos Daemon.
- Returns:
True, if the authentication succeeds. In earlier versions, authentication failures returned False. However, now an authentication failure raises an exception.
- Return type:
- Raises:
bareos.exceptions.AuthenticationError – if authentication fails.
- call(command)[source]
Calls a command on the Bareos Director and returns its result.
If the JSON-RPC result indicates an error (contains the
error
element), an exception will be raised.- Parameters:
command (str or list) – Command to execute. Best provided as a list.
- Returns:
Result received from the Bareos Director.
- Return type:
- Raises:
bareos.exceptions.JsonRpcErrorReceivedException – if an JSON-RPC error object is received.
bareos.exceptions.JsonRpcInvalidJsonReceivedException – if an invalid JSON-RPC result is received.
- call_fullresult(command)[source]
Calls a command on the Bareos Director and returns its result.
- Returns:
Result received from the Bareos Director.
- Return type:
- Raises:
bareos.exceptions.JsonRpcInvalidJsonReceivedException – if an invalid JSON-RPC result is received.
- close()
Close the connection.
- connect(address, port, dirname, connection_type, name=None, password=None)
Establish a network connection and authenticate.
- Parameters:
address (str) – Address of the Bareos Director (hostname or IP).
port (int) – Port number of the Bareos Director.
dirname (str, optional) – Name of the Bareos Director. Deprecated, normally not required.
connection_type (int) – See
bareos.bsock.connectiontype.ConnectionType
.name (str, optional) – Credential name.
password (str, bareos.util.Password) – Credential password, in cleartext or as Password object.
- Returns:
True, if the authentication succeeds. In earlier versions, authentication failures returned False. However, now an authentication failure raises an exception.
- Return type:
- Raises:
bareos.exceptions.ConnectionError – If connection can be established.
bareos.exceptions.PamAuthenticationError – If PAM authentication fails.
bareos.exceptions.AuthenticationError – If Bareos authentication fails.
- get_cipher()
If a encrypted connection is used, returns information about the encryption. Else it returns None.
- Returns:
Returns a three-value tuple containing the name of the cipher being used, the version of the SSL protocol that defines its use, and the number of secret bits being used. If the connection is unencrypted or has been established, returns None.
- Return type:
tuple or None
- get_protocol_version()
Get the Bareos Console protocol version that is used.
- Returns:
Number that represents the Bareos Console protocol version (see
bareos.bsock.protocolversions.ProtocolVersions
.)- Return type:
- get_tls_psk_identity()
Bareos TLS-PSK excepts the identiy is a specific format.
- has_data()
Is readable data available?
- Returns:
True: if readable data is available.
- Return type:
- interactive()
Enter the interactive mode.
Exit via typing “exit” or “quit”.
- Returns:
True, if exited by user command.
- Return type:
- is_connected()
Verifes that last status still indicates connected.
- Returns:
True, if still connected.
- Return type:
- is_end_of_message(data)
Checks if a Bareos signal indicates the end of a message.
- static is_tls_psk_available()
Checks if we have all required modules for TLS-PSK.
- receive_and_evaluate_response_message()
Retrieve a message and evaluate it.
Only used during in the authentication phase.
- Returns:
(code, text).
- Return type:
2-tuple
- reconnect()
Tries to reconnect.
- Returns:
True, if the connection could be reestablished.
- Return type:
- recv()
Receive a single message.
This is, header (4 bytes): if
> 0: length of the following message
< 0: Bareos signal
- Returns:
Message retrieved via the connection.
- Return type:
- Raises:
bareos.exceptions.SignalReceivedException – If a Bareos signal is received.
- recv_bytes(length, timeout=10)
Receive a number of bytes.
- Parameters:
- Raises:
bareos.exceptions.ConnectionLostError – If the socket connection gets lost.
socket.timeout – If a timeout occurs on the socket connection, meaning no data received.
- recv_msg(regex=b'^\\d\\d\\d\\d OK.*$')
Receive a full message.
It retrieves messages (header + message text), until
the message contains the specified regex or
the header indicates a signal.
- Parameters:
regex (bytes) – Descripes the expected end of the message.
- Returns:
Message retrieved via the connection.
- Return type:
- Raises:
bareos.exceptions.SignalReceivedException – If a Bareos signal is received.
- recv_submsg(length)
Retrieve a message of the specific length.