Debian Codegraph @

ansible_collections/community/mongodb/plugins/modules/mongodb_oplog.py

raw
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_oplog
13 short_description: Resizes the MongoDB oplog.
14 description:
15 - Resizes the MongoDB oplog.
16 - This module should only be used with MongoDB 3.6 and above.
17 - Old MongoDB versions should use an alternative method.
18 - Consult U(https://docs.mongodb.com/manual/tutorial/change-oplog-size) for further info.
19 author: Rhys Campbell (@rhysmeister)
20 version_added: "1.0.0"
21
22 extends_documentation_fragment:
23 - community.mongodb.login_options
24 - community.mongodb.ssl_options
25
26 options:
27 oplog_size_mb:
28 description:
29 - New size of the oplog in MB.
30 type: int
31 required: true
32 compact:
33 description:
34 - Runs compact against the oplog.rs collection in the local database to reclaim disk space.
35 - Performs no actions against PRIMARY members.
36 - The MongoDB user must have the compact role on the local database for this feature to work.
37 type: bool
38 default: false
39 required: false
40 notes:
41 - Requires the pymongo Python package on the remote host, version 4+.. This
42 can be installed using pip or the OS package manager.
43 @see U(http://api.mongodb.org/python/current/installation.html)
44 requirements:
45 - pymongo
46 '''
47
48 EXAMPLES = r'''
49 - name: Resize oplog to 16 gigabytes, or 16000 megabytes
50 community.mongodb.mongodb_oplog:
51 oplog_size_mb: 16000
52
53 - name: Resize oplog to 8 gigabytes and compact secondaries to reclaim space
54 community.mongodb.mongodb_oplog:
55 oplog_size_mb: 8000
56 compact: true
57 '''
58
59 RETURN = r'''
60 changed:
61 description: Whether the member oplog was modified.
62 returned: success
63 type: bool
64 compacted:
65 description: Whether the member oplog was compacted.
66 returned: success
67 type: bool
68 msg:
69 description: A short description of what happened.
70 returned: success
71 type: str
72 failed:
73 description: If something went wrong
74 returned: failed
75 type: bool
76 '''
77
78 from ansible.module_utils.basic import AnsibleModule
79 from ansible.module_utils._text import to_native
80 from ansible_collections.community.mongodb.plugins.module_utils.mongodb_common import (
81 missing_required_lib,
82 mongodb_common_argument_spec,
83 member_state,
84 mongo_auth,
85 PYMONGO_IMP_ERR,
86 pymongo_found,
87 get_mongodb_client,
88 )
89
90 has_ordereddict = False
91 try:
92 from collections import OrderedDict
93 has_ordereddict = True
94 except ImportError as excep:
95 try:
96 from ordereddict import OrderedDict
97 has_ordereddict = True
98 except ImportError as excep:
99 pass
100
101
102 def get_olplog_size(client):
103 return int(client["local"].command("collStats", "oplog.rs")["maxSize"]) / 1024 / 1024
104
105
106 def set_oplog_size(client, oplog_size_mb):
107 cmd_doc = OrderedDict([
108 ('replSetResizeOplog', 1),
109 ('size', oplog_size_mb)
110 ])
111 client["admin"].command(cmd_doc)
112
113
114 def compact_oplog(client):
115 client["local"].command("compact", "oplog.rs")
116
117
118 def main():
119 argument_spec = mongodb_common_argument_spec()
120 argument_spec.update(
121 compact=dict(type='bool', default=False),
122 oplog_size_mb=dict(type='int', required=True),
123 )
124 module = AnsibleModule(
125 argument_spec=argument_spec,
126 supports_check_mode=True,
127 required_together=[['login_user', 'login_password']],
128 )
129
130 if not has_ordereddict:
131 module.fail_json(msg='Cannot import OrderedDict class. You can probably install with: pip install ordereddict')
132
133 if not pymongo_found:
134 module.fail_json(msg=missing_required_lib('pymongo'),
135 exception=PYMONGO_IMP_ERR)
136
137 oplog_size_mb = float(module.params['oplog_size_mb']) # MongoDB 4.4 inists on a real
138 compact = module.params['compact']
139
140 result = dict(
141 changed=False,
142 )
143
144 try:
145 client = get_mongodb_client(module, directConnection=True)
146 client = mongo_auth(module, client, directConnection=True)
147 except Exception as excep:
148 module.fail_json(msg='Unable to connect to MongoDB: %s' % to_native(excep))
149
150 try:
151 current_oplog_size = get_olplog_size(client)
152 except Exception as excep:
153 module.fail_json(msg='Unable to get current oplog size: %s' % to_native(excep))
154 if oplog_size_mb == current_oplog_size:
155 result["msg"] = "oplog_size_mb is already {0} mb".format(int(oplog_size_mb))
156 result["compacted"] = False
157 else:
158 try:
159 state = member_state(client)
160 except Exception as excep:
161 module.fail_json(msg='Unable to get member state: %s' % to_native(excep))
162 if module.check_mode:
163 result["changed"] = True
164 result["msg"] = "oplog has been resized from {0} mb to {1} mb".format(int(current_oplog_size),
165 int(oplog_size_mb))
166 if state == "SECONDARY" and compact and current_oplog_size > oplog_size_mb:
167 result["compacted"] = True
168 else:
169 result["compacted"] = False
170 else:
171 try:
172 set_oplog_size(client, oplog_size_mb)
173 result["changed"] = True
174 result["msg"] = "oplog has been resized from {0} mb to {1} mb".format(int(current_oplog_size),
175 int(oplog_size_mb))
176 except Exception as excep:
177 module.fail_json(msg='Unable to set oplog size: %s' % to_native(excep))
178 if state == "SECONDARY" and compact and current_oplog_size > oplog_size_mb:
179 try:
180 compact_oplog(client)
181 result["compacted"] = True
182 except Exception as excep:
183 module.fail_json(msg='Error compacting member oplog: %s' % to_native(excep))
184 else:
185 result["compacted"] = False
186
187 module.exit_json(**result)
188
189
190 if __name__ == '__main__':
191 main()