|
1 | 1 | #!/usr/bin/python
|
2 | 2 | # -*- coding: utf-8 -*-
|
3 |
| - |
4 |
| -# author: IBM Corporation |
5 |
| -# description: Highly-customizable Ansible module |
6 |
| -# for installing and configuring IBM Spectrum Scale (GPFS) |
7 |
| -# company: IBM |
8 |
| -# license: Apache-2.0 |
| 3 | +# |
| 4 | +# Copyright 2020 IBM Corporation |
| 5 | +# and other contributors as indicated by the @author tags. |
| 6 | +# |
| 7 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | +# you may not use this file except in compliance with the License. |
| 9 | +# You may obtain a copy of the License at |
| 10 | +# |
| 11 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +# |
| 13 | +# Unless required by applicable law or agreed to in writing, software |
| 14 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | +# See the License for the specific language governing permissions and |
| 17 | +# limitations under the License. |
| 18 | +# |
9 | 19 |
|
10 | 20 | ANSIBLE_METADATA = {
|
11 | 21 | 'status': ['preview'],
|
|
116 | 126 |
|
117 | 127 | import json
|
118 | 128 | import sys
|
| 129 | +import traceback |
119 | 130 | from ansible.module_utils.basic import AnsibleModule
|
120 | 131 |
|
121 |
| -#TODO: FIX THIS. If the modules and utils are located in a non standard |
122 |
| -# path, the PYTHONPATH will need to be exported in the .bashrc |
123 |
| -from ansible.module_utils.ibm_ss_utils import runCmd, parse_simple_cmd_output, RC_SUCCESS |
| 132 | +try: |
| 133 | + from ansible.module_utils.ibm_ss_utils import RC_SUCCESS, SpectrumScaleLogger |
| 134 | +except: |
| 135 | + from ibm_ss_utils import RC_SUCCESS, SpectrumScaleLogger |
124 | 136 |
|
| 137 | +try: |
| 138 | + from ansible.module_utils.ibm_ss_filesystem_utils import SpectrumScaleFS |
| 139 | +except: |
| 140 | + from ibm_ss_filesystem_utils import SpectrumScaleFS |
125 | 141 |
|
126 |
| -MMLSFS_DATATYPE="filesystems" |
127 |
| -MMLSFS_KEY="deviceName" |
128 |
| -MMLSFS_PROPERTY_NAME="properties" |
129 | 142 |
|
130 |
| -def get_filesystem_info(): |
131 |
| - msg = result_json = "" |
132 |
| - stdout, stderr, rc = runCmd(["/usr/lpp/mmfs/bin/mmlsfs","all","-Y"], sh=False) |
133 |
| - |
134 |
| - if rc == RC_SUCCESS: |
135 |
| - result_dict = parse_simple_cmd_output( |
136 |
| - stdout, |
137 |
| - MMLSFS_KEY, |
138 |
| - MMLSFS_PROPERTY_NAME, |
139 |
| - MMLSFS_DATATYPE |
140 |
| - ) |
141 |
| - result_json = json.dumps(result_dict) |
142 |
| - msg = "The command \"mmlsfs\" executed successfully" |
143 |
| - else: |
144 |
| - msg = stderr |
145 |
| - |
146 |
| - return rc, msg, result_json |
147 |
| - |
148 |
| - |
149 |
| -def create_filesystem(name, stanza_path): |
150 |
| - # TODO: Make This idempotent |
151 |
| - stdout, stderr, rc = runCmd(["/usr/lpp/mmfs/bin/mmcrfs", |
152 |
| - name, |
153 |
| - "-F", stanza_path, |
154 |
| - "-B", block_size, |
155 |
| - "-m", default_metadata_replicas, |
156 |
| - "-r", default_data_replicas, |
157 |
| - "-n", num_nodes, |
158 |
| - "-A", automatic_mount_option, |
159 |
| - "-T", default_mount_point], |
160 |
| - sh=False) |
161 |
| - |
162 |
| - if rc == RC_SUCCESS: |
163 |
| - msg = stdout |
164 |
| - else: |
165 |
| - msg = stderr |
166 |
| - |
167 |
| - return rc, msg |
168 |
| - |
169 |
| - |
170 |
| -def delete_filesystem(name): |
171 |
| - # TODO: Implement |
172 |
| - rc = RC_SUCCESS |
173 |
| - msg = "" |
174 |
| - return rc, msg |
| 143 | +def main(): |
| 144 | + logger = SpectrumScaleLogger.get_logger() |
175 | 145 |
|
| 146 | + logger.debug("---------------------------------------") |
| 147 | + logger.debug("Function Entry: ibm_ss_filesystem.main()") |
| 148 | + logger.debug("---------------------------------------") |
176 | 149 |
|
177 |
| -def main(): |
178 | 150 | # Setup the module argument specifications
|
179 | 151 | scale_arg_spec = dict(
|
180 | 152 | op = dict(
|
@@ -250,28 +222,66 @@ def main():
|
250 | 222 | msg = result_json = ""
|
251 | 223 | state_changed = False
|
252 | 224 | if module.params['op'] and "get" in module.params['op']:
|
253 |
| - # Retrieve the IBM Spectrum Scae filesystem information |
254 |
| - rc, msg, result_json = get_filesystem_info() |
| 225 | + # Retrieve the IBM Spectrum Scale filesystem information |
| 226 | + try: |
| 227 | + result_dict = {} |
| 228 | + filesystem_list = [] |
| 229 | + |
| 230 | + filesystems = SpectrumScaleFS.get_filesystems() |
| 231 | + for fs in filesystems: |
| 232 | + filesystem_info = {} |
| 233 | + filesystem_info["deviceName"] = fs.get_device_name() |
| 234 | + filesystem_info["properties"] = fs.get_properties_list() |
| 235 | + filesystem_list.append(filesystem_info) |
| 236 | + |
| 237 | + result_dict["filesystems"] = filesystem_list |
| 238 | + result_json = json.dumps(result_dict) |
| 239 | + |
| 240 | + msg = "Successfully retrieved filesystem information" |
| 241 | + except Exception as e: |
| 242 | + st = traceback.format_exc() |
| 243 | + e_msg = ("Exception: {0} StackTrace: {1}".format(str(e), st)) |
| 244 | + module.fail_json(msg=e_msg) |
255 | 245 | elif module.params['state']:
|
256 | 246 | if "present" in module.params['state']:
|
257 | 247 | # Create a new IBM Spectrum Scale cluster
|
258 |
| - rc, msg = create_filesystem( |
259 |
| - module.params['stanza'], |
260 |
| - module.params['name'], |
261 |
| - module.params["block_size"], |
262 |
| - module.params["num_nodes"], |
263 |
| - module.params["default_metadata_replicas"], |
264 |
| - module.params["default_data_replicas"], |
265 |
| - module.params["automatic_mount_option"], |
266 |
| - module.params["default_mount_point"] |
267 |
| - ) |
| 248 | + try: |
| 249 | + rc, result_json = SpectrumScaleFS.create_filesystem( |
| 250 | + module.params['stanza'], |
| 251 | + module.params['name'], |
| 252 | + module.params["block_size"], |
| 253 | + module.params["num_nodes"], |
| 254 | + module.params["default_metadata_replicas"], |
| 255 | + module.params["default_data_replicas"], |
| 256 | + module.params["automatic_mount_option"], |
| 257 | + module.params["default_mount_point"] |
| 258 | + ) |
| 259 | + msg = "Successfully created filesystem" |
| 260 | + except Exception as e: |
| 261 | + st = traceback.format_exc() |
| 262 | + e_msg = ("Exception: {0} StackTrace: {1}".format(str(e), st)) |
| 263 | + module.fail_json(msg=e_msg) |
268 | 264 | else:
|
269 | 265 | # Delete the existing IBM Spectrum Scale cluster
|
270 |
| - rc, msg = delete_filesystem(module.params['name']) |
| 266 | + try: |
| 267 | + rc, result_json = SpectrumScaleFS.delete_filesystem( |
| 268 | + module.params['name'] |
| 269 | + ) |
| 270 | + msg = "Successfully deleted filesystem" |
| 271 | + except Exception as e: |
| 272 | + st = traceback.format_exc() |
| 273 | + e_msg = ("Exception: {0} StackTrace: {1}".format(str(e), st)) |
| 274 | + module.fail_json(msg=e_msg) |
271 | 275 |
|
272 | 276 | if rc == RC_SUCCESS:
|
273 | 277 | state_changed = True
|
274 | 278 |
|
| 279 | + logger.debug("---------------------------------------") |
| 280 | + logger.debug("Function Exit: ibm_ss_filesystem.main()") |
| 281 | + logger.debug("---------------------------------------") |
| 282 | + |
| 283 | + logger = SpectrumScaleLogger.shutdown() |
| 284 | + |
275 | 285 | # Module is done. Return back the result
|
276 | 286 | module.exit_json(changed=state_changed, msg=msg, rc=rc, result=result_json)
|
277 | 287 |
|
|
0 commit comments