| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright: (c) 2020, Rhys Campbell <[email protected]> |
| 4 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
|
| 6 |
from __future__ import absolute_import, division, print_function |
| 7 |
__metaclass__ = type |
| 8 |
|
| 9 |
|
| 10 |
DOCUMENTATION = r''' |
| 11 |
--- |
| 12 |
module: mongodb_shutdown |
| 13 |
short_description: Cleans up all database resources and then terminates the mongod/mongos process. |
| 14 |
description: |
| 15 |
- Cleans up all database resources and then terminates the process. |
| 16 |
author: Rhys Campbell (@rhysmeister) |
| 17 |
version_added: "1.0.0" |
| 18 |
|
| 19 |
extends_documentation_fragment: |
| 20 |
- community.mongodb.login_options |
| 21 |
- community.mongodb.ssl_options |
| 22 |
|
| 23 |
options: |
| 24 |
force: |
| 25 |
description: |
| 26 |
- Specify true to force the mongod to shut down. |
| 27 |
- Force shutdown interrupts any ongoing operations on the mongod and may result in unexpected behavior. |
| 28 |
type: bool |
| 29 |
default: false |
| 30 |
timeout: |
| 31 |
description: |
| 32 |
- The number of seconds the primary should wait for a secondary to catch up. |
| 33 |
type: int |
| 34 |
default: 10 |
| 35 |
notes: |
| 36 |
- Requires the pymongo Python package on the remote host, version 4+.. This |
| 37 |
can be installed using pip or the OS package manager. |
| 38 |
@see U(http://api.mongodb.org/python/current/installation.html) |
| 39 |
requirements: |
| 40 |
- pymongo |
| 41 |
''' |
| 42 |
|
| 43 |
EXAMPLES = r''' |
| 44 |
- name: Attempt to perform a clean shutdown |
| 45 |
community.mongodb.mongodb_shutdown: |
| 46 |
|
| 47 |
- name: Force shutdown with a timeout of 60 seconds |
| 48 |
community.mongodb.mongodb_shutdown: |
| 49 |
force: true |
| 50 |
timeout: 60 |
| 51 |
''' |
| 52 |
|
| 53 |
RETURN = r''' |
| 54 |
changed: |
| 55 |
description: Whether the member was shutdown. |
| 56 |
returned: success |
| 57 |
type: bool |
| 58 |
msg: |
| 59 |
description: A short description of what happened. |
| 60 |
returned: success |
| 61 |
type: str |
| 62 |
failed: |
| 63 |
description: If something went wrong |
| 64 |
returned: failed |
| 65 |
type: bool |
| 66 |
''' |
| 67 |
|
| 68 |
from ansible.module_utils.basic import AnsibleModule |
| 69 |
from ansible.module_utils._text import to_native |
| 70 |
from ansible_collections.community.mongodb.plugins.module_utils.mongodb_common import ( |
| 71 |
missing_required_lib, |
| 72 |
mongodb_common_argument_spec, |
| 73 |
mongo_auth, |
| 74 |
PYMONGO_IMP_ERR, |
| 75 |
pymongo_found, |
| 76 |
get_mongodb_client, |
| 77 |
) |
| 78 |
|
| 79 |
|
| 80 |
def main(): |
| 81 |
argument_spec = mongodb_common_argument_spec() |
| 82 |
argument_spec.update( |
| 83 |
force=dict(type='bool', default=False), |
| 84 |
timeout=dict(type='int', default=10) |
| 85 |
) |
| 86 |
module = AnsibleModule( |
| 87 |
argument_spec=argument_spec, |
| 88 |
supports_check_mode=True, |
| 89 |
required_together=[['login_user', 'login_password']], |
| 90 |
) |
| 91 |
|
| 92 |
try: |
| 93 |
from collections import OrderedDict |
| 94 |
except ImportError: |
| 95 |
try: |
| 96 |
from ordereddict import OrderedDict |
| 97 |
except ImportError as excep: |
| 98 |
module.fail_json(msg='Cannot import OrderedDict class. You can probably install with: pip install ordereddict: %s' |
| 99 |
% to_native(excep)) |
| 100 |
|
| 101 |
if not pymongo_found: |
| 102 |
module.fail_json(msg=missing_required_lib('pymongo'), |
| 103 |
exception=PYMONGO_IMP_ERR) |
| 104 |
|
| 105 |
force = module.params['force'] |
| 106 |
timeout = module.params['timeout'] |
| 107 |
|
| 108 |
result = dict( |
| 109 |
changed=False, |
| 110 |
) |
| 111 |
|
| 112 |
try: |
| 113 |
client = get_mongodb_client(module, directConnection=True) |
| 114 |
client = mongo_auth(module, client, directConnection=True) |
| 115 |
except Exception as excep: |
| 116 |
module.fail_json(msg='Unable to connect to MongoDB: %s' % to_native(excep)) |
| 117 |
|
| 118 |
try: |
| 119 |
cmd_doc = OrderedDict([ |
| 120 |
('shutdown', 1), |
| 121 |
('force', force), |
| 122 |
('timeoutSecs', timeout) |
| 123 |
]) |
| 124 |
client['admin'].command(cmd_doc) |
| 125 |
result["changed"] = True |
| 126 |
result["msg"] = "mongod process was terminated sucessfully" |
| 127 |
except Exception as excep: |
| 128 |
if "connection closed" in str(excep): |
| 129 |
result["changed"] = True |
| 130 |
result["msg"] = "mongod process was terminated sucessfully" |
| 131 |
else: |
| 132 |
result["msg"] = "An error occurred: {0}".format(excep) |
| 133 |
|
| 134 |
module.exit_json(**result) |
| 135 |
|
| 136 |
|
| 137 |
if __name__ == '__main__': |
| 138 |
main() |