def destroy(group_name=None): if os.path.exists(CLOUD_CONFIG_FPATH): creds_data = read_json(CLOUD_CONFIG_FPATH) else: print("Cloud credentials not found at %s" % CLOUD_CONFIG_FPATH) return 1 creds = AWSCredentials(creds_data["aws"]["key"], creds_data["aws"]["secret"]) if not os.path.exists(CLOUD_STATE_FPATH): print("No saved cloud state info") return 1 vms_info = read_json(CLOUD_STATE_FPATH) to_destroy = [] if group_name: print("Destroying hosts in the '%s' group" % group_name) if not group_name.startswith("@"): group_name = "@" + group_name if group_name not in vms_info: print("Group '%s' not found" % group_name) return 1 region = vms_info[group_name]["meta"]["region"] driver = get_cloud_driver(Providers.AWS, creds, region) for name, vm_info in vms_info[group_name].items(): if name == "meta": continue vm_uuid = vm_info["uuid"] vm = VM.get_by_uuid(vm_uuid, driver) if vm is not None: to_destroy.append(vm) else: print("VM '%s' not found in the clouds" % vm_uuid) del vms_info[group_name] else: print("Destroying all hosts") for group_name in [ key for key in vms_info.keys() if key.startswith("@") ]: region = vms_info[group_name]["meta"]["region"] driver = get_cloud_driver(Providers.AWS, creds, region) for name, vm_info in vms_info[group_name].items(): if name == "meta": continue vm_uuid = vm_info["uuid"] vm = VM.get_by_uuid(vm_uuid, driver) if vm is not None: to_destroy.append(vm) else: print("VM '%s' not found in the clouds" % vm_uuid) del vms_info[group_name] destroy_vms(to_destroy) write_json(CLOUD_STATE_FPATH, vms_info) return 0
def destroy(group_name=None): if os.path.exists(CLOUD_CONFIG_FPATH): creds_data = read_json(CLOUD_CONFIG_FPATH) else: print("Cloud credentials not found at %s" % CLOUD_CONFIG_FPATH) return 1 aws_creds = None try: aws_creds = AWSCredentials(creds_data["aws"]["key"], creds_data["aws"]["secret"]) except KeyError: # missing/incomplete AWS credentials, may not be needed, though pass gcp_creds = None try: gcp_creds = GCPCredentials(creds_data["gcp"]["project_id"], creds_data["gcp"]["service_account_id"], creds_data["gcp"]["key_path"]) except KeyError: # missing/incomplete GCP credentials, may not be needed, though pass if not os.path.exists(CLOUD_STATE_FPATH): print("No saved cloud state info") return 1 vms_info = read_json(CLOUD_STATE_FPATH) to_destroy = [] if group_name: print("Destroying hosts in the '%s' group" % group_name) if not group_name.startswith("@"): group_name = "@" + group_name if group_name not in vms_info: print("Group '%s' not found" % group_name) return 1 region = vms_info[group_name]["meta"]["region"] provider = vms_info[group_name]["meta"]["provider"] if provider == "aws": if aws_creds is None: user_error("Missing/incomplete AWS credentials") return 1 driver = get_cloud_driver(Providers.AWS, aws_creds, region) if provider == "gcp": if gcp_creds is None: user_error("Missing/incomplete GCP credentials") return 1 driver = get_cloud_driver(Providers.GCP, gcp_creds, region) nodes = driver.list_nodes() for name, vm_info in vms_info[group_name].items(): if name == "meta": continue vm_uuid = vm_info["uuid"] vm = VM.get_by_uuid(vm_uuid, nodes=nodes) if vm is not None: to_destroy.append(vm) else: print("VM '%s' not found in the clouds" % vm_uuid) del vms_info[group_name] else: print("Destroying all hosts") for group_name in [ key for key in vms_info.keys() if key.startswith("@") ]: region = vms_info[group_name]["meta"]["region"] provider = vms_info[group_name]["meta"]["provider"] if provider == "aws": if aws_creds is None: user_error("Missing/incomplete AWS credentials") return 1 driver = get_cloud_driver(Providers.AWS, aws_creds, region) if provider == "gcp": if gcp_creds is None: user_error("Missing/incomplete GCP credentials") return 1 driver = get_cloud_driver(Providers.GCP, gcp_creds, region) nodes = driver.list_nodes() for name, vm_info in vms_info[group_name].items(): if name == "meta": continue vm_uuid = vm_info["uuid"] vm = VM.get_by_uuid(vm_uuid, nodes=nodes) if vm is not None: to_destroy.append(vm) else: print("VM '%s' not found in the clouds" % vm_uuid) del vms_info[group_name] destroy_vms(to_destroy) write_json(CLOUD_STATE_FPATH, vms_info) return 0