| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright (c) 2016, Kamil Szczygiel <kamil.szczygiel () intel.com> |
| 4 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 6 |
|
| 7 |
from __future__ import annotations |
| 8 |
|
| 9 |
DOCUMENTATION = r""" |
| 10 |
module: influxdb_retention_policy |
| 11 |
short_description: Manage InfluxDB retention policies |
| 12 |
description: |
| 13 |
- Manage InfluxDB retention policies. |
| 14 |
author: "Kamil Szczygiel (@kamsz)" |
| 15 |
requirements: |
| 16 |
- "influxdb >= 0.9" |
| 17 |
- requests |
| 18 |
attributes: |
| 19 |
check_mode: |
| 20 |
support: full |
| 21 |
diff_mode: |
| 22 |
support: none |
| 23 |
options: |
| 24 |
database_name: |
| 25 |
description: |
| 26 |
- Name of the database. |
| 27 |
required: true |
| 28 |
type: str |
| 29 |
policy_name: |
| 30 |
description: |
| 31 |
- Name of the retention policy. |
| 32 |
required: true |
| 33 |
type: str |
| 34 |
state: |
| 35 |
description: |
| 36 |
- State of the retention policy. |
| 37 |
choices: [absent, present] |
| 38 |
default: present |
| 39 |
type: str |
| 40 |
version_added: 3.1.0 |
| 41 |
duration: |
| 42 |
description: |
| 43 |
- Determines how long InfluxDB should keep the data. If specified, it should be V(INF) or at least one hour. If not |
| 44 |
specified, V(INF) is assumed. Supports complex duration expressions with multiple units. |
| 45 |
- Required only if O(state) is set to V(present). |
| 46 |
type: str |
| 47 |
replication: |
| 48 |
description: |
| 49 |
- Determines how many independent copies of each point are stored in the cluster. |
| 50 |
- Required only if O(state) is set to V(present). |
| 51 |
type: int |
| 52 |
default: |
| 53 |
description: |
| 54 |
- Sets the retention policy as default retention policy. |
| 55 |
type: bool |
| 56 |
default: false |
| 57 |
shard_group_duration: |
| 58 |
description: |
| 59 |
- Determines the time range covered by a shard group. If specified it must be at least one hour. If not provided, it |
| 60 |
is determined by InfluxDB by the rentention policy's duration. Supports complex duration expressions with multiple |
| 61 |
units. |
| 62 |
type: str |
| 63 |
version_added: '2.0.0' |
| 64 |
extends_documentation_fragment: |
| 65 |
- community.general._influxdb |
| 66 |
- community.general._attributes |
| 67 |
""" |
| 68 |
|
| 69 |
EXAMPLES = r""" |
| 70 |
# Example influxdb_retention_policy command from Ansible Playbooks |
| 71 |
- name: Create 1 hour retention policy |
| 72 |
community.general.influxdb_retention_policy: |
| 73 |
hostname: "{{ influxdb_ip_address }}" |
| 74 |
database_name: "{{ influxdb_database_name }}" |
| 75 |
policy_name: test |
| 76 |
duration: 1h |
| 77 |
replication: 1 |
| 78 |
ssl: true |
| 79 |
validate_certs: true |
| 80 |
state: present |
| 81 |
|
| 82 |
- name: Create 1 day retention policy with 1 hour shard group duration |
| 83 |
community.general.influxdb_retention_policy: |
| 84 |
hostname: "{{ influxdb_ip_address }}" |
| 85 |
database_name: "{{ influxdb_database_name }}" |
| 86 |
policy_name: test |
| 87 |
duration: 1d |
| 88 |
replication: 1 |
| 89 |
shard_group_duration: 1h |
| 90 |
state: present |
| 91 |
|
| 92 |
- name: Create 1 week retention policy with 1 day shard group duration |
| 93 |
community.general.influxdb_retention_policy: |
| 94 |
hostname: "{{ influxdb_ip_address }}" |
| 95 |
database_name: "{{ influxdb_database_name }}" |
| 96 |
policy_name: test |
| 97 |
duration: 1w |
| 98 |
replication: 1 |
| 99 |
shard_group_duration: 1d |
| 100 |
state: present |
| 101 |
|
| 102 |
- name: Create infinite retention policy with 1 week of shard group duration |
| 103 |
community.general.influxdb_retention_policy: |
| 104 |
hostname: "{{ influxdb_ip_address }}" |
| 105 |
database_name: "{{ influxdb_database_name }}" |
| 106 |
policy_name: test |
| 107 |
duration: INF |
| 108 |
replication: 1 |
| 109 |
ssl: false |
| 110 |
shard_group_duration: 1w |
| 111 |
state: present |
| 112 |
|
| 113 |
- name: Create retention policy with complex durations |
| 114 |
community.general.influxdb_retention_policy: |
| 115 |
hostname: "{{ influxdb_ip_address }}" |
| 116 |
database_name: "{{ influxdb_database_name }}" |
| 117 |
policy_name: test |
| 118 |
duration: 5d1h30m |
| 119 |
replication: 1 |
| 120 |
ssl: false |
| 121 |
shard_group_duration: 1d10h30m |
| 122 |
state: present |
| 123 |
|
| 124 |
- name: Drop retention policy |
| 125 |
community.general.influxdb_retention_policy: |
| 126 |
hostname: "{{ influxdb_ip_address }}" |
| 127 |
database_name: "{{ influxdb_database_name }}" |
| 128 |
policy_name: test |
| 129 |
state: absent |
| 130 |
""" |
| 131 |
|
| 132 |
RETURN = r""" |
| 133 |
# only defaults |
| 134 |
""" |
| 135 |
|
| 136 |
import re |
| 137 |
|
| 138 |
try: |
| 139 |
import requests.exceptions |
| 140 |
from influxdb import exceptions |
| 141 |
except ImportError: |
| 142 |
pass |
| 143 |
|
| 144 |
from ansible.module_utils.basic import AnsibleModule |
| 145 |
|
| 146 |
from ansible_collections.community.general.plugins.module_utils._influxdb import InfluxDb |
| 147 |
|
| 148 |
VALID_DURATION_REGEX = re.compile(r"^(INF|(\d+(ns|u|µ|ms|s|m|h|d|w)))+$") |
| 149 |
|
| 150 |
DURATION_REGEX = re.compile(r"(\d+)(ns|u|µ|ms|s|m|h|d|w)") |
| 151 |
EXTENDED_DURATION_REGEX = re.compile(r"(?:(\d+)(ns|u|µ|ms|m|h|d|w)|(\d+(?:\.\d+)?)(s))") |
| 152 |
|
| 153 |
DURATION_UNIT_NANOSECS = { |
| 154 |
"ns": 1, |
| 155 |
"u": 1000, |
| 156 |
"µ": 1000, |
| 157 |
"ms": 1000 * 1000, |
| 158 |
"s": 1000 * 1000 * 1000, |
| 159 |
"m": 1000 * 1000 * 1000 * 60, |
| 160 |
"h": 1000 * 1000 * 1000 * 60 * 60, |
| 161 |
"d": 1000 * 1000 * 1000 * 60 * 60 * 24, |
| 162 |
"w": 1000 * 1000 * 1000 * 60 * 60 * 24 * 7, |
| 163 |
} |
| 164 |
|
| 165 |
MINIMUM_VALID_DURATION = 1 * DURATION_UNIT_NANOSECS["h"] |
| 166 |
MINIMUM_VALID_SHARD_GROUP_DURATION = 1 * DURATION_UNIT_NANOSECS["h"] |
| 167 |
|
| 168 |
|
| 169 |
def check_duration_literal(value): |
| 170 |
return VALID_DURATION_REGEX.search(value) is not None |
| 171 |
|
| 172 |
|
| 173 |
def parse_duration_literal(value, extended=False): |
| 174 |
duration = 0.0 |
| 175 |
|
| 176 |
if value == "INF": |
| 177 |
return duration |
| 178 |
|
| 179 |
lookup = (EXTENDED_DURATION_REGEX if extended else DURATION_REGEX).findall(value) |
| 180 |
|
| 181 |
for duration_literal in lookup: |
| 182 |
filtered_literal = [_f for _f in duration_literal if _f] |
| 183 |
duration_val = float(filtered_literal[0]) |
| 184 |
duration += duration_val * DURATION_UNIT_NANOSECS[filtered_literal[1]] |
| 185 |
|
| 186 |
return duration |
| 187 |
|
| 188 |
|
| 189 |
def find_retention_policy(module, client): |
| 190 |
database_name = module.params["database_name"] |
| 191 |
policy_name = module.params["policy_name"] |
| 192 |
hostname = module.params["hostname"] |
| 193 |
retention_policy = None |
| 194 |
|
| 195 |
try: |
| 196 |
retention_policies = client.get_list_retention_policies(database=database_name) |
| 197 |
for policy in retention_policies: |
| 198 |
if policy["name"] == policy_name: |
| 199 |
retention_policy = policy |
| 200 |
break |
| 201 |
except requests.exceptions.ConnectionError as e: |
| 202 |
module.fail_json(msg=f"Cannot connect to database {database_name} on {hostname} : {e}") |
| 203 |
|
| 204 |
if retention_policy is not None: |
| 205 |
retention_policy["duration"] = parse_duration_literal(retention_policy["duration"], extended=True) |
| 206 |
retention_policy["shardGroupDuration"] = parse_duration_literal( |
| 207 |
retention_policy["shardGroupDuration"], extended=True |
| 208 |
) |
| 209 |
|
| 210 |
return retention_policy |
| 211 |
|
| 212 |
|
| 213 |
def create_retention_policy(module, client): |
| 214 |
database_name = module.params["database_name"] |
| 215 |
policy_name = module.params["policy_name"] |
| 216 |
duration = module.params["duration"] |
| 217 |
replication = module.params["replication"] |
| 218 |
default = module.params["default"] |
| 219 |
shard_group_duration = module.params["shard_group_duration"] |
| 220 |
|
| 221 |
if not check_duration_literal(duration): |
| 222 |
module.fail_json(msg="Failed to parse value of duration") |
| 223 |
|
| 224 |
influxdb_duration_format = parse_duration_literal(duration) |
| 225 |
if influxdb_duration_format != 0 and influxdb_duration_format < MINIMUM_VALID_DURATION: |
| 226 |
module.fail_json(msg="duration value must be at least 1h") |
| 227 |
|
| 228 |
if shard_group_duration is not None: |
| 229 |
if not check_duration_literal(shard_group_duration): |
| 230 |
module.fail_json(msg="Failed to parse value of shard_group_duration") |
| 231 |
|
| 232 |
influxdb_shard_group_duration_format = parse_duration_literal(shard_group_duration) |
| 233 |
if influxdb_shard_group_duration_format < MINIMUM_VALID_SHARD_GROUP_DURATION: |
| 234 |
module.fail_json(msg="shard_group_duration value must be finite and at least 1h") |
| 235 |
|
| 236 |
if not module.check_mode: |
| 237 |
try: |
| 238 |
if shard_group_duration: |
| 239 |
client.create_retention_policy( |
| 240 |
policy_name, duration, replication, database_name, default, shard_group_duration |
| 241 |
) |
| 242 |
else: |
| 243 |
client.create_retention_policy(policy_name, duration, replication, database_name, default) |
| 244 |
except exceptions.InfluxDBClientError as e: |
| 245 |
module.fail_json(msg=e.content) |
| 246 |
module.exit_json(changed=True) |
| 247 |
|
| 248 |
|
| 249 |
def alter_retention_policy(module, client, retention_policy): |
| 250 |
database_name = module.params["database_name"] |
| 251 |
policy_name = module.params["policy_name"] |
| 252 |
duration = module.params["duration"] |
| 253 |
replication = module.params["replication"] |
| 254 |
default = module.params["default"] |
| 255 |
shard_group_duration = module.params["shard_group_duration"] |
| 256 |
|
| 257 |
changed = False |
| 258 |
|
| 259 |
if not check_duration_literal(duration): |
| 260 |
module.fail_json(msg="Failed to parse value of duration") |
| 261 |
|
| 262 |
influxdb_duration_format = parse_duration_literal(duration) |
| 263 |
if influxdb_duration_format != 0 and influxdb_duration_format < MINIMUM_VALID_DURATION: |
| 264 |
module.fail_json(msg="duration value must be at least 1h") |
| 265 |
|
| 266 |
if shard_group_duration is None: |
| 267 |
influxdb_shard_group_duration_format = retention_policy["shardGroupDuration"] |
| 268 |
else: |
| 269 |
if not check_duration_literal(shard_group_duration): |
| 270 |
module.fail_json(msg="Failed to parse value of shard_group_duration") |
| 271 |
|
| 272 |
influxdb_shard_group_duration_format = parse_duration_literal(shard_group_duration) |
| 273 |
if influxdb_shard_group_duration_format < MINIMUM_VALID_SHARD_GROUP_DURATION: |
| 274 |
module.fail_json(msg="shard_group_duration value must be finite and at least 1h") |
| 275 |
|
| 276 |
if ( |
| 277 |
retention_policy["duration"] != influxdb_duration_format |
| 278 |
or retention_policy["shardGroupDuration"] != influxdb_shard_group_duration_format |
| 279 |
or retention_policy["replicaN"] != int(replication) |
| 280 |
or retention_policy["default"] != default |
| 281 |
): |
| 282 |
if not module.check_mode: |
| 283 |
try: |
| 284 |
client.alter_retention_policy( |
| 285 |
policy_name, database_name, duration, replication, default, shard_group_duration |
| 286 |
) |
| 287 |
except exceptions.InfluxDBClientError as e: |
| 288 |
module.fail_json(msg=e.content) |
| 289 |
changed = True |
| 290 |
module.exit_json(changed=changed) |
| 291 |
|
| 292 |
|
| 293 |
def drop_retention_policy(module, client): |
| 294 |
database_name = module.params["database_name"] |
| 295 |
policy_name = module.params["policy_name"] |
| 296 |
|
| 297 |
if not module.check_mode: |
| 298 |
try: |
| 299 |
client.drop_retention_policy(policy_name, database_name) |
| 300 |
except exceptions.InfluxDBClientError as e: |
| 301 |
module.fail_json(msg=e.content) |
| 302 |
module.exit_json(changed=True) |
| 303 |
|
| 304 |
|
| 305 |
def main(): |
| 306 |
argument_spec = InfluxDb.influxdb_argument_spec() |
| 307 |
argument_spec.update( |
| 308 |
state=dict(default="present", type="str", choices=["present", "absent"]), |
| 309 |
database_name=dict(required=True, type="str"), |
| 310 |
policy_name=dict(required=True, type="str"), |
| 311 |
duration=dict(type="str"), |
| 312 |
replication=dict(type="int"), |
| 313 |
default=dict(default=False, type="bool"), |
| 314 |
shard_group_duration=dict(type="str"), |
| 315 |
) |
| 316 |
module = AnsibleModule( |
| 317 |
argument_spec=argument_spec, |
| 318 |
supports_check_mode=True, |
| 319 |
required_if=(("state", "present", ["duration", "replication"]),), |
| 320 |
) |
| 321 |
|
| 322 |
state = module.params["state"] |
| 323 |
|
| 324 |
influxdb = InfluxDb(module) |
| 325 |
client = influxdb.connect_to_influxdb() |
| 326 |
|
| 327 |
retention_policy = find_retention_policy(module, client) |
| 328 |
|
| 329 |
if state == "present": |
| 330 |
if retention_policy: |
| 331 |
alter_retention_policy(module, client, retention_policy) |
| 332 |
else: |
| 333 |
create_retention_policy(module, client) |
| 334 |
|
| 335 |
if state == "absent": |
| 336 |
if retention_policy: |
| 337 |
drop_retention_policy(module, client) |
| 338 |
else: |
| 339 |
module.exit_json(changed=False) |
| 340 |
|
| 341 |
|
| 342 |
if __name__ == "__main__": |
| 343 |
main() |