ops.hookcmds¶
Low-level access to the Juju hook commands.
Charm authors should use the ops.Model
(via self.model
) rather than
directly running the hook commands, where possible. This module is primarily
provided to help with developing charming alternatives to the Ops framework.
Note: hookcmds
is not covered by the semver policy that applies to the rest
of Ops. We will do our best to avoid breaking changes, but we reserve the right
to make breaking changes within this package if necessary, within the Ops 3.x
series.
All methods are 1:1 mapping to Juju hook commands. This is a low-level API, available for charm use, but expected to be used via higher-level wrappers.
See Juju | Hook commands and Juju | Hook command list for a list of all Juju hook commands.
- class ops.hookcmds.Address(*, hostname: str, value: str, cidr: str)[source]¶
Bases:
object
A Juju space address, found in
BindAddress
objects.
- class ops.hookcmds.AppStatus( )[source]¶
Bases:
object
The status of a Juju application.
- units: dict[str, UnitStatus]¶
- class ops.hookcmds.BindAddress(
- *,
- mac_address: str,
- interface_name: str,
- addresses: list[~ops.hookcmds._types.Address] = <factory>,
Bases:
object
A Juju space bind address, found in
Network
objects.
- class ops.hookcmds.CloudCredential(
- *,
- auth_type: str,
- attributes: dict[str,
- str] = <factory>,
- redacted: list[str] = <factory>,
Bases:
object
Credentials for cloud.
Used as the type of attribute credential in
CloudSpec
.
- class ops.hookcmds.CloudSpec(
- *,
- type: str,
- name: str,
- region: str | None = None,
- endpoint: str | None = None,
- identity_endpoint: str | None = None,
- storage_endpoint: str | None = None,
- credential: ~ops.hookcmds._types.CloudCredential | None = None,
- ca_certificates: list[str] = <factory>,
- skip_tls_verify: bool = False,
- is_controller_cloud: bool = False,
Bases:
object
Cloud specification information (metadata) including credentials.
- credential: CloudCredential | None = None¶
Cloud credentials with key-value attributes.
- exception ops.hookcmds.Error(*, returncode: int, cmd: list[str], stdout: str = '', stderr: str = '')[source]¶
Bases:
Exception
Raised when a hook command exits with a non-zero code.
- class ops.hookcmds.Goal(*, status: str, since: datetime)[source]¶
Bases:
object
A goal status and when it was last updated, found in
GoalState
objects.
- class ops.hookcmds.GoalState( )[source]¶
Bases:
object
The units and relations that the model should have, and the status of achieving that.
- class ops.hookcmds.Network(
- *,
- bind_addresses: Sequence[BindAddress],
- egress_subnets: Sequence[str],
- ingress_addresses: Sequence[str],
Bases:
object
A Juju space.
- bind_addresses: Sequence[BindAddress]¶
- class ops.hookcmds.Port(
- protocol: Literal['tcp', 'udp', 'icmp'] | None = 'tcp',
- port: int | None = None,
- to_port: int | None = None,
- endpoints: list[str] | None = None,
Bases:
object
A port that Juju has opened for the charm.
- class ops.hookcmds.RelationModel(*, uuid: str)[source]¶
Bases:
object
Details of the model on the remote side of the relation.
- class ops.hookcmds.SecretInfo(
- *,
- revision: int,
- id: str = '',
- label: str = '',
- description: str = '',
- expiry: datetime | None = None,
- rotation: Literal['never', 'hourly', 'daily', 'weekly', 'monthly', 'quarterly', 'yearly'] | None = None,
- rotates: datetime | None = None,
Bases:
object
Metadata for Juju secrets.
- class ops.hookcmds.Storage(*, kind: str, location: Path)[source]¶
Bases:
object
Metadata for Juju storage.
- ops.hookcmds.action_fail(message: str | None = None)[source]¶
Set action fail status with message.
For more details, see: Juju | Hook commands | action-fail
- Parameters:
message – the failure error message. Juju will provide a default message if one is not provided.
- ops.hookcmds.action_get(key: str | None = None) dict[str, Any] | str [source]¶
Get action parameters.
action_get
returns the value of the parameter at the given key. If a dotted key (for example foo.bar) is passed,action_get
will recurse into the parameter map as needed.For more details, see: Juju | Hook commands | action-get
- Parameters:
key – The key of the action parameter to retrieve. If not provided, all parameters will be returned.
- ops.hookcmds.action_log(message: str)[source]¶
Record a progress message for the current action.
For more details, see: Juju | Hook commands | action-log
- Parameters:
message – The progress message to provide to the Juju user.
- ops.hookcmds.action_set(results: Mapping[str, Any])[source]¶
Set action results.
For more details, see: Juju | Hook commands | action-set
- Parameters:
results – The results map of the action, provided to the Juju user.
- ops.hookcmds.app_version_set(version: str)[source]¶
Specify which version of the application is deployed.
For more details, see: Juju | Hook commands | application-version-set
- Parameters:
version – the version of the application software the unit is running. This could be a package version number or some other useful identifier, such as a Git hash, that indicates the version of the deployed software. It shouldn’t be confused with the charm revision.
- ops.hookcmds.close_port(
- protocol: str | None = None,
- port: int | None = None,
- *,
- to_port: int | None = None,
- endpoints: str | Iterable[str] | None = None,
Register a request to close a port or port range.
For more details, see: Juju | Hook commands | close-port
- ops.hookcmds.config_get( ) Mapping[str, bool | int | float | str] | bool | int | float | str [source]¶
Retrieve application configuration.
Note that ‘secret’ type options are returned as string secret IDs.
If called without arguments, returns a dictionary containing all config settings that are either explicitly set, or which have a non-nil default value. If called with a key, it returns the value of that config option. Missing config keys are reported as nulls, and do not return an error.
For more details, see: Juju | Hook commands | config-get
- Parameters:
key – The configuration option to retrieve.
- ops.hookcmds.credential_get() CloudSpec [source]¶
Access cloud credentials.
For more details, see: Juju | Hook commands | credential-get
- ops.hookcmds.goal_state() GoalState [source]¶
Print the status of the charm’s peers and related units.
For more details, see: Juju | Hook commands | goal-state
- ops.hookcmds.is_leader() bool [source]¶
Obtain the current leadership status for the unit the charm code is executing on.
The value is not cached. It is accurate for 30s from the time the method is successfully called.
For more details, see: Juju | Hook commands | is-leader
- ops.hookcmds.juju_log( )[source]¶
Write a message to the juju log.
For more details, see: Juju | Hook commands | juju-log
- Parameters:
message – The message to log.
level – Send the message at the given level.
- ops.hookcmds.juju_reboot(*, now: bool = False)[source]¶
Reboot the host machine.
For more details, see: Juju | Hook commands | juju-reboot
- Parameters:
now – Reboot immediately, killing the invoking process.
- ops.hookcmds.network_get( ) Network [source]¶
Get network config.
For more details, see: Juju | Hook commands | network-get
- Parameters:
binding_name – A name of a binding (relation name or extra-binding name).
relation_id – An optional relation id to get network info for.
- ops.hookcmds.open_port(
- protocol: str | None = None,
- port: int | None = None,
- *,
- to_port: int | None = None,
- endpoints: str | Iterable[str] | None = None,
Register a request to open a port or port range.
For more details, see: Juju | Hook commands | open-port
- Parameters:
protocol – Open the port(s) for the specified protocol.
port – If
to_port
is not specified, open only this port.to_port – Open a range of ports from
port
toto_port
.endpoints – If not provided, ports will be opened for all defined application endpoints. To constrain the open request to specific endpoints, provide one or more endpoint names.
- ops.hookcmds.opened_ports(*, endpoints: bool = False) list[Port] [source]¶
List all ports or port ranges opened by the unit.
For more details, see: Juju | Hook commands | opened-ports
- Parameters:
endpoints – If
True
, each entry in the port list will be augmented with a list of endpoints that the port applies to. If a port applies to all endpoints, this will be indicated by an endpoint of*
.
- ops.hookcmds.relation_get( ) dict[str, str] | str [source]¶
Get relation settings.
Note that
id
can only beNone
if the current hook is a relation event, in which case Juju will use the ID of the relation that triggered the event.For more details, see: Juju | Hook commands | relation-get
- Parameters:
app – Get the relation data for the overall application, not just a unit
id – The ID of the relation to get data for, or
None
to get data for the relation that triggered the current hook.key – The specific key to get data for, or
None
to get all data.unit – The unit to get data for, or
None
to get data for all units.
- ops.hookcmds.relation_ids(name: str) list[str] [source]¶
List all relation IDs for the given endpoint.
For more details, see: Juju | Hook commands | relation-ids
- Parameters:
name – the endpoint name.
- ops.hookcmds.relation_list(id: int | None = None, *, app: bool = False) list[str] [source]¶
List relation units.
Note that
id
can only beNone
if the current hook is a relation event, in which case Juju will use the ID of the relation that triggered the event.For more details, see: Juju | Hook commands | relation-list
- Parameters:
id – The ID of the relation to list units for, or
None
to list units for the relation that triggered the current hook.app – List remote application instead of participating units.
- ops.hookcmds.relation_model_get(id: int | None = None) RelationModel [source]¶
Get details about the model hosting a related application.
Note that
id
can only beNone
if the current hook is a relation event, in which case Juju will use the ID of the relation that triggered the event.For more details, see: Juju | Hook commands | relation-model-get
- Parameters:
id – The ID of the relation to get data for, or
None
to get data for the relation that triggered the current hook.
- ops.hookcmds.relation_set( )[source]¶
Set relation settings.
Setting the value for a key to the empty string deletes that key. The data is updated with the values provided, not replaced, so any keys that are not specified are left unchanged.
Note that
id
can only beNone
if the current hook is a relation event, in which case Juju will use the ID of the relation that triggered the event.For more details, see: Juju | Hook commands | relation-set
- Parameters:
data – The relation data to set.
id – The ID of the relation to set data for, or
None
to set data for the relation that triggered the current hook.app – Set data for the overall application, not just a unit.
- ops.hookcmds.resource_get(name: str) Path [source]¶
Get the path to the locally cached resource file.
For more details, see: Juju | Hook commands | resource-get
- Parameters:
name – The name of the resource.
- ops.hookcmds.secret_add(
- content: dict[str, str],
- *,
- label: str | None = None,
- description: str | None = None,
- expire: datetime | str | None = None,
- rotate: Literal['never', 'hourly', 'daily', 'weekly', 'monthly', 'quarterly', 'yearly'] | None = None,
- owner: Literal['application', 'unit'] = 'application',
Add a new secret.
For more details, see: Juju | Hook commands | secret-add
- Parameters:
content – The content of the secret.
label – A label used to identify the secret in hooks.
description – The secret description.
expire – Either a duration or time when the secret should expire.
rotate – The secret rotation policy.
owner – The owner of the secret, either the application or the unit.
- ops.hookcmds.secret_get( ) dict[str, str] [source]¶
Get the content of a secret.
Either the ID or the label or both must be provided. If both are provided, the secret is looked up by ID and the label is set to the one provided.
For more details, see: Juju | Hook commands | secret-get
- Parameters:
id – The ID of the secret to retrieve.
label – The label of the secret to retrieve.
peek – Get the latest revision just this time.
refresh – Get the latest revision and also get this same revision for subsequent calls.
- ops.hookcmds.secret_grant(id: str, relation_id: int, *, unit: str | None = None)[source]¶
Grant access to a secret.
For more details, see: Juju | Hook commands | secret-grant
- Parameters:
id – The ID of the secret to grant access to.
relation_id – The relation with which to associate the grant.
unit – If provided, limit access to just that unit.
- ops.hookcmds.secret_ids() list[str] [source]¶
Retrieve IDs for secrets owned by the application.
For more details, see: Juju | Hook commands | secret-ids
- ops.hookcmds.secret_info_get( ) SecretInfo [source]¶
Get a secret’s metadata info.
Either the ID or the label must be provided.
For more details, see: Juju | Hook commands | secret-info-get
- Parameters:
id – The ID of the secret to retrieve.
label – The label of the secret to retrieve.
- ops.hookcmds.secret_remove(id: str, *, revision: int | None = None)[source]¶
Remove an existing secret.
For more details, see: Juju | Hook commands | secret-remove
- Parameters:
id – The ID of the secret to remove.
revision – The revision of the secret to remove. If not provided, all revisions are removed.
- ops.hookcmds.secret_revoke( )[source]¶
Revoke access to a secret.
For more details, see: Juju | Hook commands | secret-revoke
- Parameters:
id – The ID of the secret.
relation_id – The relation for which to revoke the grant.
app – Revoke access from all units in that application.
unit – Revoke access from just this unit.
- ops.hookcmds.secret_set(
- id: str,
- *,
- content: dict[str, str] | None = None,
- label: str | None = None,
- description: str | None = None,
- expire: datetime | str | None = None,
- rotate: Literal['never', 'hourly', 'daily', 'weekly', 'monthly', 'quarterly', 'yearly'] | None = None,
- owner: Literal['application', 'unit'] = 'application',
Update an existing secret.
For more details, see: Juju | Hook commands | secret-set
- Parameters:
id – The ID of the secret to update.
content – The content of the secret.
label – A label used to identify the secret in hooks.
description – The secret description.
expire – Either a duration or time when the secret should expire.
rotate – The secret rotation policy.
owner – The owner of the secret, either the application or the unit.
- ops.hookcmds.state_delete(key: str)[source]¶
Delete server-side-state key value pairs.
For more details, see: Juju | Hook commands | state-delete
- Parameters:
key – The key of the server-side state to delete.
- ops.hookcmds.state_get(key: str | None) dict[str, str] | str [source]¶
Get server-side-state value.
For more details, see: Juju | Hook commands | state-get
- Parameters:
key – The key of the server-side state to get. If
None
, get all keys and values.
- ops.hookcmds.state_set(data: Mapping[str, str])[source]¶
Set server-side-state values.
For more details, see: Juju | Hook commands | state-set
- Parameters:
data – The key-value pairs to set in the server-side state.
- ops.hookcmds.status_get(
- *,
- app: bool = False,
Get a status of a unit or an application.
For more details, see: Juju | Hook commands | status-get
- Parameters:
app – Get status for all units of this application if this unit is the leader.
- ops.hookcmds.status_set(
- status: Literal['active', 'blocked', 'maintenance', 'waiting'],
- message: str | None = None,
- *,
- app: bool = False,
Set a status of a unit or an application.
For more details, see: Juju | Hook commands | status-set
- Parameters:
status – The status to set.
message – A message to include in the status.
app – If
True
, set this status for the application to which the unit belongs.
- ops.hookcmds.storage_add(counts: Mapping[str, int])[source]¶
Add storage instances.
For more details, see: Juju | Hook commands | storage-add
- Parameters:
counts – A maps of storage names to the number of instances of that storage to create.
- ops.hookcmds.storage_get(id: str | None = None) Storage [source]¶
Retrieve information for the storage instance with the specified ID.
Note that
id
can only beNone
if the current hook is a storage event, in which case Juju will use the ID of the storage that triggered the event.For more details, see: Juju | Hook commands | storage-get
- Parameters:
id – The ID of the storage instance.