radosgw_zone: add support zone set

Support zone set from a json doc

Signed-off-by: Seena Fallah <seenafallah@gmail.com>
pull/7502/head
Seena Fallah 2024-03-10 16:55:04 +01:00 committed by Guillaume Abrioux
parent 2a8b0668c2
commit e219892aeb
2 changed files with 109 additions and 19 deletions

View File

@ -124,20 +124,23 @@ EXAMPLES = '''
RETURN = '''# ''' RETURN = '''# '''
def container_exec(binary, container_image): def container_exec(binary, container_image, container_args=[]):
''' '''
Build the docker CLI to run a command inside a container Build the docker CLI to run a command inside a container
''' '''
container_binary = os.getenv('CEPH_CONTAINER_BINARY') container_binary = os.getenv('CEPH_CONTAINER_BINARY')
command_exec = [container_binary,
'run', command_exec = [container_binary, 'run', '--rm', '--net=host']
'--rm', command_exec.extend(container_args)
'--net=host', command_exec.extend([
'-v', '/etc/ceph:/etc/ceph:z', '-v', '/etc/ceph:/etc/ceph:z',
'-v', '/var/lib/ceph/:/var/lib/ceph/:z', '-v', '/var/lib/ceph/:/var/lib/ceph/:z',
'-v', '/var/log/ceph/:/var/log/ceph/:z', '-v', '/var/log/ceph/:/var/log/ceph/:z',
'--entrypoint=' + binary, container_image] '--entrypoint=' + binary,
container_image,
])
return command_exec return command_exec
@ -154,24 +157,24 @@ def is_containerized():
return container_image return container_image
def pre_generate_radosgw_cmd(container_image=None): def pre_generate_radosgw_cmd(container_image=None, container_args=[]):
''' '''
Generate radosgw-admin prefix comaand Generate radosgw-admin prefix comaand
''' '''
if container_image: if container_image:
cmd = container_exec('radosgw-admin', container_image) cmd = container_exec('radosgw-admin', container_image, container_args)
else: else:
cmd = ['radosgw-admin'] cmd = ['radosgw-admin']
return cmd return cmd
def generate_radosgw_cmd(cluster, args, container_image=None): def generate_radosgw_cmd(cluster, args, container_image=None, container_args=[]):
''' '''
Generate 'radosgw' command line to execute Generate 'radosgw' command line to execute
''' '''
cmd = pre_generate_radosgw_cmd(container_image=container_image) cmd = pre_generate_radosgw_cmd(container_image=container_image, container_args=container_args) # noqa: E501
base_cmd = [ base_cmd = [
'--cluster', '--cluster',
@ -383,6 +386,37 @@ def remove_zone(module, container_image=None):
return cmd return cmd
def set_zone(module, container_image=None):
'''
Set a zone
'''
cluster = module.params.get('cluster')
realm = module.params.get('realm')
zone_doc = module.params.get('zone_doc')
# store the zone_doc in a file
filename = module.tmpdir + 'zone_doc.json'
with open(filename, 'w') as f:
json.dump(zone_doc, f)
container_args = [
'-v', filename + ':' + filename + ':ro'
]
args = [
'set',
'--rgw-realm=' + realm,
'--infile=' + filename,
]
cmd = generate_radosgw_cmd(cluster=cluster,
args=args,
container_image=container_image,
container_args=container_args)
return cmd
def exit_module(module, out, rc, cmd, err, startd, changed=False): def exit_module(module, out, rc, cmd, err, startd, changed=False):
endd = datetime.datetime.now() endd = datetime.datetime.now()
delta = endd - startd delta = endd - startd
@ -404,7 +438,7 @@ def run_module():
module_args = dict( module_args = dict(
cluster=dict(type='str', required=False, default='ceph'), cluster=dict(type='str', required=False, default='ceph'),
name=dict(type='str', required=True), name=dict(type='str', required=True),
state=dict(type='str', required=False, choices=['present', 'absent', 'info'], default='present'), # noqa: E501 state=dict(type='str', required=False, choices=['present', 'absent', 'info', 'set'], default='present'), # noqa: E501
realm=dict(type='str', require=True), realm=dict(type='str', require=True),
zonegroup=dict(type='str', require=True), zonegroup=dict(type='str', require=True),
endpoints=dict(type='list', require=False, default=[]), endpoints=dict(type='list', require=False, default=[]),
@ -412,6 +446,7 @@ def run_module():
secret_key=dict(type='str', required=False, no_log=True), secret_key=dict(type='str', required=False, no_log=True),
default=dict(type='bool', required=False, default=False), default=dict(type='bool', required=False, default=False),
master=dict(type='bool', required=False, default=False), master=dict(type='bool', required=False, default=False),
zone_doc=dict(type='dict', required=False, default={})
) )
module = AnsibleModule( module = AnsibleModule(
@ -443,8 +478,19 @@ def run_module():
# will return either the image name or None # will return either the image name or None
container_image = is_containerized() container_image = is_containerized()
rc, cmd, out, err = exec_commands(module, get_zone(module, container_image=container_image)) # noqa: E501
if state == "set":
zone = json.loads(out) if rc == 0 else {}
zone_doc = module.params.get('zone_doc')
if not zone_doc:
fatal("zone_doc is required when state is set", module)
changed = zone_doc != zone
if changed:
rc, cmd, out, err = exec_commands(module, set_zone(module, container_image=container_image))
if state == "present": if state == "present":
rc, cmd, out, err = exec_commands(module, get_zone(module, container_image=container_image)) # noqa: E501
if rc == 0: if rc == 0:
zone = json.loads(out) zone = json.loads(out)
_rc, _cmd, _out, _err = exec_commands(module, get_realm(module, container_image=container_image)) # noqa: E501 _rc, _cmd, _out, _err = exec_commands(module, get_realm(module, container_image=container_image)) # noqa: E501
@ -479,7 +525,6 @@ def run_module():
changed = True changed = True
elif state == "absent": elif state == "absent":
rc, cmd, out, err = exec_commands(module, get_zone(module, container_image=container_image)) # noqa: E501
if rc == 0: if rc == 0:
rc, cmd, out, err = exec_commands(module, remove_zone(module, container_image=container_image)) # noqa: E501 rc, cmd, out, err = exec_commands(module, remove_zone(module, container_image=container_image)) # noqa: E501
changed = True changed = True
@ -487,9 +532,6 @@ def run_module():
rc = 0 rc = 0
out = "Zone {} doesn't exist".format(name) out = "Zone {} doesn't exist".format(name)
elif state == "info":
rc, cmd, out, err = exec_commands(module, get_zone(module, container_image=container_image)) # noqa: E501
exit_module(module=module, out=out, rc=rc, cmd=cmd, err=err, startd=startd, changed=changed) # noqa: E501 exit_module(module=module, out=out, rc=rc, cmd=cmd, err=err, startd=startd, changed=changed) # noqa: E501

View File

@ -73,6 +73,32 @@ class TestRadosgwZoneModule(object):
]) ])
assert radosgw_zone.generate_radosgw_cmd(fake_cluster, [], image) == expected_cmd assert radosgw_zone.generate_radosgw_cmd(fake_cluster, [], image) == expected_cmd
@pytest.mark.parametrize('image', fake_container_image)
@patch.dict(os.environ, {'CEPH_CONTAINER_BINARY': fake_container_binary})
def test_generate_radosgw_cmd_container_args(self, image):
container_args = [
'-v', '/test:/test:ro',
]
expected_cmd = [
fake_container_binary,
'run',
'--rm',
'--net=host',
'-v', '/test:/test:ro',
'-v', '/etc/ceph:/etc/ceph:z',
'-v', '/var/lib/ceph/:/var/lib/ceph/:z',
'-v', '/var/log/ceph/:/var/log/ceph/:z',
'--entrypoint=' + fake_binary,
fake_container_image
]
expected_cmd.extend([
'--cluster',
fake_cluster,
'zone'
])
assert radosgw_zone.generate_radosgw_cmd(fake_cluster, [], image, container_args) == expected_cmd
def test_create_zone(self): def test_create_zone(self):
fake_module = MagicMock() fake_module = MagicMock()
fake_module.params = fake_params fake_module.params = fake_params
@ -163,3 +189,25 @@ class TestRadosgwZoneModule(object):
] ]
assert radosgw_zone.remove_zone(fake_module) == expected_cmd assert radosgw_zone.remove_zone(fake_module) == expected_cmd
def test_set_zone(self):
fake_module = MagicMock()
fake_module.params = {
'cluster': fake_cluster,
'name': fake_zone,
'realm': fake_realm,
'zonegroup': fake_zonegroup,
'zone_doc': {'id': 'fake_id'},
}
zonefile = fake_module.tmpdir + '/zone.json'
expected_cmd = [
fake_binary,
'--cluster', fake_cluster,
'zone', 'set',
'--rgw-realm=' + fake_realm,
'--infile=' + zonefile,
]
assert radosgw_zone.set_zone(fake_module) == expected_cmd