|
1 | 1 | import json
|
2 | 2 | import tempfile
|
3 | 3 |
|
| 4 | +from ceph.rbd.utils import random_string |
| 5 | +from ceph.rbd.workflows.rbd import create_single_pool_and_images |
4 | 6 | from cli.rbd.rbd import Rbd
|
5 | 7 | from utility.log import Log
|
6 | 8 |
|
@@ -73,3 +75,104 @@ def prepare_migration_source_spec(
|
73 | 75 | spec_file.flush()
|
74 | 76 |
|
75 | 77 | return temp_file.name
|
| 78 | + |
| 79 | + |
| 80 | +def run_prepare_execute_commit(rbd, pool, image, **kw): |
| 81 | + """ |
| 82 | + Function to carry out the following: |
| 83 | + - Create Target/destination pool for migration |
| 84 | + - Migration prepare |
| 85 | + - Migration Execute |
| 86 | + - Migration commit |
| 87 | + Args: |
| 88 | + kw: rbd object, pool, image, test data |
| 89 | + Returns: |
| 90 | + int: The return value. 0 for success, 1 otherwise |
| 91 | +
|
| 92 | + """ |
| 93 | + # Create Target Pool/ Destination Pool for migration |
| 94 | + is_ec_pool = True if "ec" in kw[pool]["pool_type"] else False |
| 95 | + config = kw.get("config", {}) |
| 96 | + target_pool = "target_pool_" + random_string(len=3) |
| 97 | + target_pool_config = {} |
| 98 | + if is_ec_pool: |
| 99 | + data_pool_target = "data_pool_new_" + random_string(len=3) |
| 100 | + target_pool_config["data_pool"] = data_pool_target |
| 101 | + rc = create_single_pool_and_images( |
| 102 | + config=config, |
| 103 | + pool=target_pool, |
| 104 | + pool_config=target_pool_config, |
| 105 | + client=kw["client"], |
| 106 | + cluster="ceph", |
| 107 | + rbd=rbd, |
| 108 | + ceph_version=int(config.get("rhbuild")[0]), |
| 109 | + is_ec_pool=is_ec_pool, |
| 110 | + is_secondary=False, |
| 111 | + do_not_create_image=True, |
| 112 | + ) |
| 113 | + if rc: |
| 114 | + log.error(f"Creation of target pool {target_pool} failed") |
| 115 | + return rc |
| 116 | + |
| 117 | + # Adding the new pool details to config so that they are handled in cleanup |
| 118 | + if kw[pool]["pool_type"] == "rep_pool_config": |
| 119 | + kw["config"]["rep_pool_config"][target_pool] = {} |
| 120 | + elif kw[pool]["pool_type"] == "ec_pool_config": |
| 121 | + kw["config"]["ec_pool_config"][target_pool] = {"data_pool": data_pool_target} |
| 122 | + |
| 123 | + # Prepare Migration |
| 124 | + target_image = "target_image_" + random_string(len=3) |
| 125 | + rbd.migration.prepare( |
| 126 | + source_spec=kw[pool]["spec"], |
| 127 | + dest_spec=f"{target_pool}/{target_image}", |
| 128 | + client_node=kw["client"], |
| 129 | + ) |
| 130 | + kw[pool].update({"target_pool": target_pool}) |
| 131 | + kw[pool].update({"target_image": target_image}) |
| 132 | + |
| 133 | + # Verify prepare migration status |
| 134 | + if verify_migration_state( |
| 135 | + action="prepare", |
| 136 | + image_spec=f"{target_pool}/{target_image}", |
| 137 | + **kw, |
| 138 | + ): |
| 139 | + log.error("Failed to prepare migration") |
| 140 | + return 1 |
| 141 | + else: |
| 142 | + log.info("Migration prepare status verfied successfully") |
| 143 | + |
| 144 | + # execute migration |
| 145 | + rbd.migration.action( |
| 146 | + action="execute", |
| 147 | + dest_spec=f"{target_pool}/{target_image}", |
| 148 | + client_node=kw["client"], |
| 149 | + ) |
| 150 | + |
| 151 | + # verify execute migration status |
| 152 | + if verify_migration_state( |
| 153 | + action="execute", |
| 154 | + image_spec=f"{target_pool}/{target_image}", |
| 155 | + **kw, |
| 156 | + ): |
| 157 | + log.error("Failed to execute migration") |
| 158 | + return 1 |
| 159 | + else: |
| 160 | + log.info("Migration executed successfully") |
| 161 | + |
| 162 | + # commit migration |
| 163 | + rbd.migration.action( |
| 164 | + action="commit", |
| 165 | + dest_spec=f"{target_pool}/{target_image}", |
| 166 | + client_node=kw["client"], |
| 167 | + ) |
| 168 | + |
| 169 | + # verify commit migration status |
| 170 | + if verify_migration_state( |
| 171 | + action="commit", |
| 172 | + image_spec=f"{target_pool}/{target_image}", |
| 173 | + **kw, |
| 174 | + ): |
| 175 | + log.error("Failed to commit migration") |
| 176 | + return 1 |
| 177 | + else: |
| 178 | + log.info("Migration committed successfully") |
0 commit comments