-
-
Save sky-joker/a5e989367cb3e559dc3bc94357018110 to your computer and use it in GitHub Desktop.
Migration tool for molecule-vmware
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
#!/usr/bin/env python | |
import argparse | |
import copy | |
import glob | |
import os | |
import shutil | |
import subprocess | |
from subprocess import PIPE | |
def options() -> argparse.Namespace: | |
parser = argparse.ArgumentParser(prog="migration_molecule_vmware.py", | |
add_help=True, | |
description="This tool can be used molecule-vmware scenarios 3.5.0 to 3.6.0 migration.") | |
parser.add_argument("--delete-backup", | |
action="store_true", | |
help="Whether deleting a backup directory that created in migration.") | |
args = parser.parse_args() | |
return args | |
def get_role_path(molecule_dir: str) -> str: | |
molecule_dir_array = molecule_dir.split("/") | |
molecule_dir_array.pop() | |
return "/".join(molecule_dir_array) | |
def generate_new_scenario(role_path: str, scenarios: list): | |
_scenarios = copy.copy(scenarios) | |
try: | |
proc = subprocess.Popen(["molecule", "init", "scenario", "-d", "vmware"], cwd=role_path, stdout=PIPE, stderr=PIPE) | |
proc.communicate() | |
except Exception as e: | |
print(e) | |
_scenarios.remove('default') | |
for scenario in _scenarios: | |
try: | |
proc = subprocess.Popen(["molecule", "init", "scenario", scenario, "-d", "vmware"], cwd=role_path, stdout=PIPE, stderr=PIPE) | |
proc.communicate() | |
except Exception as e: | |
print(e) | |
def copy_molecule_configuration_from_backup_dir(backup_molecule_dir: str, scenarios: list): | |
for scenario in scenarios: | |
back_scenario_dir = backup_molecule_dir + "/" + scenario | |
back_molecule_configuration_file = back_scenario_dir + "/molecule.yml" | |
if os.path.isfile(back_molecule_configuration_file): | |
shutil.copy(back_molecule_configuration_file, back_molecule_configuration_file.replace("_back", "")) | |
def main(): | |
args = options() | |
molecule_directories = glob.glob("roles/*/molecule", recursive=True) | |
for molecule_dir in molecule_directories: | |
scenarios = [scenario for scenario in os.listdir(molecule_dir) if os.path.isdir("%s/%s" % (molecule_dir, scenario))] | |
role_path = get_role_path(molecule_dir) | |
if scenarios: | |
# Create molecule backup that role has. | |
backup_molecule_dir = molecule_dir + "_back" | |
os.rename(molecule_dir, backup_molecule_dir) | |
# Generate new scenarios based on molecule backup. | |
generate_new_scenario(role_path, scenarios) | |
copy_molecule_configuration_from_backup_dir(backup_molecule_dir, scenarios) | |
# Remove moleucle backup directory if delete flag on. | |
if args.delete_backup: | |
shutil.rmtree(backup_molecule_dir) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment