-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ceph_orch_spec: Add ceph orch apply spec feature
Signed-off-by: Teoman ONAY <[email protected]>
- Loading branch information
Showing
4 changed files
with
231 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,186 @@ | ||
#!/usr/bin/python | ||
|
||
# Copyright: (c) 2024, Teoman Onay <[email protected]> | ||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) | ||
from __future__ import (absolute_import, division, print_function) | ||
__metaclass__ = type | ||
from ansible.module_utils.basic import AnsibleModule | ||
|
||
from ansible.module_utils.basic import AnsibleModule | ||
try: | ||
from ansible.module_utils.ca_common import exit_module, generate_cmd, is_containerized # noqa: E501 | ||
except ImportError: | ||
from module_utils.ca_common import exit_module, generate_cmd, is_containerized # noqa: E501 | ||
|
||
import datetime | ||
import yaml | ||
from typing import Tuple, List | ||
|
||
|
||
ANSIBLE_METADATA = { | ||
'metadata_version': '1.0', | ||
'status': ['preview'], | ||
'supported_by': 'community' | ||
} | ||
|
||
DOCUMENTATION = r''' | ||
--- | ||
module: ceph_orch_spec | ||
short_description: This is my test module | ||
# If this is part of a collection, you need to use semantic versioning, | ||
# i.e. the version is of the form "2.5.0" and not "2.4". | ||
version_added: "1.0.0" | ||
description: This is my longer description explaining my test module. | ||
options: | ||
spec: | ||
description: This is the message to send to the test module. | ||
required: true | ||
type: str | ||
# Specify this value according to your collection | ||
# in format of namespace.collection.doc_fragment_name | ||
# extends_documentation_fragment: | ||
# - my_namespace.my_collection.my_doc_fragment_name | ||
author: | ||
- Teoman ONAY ([email protected]) | ||
''' | ||
|
||
EXAMPLES = r''' | ||
# Pass in a message | ||
- name: Test with a message | ||
my_namespace.my_collection.ceph_orch_spec: | ||
spec: >- | ||
service_type: rgw | ||
service_id: realm.zone | ||
placement: | ||
hosts: | ||
- host1 | ||
- host2 | ||
- host3 | ||
config: | ||
param_1: val_1 | ||
... | ||
param_N: val_N | ||
unmanaged: false | ||
networks: | ||
- 192.169.142.0/24 | ||
# pass in a message and have changed true | ||
- name: Test with a message and changed output | ||
my_namespace.my_collection.ceph_orch_spec: | ||
name: hello world | ||
new: true | ||
# fail the module | ||
- name: Test failure of the module | ||
my_namespace.my_collection.ceph_orch_spec: | ||
name: fail me | ||
''' | ||
|
||
RETURN = r''' | ||
# These are examples of possible return values, and in general should use other names for return values. | ||
original_message: | ||
description: The original name param that was passed in. | ||
type: str | ||
returned: always | ||
sample: 'hello world' | ||
message: | ||
description: The output message that the test module generates. | ||
type: str | ||
returned: always | ||
sample: 'goodbye' | ||
''' | ||
|
||
|
||
def parse_spec(spec: str) -> yaml: | ||
""" parse spec string to yaml """ | ||
yaml_spec: yaml = yaml.safe_load(spec) | ||
return yaml_spec | ||
|
||
|
||
def retrieve_current_spec(module: AnsibleModule, expected_spec: yaml) -> yaml: | ||
""" retrieve current config of the service """ | ||
service: str = expected_spec["service_type"] | ||
cmd = build_base_cmd_orch(module) | ||
cmd.extend(['ls', service, '--format=yaml']) | ||
out = module.run_command(cmd) | ||
return yaml.safe_load(out[1]) | ||
|
||
|
||
def compare_specs(current: yaml, expected: yaml) -> bool: | ||
result: bool = True | ||
for key, value in expected.items(): | ||
if current[key] != value: | ||
result = False | ||
break | ||
else: | ||
continue | ||
return result | ||
|
||
def apply_spec(module: "AnsibleModule", | ||
data: str) -> Tuple[int, List[str], str, str]: | ||
cmd = build_base_cmd_orch(module) | ||
cmd.extend(['apply', '-i', '-']) | ||
rc, out, err = module.run_command(cmd, data=data) | ||
|
||
if rc: | ||
raise RuntimeError(err) | ||
|
||
return rc, cmd, out, err | ||
|
||
|
||
def run_module(): | ||
|
||
module_args = dict( | ||
spec=dict(type='str', required=True), | ||
fsid=dict(type='str', required=False), | ||
) | ||
|
||
module = AnsibleModule( | ||
argument_spec=module_args, | ||
supports_check_mode=True | ||
) | ||
|
||
startd = datetime.datetime.now() | ||
spec = module.params.get('spec') | ||
|
||
if module.check_mode: | ||
exit_module( | ||
module=module, | ||
out='', | ||
rc=0, | ||
cmd=[], | ||
err='', | ||
startd=startd, | ||
changed=False | ||
) | ||
|
||
# Idempotency check | ||
expected = parse_spec(module.params.get('spec')) | ||
change_required = compare_specs(retrieve_current_spec(module, expected), expected) | ||
|
||
if change_required: | ||
rc, cmd, out, err = apply_spec(module, spec) | ||
changed = True | ||
|
||
exit_module( | ||
module=module, | ||
out=out, | ||
rc=rc, | ||
cmd=cmd, | ||
err=err, | ||
startd=startd, | ||
changed=changed | ||
) | ||
|
||
|
||
def main(): | ||
run_module() | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,3 +8,4 @@ mock | |
jmespath | ||
pytest-rerunfailures | ||
pytest-cov | ||
setuptools |