| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2024, Andrew Klychkov (@Andersson007) <[email protected]> |
| 5 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
|
| 7 |
from __future__ import (absolute_import, division, print_function) |
| 8 |
__metaclass__ = type |
| 9 |
|
| 10 |
DOCUMENTATION = r''' |
| 11 |
--- |
| 12 |
module: clickhouse_db |
| 13 |
|
| 14 |
short_description: Creates or removes a ClickHouse database using the clickhouse-driver Client interface |
| 15 |
|
| 16 |
description: |
| 17 |
- Creates or removes a ClickHouse database using the |
| 18 |
L(clickhouse-driver,https://clickhouse-driver.readthedocs.io/en/latest) Client interface. |
| 19 |
|
| 20 |
attributes: |
| 21 |
check_mode: |
| 22 |
description: Supports check_mode. |
| 23 |
support: full |
| 24 |
|
| 25 |
version_added: '0.3.0' |
| 26 |
|
| 27 |
author: |
| 28 |
- Andrew Klychkov (@Andersson007) |
| 29 |
- Aleksandr Vagachev (@aleksvagachev) |
| 30 |
|
| 31 |
extends_documentation_fragment: |
| 32 |
- community.clickhouse.client_inst_opts |
| 33 |
|
| 34 |
options: |
| 35 |
state: |
| 36 |
description: |
| 37 |
- Database state. |
| 38 |
- If C(present), will create the database if not exists. |
| 39 |
- If C(absent), will drop the database if exists. |
| 40 |
- If C(rename), will rename the database if exists. |
| 41 |
type: str |
| 42 |
choices: ['present', 'absent', 'rename'] |
| 43 |
default: 'present' |
| 44 |
name: |
| 45 |
description: |
| 46 |
- Database name to add or remove. |
| 47 |
type: str |
| 48 |
required: true |
| 49 |
engine: |
| 50 |
description: |
| 51 |
- Database engine. Once set, cannot be changed. |
| 52 |
type: str |
| 53 |
cluster: |
| 54 |
description: |
| 55 |
- Run the command on all cluster hosts. |
| 56 |
- If the cluster is not configured, the command will crash with an error. |
| 57 |
type: str |
| 58 |
version_added: '0.4.0' |
| 59 |
target: |
| 60 |
description: |
| 61 |
- Name for renaming the database. |
| 62 |
type: str |
| 63 |
version_added: '0.4.0' |
| 64 |
comment: |
| 65 |
description: |
| 66 |
- Database comment. Once set, cannot be changed. |
| 67 |
type: str |
| 68 |
version_added: '0.4.0' |
| 69 |
''' |
| 70 |
|
| 71 |
EXAMPLES = r''' |
| 72 |
- name: Create database |
| 73 |
community.clickhouse.clickhouse_db: |
| 74 |
login_host: localhost |
| 75 |
login_user: alice |
| 76 |
login_db: foo |
| 77 |
login_password: my_password |
| 78 |
name: test_db |
| 79 |
engine: Memory |
| 80 |
state: present |
| 81 |
comment: Test DB |
| 82 |
|
| 83 |
- name: Drop database |
| 84 |
community.clickhouse.clickhouse_db: |
| 85 |
login_host: localhost |
| 86 |
login_user: alice |
| 87 |
login_db: foo |
| 88 |
login_password: my_password |
| 89 |
name: test_db |
| 90 |
state: absent |
| 91 |
|
| 92 |
# Rename the database test_db to prod_db. |
| 93 |
# If the database test_db exists, it will be renamed to prod_db. |
| 94 |
# If the database test_db does not exist and the prod_db database exists, |
| 95 |
# the module will report that nothing has changed. |
| 96 |
# If both the databases exist, an error will be raised. |
| 97 |
- name: Rename database |
| 98 |
community.clickhouse.clickhouse_db: |
| 99 |
login_host: localhost |
| 100 |
login_user: alice |
| 101 |
login_db: foo |
| 102 |
login_password: my_password |
| 103 |
name: test_db |
| 104 |
target: prod_db |
| 105 |
cluster: test_cluster |
| 106 |
state: rename |
| 107 |
''' |
| 108 |
|
| 109 |
RETURN = r''' |
| 110 |
executed_statements: |
| 111 |
description: |
| 112 |
- Data-modifying executed statements. |
| 113 |
returned: on success |
| 114 |
type: list |
| 115 |
sample: ['CREATE DATABASE test_db'] |
| 116 |
''' |
| 117 |
|
| 118 |
from ansible.module_utils.basic import AnsibleModule |
| 119 |
|
| 120 |
from ansible_collections.community.clickhouse.plugins.module_utils.clickhouse import ( |
| 121 |
check_clickhouse_driver, |
| 122 |
client_common_argument_spec, |
| 123 |
connect_to_db_via_client, |
| 124 |
execute_query, |
| 125 |
get_main_conn_kwargs, |
| 126 |
get_server_version, |
| 127 |
) |
| 128 |
|
| 129 |
|
| 130 |
executed_statements = [] |
| 131 |
|
| 132 |
|
| 133 |
class ClickHouseDB(): |
| 134 |
def __init__(self, module, client, name, cluster): |
| 135 |
self.module = module |
| 136 |
self.client = client |
| 137 |
self.name = name |
| 138 |
self.cluster = cluster |
| 139 |
self.srv_version = get_server_version(self.module, self.client) |
| 140 |
# Set default values, then update |
| 141 |
self.exists = False |
| 142 |
self.engine = None |
| 143 |
self.comment = None |
| 144 |
self.__populate_info() |
| 145 |
|
| 146 |
def __populate_info(self): |
| 147 |
# TODO: If anyone can determine the version when the comment feature |
| 148 |
# was added to database more precisely, you're welcome to adjust it here |
| 149 |
if self.srv_version['year'] >= 22: |
| 150 |
# The comment is not supported in all versions |
| 151 |
query = ("SELECT engine, comment " |
| 152 |
"FROM system.databases " |
| 153 |
"WHERE name = %(name)s") |
| 154 |
else: |
| 155 |
query = "SELECT engine FROM system.databases WHERE name = %(name)s" |
| 156 |
|
| 157 |
# Will move this function to the lib later and reuse |
| 158 |
exec_kwargs = {'params': {'name': self.name}} |
| 159 |
result = execute_query(self.module, self.client, query, exec_kwargs) |
| 160 |
|
| 161 |
# Assume the DB does not exist by default |
| 162 |
if result: |
| 163 |
# If exists |
| 164 |
self.exists = True |
| 165 |
self.engine = result[0][0] |
| 166 |
if self.srv_version['year'] >= 22: |
| 167 |
self.comment = result[0][1] |
| 168 |
|
| 169 |
def create(self, engine, comment): |
| 170 |
query = "CREATE DATABASE %s" % self.name |
| 171 |
if self.cluster: |
| 172 |
query += " ON CLUSTER %s" % self.cluster |
| 173 |
|
| 174 |
if engine: |
| 175 |
query += " ENGINE = %s" % engine |
| 176 |
|
| 177 |
if comment: |
| 178 |
query += " COMMENT '%s'" % comment |
| 179 |
|
| 180 |
executed_statements.append(query) |
| 181 |
|
| 182 |
if not self.module.check_mode: |
| 183 |
execute_query(self.module, self.client, query) |
| 184 |
|
| 185 |
return True |
| 186 |
|
| 187 |
def update(self, engine, comment): |
| 188 |
# IMPORTANT: In case in future any items here can change |
| 189 |
# please add check_mode handling |
| 190 |
|
| 191 |
# There's no way to change the engine |
| 192 |
# so just inform the users they have to recreate |
| 193 |
# the DB in order to change them |
| 194 |
if engine and engine != self.engine: |
| 195 |
msg = ("The provided engine '%s' is different from " |
| 196 |
"the current one '%s'. It is NOT possible to " |
| 197 |
"change it. The recreation of the database is required " |
| 198 |
"in order to change it." % (engine, self.engine)) |
| 199 |
self.module.warn(msg) |
| 200 |
|
| 201 |
if comment and comment != self.comment: |
| 202 |
msg = ("The provided comment '%s' is different from " |
| 203 |
"the current one '%s'. It is NOT possible to " |
| 204 |
"change it. The recreation of the database is required " |
| 205 |
"in order to change it." % (comment, self.comment)) |
| 206 |
self.module.warn(msg) |
| 207 |
|
| 208 |
return False |
| 209 |
|
| 210 |
def rename(self, target): |
| 211 |
query = "RENAME DATABASE %s TO %s" % (self.name, target) |
| 212 |
if self.cluster: |
| 213 |
query += " ON CLUSTER %s" % self.cluster |
| 214 |
|
| 215 |
executed_statements.append(query) |
| 216 |
|
| 217 |
if not self.module.check_mode: |
| 218 |
execute_query(self.module, self.client, query) |
| 219 |
|
| 220 |
return True |
| 221 |
|
| 222 |
def drop(self): |
| 223 |
query = "DROP DATABASE %s" % self.name |
| 224 |
if self.cluster: |
| 225 |
query += " ON CLUSTER %s" % self.cluster |
| 226 |
|
| 227 |
executed_statements.append(query) |
| 228 |
|
| 229 |
if not self.module.check_mode: |
| 230 |
execute_query(self.module, self.client, query) |
| 231 |
|
| 232 |
return True |
| 233 |
|
| 234 |
|
| 235 |
def main(): |
| 236 |
# Set up arguments. |
| 237 |
# If there are common arguments shared across several modules, |
| 238 |
# create the common_argument_spec() function under plugins/module_utils/* |
| 239 |
# and invoke here to return a dict with those arguments |
| 240 |
argument_spec = client_common_argument_spec() |
| 241 |
argument_spec.update( |
| 242 |
state=dict(type='str', choices=['present', 'absent', 'rename'], default='present'), |
| 243 |
name=dict(type='str', required=True), |
| 244 |
engine=dict(type='str', default=None), |
| 245 |
cluster=dict(type='str', default=None), |
| 246 |
target=dict(type='str', default=None), |
| 247 |
comment=dict(type='str', default=None), |
| 248 |
) |
| 249 |
|
| 250 |
# Instantiate an object of module class |
| 251 |
module = AnsibleModule( |
| 252 |
argument_spec=argument_spec, |
| 253 |
supports_check_mode=True, |
| 254 |
) |
| 255 |
|
| 256 |
# Assign passed options to variables |
| 257 |
client_kwargs = module.params['client_kwargs'] |
| 258 |
# The reason why these arguments are separate from client_kwargs |
| 259 |
# is that we need to protect some sensitive data like passwords passed |
| 260 |
# to the module from logging (see the arguments above with no_log=True); |
| 261 |
# Such data must be passed as module arguments (not nested deep in values). |
| 262 |
main_conn_kwargs = get_main_conn_kwargs(module) |
| 263 |
state = module.params['state'] |
| 264 |
name = module.params['name'] |
| 265 |
engine = module.params['engine'] |
| 266 |
cluster = module.params['cluster'] |
| 267 |
target = module.params['target'] |
| 268 |
comment = module.params['comment'] |
| 269 |
|
| 270 |
# Will fail if no driver informing the user |
| 271 |
check_clickhouse_driver(module) |
| 272 |
|
| 273 |
# Connect to DB |
| 274 |
client = connect_to_db_via_client(module, main_conn_kwargs, client_kwargs) |
| 275 |
|
| 276 |
# Do the job |
| 277 |
changed = False |
| 278 |
database = ClickHouseDB(module, client, name, cluster) |
| 279 |
|
| 280 |
if comment and database.srv_version['year'] < 22: |
| 281 |
msg = ('The module supports the comment feature for ClickHouse ' |
| 282 |
'versions equal to or higher than 22.*. Ignored.') |
| 283 |
module.warn(msg) |
| 284 |
comment = None |
| 285 |
|
| 286 |
if state == 'present': |
| 287 |
if not database.exists: |
| 288 |
changed = database.create(engine, comment) |
| 289 |
else: |
| 290 |
# If database exists |
| 291 |
changed = database.update(engine, comment) |
| 292 |
elif state == 'rename': |
| 293 |
if database.exists: |
| 294 |
changed = database.rename(target) |
| 295 |
else: |
| 296 |
target_db = ClickHouseDB(module, client, target, cluster) |
| 297 |
if target_db.exists: |
| 298 |
changed = False |
| 299 |
msg = "There is nothing to rename" |
| 300 |
module.warn(msg) |
| 301 |
else: |
| 302 |
msg = "The %s and %s databases do not exist" % (name, target) |
| 303 |
module.fail_json(msg=msg) |
| 304 |
else: |
| 305 |
# If state is absent |
| 306 |
if database.exists: |
| 307 |
changed = database.drop() |
| 308 |
|
| 309 |
# Close connection |
| 310 |
client.disconnect_connection() |
| 311 |
|
| 312 |
# Users will get this in JSON output after execution |
| 313 |
module.exit_json(changed=changed, executed_statements=executed_statements) |
| 314 |
|
| 315 |
|
| 316 |
if __name__ == '__main__': |
| 317 |
main() |