Skip to content

Commit

Permalink
Merge branch 'devel' into fix-prerequisites
Browse files Browse the repository at this point in the history
  • Loading branch information
droidben authored Jul 9, 2024
2 parents cfacb65 + 40ce4c8 commit 8cf47aa
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 20 deletions.
1 change: 1 addition & 0 deletions ceph_defaults/defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ infra_pkgs:
- chrony
- podman
- lvm2
- sos
client_group: clients
4 changes: 3 additions & 1 deletion cephadm-preflight.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
- rhceph-4-osd-for-rhel-{{ ansible_facts['distribution_major_version'] }}-{{ ansible_facts['architecture'] }}-rpms
repos_5_to_disable:
- rhceph-5-tools-for-rhel-{{ ansible_facts['distribution_major_version'] }}-{{ ansible_facts['architecture'] }}-rpms
repos_6_to_disable:
- rhceph-6-tools-for-rhel-{{ ansible_facts['distribution_major_version'] }}-{{ ansible_facts['architecture'] }}-rpms
packages_to_uninstall:
- ceph-mds
- ceph-mgr
Expand Down Expand Up @@ -62,7 +64,7 @@
- name: disable older rhceph repositories if any on RHEL{{ansible_facts['distribution_major_version']}}
when: ansible_facts['distribution_major_version'] == '9'
rhsm_repository:
name: "{{ repos_5_to_disable }}"
name: "{{ repos_5_to_disable + repos_6_to_disable }}"
state: absent

- name: enable ceph package repositories
Expand Down
84 changes: 68 additions & 16 deletions library/ceph_orch_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,17 @@
# limitations under the License.

from __future__ import absolute_import, division, print_function
from typing import List, Tuple
from typing import List, Tuple, Dict
__metaclass__ = type

import datetime
import yaml

from ansible.module_utils.basic import AnsibleModule # type: ignore
try:
from ansible.module_utils.ceph_common import exit_module, build_base_cmd_orch # type: ignore
except ImportError:
from module_utils.ceph_common import exit_module, build_base_cmd_orch
import datetime


ANSIBLE_METADATA = {
Expand Down Expand Up @@ -70,6 +72,25 @@
'''


def parse_spec(spec: str) -> Dict:
""" parse spec string to yaml """
yaml_spec = yaml.safe_load(spec)
return yaml_spec


def retrieve_current_spec(module: AnsibleModule, expected_spec: Dict) -> Dict:
""" 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)
if isinstance(out, str):
# if there is no existing service, cephadm returns the string 'No services reported'
return {}
else:
return yaml.safe_load(out[1])


def apply_spec(module: "AnsibleModule",
data: str) -> Tuple[int, List[str], str, str]:
cmd = build_base_cmd_orch(module)
Expand All @@ -82,23 +103,39 @@ def apply_spec(module: "AnsibleModule",
return rc, cmd, out, err


def main() -> None:
def change_required(current: Dict, expected: Dict) -> bool:
""" checks if the current config differs from what is expected """
if not current:
return True

for key, value in expected.items():
if key in current:
if current[key] != value:
return True
continue
else:
return True
return False


def run_module() -> None:

module_args = dict(
spec=dict(type='str', required=True),
fsid=dict(type='str', required=False),
docker=dict(type=bool,
required=False,
default=False),
image=dict(type='str', required=False)
)

module = AnsibleModule(
argument_spec=dict(
fsid=dict(type='str', required=False),
spec=dict(type='str', required=True),
docker=dict(type=bool,
required=False,
default=False),
image=dict(type='str', required=False)
),
argument_spec=module_args,
supports_check_mode=True
)

spec = module.params.get('spec')

startd = datetime.datetime.now()
changed = False
spec = module.params.get('spec')

if module.check_mode:
exit_module(
Expand All @@ -111,8 +148,19 @@ def main() -> None:
changed=False
)

rc, cmd, out, err = apply_spec(module, spec)
changed = True
# Idempotency check
expected = parse_spec(module.params.get('spec'))
current_spec = retrieve_current_spec(module, expected)

if change_required(current_spec, expected):
rc, cmd, out, err = apply_spec(module, spec)
changed = True
else:
rc = 0
cmd = []
out = ''
err = ''
changed = False

exit_module(
module=module,
Expand All @@ -125,5 +173,9 @@ def main() -> None:
)


def main() -> None:
run_module()


if __name__ == '__main__':
main()
6 changes: 3 additions & 3 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
[tox]
envlist =
flake8,mypy,unittests
{el8,el9,rocky8,rocky9,ubuntu_lts}-{functional}
{el9,rocky8,rocky9,ubuntu_lts}-{functional}
skipsdist = True

[testenv:mypy]
basepython = python3
deps =
mypy
types-PyYAML
commands = mypy --config-file ./mypy.ini {toxinidir}/library {toxinidir}/module_utils

[testenv:flake8]
Expand All @@ -27,7 +28,7 @@ setenv=
PYTHONPATH = {env:PYTHONPATH:}:{toxinidir}/library:{toxinidir}/module_utils:{toxinidir}/tests/library
commands = py.test -vvv -n=auto {toxinidir}/tests/library/ {toxinidir}/tests/module_utils

[testenv:{el8,el9,rocky8,rocky9,ubuntu_lts}-functional]
[testenv:{el9,rocky8,rocky9,ubuntu_lts}-functional]
allowlist_externals =
vagrant
bash
Expand All @@ -36,7 +37,6 @@ passenv=*
sitepackages=True
setenv=
# Set the vagrant box image to use
el8: CEPH_ANSIBLE_VAGRANT_BOX = centos/stream8
el9: CEPH_ANSIBLE_VAGRANT_BOX = centos/stream9
rocky8: CEPH_ANSIBLE_VAGRANT_BOX = generic/rocky8
rocky9: CEPH_ANSIBLE_VAGRANT_BOX = generic/rocky9
Expand Down

0 comments on commit 8cf47aa

Please sign in to comment.